Search in sources :

Example 6 with QueryResourceHandler

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

the class ApplicationsResourceTest method shouldHandleJsonParsingFailure.

@Test(expectedExceptions = InternalServerErrorException.class)
public void shouldHandleJsonParsingFailure() throws EntitlementException, 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 {
                // Throws an EntitlementException when attempting to parse the json.
                EntitlementException entitlementException = new EntitlementException(1);
                given(wrapper.toJsonValue()).willThrow(entitlementException);
            } catch (EntitlementException e) {
                fail();
            }
            return wrapper;
        }
    };
    // 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);
    Set<String> appNames = asOrderedSet("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));
    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) Set(java.util.Set) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ApplicationTypeManagerWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper) 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)

Example 7 with QueryResourceHandler

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

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

the class ApplicationsResourceTest method reservedInternalAppIsMappedDuringQuery.

@Test
public void reservedInternalAppIsMappedDuringQuery() throws EntitlementException, IllegalAccessException, InstantiationException {
    // 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 = JsonValueBuilder.jsonValue().put("name", "agentProtectedApplication").build();
                given(wrapper.toJsonValue()).willReturn(jsonValue);
            } catch (EntitlementException e) {
                fail();
            }
            return wrapper;
        }
    };
    // Given...
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    realmContext.setSubRealm("abc", "abc");
    Context serverContext = ClientContext.newInternalClientContext(realmContext);
    QueryRequest request = mock(QueryRequest.class);
    given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    Set<String> appNames = asSet("iPlanetAMWebAgentService");
    given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
    Application app = mock(Application.class);
    given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq("iPlanetAMWebAgentService"))).willReturn(app);
    given(app.getName()).willReturn("agentProtectedApplication");
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    // When...
    applicationsResource.queryCollection(serverContext, request, handler);
    // Then...
    verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
    verify(applicationManagerWrapper).getApplication(eq(subject), eq("/abc"), anyString());
    ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(handler).handleResource(resourceCapture.capture());
    ResourceResponse resource = resourceCapture.getValue();
    assertThat(resource.getId()).isEqualTo("agentProtectedApplication");
}
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) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Application(com.sun.identity.entitlement.Application) Test(org.testng.annotations.Test)

Example 9 with QueryResourceHandler

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

the class ResourceSetResourceTest method queryShouldNotBeSupported.

@Test
public void queryShouldNotBeSupported() {
    //Given
    Context context = mock(Context.class);
    QueryRequest request = mock(QueryRequest.class);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(request.getQueryFilter()).willReturn(QueryFilter.equalTo(new JsonPointer("/fred"), 5));
    //When
    Promise<QueryResponse, ResourceException> promise = resource.queryCollection(context, request, handler);
    //Then
    assertThat(promise).failedWithException().isInstanceOf(NotSupportedException.class);
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) JsonPointer(org.forgerock.json.JsonPointer) Test(org.testng.annotations.Test)

Example 10 with QueryResourceHandler

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

the class PolicyResourceDelegateTest method shouldQueryPoliciesWithHandler.

@Test
public void shouldQueryPoliciesWithHandler() throws ResourceException {
    //Given
    Context context = mock(Context.class);
    QueryRequest request = mock(QueryRequest.class);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    //When
    delegate.queryPolicies(context, request, handler);
    //Then
    verify(policyResource).handleQuery(context, request, handler);
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) 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