use of org.apache.syncope.common.lib.to.EntityTO in project syncope by apache.
the class ApplicationITCase method crud.
@Test
public void crud() {
// 1. create application
ApplicationTO application = new ApplicationTO();
application.setKey(UUID.randomUUID().toString());
PrivilegeTO privilegeTO = new PrivilegeTO();
privilegeTO.setKey(UUID.randomUUID().toString());
privilegeTO.setSpec("{ \"one\": true }");
application.getPrivileges().add(privilegeTO);
privilegeTO = new PrivilegeTO();
privilegeTO.setKey(UUID.randomUUID().toString());
privilegeTO.setSpec("{ \"two\": true }");
application.getPrivileges().add(privilegeTO);
privilegeTO = new PrivilegeTO();
privilegeTO.setKey(UUID.randomUUID().toString());
privilegeTO.setSpec("{ \"three\": true }");
application.getPrivileges().add(privilegeTO);
Response response = applicationService.create(application);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
application = getObject(response.getLocation(), ApplicationService.class, ApplicationTO.class);
assertNotNull(application);
assertNull(application.getDescription());
assertEquals(3, application.getPrivileges().size());
// 2. update application
application.setDescription("A description");
application.getPrivileges().remove(1);
applicationService.update(application);
application = applicationService.read(application.getKey());
assertNotNull(application);
assertNotNull(application.getDescription());
assertEquals(2, application.getPrivileges().size());
// 3. assign application's privileges to a new role
RoleTO role = new RoleTO();
role.setKey("privileged");
role.getPrivileges().addAll(application.getPrivileges().stream().map(EntityTO::getKey).collect(Collectors.toList()));
response = roleService.create(role);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
assertNotNull(role);
assertEquals(2, role.getPrivileges().size());
// 4. delete application => delete privileges
applicationService.delete(application.getKey());
try {
applicationService.read(application.getKey());
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.NotFound, e.getType());
}
role = roleService.read(role.getKey());
assertNotNull(role);
assertTrue(role.getPrivileges().isEmpty());
}
use of org.apache.syncope.common.lib.to.EntityTO in project syncope by apache.
the class GroupServiceImpl method replace.
@Override
public Response replace(final String id, final SCIMGroup group) {
if (!id.equals(group.getId())) {
throw new BadRequestException(ErrorType.invalidPath, "Expected " + id + ", found " + group.getId());
}
ResponseBuilder builder = checkETag(Resource.Group, id);
if (builder != null) {
return builder.build();
}
// save current group members
Set<String> beforeMembers = new HashSet<>();
MembershipCond membCond = new MembershipCond();
membCond.setGroup(id);
SearchCond searchCond = SearchCond.getLeafCond(membCond);
int count = userLogic().search(searchCond, 1, 1, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getLeft();
for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
beforeMembers.addAll(userLogic().search(searchCond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getRight().stream().map(EntityTO::getKey).collect(Collectors.toSet()));
}
// update group, don't change members
ProvisioningResult<GroupTO> result = groupLogic().update(AnyOperations.diff(binder().toGroupTO(group), groupLogic().read(id), false), false);
// assign new members
Set<String> afterMembers = new HashSet<>();
group.getMembers().forEach(member -> {
afterMembers.add(member.getValue());
if (!beforeMembers.contains(member.getValue())) {
UserPatch patch = new UserPatch();
patch.setKey(member.getValue());
patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(result.getEntity().getKey()).build());
try {
userLogic().update(patch, false);
} catch (Exception e) {
LOG.error("While setting membership of {} to {}", result.getEntity().getKey(), member.getValue(), e);
}
}
});
// remove unconfirmed members
beforeMembers.stream().filter(member -> !afterMembers.contains(member)).forEach(user -> {
UserPatch patch = new UserPatch();
patch.setKey(user);
patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(result.getEntity().getKey()).build());
try {
userLogic().update(patch, false);
} catch (Exception e) {
LOG.error("While removing membership of {} from {}", result.getEntity().getKey(), user, e);
}
});
return updateResponse(result.getEntity().getKey(), binder().toSCIMGroup(result.getEntity(), uriInfo.getAbsolutePathBuilder().path(result.getEntity().getKey()).build().toASCIIString(), Collections.<String>emptyList(), Collections.<String>emptyList()));
}
Aggregations