Search in sources :

Example 81 with ResourceException

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

the class Requester method query.

/**
     * Request to perform a query at a specified endpoint.
     *
     * @param location Endpoint destination of this request. May not be null.
     * @param queryId Specific query ID to perform. May be null.
     * @param context Context of this request.
     * @return The {@link org.forgerock.json.JsonValue} returned from the endpoint.
     * @throws ResourceException If any exception occurred during processing.
     */
public JsonValue query(String location, String queryId, Context context) throws ResourceException {
    Reject.ifTrue(StringUtils.isEmpty(location), "The endpoint destination may not be null or empty.");
    final Router rootRouter = router.get();
    final QueryRequest queryRequest = Requests.newQueryRequest(location);
    if (queryId != null) {
        queryRequest.setQueryId(queryId);
    }
    final InMemoryQueryResourceHandler resourceHandler = new InMemoryQueryResourceHandler();
    return rootRouter.handleQuery(context, queryRequest, resourceHandler).thenAsync(new AsyncFunction<QueryResponse, JsonValue, ResourceException>() {

        @Override
        public Promise<JsonValue, ResourceException> apply(QueryResponse value) {
            final JsonArray responses = JsonValueBuilder.jsonValue().array("results");
            for (ResourceResponse resource : resourceHandler.getResources()) {
                responses.add(resource.getContent());
            }
            return newResultPromise(responses.build().build());
        }
    }).getOrThrowUninterruptibly();
}
Also used : JsonArray(org.forgerock.openam.utils.JsonArray) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) JsonValue(org.forgerock.json.JsonValue) Router(org.forgerock.json.resource.Router) ResourceException(org.forgerock.json.resource.ResourceException) AsyncFunction(org.forgerock.util.AsyncFunction)

Example 82 with ResourceException

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

the class ApplicationTypesResourceTest method shouldReadInstanceCorrectly.

@Test
public void shouldReadInstanceCorrectly() throws IllegalAccessException, InstantiationException, ExecutionException, InterruptedException {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    ReadRequest request = mock(ReadRequest.class);
    ApplicationType mockApplicationType = new ApplicationType("test", null, null, null, null);
    given(mockApplicationTypeManager.getApplicationType(subject, "test")).willReturn(mockApplicationType);
    //when
    Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
    //then
    assertTrue(result.get().getId().equals("test"));
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ApplicationType(com.sun.identity.entitlement.ApplicationType) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 83 with ResourceException

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

the class ApplicationV1FilterTest method updateFailsWhenApplicationMissing.

/**
     * Verifies that update fails when the selected application cannot be found.
     */
@Test(expectedExceptions = NotFoundException.class)
public void updateFailsWhenApplicationMissing() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    // Build application JSON representation.
    JsonValue jsonValue = json(object(TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
    UpdateRequest updateRequest = mock(UpdateRequest.class);
    given(updateRequest.getContent()).willReturn(jsonValue);
    given(updateRequest.getResourcePath()).willReturn("testApplication");
    given(applicationServiceFactory.create(subject, "/abc")).willReturn(applicationService);
    given(applicationService.getApplication("testApplication")).willReturn(null);
    // When
    Promise<ResourceResponse, ResourceException> result = filter.filterUpdate(context, updateRequest, requestHandler);
    // Then
    result.getOrThrowUninterruptibly();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 84 with ResourceException

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

the class PolicyResourceDelegateTest method shouldHandleFailureToDeleteFailedCreationOfPolicies.

@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToDeleteFailedCreationOfPolicies() throws ResourceException {
    //Given
    //Given
    Context context = mock(Context.class);
    Set<JsonValue> policies = new HashSet<JsonValue>();
    JsonValue policyOne = json(object(field("name", "POLICY_ONE")));
    JsonValue policyTwo = json(object(field("name", "POLICY_TWO")));
    policies.add(policyOne);
    policies.add(policyTwo);
    ResourceResponse createdPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
    ResourceException createException = mock(ResourceException.class);
    ResourceException deleteException = mock(ResourceException.class);
    Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newExceptionPromise(createException);
    Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newExceptionPromise(deleteException);
    given(policyResource.handleCreate(eq(context), Matchers.<CreateRequest>anyObject())).willReturn(createPolicyOnePromise).willReturn(createPolicyTwoPromise);
    given(policyResource.handleDelete(eq(context), Matchers.<DeleteRequest>anyObject())).willReturn(deletePolicyOnePromise);
    //When
    try {
        delegate.createPolicies(context, policies).getOrThrowUninterruptibly();
    } catch (ResourceException e) {
        //Then
        ArgumentCaptor<DeleteRequest> requestCaptor = ArgumentCaptor.forClass(DeleteRequest.class);
        verify(policyResource).handleDelete(eq(context), requestCaptor.capture());
        assertThat(requestCaptor.getValue().getResourcePathObject().leaf()).isEqualTo("ID_1");
        assertThat(e).isEqualTo(deleteException);
        throw e;
    }
}
Also used : Context(org.forgerock.services.context.Context) ArgumentCaptor(org.mockito.ArgumentCaptor) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 85 with ResourceException

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

the class PolicyResourceDelegateTest method shouldDeletePolicies.

@Test
public void shouldDeletePolicies() throws ResourceException {
    //Given
    Context context = mock(Context.class);
    List<ResourceResponse> policies = new ArrayList<ResourceResponse>();
    ResourceResponse policyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
    ResourceResponse policyTwo = newResourceResponse("ID_2", "REVISION_2", json(object()));
    policies.add(policyOne);
    policies.add(policyTwo);
    Set<String> policyIds = new HashSet<String>();
    policyIds.add("ID_1");
    policyIds.add("ID_2");
    Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newResultPromise(policyOne);
    Promise<ResourceResponse, ResourceException> deletePolicyTwoPromise = Promises.newResultPromise(policyTwo);
    given(policyResource.handleDelete(eq(context), Matchers.<DeleteRequest>anyObject())).willReturn(deletePolicyOnePromise).willReturn(deletePolicyTwoPromise);
    //When
    List<ResourceResponse> deletedPolicies = delegate.deletePolicies(context, policyIds).getOrThrowUninterruptibly();
    //Then
    assertThat(deletedPolicies).isEqualTo(policies);
}
Also used : Context(org.forgerock.services.context.Context) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ResourceException (org.forgerock.json.resource.ResourceException)323 Test (org.testng.annotations.Test)233 ResourceResponse (org.forgerock.json.resource.ResourceResponse)179 JsonValue (org.forgerock.json.JsonValue)145 Context (org.forgerock.services.context.Context)145 RealmContext (org.forgerock.openam.rest.RealmContext)110 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)71 Subject (javax.security.auth.Subject)58 ClientContext (org.forgerock.services.context.ClientContext)56 NotFoundException (org.forgerock.json.resource.NotFoundException)47 BadRequestException (org.forgerock.json.resource.BadRequestException)44 QueryResponse (org.forgerock.json.resource.QueryResponse)43 HashSet (java.util.HashSet)42 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)42 CreateRequest (org.forgerock.json.resource.CreateRequest)40 SSOException (com.iplanet.sso.SSOException)38 ActionResponse (org.forgerock.json.resource.ActionResponse)37 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)37 Matchers.anyString (org.mockito.Matchers.anyString)37 ArrayList (java.util.ArrayList)35