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();
}
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"));
}
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();
}
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;
}
}
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);
}
Aggregations