Search in sources :

Example 21 with QueryResourceHandler

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

the class SubjectTypesResourceTest method testSuccessfulJsonificationAndLogicalIsCorrect.

@Test
public void testSuccessfulJsonificationAndLogicalIsCorrect() throws JsonMappingException {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    ReadRequest mockRequest = mock(ReadRequest.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<ResourceResponse, ResourceException> promise = testResource.readInstance(mockServerContext, TEST_LOGICAL_CONDITION, mockRequest);
    //then
    assertThat(promise).succeeded().withContent().hasBoolean("logical");
    assertThat(promise).succeeded().withContent().booleanAt("logical").isTrue();
}
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) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) Subject(javax.security.auth.Subject) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 22 with QueryResourceHandler

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

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

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

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

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