Search in sources :

Example 61 with CreateRequest

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

the class ScriptResourceTest method shouldFailIfNoScriptIsSuppliedOnCreate.

@Test(expectedExceptions = BadRequestException.class)
public void shouldFailIfNoScriptIsSuppliedOnCreate() throws ResourceException {
    // given
    JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_LANGUAGE, "JAVASCRIPT"), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
    CreateRequest createRequest = mock(CreateRequest.class);
    when(createRequest.getContent()).thenReturn(requestJson);
    // when
    scriptResource.createInstance(context, createRequest).getOrThrowUninterruptibly();
// then - exception
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 62 with CreateRequest

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

the class ScriptResourceTest method shouldFailIfNoLanguageIsSuppliedOnCreate.

@Test(expectedExceptions = BadRequestException.class)
public void shouldFailIfNoLanguageIsSuppliedOnCreate() throws ResourceException {
    // given
    JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_TEXT, encodeScript), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
    CreateRequest createRequest = mock(CreateRequest.class);
    when(createRequest.getContent()).thenReturn(requestJson);
    // when
    scriptResource.createInstance(context, createRequest).getOrThrowUninterruptibly();
// then - exception
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 63 with CreateRequest

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

the class PolicyResourceTest method shouldAcceptConsistentPolicyNamesFromURLandJSON.

@Test
public void shouldAcceptConsistentPolicyNamesFromURLandJSON() throws Exception {
    // Given
    String policyName = "policyName";
    // Policy name can be specified in *both* URL and JSON so long as it is equal
    JsonValue json = JsonValue.json(JsonValue.object(JsonValue.field("name", policyName)));
    CreateRequest request = mockCreateRequest(policyName, json);
    Privilege policy = mockPrivilege(policyName, 123l);
    given(mockParser.parsePolicy(policyName, json)).willReturn(policy);
    // When
    policyResource.createInstance(mockServerContext, request);
    // Then
    verify(mockParser).parsePolicy(policyName, json);
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Privilege(com.sun.identity.entitlement.Privilege) Test(org.testng.annotations.Test)

Example 64 with CreateRequest

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

the class PolicyV1FilterTest method createFailsWhenMissingApplication.

/**
     * Verifies that the application name missing from the request is handled appropriately.
     */
@Test
public void createFailsWhenMissingApplication() {
    // Given
    CreateRequest createRequest = mock(CreateRequest.class);
    JsonValue jsonValue = json(object());
    given(createRequest.getContent()).willReturn(jsonValue);
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterCreate(context, createRequest, requestHandler);
    // Then
    assertThat(promise).failedWithResourceException().withCode(ResourceException.BAD_REQUEST);
    // Request should not be forwarded.
    verifyNoMoreInteractions(requestHandler);
}
Also used : 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 65 with CreateRequest

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

the class PolicyV1FilterTest method resourceTypeAssociationOnCreate.

/**
     * Verifies that the appropriate resource type is associated with the policy being created.
     */
@Test
public void resourceTypeAssociationOnCreate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    CreateRequest createRequest = mock(CreateRequest.class);
    JsonValue jsonValue = json(object(field("applicationName", "testApp")));
    given(createRequest.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.filterCreate(context, createRequest, 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) CreateRequest(org.forgerock.json.resource.CreateRequest) 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)

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