use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class UmaEnabledFilterTest method testFilterCreate.
@Test(dataProvider = "factories")
public void testFilterCreate(UmaProviderSettingsFactory factory, boolean expectFailure) throws Exception {
// Given
UmaEnabledFilter filter = new UmaEnabledFilter(factory);
CreateRequest request = Requests.newCreateRequest("test", json(object()));
// When
Promise<ResourceResponse, ResourceException> promise = filter.filterCreate(context, request, requestHandler);
// Then
checkResult(expectFailure, promise);
if (expectFailure) {
verifyNoMoreInteractions(requestHandler);
} else {
verify(requestHandler).handleCreate(context, request);
}
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class PolicyResourceWithCopyMoveSupport method copyPolicy.
private ActionResponse copyPolicy(Context context, String resourceId, ActionRequest request) throws ResourceException {
String sourceRealm = RealmContext.getRealm(context);
JsonValue payload = request.getContent().get("to");
if (payload.isNull()) {
throw new BadRequestException("to definition is missing");
}
String destinationRealm = payload.get("realm").defaultTo(sourceRealm).asString();
ReadRequest readRequest = Requests.newReadRequest("policies", resourceId);
JsonValue policy = router.handleRead(context, readRequest).getOrThrowUninterruptibly().getContent();
String sourceApplication = policy.get("applicationName").asString();
String sourceResourceType = policy.get("resourceTypeUuid").asString();
String destinationApplication = payload.get("application").defaultTo(sourceApplication).asString();
String destinationResourceTypeId = payload.get("resourceType").defaultTo(sourceResourceType).asString();
String copiedName = payload.get("name").defaultTo(resourceId).asString();
if (sourceRealm.equals(destinationRealm) && resourceId.equals(copiedName)) {
throw new BadRequestException("policy name already exists within the realm");
}
policy.put("name", copiedName);
policy.put("applicationName", destinationApplication);
policy.put("resourceTypeUuid", destinationResourceTypeId);
RealmContext updatedContext = new RealmContext(context);
updatedContext.setOverrideRealm(destinationRealm);
CreateRequest createRequest = Requests.newCreateRequest("policies", policy);
JsonValue copiedPolicy = router.handleCreate(updatedContext, createRequest).getOrThrowUninterruptibly().getContent();
return Responses.newActionResponse(copiedPolicy);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldReturnBadRequestIfCannotReturnResource.
@Test(expectedExceptions = BadRequestException.class)
public void shouldReturnBadRequestIfCannotReturnResource() throws EntitlementException, ResourceException {
//given
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSSOTokenContext);
realmContext.setSubRealm("/", "/");
CreateRequest mockCreateRequest = mock(CreateRequest.class);
Subject subject = null;
given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
doThrow(new EntitlementException(1)).when(applicationWrapper).toJsonValue();
//when
Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(realmContext, mockCreateRequest);
//then
result.getOrThrowUninterruptibly();
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ApplicationV1FilterTest method createFailsWhenNoActionsDefined.
/**
* Verifies that creation fails when no actions are defined.
*/
@Test(expectedExceptions = BadRequestException.class)
public void createFailsWhenNoActionsDefined() throws ResourceException {
// Given
// Build application JSON representation.
JsonValue jsonValue = json(object(TestData.DATA_SET_1.getResources().asJson()));
CreateRequest createRequest = mock(CreateRequest.class);
given(createRequest.getContent()).willReturn(jsonValue);
// When
Promise<ResourceResponse, ResourceException> result = filter.filterCreate(context, createRequest, requestHandler);
// Then
result.getOrThrowUninterruptibly();
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ApplicationV1FilterTest method resourceTypeAssociationOnCreate.
/**
* Verifies that the appropriate resource type is associated with the application being created.
*/
@Test
public void resourceTypeAssociationOnCreate() 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")));
CreateRequest createRequest = mock(CreateRequest.class);
given(createRequest.getContent()).willReturn(jsonValue);
Set<ResourceType> resourceTypes = CollectionUtils.asSet(ResourceType.builder().setName("test").setUUID(TestData.DATA_SET_1.getResourceTypeUuid()).build());
given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
given(requestHandler.handleCreate(eq(context), eq(createRequest))).willReturn(mockPromise);
// When
Promise<ResourceResponse, ResourceException> result = filter.filterCreate(context, createRequest, requestHandler);
// Then
assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly(TestData.DATA_SET_1.getResourceTypeUuid());
verify(requestHandler).handleCreate(eq(context), eq(createRequest));
}
Aggregations