Search in sources :

Example 1 with ListResponse

use of org.apache.syncope.ext.scimv2.api.data.ListResponse in project syncope by apache.

the class SCIMITCase method list.

@Test
public void list() throws IOException {
    assumeTrue(SCIMDetector.isSCIMAvailable(webClient()));
    Response response = webClient().path("Groups").query("count", 1100000).get();
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
    SCIMError error = response.readEntity(SCIMError.class);
    assertEquals(ErrorType.tooMany, error.getScimType());
    response = webClient().path("Groups").query("sortBy", "displayName").query("count", 11).get();
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    assertEquals(SCIMConstants.APPLICATION_SCIM_JSON, StringUtils.substringBefore(response.getHeaderString(HttpHeaders.CONTENT_TYPE), ";"));
    ListResponse<SCIMGroup> result = response.readEntity(new GenericType<ListResponse<SCIMGroup>>() {
    });
    assertNotNull(result);
    assertTrue(result.getTotalResults() > 0);
    assertEquals(11, result.getItemsPerPage());
    assertFalse(result.getResources().isEmpty());
    result.getResources().forEach(group -> {
        assertNotNull(group.getId());
        assertNotNull(group.getDisplayName());
    });
}
Also used : ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) Response(javax.ws.rs.core.Response) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) SCIMError(org.apache.syncope.ext.scimv2.api.data.SCIMError) Test(org.junit.jupiter.api.Test)

Example 2 with ListResponse

use of org.apache.syncope.ext.scimv2.api.data.ListResponse in project syncope by apache.

the class AbstractService method doSearch.

@SuppressWarnings("unchecked")
protected ListResponse<R> doSearch(final Resource type, final SCIMSearchRequest request) {
    if (type == null) {
        throw new UnsupportedOperationException();
    }
    if (request.getCount() > confManager().get().getFilterMaxResults()) {
        throw new BadRequestException(ErrorType.tooMany, "Too many results requested");
    }
    SearchCondVisitor visitor = new SearchCondVisitor(type, confManager().get());
    int startIndex = request.getStartIndex() <= 1 ? 1 : (request.getStartIndex() / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
    int itemsPerPage = request.getCount() <= 1 ? AnyDAO.DEFAULT_PAGE_SIZE : request.getCount();
    List<OrderByClause> sort;
    if (request.getSortBy() == null) {
        sort = Collections.<OrderByClause>emptyList();
    } else {
        OrderByClause clause = new OrderByClause();
        clause.setField(visitor.createAttributeCond(request.getSortBy()).getSchema());
        clause.setDirection(request.getSortOrder() == null || request.getSortOrder() == SortOrder.ascending ? OrderByClause.Direction.ASC : OrderByClause.Direction.DESC);
        sort = Collections.singletonList(clause);
    }
    Pair<Integer, ? extends List<? extends AnyTO>> result = anyLogic(type).search(StringUtils.isBlank(request.getFilter()) ? null : SearchCondConverter.convert(visitor, request.getFilter()), startIndex, itemsPerPage, sort, SyncopeConstants.ROOT_REALM, false);
    if (result.getLeft() > confManager().get().getFilterMaxResults()) {
        throw new BadRequestException(ErrorType.tooMany, "Too many results found");
    }
    ListResponse<R> response = new ListResponse<>(result.getLeft(), startIndex == 1 ? 1 : startIndex - 1, itemsPerPage);
    result.getRight().forEach(anyTO -> {
        SCIMResource resource = null;
        if (anyTO instanceof UserTO) {
            resource = binder().toSCIMUser((UserTO) anyTO, uriInfo.getAbsolutePathBuilder().path(anyTO.getKey()).build().toASCIIString(), request.getAttributes(), request.getExcludedAttributes());
        } else if (anyTO instanceof GroupTO) {
            resource = binder().toSCIMGroup((GroupTO) anyTO, uriInfo.getAbsolutePathBuilder().path(anyTO.getKey()).build().toASCIIString(), request.getAttributes(), request.getExcludedAttributes());
        }
        if (resource != null) {
            response.getResources().add((R) resource);
        }
    });
    return response;
}
Also used : SCIMResource(org.apache.syncope.ext.scimv2.api.data.SCIMResource) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) GroupTO(org.apache.syncope.common.lib.to.GroupTO) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) UserTO(org.apache.syncope.common.lib.to.UserTO) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) SearchCondVisitor(org.apache.syncope.core.logic.scim.SearchCondVisitor)

