Search in sources :

Example 51 with QueryResponse

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

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

the class QueryResponsePresentation method perform.

/**
     * Given a set of JsonValue results from a query, convert them to the required {@link ResourceResponse}
     * and apply them to the given handler.
     *
     * This function implicitly supports paging and sorting, based on the values supplied in the
     * {@link QueryRequest}.
     *
     * The QueryResponse is wrapped in the appropriate Promise for convenience. The response will indicate
     * either the total number of results, or the remaining depending on the request.
     * See {@link #enableDeprecatedRemainingQueryResponse(QueryRequest)} for details.
     *
     * @param handler Non null QueryResourceHandler which will receive the results from the query.
     * @param request Non null QueryRequest required to determine how results should be processed before returning.
     * @param resources Non null, possibly empty list of ResourceResponse results from a query.
     * @return Non null Promise containing the QueryResponse which includes a count of the results remaining.
     */
public static Promise<QueryResponse, ResourceException> perform(QueryResourceHandler handler, QueryRequest request, List<ResourceResponse> resources) {
    if (isSortingRequested(request)) {
        resources = sortItems(request, resources);
    }
    if (isPagingRequested(request)) {
        handler = createPagingHandler(handler, request);
    }
    int handledCount = handleResources(handler, resources);
    QueryResponse response = generateQueryResponse(request, resources.size(), handledCount);
    return Promises.newResultPromise(response);
}
Also used : QueryResponse(org.forgerock.json.resource.QueryResponse)

Example 53 with QueryResponse

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

the class ScriptResourceTest method shouldQueryScriptConfigurationWithoutError.

@Test
public void shouldQueryScriptConfigurationWithoutError() throws Exception {
    // given
    scriptConfigSet.clear();
    scriptConfigSet.put("1234567890", ScriptConfiguration.builder().setId("1234567890").setName("MyJavaScript").setDescription("A test JavaScript configuration").setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build());
    scriptConfigSet.put("0987654321", ScriptConfiguration.builder().setId("0987654321").setName("MyGroovyScript").setDescription("A test Groovy script configuration").setScript(script).setLanguage(GROOVY).setContext(POLICY_CONDITION).build());
    QueryRequest queryRequest = mock(QueryRequest.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    // when
    Promise<QueryResponse, ResourceException> promise = scriptResource.queryCollection(context, queryRequest, mockHandler);
    QueryResponse response = promise.getOrThrowUninterruptibly();
    // then
    assertNotNull(response);
    ArgumentCaptor<ResourceResponse> responses = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(mockHandler, times(2)).handleResource(responses.capture());
    Iterator<ResourceResponse> iterator = responses.getAllValues().iterator();
    JsonValue responseJsonOne = iterator.next().getContent();
    JsonValue responseJsonTwo = iterator.next().getContent();
    assertEquals(responseJsonOne.get(JSON_UUID).asString(), "1234567890");
    assertEquals(responseJsonOne.get(SCRIPT_NAME).asString(), "MyJavaScript");
    assertEquals(responseJsonOne.get(SCRIPT_DESCRIPTION).asString(), "A test JavaScript configuration");
    assertEquals(responseJsonOne.get(SCRIPT_TEXT).asString(), encodeScript);
    assertEquals(getLanguageFromString(responseJsonOne.get(SCRIPT_LANGUAGE).asString()), JAVASCRIPT);
    assertEquals(getContextFromString(responseJsonOne.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
    assertEquals(responseJsonTwo.get(JSON_UUID).asString(), "0987654321");
    assertEquals(responseJsonTwo.get(SCRIPT_NAME).asString(), "MyGroovyScript");
    assertEquals(responseJsonTwo.get(SCRIPT_DESCRIPTION).asString(), "A test Groovy script configuration");
    assertEquals(responseJsonTwo.get(SCRIPT_TEXT).asString(), encodeScript);
    assertEquals(getLanguageFromString(responseJsonTwo.get(SCRIPT_LANGUAGE).asString()), GROOVY);
    assertEquals(getContextFromString(responseJsonTwo.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Test(org.testng.annotations.Test)

Example 54 with QueryResponse

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

the class RequesterTest method shouldExecuteQueryOperation.

@Test
public void shouldExecuteQueryOperation() throws ResourceException {
    // given
    String location = "location";
    String queryId = "queryId";
    JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } ");
    // Create mocks
    Promise<QueryResponse, ResourceException> mockResponse = mock(Promise.class);
    QueryResponse mockResourceResponse = mock(QueryResponse.class);
    Promise<JsonValue, ResourceException> asyncQueryResponse = mock(Promise.class);
    // Stub mocks
    given(mockRealmRouterProvider.get()).willReturn(mockRouter);
    given(mockRouter.handleQuery(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willReturn(mockResponse);
    given(mockResponse.thenAsync(any(AsyncFunction.class))).willReturn(asyncQueryResponse);
    given(asyncQueryResponse.getOrThrowUninterruptibly()).willReturn(expectedResult);
    // when
    JsonValue actualResult = requester.query(location, queryId, mockServerContext);
    // then
    verify(mockRealmRouterProvider, times(1)).get();
    verifyNoMoreInteractions(mockRealmRouterProvider);
    verify(mockRouter, times(1)).handleQuery(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class));
    verifyNoMoreInteractions(mockRouter);
    verify(mockResponse, times(1)).thenAsync(any(AsyncFunction.class));
    verifyNoMoreInteractions(mockResponse);
    verify(asyncQueryResponse, times(1)).getOrThrowUninterruptibly();
    verifyNoMoreInteractions(mockResourceResponse);
    assertThat(actualResult).isEqualTo(expectedResult);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) AsyncFunction(org.forgerock.util.AsyncFunction) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 55 with QueryResponse

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

the class PolicyV1FilterTest method forwardOnQuery.

/**
     * Verify that query requests are forwarded on and that the response is tailored to represent v1.0 policy.
     */
@Test
public void forwardOnQuery() {
    // Given
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(requestHandler.handleQuery(any(Context.class), any(QueryRequest.class), queryResultHandlerCaptor.capture())).thenReturn(Promises.<QueryResponse, ResourceException>newResultPromise(newQueryResponse()));
    // When
    Promise<QueryResponse, ResourceException> promise = filter.filterQuery(context, queryRequest, queryResultHandler, requestHandler);
    // Then
    assertThat(promise).succeeded();
    verify(requestHandler).handleQuery(eq(context), eq(queryRequest), queryResultHandlerCaptor.capture());
    // Now exercise the internal static result handler.
    exerciseTransformationHandler(queryResultHandlerCaptor.getValue());
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) 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