Search in sources :

Example 1 with QueryResponseHandler

use of org.forgerock.openam.rest.query.QueryResponseHandler in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method makeQueryResponseHandler.

private static QueryResponseHandler makeQueryResponseHandler() {
    QueryResponseHandler mock = mock(QueryResponseHandler.class);
    given(mock.handleResource(any(ResourceResponse.class))).willReturn(true);
    return mock;
}
Also used : QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) ResourceResponse(org.forgerock.json.resource.ResourceResponse)

Example 2 with QueryResponseHandler

use of org.forgerock.openam.rest.query.QueryResponseHandler in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method queryShouldHandleQueryException.

@Test
public void queryShouldHandleQueryException() throws Exception {
    //given
    setupExistingResourceTypeFromJson(jsonResourceType);
    QueryRequest queryRequest = mock(QueryRequest.class);
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    Throwable t = new QueryException(QueryException.QueryErrorCode.FILTER_BOOLEAN_LITERAL_FALSE);
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenThrow(t);
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    assertThat(promise).failedWithResourceException().withCode(ResourceException.BAD_REQUEST);
}
Also used : QueryException(org.forgerock.openam.rest.query.QueryException) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 3 with QueryResponseHandler

use of org.forgerock.openam.rest.query.QueryResponseHandler in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method shouldHandleQueryPageLargerThanResults.

@Test
public void shouldHandleQueryPageLargerThanResults() throws Exception {
    //given
    QueryRequest queryRequest = makeMockQueryRequest();
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    final int lastPageSize = 5;
    final int pageOffset = 10;
    final int resultSize = 11;
    Map<String, Map<String, Set<String>>> resourceTypes = new HashMap<>();
    for (int i = 0; i < resultSize; i++) {
        resourceTypes.put(UUID.randomUUID().toString(), rawData);
    }
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenReturn(resourceTypes);
    when(resourceTypeService.getResourceType(any(Subject.class), anyString(), anyString())).thenReturn(EntitlementUtils.resourceTypeFromMap(UUID.randomUUID().toString(), rawData));
    when(queryRequest.getPagedResultsOffset()).thenReturn(pageOffset);
    when(queryRequest.getPageSize()).thenReturn(lastPageSize);
    Answer<Boolean> onlyFirstPage = new Answer<Boolean>() {

        int count = pageOffset;

        @Override
        public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
            return count++ < resultSize;
        }
    };
    when(queryHandler.handleResource(any(ResourceResponse.class))).thenAnswer(onlyFirstPage);
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    QueryResponse queryResponse = promise.getOrThrowUninterruptibly();
    verify(queryHandler, times(resultSize - pageOffset)).handleResource(any(ResourceResponse.class));
    assertThat(queryResponse.getRemainingPagedResults()).isEqualTo(0);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) HashMap(java.util.HashMap) Subject(javax.security.auth.Subject) Answer(org.mockito.stubbing.Answer) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 4 with QueryResponseHandler

use of org.forgerock.openam.rest.query.QueryResponseHandler in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method queryShouldPageResults.

@Test
public void queryShouldPageResults() throws Exception {
    //given
    QueryRequest queryRequest = makeMockQueryRequest();
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    final int firstPageSize = 2;
    final int pageOffset = 0;
    final int resultSize = 10;
    Map<String, Map<String, Set<String>>> resourceTypes = new HashMap<>();
    for (int i = 0; i < resultSize; i++) {
        resourceTypes.put(UUID.randomUUID().toString(), rawData);
    }
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenReturn(resourceTypes);
    when(resourceTypeService.getResourceType(any(Subject.class), anyString(), anyString())).thenReturn(EntitlementUtils.resourceTypeFromMap(UUID.randomUUID().toString(), rawData));
    when(queryRequest.getPagedResultsOffset()).thenReturn(pageOffset);
    when(queryRequest.getPageSize()).thenReturn(firstPageSize);
    Answer<Boolean> onlyFirstPage = new Answer<Boolean>() {

        int count = 0;

        @Override
        public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
            return count++ < firstPageSize;
        }
    };
    when(queryHandler.handleResource(any(ResourceResponse.class))).thenAnswer(onlyFirstPage);
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    QueryResponse queryResponse = promise.getOrThrowUninterruptibly();
    verify(queryHandler, times(firstPageSize)).handleResource(any(ResourceResponse.class));
    assertThat(queryResponse.getRemainingPagedResults()).isEqualTo(resultSize - firstPageSize);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) HashMap(java.util.HashMap) Subject(javax.security.auth.Subject) Answer(org.mockito.stubbing.Answer) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with QueryResponseHandler

use of org.forgerock.openam.rest.query.QueryResponseHandler in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method queryShouldHandleAllResults.

@Test
public void queryShouldHandleAllResults() throws Exception {
    //given
    QueryRequest queryRequest = makeMockQueryRequest();
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    Map<String, Map<String, Set<String>>> resourceTypes = new HashMap<>();
    final int resultSize = 10;
    for (int i = 0; i < resultSize; i++) {
        resourceTypes.put(UUID.randomUUID().toString(), rawData);
    }
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenReturn(resourceTypes);
    when(resourceTypeService.getResourceType(any(Subject.class), anyString(), anyString())).thenReturn(EntitlementUtils.resourceTypeFromMap(UUID.randomUUID().toString(), rawData));
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    QueryResponse queryResponse = promise.getOrThrowUninterruptibly();
    verify(queryHandler, times(resultSize)).handleResource(any(ResourceResponse.class));
    assertThat(queryResponse.getRemainingPagedResults()).isEqualTo(0);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) ResourceResponse(org.forgerock.json.resource.ResourceResponse) HashMap(java.util.HashMap) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) HashMap(java.util.HashMap) Map(java.util.Map) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

QueryResponseHandler (org.forgerock.openam.rest.query.QueryResponseHandler)5 Subject (javax.security.auth.Subject)4 QueryRequest (org.forgerock.json.resource.QueryRequest)4 QueryResponse (org.forgerock.json.resource.QueryResponse)4 ResourceException (org.forgerock.json.resource.ResourceException)4 ResourceResponse (org.forgerock.json.resource.ResourceResponse)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 QueryException (org.forgerock.openam.rest.query.QueryException)1