Example 3 with ListResponse

use of org.apache.syncope.ext.scimv2.api.data.ListResponse in project syncope by apache.

the class SCIMITCase method search.

@Test
public void search() {
    assumeTrue(SCIMDetector.isSCIMAvailable(webClient()));
    // invalid filter
    Response response = webClient().path("Groups").query("filter", "invalid").get();
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
    SCIMError error = response.readEntity(SCIMError.class);
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), error.getStatus());
    assertEquals(ErrorType.invalidFilter, error.getScimType());
    // eq
    response = webClient().path("Groups").query("filter", "displayName eq \"additional\"").get();
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    assertEquals(SCIMConstants.APPLICATION_SCIM_JSON, StringUtils.substringBefore(response.getHeaderString(HttpHeaders.CONTENT_TYPE), ";"));
    ListResponse<SCIMGroup> groups = response.readEntity(new GenericType<ListResponse<SCIMGroup>>() {
    });
    assertNotNull(groups);
    assertEquals(1, groups.getTotalResults());
    SCIMGroup additional = groups.getResources().get(0);
    assertEquals("additional", additional.getDisplayName());
    // eq via POST
    SCIMSearchRequest request = new SCIMSearchRequest("displayName eq \"additional\"", null, null, null, null);
    response = webClient().path("Groups").path("/.search").post(request);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    assertEquals(SCIMConstants.APPLICATION_SCIM_JSON, StringUtils.substringBefore(response.getHeaderString(HttpHeaders.CONTENT_TYPE), ";"));
    groups = response.readEntity(new GenericType<ListResponse<SCIMGroup>>() {
    });
    assertNotNull(groups);
    assertEquals(1, groups.getTotalResults());
    additional = groups.getResources().get(0);
    assertEquals("additional", additional.getDisplayName());
    // gt
    UserTO newUser = userService.create(UserITCase.getUniqueSampleTO("scimsearch@syncope.apache.org"), true).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
    }).getEntity();
    Date value = new Date(newUser.getCreationDate().getTime() - 1000);
    response = webClient().path("Users").query("filter", "meta.created gt \"" + DATE_FORMAT.get().format(value) + "\"").get();
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    assertEquals(SCIMConstants.APPLICATION_SCIM_JSON, StringUtils.substringBefore(response.getHeaderString(HttpHeaders.CONTENT_TYPE), ";"));
    ListResponse<SCIMUser> users = response.readEntity(new GenericType<ListResponse<SCIMUser>>() {
    });
    assertNotNull(users);
    assertEquals(1, users.getTotalResults());
    SCIMUser newSCIMUser = users.getResources().get(0);
    assertEquals(newUser.getUsername(), newSCIMUser.getUserName());
}
Also used : GenericType(javax.ws.rs.core.GenericType) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) SCIMUser(org.apache.syncope.ext.scimv2.api.data.SCIMUser) Date(java.util.Date) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) Response(javax.ws.rs.core.Response) UserTO(org.apache.syncope.common.lib.to.UserTO) SCIMError(org.apache.syncope.ext.scimv2.api.data.SCIMError) SCIMSearchRequest(org.apache.syncope.ext.scimv2.api.data.SCIMSearchRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ListResponse (org.apache.syncope.ext.scimv2.api.data.ListResponse)3 Response (javax.ws.rs.core.Response)2 UserTO (org.apache.syncope.common.lib.to.UserTO)2 SCIMError (org.apache.syncope.ext.scimv2.api.data.SCIMError)2 SCIMGroup (org.apache.syncope.ext.scimv2.api.data.SCIMGroup)2 Test (org.junit.jupiter.api.Test)2 Date (java.util.Date)1 GenericType (javax.ws.rs.core.GenericType)1 GroupTO (org.apache.syncope.common.lib.to.GroupTO)1 SearchCondVisitor (org.apache.syncope.core.logic.scim.SearchCondVisitor)1 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)1 BadRequestException (org.apache.syncope.ext.scimv2.api.BadRequestException)1 SCIMResource (org.apache.syncope.ext.scimv2.api.data.SCIMResource)1 SCIMSearchRequest (org.apache.syncope.ext.scimv2.api.data.SCIMSearchRequest)1 SCIMUser (org.apache.syncope.ext.scimv2.api.data.SCIMUser)1