use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ApplicationV1FilterTest method resourceTypeCreationOnCreate.
/**
* Verifies that the appropriate resource type is created for the application being created.
*/
@Test
public void resourceTypeCreationOnCreate() throws Exception {
// Given
given(contextHelper.getRealm(context)).willReturn("/abc");
given(contextHelper.getSubject(context)).willReturn(subject);
// Build application JSON representation.
JsonValue jsonValue = json(object(field("name", "testApplication"), TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
CreateRequest createRequest = mock(CreateRequest.class);
given(createRequest.getContent()).willReturn(jsonValue);
Set<ResourceType> resourceTypes = Collections.emptySet();
given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
ResourceType resourceType = ResourceType.builder().setName("test").setUUID("some-test-uuid").setActions(TestData.DATA_SET_1.getActions().getUnderlyingMap()).setPatterns(TestData.DATA_SET_1.getResources().getUnderlyingSet()).build();
given(resourceTypeService.saveResourceType(eq(subject), eq("/abc"), resourceTypeCaptor.capture())).willReturn(resourceType);
// When
filter.filterCreate(context, createRequest, requestHandler);
// Then
assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly("some-test-uuid");
verify(requestHandler).handleCreate(eq(context), eq(createRequest));
ResourceType capturedResourceType = resourceTypeCaptor.getValue();
assertThat(capturedResourceType.getName()).startsWith("testApplicationResourceType");
assertThat(capturedResourceType.getActions()).isEqualTo(TestData.DATA_SET_1.getActions().getUnderlyingMap());
assertThat(capturedResourceType.getPatterns()).isEqualTo(TestData.DATA_SET_1.getResources().getUnderlyingSet());
}
use of org.forgerock.json.resource.CreateRequest 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.CreateRequest in project OpenAM by OpenRock.
the class ResourceSetResourceTest method createShouldNotBeSupported.
@Test
public void createShouldNotBeSupported() {
//Given
Context context = mock(Context.class);
CreateRequest request = mock(CreateRequest.class);
//When
Promise<ResourceResponse, ResourceException> instancePromise = resource.createInstance(context, request);
//Then
assertThat(instancePromise).failedWithException().isInstanceOf(NotSupportedException.class);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class PrivilegeAuthzModuleTest method crestRequestNotAllowed.
@Test
public void crestRequestNotAllowed() throws SSOException, DelegationException {
// Given...
final Set<String> actions = new HashSet<>(Arrays.asList("MODIFY"));
final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS, DUMB_FUNC);
given(factory.newInstance("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS)).willReturn(permission);
given(subjectContext.getCallerSSOToken()).willReturn(token);
given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(false);
// When...
final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
final Router router = new Router();
router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
final RealmContext context = new RealmContext(subjectContext);
context.setSubRealm("abc", "abc");
final CreateRequest request = Requests.newCreateRequest("/policies", JsonValue.json(new Object()));
Promise<ResourceResponse, ResourceException> promise = router.handleCreate(context, request);
// Then...
assertThat(promise).failedWithException().isInstanceOf(ForbiddenException.class);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class RealmContextFilterTest method filterShouldConsumeRealmFromRequestWithUriRealmAndOverrideRealm.
@Test
public void filterShouldConsumeRealmFromRequestWithUriRealmAndOverrideRealm() throws Exception {
//Given
Context context = mockContext(SUB_REALM + "/" + ENDPOINT_PATH_ELEMENT);
Request request = createRequest(HOSTNAME, SUB_REALM + "/" + ENDPOINT_PATH_ELEMENT + "?realm=" + OVERRIDE_REALM);
mockDnsAlias(HOSTNAME, "/");
mockRealmAlias("/" + SUB_REALM, "/" + SUB_REALM);
mockRealmAlias(OVERRIDE_REALM_ALIAS, OVERRIDE_REALM);
//When
filter.filter(context, request, handler);
//Then
ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
verify(handler).handle(contextCaptor.capture(), eq(request));
verifyRealmContext(contextCaptor.getValue(), "", "/" + SUB_REALM, OVERRIDE_REALM);
verifyUriRouterContext(contextCaptor.getValue(), SUB_REALM);
}
Aggregations