Search in sources :

Example 26 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse 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 27 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse 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)

Example 28 with ResourceResponse

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

the class PolicyResourceTest method shouldReportBadCreateRequests.

@Test
public void shouldReportBadCreateRequests() throws Exception {
    // Given
    String id = "uniqueId";
    JsonValue json = new JsonValue("");
    CreateRequest request = mockCreateRequest(id, json);
    given(mockParser.parsePolicy(id, json)).willThrow(new EntitlementException(EntitlementException.INVALID_JSON, "Mock error message"));
    // When
    Promise<ResourceResponse, ResourceException> promise = policyResource.createInstance(mockServerContext, request);
    // Then
    assertThat(promise).failedWithException().isInstanceOf(BadRequestException.class);
}
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) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 29 with ResourceResponse

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

the class PolicyResourceTest method shouldDeletePoliciesFromStore.

@Test
public void shouldDeletePoliciesFromStore() throws Exception {
    // Given
    String id = "uniqueId";
    DeleteRequest request = mock(DeleteRequest.class);
    // When
    Promise<ResourceResponse, ResourceException> promise = policyResource.deleteInstance(mockServerContext, id, request);
    // Then
    verify(mockStore).delete(id);
    assertThat(promise).succeeded().withId().isNotNull();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Test(org.testng.annotations.Test)

Example 30 with ResourceResponse

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

the class ClientResourceTest method shouldCreateIdentity.

@Test
public void shouldCreateIdentity() throws SSOException, IdRepoException, ResourceException {
    // Given
    Map<String, ArrayList<String>> client = new HashMap<>();
    //must contain userpassword, realm, client_id, "com.forgerock.openam.oauth2provider.clientType"
    client.put("client_id", new ArrayList(Arrays.asList("client")));
    client.put("userpassword", new ArrayList(Arrays.asList("password")));
    client.put("realm", new ArrayList(Arrays.asList("/")));
    client.put("com.forgerock.openam.oauth2provider.clientType", new ArrayList(Arrays.asList("Public")));
    when(mockSubSchema.getAttributeSchema(eq("client"))).thenReturn(mock(AttributeSchema.class));
    when(mockSubSchema.getAttributeSchema(eq("userpassword"))).thenReturn(mock(AttributeSchema.class));
    when(mockSubSchema.getAttributeSchema(eq("realm"))).thenReturn(mock(AttributeSchema.class));
    when(mockSubSchema.getAttributeSchema(eq("com.forgerock.openam.oauth2provider.clientType"))).thenReturn(mock(AttributeSchema.class));
    CreateRequest request = mock(CreateRequest.class);
    JsonValue val = mock(JsonValue.class);
    when(request.getContent()).thenReturn(val);
    when(val.getObject()).thenReturn(client);
    Map<String, String> responseVal = new HashMap<String, String>();
    responseVal.put("success", "true");
    JsonValue response = new JsonValue(responseVal);
    ResourceResponse expectedResource = newResourceResponse("results", "1", response);
    // When
    Promise<ResourceResponse, ResourceException> createInstancePromise = resource.createInstance(null, request);
    // Then
    assertThat(createInstancePromise).succeeded().withObject().isEqualToIgnoringGivenFields(expectedResource, ResourceResponse.FIELD_REVISION, ResourceResponse.FIELD_CONTENT);
    ResourceResponse resourceResponse = createInstancePromise.getOrThrowUninterruptibly();
    assertEquals(resourceResponse.getContent().toString(), response.toString());
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) HashMap(java.util.HashMap) CreateRequest(org.forgerock.json.resource.CreateRequest) ArrayList(java.util.ArrayList) AttributeSchema(com.sun.identity.sm.AttributeSchema) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ResourceResponse (org.forgerock.json.resource.ResourceResponse)227 ResourceException (org.forgerock.json.resource.ResourceException)171 Test (org.testng.annotations.Test)154 JsonValue (org.forgerock.json.JsonValue)123 Context (org.forgerock.services.context.Context)83 RealmContext (org.forgerock.openam.rest.RealmContext)72 Subject (javax.security.auth.Subject)57 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)48 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)44 CreateRequest (org.forgerock.json.resource.CreateRequest)41 ClientContext (org.forgerock.services.context.ClientContext)41 ArrayList (java.util.ArrayList)38 EntitlementException (com.sun.identity.entitlement.EntitlementException)30 ReadRequest (org.forgerock.json.resource.ReadRequest)30 NotFoundException (org.forgerock.json.resource.NotFoundException)29 UpdateRequest (org.forgerock.json.resource.UpdateRequest)29 Matchers.anyString (org.mockito.Matchers.anyString)28 HashSet (java.util.HashSet)27 BadRequestException (org.forgerock.json.resource.BadRequestException)26 DeleteRequest (org.forgerock.json.resource.DeleteRequest)26