Search in sources :

Example 81 with ResourceResponse

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

the class RequesterTest method shouldExecuteReadOperation.

@Test
public void shouldExecuteReadOperation() throws ResourceException {
    // *** GIVEN ***
    String location = "location";
    String resourceId = "resourceId";
    JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } ");
    // Set up mocks
    Promise<ResourceResponse, ResourceException> mockResponse = mock(Promise.class);
    ResourceResponse mockResourceResponse = mock(ResourceResponse.class);
    // Stub mocks
    given(mockRealmRouterProvider.get()).willReturn(mockRouter);
    given(mockRouter.handleRead(any(Context.class), any(ReadRequest.class))).willReturn(mockResponse);
    given(mockResponse.getOrThrowUninterruptibly()).willReturn(mockResourceResponse);
    given(mockResourceResponse.getContent()).willReturn(expectedResult);
    // *** WHEN ***
    JsonValue actualResult = requester.read(location, resourceId, mockServerContext);
    // *** THEN ***
    verify(mockRealmRouterProvider, times(1)).get();
    verifyNoMoreInteractions(mockRealmRouterProvider);
    verify(mockRouter, times(1)).handleRead(any(Context.class), any(ReadRequest.class));
    verifyNoMoreInteractions(mockRouter);
    verify(mockResponse, times(1)).getOrThrowUninterruptibly();
    verifyNoMoreInteractions(mockResponse);
    verify(mockResourceResponse, times(1)).getContent();
    verifyNoMoreInteractions(mockResourceResponse);
    assertThat(actualResult).isEqualTo(expectedResult);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 82 with ResourceResponse

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

the class RequesterTest method shouldExecuteDeleteRequest.

@Test
public void shouldExecuteDeleteRequest() throws ResourceException {
    // given
    String location = "location";
    String resourceId = "resourceId";
    JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } ");
    // Set up mocks
    Promise<ResourceResponse, ResourceException> mockResponse = mock(Promise.class);
    ResourceResponse mockResourceResponse = mock(ResourceResponse.class);
    // Stub mocks
    given(mockRealmRouterProvider.get()).willReturn(mockRouter);
    given(mockRouter.handleDelete(any(Context.class), any(DeleteRequest.class))).willReturn(mockResponse);
    given(mockResponse.getOrThrowUninterruptibly()).willReturn(mockResourceResponse);
    given(mockResourceResponse.getContent()).willReturn(expectedResult);
    // when
    JsonValue actualResult = requester.delete(location, resourceId, mockServerContext);
    // then
    verify(mockRealmRouterProvider, times(1)).get();
    verifyNoMoreInteractions(mockRealmRouterProvider);
    verify(mockRouter, times(1)).handleDelete(any(Context.class), any(DeleteRequest.class));
    verifyNoMoreInteractions(mockRouter);
    verify(mockResponse, times(1)).getOrThrowUninterruptibly();
    verifyNoMoreInteractions(mockResponse);
    verify(mockResourceResponse, times(1)).getContent();
    verifyNoMoreInteractions(mockResourceResponse);
    assertThat(actualResult).isEqualTo(expectedResult);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 83 with ResourceResponse

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

the class ScriptResourceTest method shouldUpdateScriptConfigurationWithoutError.

@Test
public void shouldUpdateScriptConfigurationWithoutError() throws Exception {
    // given
    String resourceId = "1234567890";
    JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_DESCRIPTION, "A test script configuration"), field(SCRIPT_TEXT, encodeScript), field(SCRIPT_LANGUAGE, "JAVASCRIPT"), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
    UpdateRequest updateRequest = mock(UpdateRequest.class);
    when(updateRequest.getContent()).thenReturn(requestJson);
    // when
    ResourceResponse response = scriptResource.updateInstance(context, resourceId, updateRequest).getOrThrowUninterruptibly();
    // then
    assertNotNull(response);
    assertNotNull(response.getId());
    JsonValue responseJson = response.getContent();
    assertEquals(responseJson.get(SCRIPT_NAME).asString(), "MyJavaScript");
    assertEquals(responseJson.get(SCRIPT_DESCRIPTION).asString(), "A test script configuration");
    assertEquals(responseJson.get(SCRIPT_TEXT).asString(), encodeScript);
    assertEquals(getLanguageFromString(responseJson.get(SCRIPT_LANGUAGE).asString()), JAVASCRIPT);
    assertEquals(getContextFromString(responseJson.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 84 with ResourceResponse

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

the class ScriptResourceTest method shouldQueryScriptConfigurationWithPaging.

@Test
public void shouldQueryScriptConfigurationWithPaging() throws ScriptException, ResourceException {
    // given
    scriptConfigSet.clear();
    for (int i = 0; i < 9; i++) {
        ScriptConfiguration sc = ScriptConfiguration.builder().generateId().setName("MyJavaScript" + i).setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build();
        scriptConfigSet.put(sc.getId(), sc);
    }
    QueryResourceHandler resultHandler = mock(QueryResourceHandler.class);
    given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(queryRequest.getPageSize()).thenReturn(5);
    // when
    when(queryRequest.getPagedResultsOffset()).thenReturn(0);
    scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
    // then
    ArgumentCaptor<ResourceResponse> resources = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(resultHandler, times(5)).handleResource(resources.capture());
    List<ResourceResponse> responses = resources.getAllValues();
    assertThat(responses).isNotNull().hasSize(5);
    int count = 0;
    for (ResourceResponse resource : responses) {
        assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
    }
    // when
    Mockito.reset(resultHandler);
    given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    resources = ArgumentCaptor.forClass(ResourceResponse.class);
    when(queryRequest.getPagedResultsOffset()).thenReturn(5);
    scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
    verify(resultHandler, times(4)).handleResource(resources.capture());
    // then
    responses = resources.getAllValues();
    assertThat(responses).isNotNull().hasSize(4);
    for (ResourceResponse resource : responses) {
        assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
    }
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryRequest(org.forgerock.json.resource.QueryRequest) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Test(org.testng.annotations.Test)

Example 85 with ResourceResponse

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

the class UmaPolicyResourceTest method shouldSuccessfullyCreatePolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldSuccessfullyCreatePolicy() {
    //Given
    Context context = mock(Context.class);
    CreateRequest request = Requests.newCreateRequest("/policies", json(object()));
    UmaPolicy policy = mock(UmaPolicy.class);
    Promise<UmaPolicy, ResourceException> promise = Promises.newResultPromise(policy);
    given(policy.getId()).willReturn("ID");
    given(policy.getRevision()).willReturn("REVISION");
    given(policyService.createPolicy(context, request.getContent())).willReturn(promise);
    //When
    Promise<ResourceResponse, ResourceException> result = policyResource.createInstance(context, request);
    //Then
    assertThat(result).succeeded().withId().isEqualTo("ID");
    assertThat(result).succeeded().withRevision().isEqualTo("REVISION");
    assertThat(result).succeeded().withContent().isEmpty();
}
Also used : Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Test(org.testng.annotations.Test)

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