Search in sources :

Example 16 with QueryResourceHandler

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

the class PolicyResourceTest method shouldHandleInvalidQueryErrors.

@Test
public void shouldHandleInvalidQueryErrors() throws Exception {
    // Given
    QueryRequest request = mock(QueryRequest.class);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(mockStore.query(request)).willThrow(new EntitlementException(EntitlementException.INVALID_SEARCH_FILTER));
    // When
    Promise<QueryResponse, ResourceException> promise = policyResource.queryCollection(mockServerContext, request, handler);
    // Then
    assertQueryPromiseFailedWithCodes(promise, ResourceException.BAD_REQUEST, EntitlementException.INVALID_SEARCH_FILTER);
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Test(org.testng.annotations.Test)

Example 17 with QueryResourceHandler

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

the class PolicyResourceWithCopyMoveSupport method copyOrMovePoliciesByApplication.

private ActionResponse copyOrMovePoliciesByApplication(Context context, ActionRequest request, PolicyAction copyOrMoveAction) throws ResourceException {
    JsonValue payload = request.getContent();
    JsonValue from = payload.get("from");
    JsonValue to = payload.get("to");
    if (from.isNull()) {
        throw new BadRequestException("from definition is missing");
    }
    if (!from.isDefined("application")) {
        throw new BadRequestException("from application definition is missing");
    }
    String sourceApplication = from.get("application").asString();
    if (to.isNull()) {
        throw new BadRequestException("to definition is missing");
    }
    String sourceRealm = RealmContext.getRealm(context);
    String destinationRealm = to.get("realm").defaultTo(sourceRealm).asString();
    String destinationApplication = to.get("application").defaultTo(sourceApplication).asString();
    JsonValue resourceTypeMapping = payload.get("resourceTypeMapping").defaultTo(Collections.emptyMap());
    String namePostfix = to.get("namePostfix").defaultTo("").asString();
    QueryRequest queryRequest = Requests.newQueryRequest("policies");
    queryRequest.setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), sourceApplication));
    final List<JsonValue> policies = new ArrayList<>();
    router.handleQuery(context, queryRequest, new QueryResourceHandler() {

        @Override
        public boolean handleResource(ResourceResponse resourceResponse) {
            policies.add(resourceResponse.getContent());
            return true;
        }
    }).getOrThrowUninterruptibly();
    JsonValue actionResponseContent = json(array());
    for (JsonValue policy : policies) {
        ActionResponse response = copyOrMoveGivenPolicy(context, policy, destinationRealm, destinationApplication, namePostfix, resourceTypeMapping, copyOrMoveAction);
        actionResponseContent.add(response.getJsonContent().asMap());
    }
    return Responses.newActionResponse(actionResponseContent);
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) BadRequestException(org.forgerock.json.resource.BadRequestException) JsonPointer(org.forgerock.json.JsonPointer) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) ActionResponse(org.forgerock.json.resource.ActionResponse)

Example 18 with QueryResourceHandler

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

the class DecisionCombinersResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws JsonMappingException {
    //given
    SubjectContext mockSubjectContext = mock(SubjectContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    verify(mockHandler, times(1)).handleResource(any(ResourceResponse.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 19 with QueryResourceHandler

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

the class ConditionTypesResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws JsonMappingException {
    //given
    SubjectContext mockSubjectContext = mock(SubjectContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockRequest.getPageSize()).willReturn(2);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 20 with QueryResourceHandler

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

Aggregations

QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)29 QueryRequest (org.forgerock.json.resource.QueryRequest)28 Test (org.testng.annotations.Test)26 Context (org.forgerock.services.context.Context)21 ResourceException (org.forgerock.json.resource.ResourceException)19 QueryResponse (org.forgerock.json.resource.QueryResponse)18 ResourceResponse (org.forgerock.json.resource.ResourceResponse)18 RealmContext (org.forgerock.openam.rest.RealmContext)15 Subject (javax.security.auth.Subject)11 ClientContext (org.forgerock.services.context.ClientContext)11 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)9 EntitlementException (com.sun.identity.entitlement.EntitlementException)6 JsonValue (org.forgerock.json.JsonValue)6 Matchers.anyString (org.mockito.Matchers.anyString)6 JsonSchema (com.fasterxml.jackson.databind.jsonschema.JsonSchema)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 JsonPointer (org.forgerock.json.JsonPointer)4 Application (com.sun.identity.entitlement.Application)3