use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class CoreTokenResourceTest method shouldGetBadRequestForMissingTokenId.
@Test
public void shouldGetBadRequestForMissingTokenId() throws CoreTokenException {
// Given
CreateRequest request = mock(CreateRequest.class);
given(request.getContent()).willReturn(new JsonValue(""));
doThrow(IllegalArgumentException.class).when(mockStore).createAsync(any(Token.class));
// When
Promise<ResourceResponse, ResourceException> promise = resource.createInstance(null, request);
// Then
assertThat(promise).failedWithException().isInstanceOf(BadRequestException.class);
}
use of org.forgerock.json.resource.CreateRequest 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);
}
});
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class AuditEventPublisherImpl method publishForRealm.
private void publishForRealm(String realm, String topic, AuditEvent auditEvent) throws ResourceException {
AMAuditService auditService = auditServiceProvider.getAuditService(realm);
Connection connection = newInternalConnection(auditService);
CreateRequest request = newCreateRequest(topic, auditEvent.getValue());
try {
connection.create(new RootContext(), request);
} catch (ServiceUnavailableException e) {
debug.message("Audit Service for realm {} is unavailable. Trying the default Audit Service.", realm, e);
publishToDefault(topic, auditEvent);
}
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldCreatePolicies.
@Test
public void shouldCreatePolicies() 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);
List<ResourceResponse> createdPolicies = new ArrayList<ResourceResponse>();
ResourceResponse createdPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
ResourceResponse createdPolicyTwo = newResourceResponse("ID_2", "REVISION_2", json(object()));
createdPolicies.add(createdPolicyOne);
createdPolicies.add(createdPolicyTwo);
Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newResultPromise(createdPolicyTwo);
given(policyResource.handleCreate(eq(context), Matchers.<CreateRequest>anyObject())).willReturn(createPolicyOnePromise).willReturn(createPolicyTwoPromise);
//When
List<ResourceResponse> returnedPolicies = delegate.createPolicies(context, policies).getOrThrowUninterruptibly();
//Then
verify(policyResource, never()).handleDelete(eq(context), Matchers.<DeleteRequest>anyObject());
assertThat(returnedPolicies).isEqualTo(createdPolicies);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldHandleFailureToCreatePoliciesByDeletingCreatedPolicies.
@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToCreatePoliciesByDeletingCreatedPolicies() 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 exception = mock(ResourceException.class);
Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newExceptionPromise(exception);
Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
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");
throw e;
}
}
Aggregations