Search in sources :

Example 46 with QueryResponse

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

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

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

Example 49 with QueryResponse

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

the class SubjectAttributesResourceV1Test method shouldPerformQueryForSubjectAttributes.

@Test
public void shouldPerformQueryForSubjectAttributes() throws Exception {
    //given
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSSOTokenContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    Set<String> attributes = new HashSet<>();
    attributes.add("attr");
    attributes.add("attr2");
    given(mockSAM.getAvailableSubjectAttributeNames()).willReturn(attributes);
    //when
    Promise<QueryResponse, ResourceException> promise = subjectAttributesResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    promise.getOrThrowUninterruptibly();
    ArgumentCaptor<ResourceResponse> captor = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(mockHandler, times(2)).handleResource(captor.capture());
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) Matchers.anyString(org.mockito.Matchers.anyString) Subject(javax.security.auth.Subject) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 50 with QueryResponse

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

the class SubjectAttributesResourceV1Test method shouldErrorWhenAttributeRetrievalFails.

@Test
public void shouldErrorWhenAttributeRetrievalFails() throws EntitlementException {
    //given
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSSOTokenContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    given(mockSAM.getAvailableSubjectAttributeNames()).willThrow(new EntitlementException(401));
    //when
    Promise<QueryResponse, ResourceException> promise = subjectAttributesResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    verify(mockDebug, times(1)).error(anyString());
    assertThat(promise).failedWithResourceException().withCode(ResourceException.INTERNAL_ERROR);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) EntitlementException(com.sun.identity.entitlement.EntitlementException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

QueryResponse (org.forgerock.json.resource.QueryResponse)57 Test (org.testng.annotations.Test)49 ResourceException (org.forgerock.json.resource.ResourceException)43 QueryRequest (org.forgerock.json.resource.QueryRequest)42 Context (org.forgerock.services.context.Context)42 RealmContext (org.forgerock.openam.rest.RealmContext)37 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)26 ClientContext (org.forgerock.services.context.ClientContext)25 ResourceResponse (org.forgerock.json.resource.ResourceResponse)24 Collection (java.util.Collection)23 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)20 JsonPointer (org.forgerock.json.JsonPointer)19 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)19 Pair (org.forgerock.util.Pair)19 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)16 HashSet (java.util.HashSet)14 JsonValue (org.forgerock.json.JsonValue)13 ArrayList (java.util.ArrayList)12 Subject (javax.security.auth.Subject)12 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)12