use of org.apache.syncope.common.lib.to.PagedResult in project syncope by apache.
the class AbstractServiceImpl method buildPagedResult.
/**
* Builds a paged result out of a list of items and additional information.
*
* @param <T> result type
* @param list bare list of items to be returned
* @param page current page
* @param size requested size
* @param totalCount total result size (not considering pagination)
* @return paged result
*/
protected <T extends AbstractBaseBean> PagedResult<T> buildPagedResult(final List<T> list, final int page, final int size, final int totalCount) {
PagedResult<T> result = new PagedResult<>();
result.getResult().addAll(list);
result.setPage(page);
result.setSize(result.getResult().size());
result.setTotalCount(totalCount);
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
for (Map.Entry<String, List<String>> queryParam : queryParams.entrySet()) {
builder.queryParam(queryParam.getKey(), queryParam.getValue().toArray());
}
if (result.getPage() > 1) {
result.setPrev(builder.replaceQueryParam(PARAM_PAGE, result.getPage() - 1).replaceQueryParam(PARAM_SIZE, size).build());
}
if ((result.getPage() - 1) * size + result.getSize() < totalCount) {
result.setNext(builder.replaceQueryParam(PARAM_PAGE, result.getPage() + 1).replaceQueryParam(PARAM_SIZE, size).build());
}
return result;
}
use of org.apache.syncope.common.lib.to.PagedResult in project testcases by coheigea.
the class SyncopeDeployer method deployUserData.
@SuppressWarnings("unchecked")
public void deployUserData() {
WebClient client = WebClient.create(address);
client = client.type("application/xml");
String authorizationHeader = "Basic " + Base64Utility.encode(("admin" + ":" + "password").getBytes());
client.header("Authorization", authorizationHeader);
client.accept("application/xml");
// Create the groups first
client = client.path("groups");
PagedResult<GroupTO> existingGroups = (PagedResult<GroupTO>) client.get(PagedResult.class);
GroupTO bossGroup = findOrCreateGroup("boss", existingGroups, client);
GroupTO employeeGroup = findOrCreateGroup("employee", existingGroups, client);
// Now create the users
client = client.replacePath("users");
PagedResult<UserTO> existingUsers = (PagedResult<UserTO>) client.get(PagedResult.class);
if (!doesUserAlreadyExist("alice", existingUsers.getResult())) {
UserTO user = new UserTO();
user.setUsername("alice");
user.setPassword("security");
user.setRealm("/");
MembershipTO membership = new MembershipTO();
membership.setGroupKey(employeeGroup.getKey());
// membership.setGroupName(employeeGroup.getName());
user.getMemberships().add(membership);
membership = new MembershipTO();
// membership.setGroupName(bossGroup.getName());
membership.setGroupKey(bossGroup.getKey());
user.getMemberships().add(membership);
client.post(user, ProvisioningResult.class);
}
if (!doesUserAlreadyExist("bob", existingUsers.getResult())) {
UserTO user = new UserTO();
user.setUsername("bob");
user.setPassword("security");
user.setRealm("/");
MembershipTO membership = new MembershipTO();
membership.setGroupKey(employeeGroup.getKey());
// membership.setGroupName(employeeGroup.getName());
user.getMemberships().add(membership);
client.post(user, ProvisioningResult.class);
}
client.close();
// Check via the client API that the users were created correctly
SyncopeClientFactoryBean clientFactory = new SyncopeClientFactoryBean().setAddress(address);
SyncopeClient syncopeClient = clientFactory.create("admin", "password");
UserService userService = syncopeClient.getService(UserService.class);
int count = userService.search(new AnyQuery.Builder().build()).getTotalCount();
Assert.assertEquals(2, count);
}
use of org.apache.syncope.common.lib.to.PagedResult in project syncope by apache.
the class MembershipITCase method pull.
@Test
public void pull() {
// 0. create ad-hoc resource, with adequate mapping
ResourceTO newResource = resourceService.read(RESOURCE_NAME_DBPULL);
newResource.setKey(getUUIDString());
ItemTO item = newResource.getProvision("USER").get().getMapping().getItems().stream().filter(object -> "firstname".equals(object.getIntAttrName())).findFirst().get();
assertNotNull(item);
assertEquals("ID", item.getExtAttrName());
item.setIntAttrName("memberships[additional].aLong");
item.setPurpose(MappingPurpose.BOTH);
item = newResource.getProvision("USER").get().getMapping().getItems().stream().filter(object -> "fullname".equals(object.getIntAttrName())).findFirst().get();
item.setPurpose(MappingPurpose.PULL);
PullTaskTO newTask = null;
try {
newResource = createResource(newResource);
assertNotNull(newResource);
// 1. create user with new resource assigned
UserTO user = UserITCase.getUniqueSampleTO("memb@apache.org");
user.setRealm("/even/two");
user.getPlainAttrs().remove(user.getPlainAttr("ctype").get());
user.getResources().clear();
user.getResources().add(newResource.getKey());
MembershipTO membership = new MembershipTO.Builder().group("034740a9-fa10-453b-af37-dc7897e98fb1").build();
membership.getPlainAttrs().add(new AttrTO.Builder().schema("aLong").value("5432").build());
user.getMemberships().add(membership);
user = createUser(user).getEntity();
assertNotNull(user);
// 2. verify that user was found on resource
JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);
String idOnResource = queryForObject(jdbcTemplate, 50, "SELECT id FROM testpull WHERE id=?", String.class, "5432");
assertEquals("5432", idOnResource);
// 3. unlink user from resource, then remove it
DeassociationPatch patch = new DeassociationPatch();
patch.setKey(user.getKey());
patch.setAction(ResourceDeassociationAction.UNLINK);
patch.getResources().add(newResource.getKey());
assertNotNull(userService.deassociate(patch).readEntity(BulkActionResult.class));
userService.delete(user.getKey());
// 4. create pull task and execute
newTask = taskService.read(TaskType.PULL, "7c2242f4-14af-4ab5-af31-cdae23783655", true);
newTask.setResource(newResource.getKey());
newTask.setDestinationRealm("/even/two");
Response response = taskService.create(TaskType.PULL, newTask);
newTask = getObject(response.getLocation(), TaskService.class, PullTaskTO.class);
assertNotNull(newTask);
ExecTO execution = AbstractTaskITCase.execProvisioningTask(taskService, TaskType.PULL, newTask.getKey(), 50, false);
assertEquals(PropagationTaskExecStatus.SUCCESS, PropagationTaskExecStatus.valueOf(execution.getStatus()));
// 5. verify that pulled user has
PagedResult<UserTO> users = userService.search(new AnyQuery.Builder().realm("/").fiql(SyncopeClient.getUserSearchConditionBuilder().is("username").equalTo(user.getUsername()).query()).build());
assertEquals(1, users.getTotalCount());
assertEquals(1, users.getResult().get(0).getMemberships().size());
assertEquals("5432", users.getResult().get(0).getMemberships().get(0).getPlainAttr("aLong").get().getValues().get(0));
} catch (Exception e) {
LOG.error("Unexpected error", e);
fail(e.getMessage());
} finally {
if (newTask != null && !"83f7e85d-9774-43fe-adba-ccd856312994".equals(newTask.getKey())) {
taskService.delete(TaskType.PULL, newTask.getKey());
}
resourceService.delete(newResource.getKey());
}
}
use of org.apache.syncope.common.lib.to.PagedResult in project syncope by apache.
the class SearchITCase method searchByDynRole.
@Test
public void searchByDynRole() {
RoleTO role = RoleITCase.getSampleRoleTO("dynMembership");
role.setDynMembershipCond("cool==true");
Response response = roleService.create(role);
role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
assertNotNull(role);
if (ElasticsearchDetector.isElasticSearchEnabled(syncopeService)) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}
PagedResult<UserTO> matchingUsers = userService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).fiql(SyncopeClient.getUserSearchConditionBuilder().inRoles(role.getKey()).query()).build());
assertNotNull(matchingUsers);
assertFalse(matchingUsers.getResult().isEmpty());
assertTrue(matchingUsers.getResult().stream().anyMatch(user -> "c9b2dec2-00a7-4855-97c0-d854842b4b24".equals(user.getKey())));
}
use of org.apache.syncope.common.lib.to.PagedResult in project syncope by apache.
the class SearchITCase method searchByDynGroup.
@Test
public void searchByDynGroup() {
GroupTO group = GroupITCase.getBasicSampleTO("dynMembership");
group.setUDynMembershipCond("cool==true");
group = createGroup(group).getEntity();
assertNotNull(group);
if (ElasticsearchDetector.isElasticSearchEnabled(syncopeService)) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}
PagedResult<UserTO> matchingUsers = userService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).fiql(SyncopeClient.getUserSearchConditionBuilder().inGroups(group.getKey()).query()).build());
assertNotNull(matchingUsers);
assertFalse(matchingUsers.getResult().isEmpty());
assertTrue(matchingUsers.getResult().stream().anyMatch(user -> "c9b2dec2-00a7-4855-97c0-d854842b4b24".equals(user.getKey())));
}
Aggregations