Search in sources :

Example 26 with QueryResponse

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

the class ApplicationsResourceTest method shouldHandleApplicationFindFailure.

@Test(expectedExceptions = NotFoundException.class)
public void shouldHandleApplicationFindFailure() throws EntitlementException, ResourceException {
    // Given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    realmContext.setSubRealm("abc", "abc");
    Context serverContext = ClientContext.newInternalClientContext(realmContext);
    // Set the page size to be three starting from the second item.
    QueryRequest request = mock(QueryRequest.class);
    given(request.getPageSize()).willReturn(3);
    given(request.getPagedResultsOffset()).willReturn(1);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    EntitlementException exception = new EntitlementException(EntitlementException.APP_RETRIEVAL_ERROR);
    given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willThrow(exception);
    // When
    Promise<QueryResponse, ResourceException> result = applicationsResource.queryCollection(serverContext, request, handler);
    // Then
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) EntitlementException(com.sun.identity.entitlement.EntitlementException) Set(java.util.Set) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) 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 27 with QueryResponse

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

the class UmaPolicyApplicationListener method deletePolicies.

private void deletePolicies(String realm, String resourceServerId) {
    RealmContext realmContext = new RealmContext(new RootContext());
    realmContext.setDnsAlias("/", realm);
    final Context context = new AdminSubjectContext(logger, sessionCache, realmContext);
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), resourceServerId));
    final List<ResourceResponse> resources = new ArrayList<>();
    policyResource.handleQuery(context, request, new QueryResourceHandler() {

        @Override
        public boolean handleResource(ResourceResponse resource) {
            resources.add(resource);
            return true;
        }
    }).thenAsync(new AsyncFunction<QueryResponse, List<ResourceResponse>, ResourceException>() {

        @Override
        public Promise<List<ResourceResponse>, ResourceException> apply(QueryResponse response) {
            List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
            for (ResourceResponse policy : resources) {
                DeleteRequest deleteRequest = Requests.newDeleteRequest("", policy.getId());
                promises.add(policyResource.handleDelete(context, deleteRequest));
            }
            Promise<List<ResourceResponse>, ResourceException> when = Promises.when(promises);
            return when;
        }
    }).thenOnException(new ExceptionHandler<ResourceException>() {

        @Override
        public void handleException(ResourceException error) {
            logger.error(error.getReason());
        }
    });
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) Context(org.forgerock.services.context.Context) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) ArrayList(java.util.ArrayList) JsonPointer(org.forgerock.json.JsonPointer) AsyncFunction(org.forgerock.util.AsyncFunction) RootContext(org.forgerock.services.context.RootContext) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) DenyOverride(com.sun.identity.entitlement.DenyOverride) DeleteRequest(org.forgerock.json.resource.DeleteRequest)

Example 28 with QueryResponse

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

the class UmaPolicyServiceImpl method internalReadPolicy.

/**
     * {@inheritDoc}
     */
private Promise<UmaPolicy, ResourceException> internalReadPolicy(final Context context, final String resourceSetId) {
    String resourceOwnerUid = getResourceOwnerUid(context);
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("resourceTypeUuid"), resourceSetId), QueryFilter.equalTo(new JsonPointer("createdBy"), resourceOwnerUid)));
    return policyResourceDelegate.queryPolicies(context, request).thenAsync(new AsyncFunction<Pair<QueryResponse, List<ResourceResponse>>, UmaPolicy, ResourceException>() {

        @Override
        public Promise<UmaPolicy, ResourceException> apply(Pair<QueryResponse, List<ResourceResponse>> value) {
            try {
                if (value.getSecond().isEmpty()) {
                    return new NotFoundException("UMA Policy not found, " + resourceSetId).asPromise();
                } else {
                    ResourceSetDescription resourceSet = getResourceSet(getRealm(context), resourceSetId);
                    UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, value.getSecond());
                    return newResultPromise(umaPolicy);
                }
            } catch (ResourceException e) {
                return e.asPromise();
            }
        }
    });
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) NotFoundException(org.forgerock.json.resource.NotFoundException) JsonPointer(org.forgerock.json.JsonPointer) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Promise(org.forgerock.util.promise.Promise) 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) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair)

Example 29 with QueryResponse

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

the class ResourceSetResourceTest method nameQueryShouldBeSupported.

@Test
public void nameQueryShouldBeSupported() throws Exception {
    //Given
    Context context = mock(Context.class);
    QueryRequest request = mock(QueryRequest.class);
    given(request.getFields()).willReturn(Arrays.asList(new JsonPointer("/fred")));
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    ResourceSetDescription resourceSet = mock(ResourceSetDescription.class);
    QueryFilter<JsonPointer> queryFilter = QueryFilter.and(QueryFilter.equalTo(new JsonPointer("/name"), "NAME"), QueryFilter.equalTo(new JsonPointer("/resourceServer"), "myclient"), QueryFilter.equalTo(new JsonPointer("/policy/permissions/subject"), "SUBJECT"));
    Promise<Collection<ResourceSetDescription>, ResourceException> resourceSetsPromise = Promises.newResultPromise((Collection<ResourceSetDescription>) asSet(resourceSet));
    given(contextHelper.getRealm(context)).willReturn("REALM");
    given(contextHelper.getUserId(context)).willReturn("RESOURCE_OWNER_ID");
    given(request.getQueryFilter()).willReturn(queryFilter);
    given(resourceSetService.getResourceSets(eq(context), eq("REALM"), Matchers.<ResourceSetWithPolicyQuery>anyObject(), eq("RESOURCE_OWNER_ID"), eq(false))).willReturn(resourceSetsPromise);
    //When
    Promise<QueryResponse, ResourceException> promise = resource.queryCollection(context, request, handler);
    //Then
    ArgumentCaptor<ResourceSetWithPolicyQuery> queryCaptor = ArgumentCaptor.forClass(ResourceSetWithPolicyQuery.class);
    verify(resourceSetService).getResourceSets(eq(context), eq("REALM"), queryCaptor.capture(), eq("RESOURCE_OWNER_ID"), eq(false));
    assertThat(queryCaptor.getValue().getOperator()).isEqualTo(AggregateQuery.Operator.AND);
    assertThat(queryCaptor.getValue().getPolicyQuery()).isEqualTo(QueryFilter.equalTo(new JsonPointer("/permissions/subject"), "SUBJECT"));
    assertThat(queryCaptor.getValue().getResourceSetQuery()).isEqualTo(QueryFilter.and(QueryFilter.equalTo("name", "NAME"), QueryFilter.equalTo("clientId", "myclient")));
    assertThat(promise).succeeded().withObject().isNotNull();
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) JsonPointer(org.forgerock.json.JsonPointer) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Test(org.testng.annotations.Test)

Example 30 with QueryResponse

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

the class PendingRequestResourceTest method shouldQueryPendingRequests.

@Test
public void shouldQueryPendingRequests() throws Exception {
    //Given
    Context context = mockContext("REALM");
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.<JsonPointer>alwaysTrue());
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    mockPendingRequestsForUser("alice", "REALM", 2);
    //When
    Promise<QueryResponse, ResourceException> promise = resource.queryCollection(context, request, handler);
    //Then
    verify(handler, times(2)).handleResource(any(ResourceResponse.class));
    assertThat(promise).succeeded();
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) 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