Search in sources :

Example 1 with QueryRequest

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

the class PrivilegeAuthzModuleTest method crestQueryIsAllowed.

@Test
public void crestQueryIsAllowed() throws SSOException, DelegationException, ResourceException {
    // Given...
    final Set<String> actions = new HashSet<>(Arrays.asList("READ"));
    final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS, DUMB_FUNC);
    given(factory.newInstance("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS)).willReturn(permission);
    given(subjectContext.getCallerSSOToken()).willReturn(token);
    given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(true);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    Promise<QueryResponse, ResourceException> promise = Promises.newResultPromise(Responses.newQueryResponse("abc-def"));
    given(provider.queryCollection(isA(Context.class), isA(QueryRequest.class), isA(QueryResourceHandler.class))).willReturn(promise);
    // When...
    final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
    final Router router = new Router();
    router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
    final RealmContext context = new RealmContext(subjectContext);
    context.setSubRealm("abc", "abc");
    final QueryRequest request = Requests.newQueryRequest("/policies");
    Promise<QueryResponse, ResourceException> result = router.handleQuery(context, request, handler);
    // Then...
    QueryResponse response = result.getOrThrowUninterruptibly();
    assertThat(response.getPagedResultsCookie()).isEqualTo("abc-def");
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) RealmContext(org.forgerock.openam.rest.RealmContext) FilterChain(org.forgerock.json.resource.FilterChain) Router(org.forgerock.json.resource.Router) Matchers.anyString(org.mockito.Matchers.anyString) DelegationPermission(com.sun.identity.delegation.DelegationPermission) 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 2 with QueryRequest

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

the class AuditFilterTest method shouldReturnNullForQuerySuccess.

@Test
public void shouldReturnNullForQuerySuccess() {
    QueryRequest queryRequest = mock(QueryRequest.class);
    QueryResponse queryResponse = mock(QueryResponse.class);
    JsonValue filterResponse = auditFilter.getQuerySuccessDetail(queryRequest, queryResponse);
    assertThat(filterResponse).isEqualTo(null);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 3 with QueryRequest

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

the class ScriptResourceTest method shouldQueryScriptConfigurationWithPaging.

@Test
public void shouldQueryScriptConfigurationWithPaging() throws ScriptException, ResourceException {
    // given
    scriptConfigSet.clear();
    for (int i = 0; i < 9; i++) {
        ScriptConfiguration sc = ScriptConfiguration.builder().generateId().setName("MyJavaScript" + i).setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build();
        scriptConfigSet.put(sc.getId(), sc);
    }
    QueryResourceHandler resultHandler = mock(QueryResourceHandler.class);
    given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(queryRequest.getPageSize()).thenReturn(5);
    // when
    when(queryRequest.getPagedResultsOffset()).thenReturn(0);
    scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
    // then
    ArgumentCaptor<ResourceResponse> resources = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(resultHandler, times(5)).handleResource(resources.capture());
    List<ResourceResponse> responses = resources.getAllValues();
    assertThat(responses).isNotNull().hasSize(5);
    int count = 0;
    for (ResourceResponse resource : responses) {
        assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
    }
    // when
    Mockito.reset(resultHandler);
    given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    resources = ArgumentCaptor.forClass(ResourceResponse.class);
    when(queryRequest.getPagedResultsOffset()).thenReturn(5);
    scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
    verify(resultHandler, times(4)).handleResource(resources.capture());
    // then
    responses = resources.getAllValues();
    assertThat(responses).isNotNull().hasSize(4);
    for (ResourceResponse resource : responses) {
        assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
    }
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryRequest(org.forgerock.json.resource.QueryRequest) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Test(org.testng.annotations.Test)

Example 4 with QueryRequest

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

the class UmaPolicyResourceTest method shouldSuccessfullyQueryPolicies.

@Test
public void shouldSuccessfullyQueryPolicies() {
    //Given
    Context context = mock(Context.class);
    QueryRequest request = Requests.newQueryRequest("/policies");
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    QueryResponse queryResult = newQueryResponse();
    Collection<UmaPolicy> umaPolicies = new HashSet<UmaPolicy>();
    UmaPolicy policy1 = mock(UmaPolicy.class);
    UmaPolicy policy2 = mock(UmaPolicy.class);
    umaPolicies.add(policy1);
    umaPolicies.add(policy2);
    Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> promise = Promises.newResultPromise(Pair.of(queryResult, umaPolicies));
    given(policyService.queryPolicies(context, request)).willReturn(promise);
    //When
    policyResource.queryCollection(context, request, handler);
    //Then
    verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 5 with QueryRequest

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

the class UmaPolicyServiceImplTest method shouldQueryUmaPoliciesBySubjectOrUnknownResourceServer.

@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectOrUnknownResourceServer() throws Exception {
    //Given
    Context context = createContext();
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.or(QueryFilter.equalTo(new JsonPointer("permissions/subject"), "SUBJECT_ONE"), QueryFilter.equalTo(new JsonPointer("resourceServer"), "OTHER_CLIENT_ID")));
    mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
    //When
    Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request).getOrThrowUninterruptibly();
    //Then
    assertThat(queryResult.getSecond()).hasSize(1);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) JsonPointer(org.forgerock.json.JsonPointer) 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