Search in sources :

Example 46 with ResourceException

use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method createShouldIgnoreUUIDInJson.

/*
     * A UUID is supplied on create, but it is ignored
     */
@Test
public void createShouldIgnoreUUIDInJson() throws EntitlementException {
    //given
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    JsonValue content = mock(JsonValue.class);
    given(mockCreateRequest.getContent()).willReturn(content);
    given(mockCreateRequest.getNewResourceId()).willReturn(null);
    String jsonWithUuid = "{" + "    \"actions\": {" + "            \"ACTION\": true," + "            \"CREATE\": true," + "            \"DELETE\": true," + "            \"PATCH\": true," + "            \"QUERY\": true," + "            \"READ\": true," + "            \"UPDATE\": true" + "    }," + "    \"description\": \"An example resource type\"," + "    \"uuid\": \"123.456.789\"," + "    \"name\": \"myResourceType\"," + "    \"patterns\": [" + "            \"http://example.com:80/*\"" + "    ]" + "}" + ";";
    // Json has unnecessary UUID
    given(content.toString()).willReturn(jsonWithUuid);
    doCallRealMethod().when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
    //when
    Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
    //then
    assertThat(promise).succeeded();
}
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 47 with ResourceException

use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method queryShouldHandleQueryException.

@Test
public void queryShouldHandleQueryException() throws Exception {
    //given
    setupExistingResourceTypeFromJson(jsonResourceType);
    QueryRequest queryRequest = mock(QueryRequest.class);
    QueryResponseHandler queryHandler = makeQueryResponseHandler();
    Throwable t = new QueryException(QueryException.QueryErrorCode.FILTER_BOOLEAN_LITERAL_FALSE);
    when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenThrow(t);
    //when
    Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
    //then
    assertThat(promise).failedWithResourceException().withCode(ResourceException.BAD_REQUEST);
}
Also used : QueryException(org.forgerock.openam.rest.query.QueryException) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 48 with ResourceException

use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method shouldCreateResourceType.

@Test
public void shouldCreateResourceType() throws EntitlementException {
    //given
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    JsonValue content = mock(JsonValue.class);
    given(mockCreateRequest.getContent()).willReturn(content);
    given(content.toString()).willReturn(jsonResourceType);
    doCallRealMethod().when(resourceTypeService).saveResourceType(any(Subject.class), anyString(), any(ResourceType.class));
    //when
    Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
    //then
    assertThat(promise).succeeded().withContent().stringAt("name").isEqualTo("myResourceType");
}
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 49 with ResourceException

use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.

the class PolicyV1FilterTest method resourceTypeAssociationOnUpdate.

/**
     * Verifies that the appropriate resource type is associated with the policy being updated.
     */
@Test
public void resourceTypeAssociationOnUpdate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    UpdateRequest updateRequest = mock(UpdateRequest.class);
    JsonValue jsonValue = json(object(field("applicationName", "testApp")));
    given(updateRequest.getContent()).willReturn(jsonValue);
    given(applicationServiceFactory.create(subject, "/abc")).willReturn(applicationService);
    Application application = mock(Application.class);
    given(applicationService.getApplication("testApp")).willReturn(application);
    Set<String> resourceTypeUUIDs = new HashSet<>(CollectionUtils.asSet("abc-def-hij"));
    given(application.getResourceTypeUuids()).willReturn(resourceTypeUUIDs);
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterUpdate(context, updateRequest, requestHandler);
    // Then
    assertThat(promise).succeeded();
    verify(applicationServiceFactory).create(subject, "/abc");
    verify(applicationService).getApplication("testApp");
    assertThat(jsonValue.get("resourceTypeUuid").asString()).isEqualTo("abc-def-hij");
    assertThat(promise.get().getContent().contains("resourceTypeUuid")).isFalse();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Application(com.sun.identity.entitlement.Application) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 50 with ResourceException

use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method createShouldFailIfCallerSubjectNotPresent.

@Test
public void createShouldFailIfCallerSubjectNotPresent() {
    //given
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    // subject is null, which will represent a broken SSOTokenContext
    callerSubject = null;
    //when
    Promise<ResourceResponse, ResourceException> promise = resourceTypesResource.createInstance(mockServerContext, mockCreateRequest);
    //then
    assertResourcePromiseFailedWithCodes(promise, ResourceException.INTERNAL_ERROR, EntitlementException.INTERNAL_ERROR);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Aggregations

ResourceException (org.forgerock.json.resource.ResourceException)323 Test (org.testng.annotations.Test)233 ResourceResponse (org.forgerock.json.resource.ResourceResponse)179 JsonValue (org.forgerock.json.JsonValue)145 Context (org.forgerock.services.context.Context)145 RealmContext (org.forgerock.openam.rest.RealmContext)110 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)71 Subject (javax.security.auth.Subject)58 ClientContext (org.forgerock.services.context.ClientContext)56 NotFoundException (org.forgerock.json.resource.NotFoundException)47 BadRequestException (org.forgerock.json.resource.BadRequestException)44 QueryResponse (org.forgerock.json.resource.QueryResponse)43 HashSet (java.util.HashSet)42 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)42 CreateRequest (org.forgerock.json.resource.CreateRequest)40 SSOException (com.iplanet.sso.SSOException)38 ActionResponse (org.forgerock.json.resource.ActionResponse)37 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)37 Matchers.anyString (org.mockito.Matchers.anyString)37 ArrayList (java.util.ArrayList)35