Search in sources :

Example 56 with QueryRequest

use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.

the class ApplicationsResourceTest method mockQueryRequest.

private QueryRequest mockQueryRequest(QueryFilter<JsonPointer> filter) {
    QueryRequest request = mock(QueryRequest.class);
    given(request.getQueryFilter()).willReturn(filter);
    return request;
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest)

Example 57 with QueryRequest

use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.

the class ConditionTypesResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws JsonMappingException {
    //given
    SubjectContext mockSubjectContext = mock(SubjectContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockRequest.getPageSize()).willReturn(2);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 58 with QueryRequest

use of org.forgerock.json.resource.QueryRequest 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 59 with QueryRequest

use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method makeMockQueryRequest.

/**
     * Generates a mock request which will simulate the required behaviour of requesting
     * the now deprecated behaviour of returning the remaining results.
     *
     * @return Non null.
     */
private static QueryRequest makeMockQueryRequest() {
    QueryRequest mockRequest = mock(QueryRequest.class);
    given(mockRequest.getAdditionalParameter(QueryResponsePresentation.REMAINING)).willReturn("true");
    return mockRequest;
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest)

Example 60 with QueryRequest

use of org.forgerock.json.resource.QueryRequest 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)

Aggregations

QueryRequest (org.forgerock.json.resource.QueryRequest)79 Test (org.testng.annotations.Test)68 QueryResponse (org.forgerock.json.resource.QueryResponse)40 JsonPointer (org.forgerock.json.JsonPointer)34 Context (org.forgerock.services.context.Context)34 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)29 ResourceException (org.forgerock.json.resource.ResourceException)28 Subject (javax.security.auth.Subject)26 RealmContext (org.forgerock.openam.rest.RealmContext)26 ResourceResponse (org.forgerock.json.resource.ResourceResponse)24 ClientContext (org.forgerock.services.context.ClientContext)22 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)20 Collection (java.util.Collection)15 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)15 SearchFilter (com.sun.identity.entitlement.util.SearchFilter)10 Matchers.anyString (org.mockito.Matchers.anyString)9 JsonValue (org.forgerock.json.JsonValue)8 ArrayList (java.util.ArrayList)7 Set (java.util.Set)7 EntitlementException (com.sun.identity.entitlement.EntitlementException)6