Search in sources :

Example 41 with CreateRequest

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();
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 42 with CreateRequest

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();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject)

Example 43 with CreateRequest

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);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 44 with CreateRequest

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);
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 45 with CreateRequest

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);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

CreateRequest (org.forgerock.json.resource.CreateRequest)70 Test (org.testng.annotations.Test)62 ResourceResponse (org.forgerock.json.resource.ResourceResponse)42 JsonValue (org.forgerock.json.JsonValue)39 ResourceException (org.forgerock.json.resource.ResourceException)38 Context (org.forgerock.services.context.Context)27 DeleteRequest (org.forgerock.json.resource.DeleteRequest)19 ReadRequest (org.forgerock.json.resource.ReadRequest)18 RootContext (org.forgerock.services.context.RootContext)18 UriRouterContext (org.forgerock.http.routing.UriRouterContext)17 ActionRequest (org.forgerock.json.resource.ActionRequest)17 PatchRequest (org.forgerock.json.resource.PatchRequest)17 QueryRequest (org.forgerock.json.resource.QueryRequest)17 UpdateRequest (org.forgerock.json.resource.UpdateRequest)17 Request (org.forgerock.http.protocol.Request)16 AttributesContext (org.forgerock.services.context.AttributesContext)16 Subject (javax.security.auth.Subject)13 RealmContext (org.forgerock.openam.rest.RealmContext)11 EntitlementException (com.sun.identity.entitlement.EntitlementException)9 ResourceType (org.forgerock.openam.entitlement.ResourceType)9