Search in sources :

Example 66 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 67 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 68 with CreateRequest

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

the class PolicyResourceDelegateTest method shouldCreatePolicies.

@Test
public void shouldCreatePolicies() throws ResourceException {
    //Given
    //Given
    Context context = mock(Context.class);
    Set<JsonValue> policies = new HashSet<JsonValue>();
    JsonValue policyOne = json(object(field("name", "POLICY_ONE")));
    JsonValue policyTwo = json(object(field("name", "POLICY_TWO")));
    policies.add(policyOne);
    policies.add(policyTwo);
    List<ResourceResponse> createdPolicies = new ArrayList<ResourceResponse>();
    ResourceResponse createdPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
    ResourceResponse createdPolicyTwo = newResourceResponse("ID_2", "REVISION_2", json(object()));
    createdPolicies.add(createdPolicyOne);
    createdPolicies.add(createdPolicyTwo);
    Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newResultPromise(createdPolicyTwo);
    given(policyResource.handleCreate(eq(context), Matchers.<CreateRequest>anyObject())).willReturn(createPolicyOnePromise).willReturn(createPolicyTwoPromise);
    //When
    List<ResourceResponse> returnedPolicies = delegate.createPolicies(context, policies).getOrThrowUninterruptibly();
    //Then
    verify(policyResource, never()).handleDelete(eq(context), Matchers.<DeleteRequest>anyObject());
    assertThat(returnedPolicies).isEqualTo(createdPolicies);
}
Also used : Context(org.forgerock.services.context.Context) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 69 with CreateRequest

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

the class PolicyResourceDelegateTest method shouldHandleFailureToCreatePoliciesByDeletingCreatedPolicies.

@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToCreatePoliciesByDeletingCreatedPolicies() throws ResourceException {
    //Given
    //Given
    Context context = mock(Context.class);
    Set<JsonValue> policies = new HashSet<JsonValue>();
    JsonValue policyOne = json(object(field("name", "POLICY_ONE")));
    JsonValue policyTwo = json(object(field("name", "POLICY_TWO")));
    policies.add(policyOne);
    policies.add(policyTwo);
    ResourceResponse createdPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
    ResourceException exception = mock(ResourceException.class);
    Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newExceptionPromise(exception);
    Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    given(policyResource.handleCreate(eq(context), Matchers.<CreateRequest>anyObject())).willReturn(createPolicyOnePromise).willReturn(createPolicyTwoPromise);
    given(policyResource.handleDelete(eq(context), Matchers.<DeleteRequest>anyObject())).willReturn(deletePolicyOnePromise);
    //When
    try {
        delegate.createPolicies(context, policies).getOrThrowUninterruptibly();
    } catch (ResourceException e) {
        //Then
        ArgumentCaptor<DeleteRequest> requestCaptor = ArgumentCaptor.forClass(DeleteRequest.class);
        verify(policyResource).handleDelete(eq(context), requestCaptor.capture());
        assertThat(requestCaptor.getValue().getResourcePathObject().leaf()).isEqualTo("ID_1");
        throw e;
    }
}
Also used : Context(org.forgerock.services.context.Context) ArgumentCaptor(org.mockito.ArgumentCaptor) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 70 with CreateRequest

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

the class UmaEnabledFilterTest method testFilterCreate.

@Test(dataProvider = "factories")
public void testFilterCreate(UmaProviderSettingsFactory factory, boolean expectFailure) throws Exception {
    // Given
    UmaEnabledFilter filter = new UmaEnabledFilter(factory);
    CreateRequest request = Requests.newCreateRequest("test", json(object()));
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterCreate(context, request, requestHandler);
    // Then
    checkResult(expectFailure, promise);
    if (expectFailure) {
        verifyNoMoreInteractions(requestHandler);
    } else {
        verify(requestHandler).handleCreate(context, request);
    }
}
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

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