use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldThrowInternalErrorIfResourceWillNotSave.
@Test(expectedExceptions = InternalServerErrorException.class)
public void shouldThrowInternalErrorIfResourceWillNotSave() throws EntitlementException, ResourceException {
//given
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSSOTokenContext);
realmContext.setSubRealm("/", "/");
CreateRequest mockCreateRequest = mock(CreateRequest.class);
Subject subject = new Subject();
Application mockApplication = mock(Application.class);
given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
given(applicationWrapper.getApplication()).willReturn(mockApplication);
given(mockApplication.getName()).willReturn("newApplication");
doThrow(new EntitlementException(1)).when(applicationManagerWrapper).saveApplication(any(Subject.class), anyString(), any(Application.class));
//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 ResourceTypesResourceTest method setupExistingResourceTypeFromJson.
private ResourceResponse setupExistingResourceTypeFromJson(String jsonResourceType) throws EntitlementException, ExecutionException, InterruptedException {
//given
CreateRequest createRequest = mock(CreateRequest.class);
JsonValue requestContent = mock(JsonValue.class);
given(createRequest.getContent()).willReturn(requestContent);
given(requestContent.toString()).willReturn(jsonResourceType);
doCallRealMethod().when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
//when
Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, createRequest);
//then
assertThat(promise).succeeded().withId();
return promise.get();
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method createShouldFailIfResourceTypeNameAbsent.
/*
* This seems to throw an unchecked exception (NPE) when the name is not present
*/
@Test
public void createShouldFailIfResourceTypeNameAbsent() throws EntitlementException {
//given
JsonValue content = mock(JsonValue.class);
CreateRequest mockCreateRequest = mock(CreateRequest.class);
given(mockCreateRequest.getContent()).willReturn(content);
// the name attribute is required, but here replaced by an unrecognised attribute
given(content.toString()).willReturn(jsonResourceType.replaceAll("\"name\":.*,", ""));
doCallRealMethod().when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
//when
Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
//then
assertResourcePromiseFailedWithCodes(promise, ResourceException.BAD_REQUEST, EntitlementException.MISSING_RESOURCE_TYPE_NAME);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method createShouldHandleResourceTypeServiceFailure.
@Test
public void createShouldHandleResourceTypeServiceFailure() throws EntitlementException {
//given
CreateRequest mockCreateRequest = mock(CreateRequest.class);
JsonValue content = mock(JsonValue.class);
given(mockCreateRequest.getContent()).willReturn(content);
given(mockCreateRequest.getNewResourceId()).willReturn(null);
given(content.toString()).willReturn(jsonResourceType);
// simulate failure in the resource type service
Throwable t = new EntitlementException(EntitlementException.RESOURCE_TYPE_ALREADY_EXISTS);
Mockito.doThrow(t).when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
//when
Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
//then
assertResourcePromiseFailedWithCodes(promise, ResourceException.CONFLICT, EntitlementException.RESOURCE_TYPE_ALREADY_EXISTS);
}
use of org.forgerock.json.resource.CreateRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method createShouldFailIfJsonResourceTypeInvalid.
@Test
public void createShouldFailIfJsonResourceTypeInvalid() throws EntitlementException {
//given
JsonValue content = mock(JsonValue.class);
CreateRequest mockCreateRequest = mock(CreateRequest.class);
given(mockCreateRequest.getContent()).willReturn(content);
// the name attribute is required, but here replaced by an unrecognised attribute
given(content.toString()).willReturn(jsonResourceType.replaceAll("\"name\"", "\"id\""));
doCallRealMethod().when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
//when
Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
//then
assertResourcePromiseFailedWithCodes(promise, ResourceException.BAD_REQUEST, EntitlementException.INVALID_CLASS);
}
Aggregations