Search in sources :

Example 1 with QueryResourceHandler

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

the class AuditRestRouteProvider method addResourceRoutes.

@Override
public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
    rootRouter.route("global-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).toRequestHandler(STARTS_WITH, auditServiceProvider.getDefaultAuditService());
    rootRouter.route("realm-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).through(RealmContextFilter.class).toRequestHandler(STARTS_WITH, new RequestHandler() {

        @Override
        public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest actionRequest) {
            return getAuditService(context).handleAction(context, actionRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest createRequest) {
            return getAuditService(context).handleCreate(context, createRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest deleteRequest) {
            return getAuditService(context).handleDelete(context, deleteRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest patchRequest) {
            return getAuditService(context).handlePatch(context, patchRequest);
        }

        @Override
        public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest queryRequest, QueryResourceHandler queryResourceHandler) {
            return getAuditService(context).handleQuery(context, queryRequest, queryResourceHandler);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest readRequest) {
            return getAuditService(context).handleRead(context, readRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest updateRequest) {
            return getAuditService(context).handleUpdate(context, updateRequest);
        }

        private AMAuditService getAuditService(Context context) {
            String realm = context.asContext(RealmContext.class).getResolvedRealm();
            if (StringUtils.isEmpty(realm)) {
                logger.warn("Context contained RealmContext but had an empty resolved realm");
                return auditServiceProvider.getDefaultAuditService();
            }
            return auditServiceProvider.getAuditService(realm);
        }
    });
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) AuditEndpointAuditFilter(org.forgerock.openam.rest.fluent.AuditEndpointAuditFilter) UpdateRequest(org.forgerock.json.resource.UpdateRequest) CreateRequest(org.forgerock.json.resource.CreateRequest) AMAuditService(org.forgerock.openam.audit.AMAuditService) PatchRequest(org.forgerock.json.resource.PatchRequest) Promise(org.forgerock.util.promise.Promise) RequestHandler(org.forgerock.json.resource.RequestHandler) ActionRequest(org.forgerock.json.resource.ActionRequest) RealmContextFilter(org.forgerock.openam.rest.RealmContextFilter) SpecialOrAdminOrAgentAuthzModule(org.forgerock.openam.rest.authz.SpecialOrAdminOrAgentAuthzModule) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) DeleteRequest(org.forgerock.json.resource.DeleteRequest) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 2 with QueryResourceHandler

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

the class TrustedDevicesResourceTest method shouldQueryTrustedDevices.

@Test
public void shouldQueryTrustedDevices() throws ResourceException {
    //Given
    QueryRequest request = Requests.newQueryRequest("");
    Connection connection = newInternalConnection(newCollection(resource));
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    List<JsonValue> devices = new ArrayList<>();
    devices.add(json(object(field("name", "NAME_1"), field("lastSelectedDate", new Date().getTime()))));
    devices.add(json(object(field("name", "NAME_2"), field("lastSelectedDate", new Date().getTime() + 1000))));
    given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
    //When
    connection.query(ctx(), request, handler);
    //Then
    verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) Connection(org.forgerock.json.resource.Connection) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 3 with QueryResourceHandler

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

the class PolicyResourceTest method shouldHandleQueryRequests.

@Test
public void shouldHandleQueryRequests() throws Exception {
    // Given
    QueryRequest request = mock(QueryRequest.class);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    List<Privilege> policies = Arrays.<Privilege>asList(new StubPrivilege("one"), new StubPrivilege("two"));
    given(mockStore.query(request)).willReturn(policies);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    // When
    policyResource.queryCollection(mockServerContext, request, handler);
    // Then
    verify(handler, times(policies.size())).handleResource(any(ResourceResponse.class));
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Privilege(com.sun.identity.entitlement.Privilege) Test(org.testng.annotations.Test)

Example 4 with QueryResourceHandler

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

the class SubjectTypesResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws Exception {
    //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);
    QueryRequest mockRequest = mock(QueryRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    given(mockRequest.getPageSize()).willReturn(2);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<QueryResponse, ResourceException> promise = testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    assertThat(promise).succeeded();
    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) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) 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) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResponse(org.forgerock.json.resource.QueryResponse) 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) Test(org.testng.annotations.Test)

Example 5 with QueryResourceHandler

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

the class ApplicationsResourceTest method shouldReturnThreeResultsOnQuery.

@Test
public void shouldReturnThreeResultsOnQuery() throws EntitlementException, IllegalAccessException, InstantiationException, ResourceException {
    // Override the creation of the application wrapper so to return a mocked version.
    applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {

        @Override
        protected ApplicationWrapper createApplicationWrapper(Application application, ApplicationTypeManagerWrapper type) {
            ApplicationWrapper wrapper = mock(ApplicationWrapper.class);
            String appName = application.getName();
            given(wrapper.getName()).willReturn(appName);
            try {
                JsonValue jsonValue = json(object(field("name", appName)));
                given(wrapper.toJsonValue()).willReturn(jsonValue);
            } catch (EntitlementException e) {
                fail();
            }
            return wrapper;
        }
    };
    // Given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    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.getAdditionalParameter(QueryResponsePresentation.REMAINING)).willReturn("true");
    given(request.getPageSize()).willReturn(3);
    given(request.getPagedResultsOffset()).willReturn(1);
    given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    Set<String> appNames = asSet("app1", "app2", "app3", "app4", "app5", "iPlanetAMWebAgentService");
    given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
    for (String appName : appNames) {
        Application app = mock(Application.class);
        given(app.getName()).willReturn(appName);
        given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq(appName))).willReturn(app);
    }
    // When
    Promise<QueryResponse, ResourceException> result = applicationsResource.queryCollection(serverContext, request, handler);
    // Then
    verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
    verify(applicationManagerWrapper, times(appNames.size())).getApplication(eq(subject), eq("/abc"), anyString());
    ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(handler, times(3)).handleResource(resourceCapture.capture());
    List<String> selectedApps = transformList(resourceCapture.getAllValues(), new ResourceToIdMapper());
    assertThat(selectedApps).containsOnly("app2", "app3", "app4");
    QueryResponse response = result.getOrThrowUninterruptibly();
    assertThat(response.getRemainingPagedResults()).isEqualTo(2);
}
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) Set(java.util.Set) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ApplicationTypeManagerWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper) JsonValue(org.forgerock.json.JsonValue) Matchers.anyString(org.mockito.Matchers.anyString) Subject(javax.security.auth.Subject) EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) 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) Application(com.sun.identity.entitlement.Application) 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