Search in sources :

Example 61 with QueryRequest

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

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

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

Example 64 with QueryRequest

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

the class SubjectAttributesResourceV1Test method shouldReturnNoResultWhenNoAttributes.

@Test
public void shouldReturnNoResultWhenNoAttributes() 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<>();
    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, never()).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 65 with QueryRequest

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

the class RealmContextFilter method filterQuery.

@Override
public Promise<QueryResponse, ResourceException> filterQuery(Context context, QueryRequest request, QueryResourceHandler handler, RequestHandler next) {
    try {
        Context newContext = evaluate(context, request);
        QueryRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfQueryRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
        return next.handleQuery(evaluate(context, request), evaluatedRequest, handler);
    } catch (ResourceException e) {
        return newExceptionPromise(e);
    }
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) QueryRequest(org.forgerock.json.resource.QueryRequest) UriRouterContext(org.forgerock.http.routing.UriRouterContext) ResourceException(org.forgerock.json.resource.ResourceException)

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