Search in sources :

Example 31 with ReadRequest

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

the class Requester method read.

/**
     * Request to read a specified resource at a specified endpoint.
     *
     * @param location Endpoint destination of this request. May not be null.
     * @param resourceId Resource ID to read. May not be null.
     * @param context Context of this request.
     * @return The {@link org.forgerock.json.JsonValue} returned from the endpoint.
     * @throws ResourceException If any exception occurred during processing.
     */
public JsonValue read(String location, String resourceId, Context context) throws ResourceException {
    Reject.ifTrue(StringUtils.isEmpty(location), "The endpoint destination may not be null or empty.");
    Reject.ifTrue(StringUtils.isEmpty(resourceId), "The resourceId to read may not be null or empty.");
    final Router rootRouter = router.get();
    final ReadRequest readRequest = Requests.newReadRequest(location, resourceId);
    return rootRouter.handleRead(context, readRequest).getOrThrowUninterruptibly().getContent();
}
Also used : Router(org.forgerock.json.resource.Router) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 32 with ReadRequest

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

the class ScriptResourceTest method shouldReadScriptConfigurationWithoutError.

@Test
public void shouldReadScriptConfigurationWithoutError() throws Exception {
    // given
    String resourceId = "1234567890";
    scriptConfigSet.clear();
    scriptConfigSet.put(resourceId, ScriptConfiguration.builder().setId(resourceId).setName("MyJavaScript").setDescription("A test JavaScript configuration").setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build());
    ReadRequest readRequest = mock(ReadRequest.class);
    // when
    Promise<ResourceResponse, ResourceException> promise = scriptResource.readInstance(context, resourceId, readRequest);
    ResourceResponse response = promise.getOrThrowUninterruptibly();
    // then
    assertNotNull(response);
    assertNotNull(response.getId());
    assertEquals(response.getId(), resourceId);
    JsonValue responseJson = response.getContent();
    assertEquals(responseJson.get(JSON_UUID).asString(), resourceId);
    assertEquals(responseJson.get(SCRIPT_NAME).asString(), "MyJavaScript");
    assertEquals(responseJson.get(SCRIPT_DESCRIPTION).asString(), "A test JavaScript 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) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 33 with ReadRequest

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

the class PolicyResourceTest method shouldReadPoliciesFromStore.

@Test
public void shouldReadPoliciesFromStore() throws Exception {
    // Given
    String id = "testPolicy";
    long lastModified = 1234l;
    Privilege policy = mockPrivilege(id, lastModified);
    ReadRequest request = mock(ReadRequest.class);
    given(mockStore.read(id)).willReturn(policy);
    JsonValue content = new JsonValue("content");
    given(mockParser.printPolicy(policy)).willReturn(content);
    // When
    Promise<ResourceResponse, ResourceException> promise = policyResource.readInstance(mockServerContext, id, request);
    // Then
    assertThat(promise).succeeded().withId().isNotNull();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Privilege(com.sun.identity.entitlement.Privilege) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 34 with ReadRequest

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

the class PolicyResourceTest method shouldReportMissingPoliciesInRead.

@Test
public void shouldReportMissingPoliciesInRead() throws Exception {
    // Given
    String id = "unknown";
    given(mockStore.read(id)).willThrow(new EntitlementException(EntitlementException.NO_SUCH_POLICY));
    ReadRequest request = mock(ReadRequest.class);
    // When
    Promise<ResourceResponse, ResourceException> promise = policyResource.readInstance(mockServerContext, id, request);
    // Then
    assertThat(promise).failedWithException().isInstanceOf(NotFoundException.class);
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 35 with ReadRequest

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

the class PolicyV1FilterTest method forwardOnRead.

/**
     * Verify that read requests are forwarded on and that the response is tailored to represent v1.0 policy.
     */
@Test
public void forwardOnRead() throws Exception {
    // Given
    ReadRequest readRequest = mock(ReadRequest.class);
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterRead(context, readRequest, requestHandler);
    // Then
    assertThat(promise).succeeded();
    verify(requestHandler).handleRead(eq(context), eq(readRequest));
    assertThat(promise.get().getContent().contains("resourceTypeUuid")).isFalse();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

ReadRequest (org.forgerock.json.resource.ReadRequest)35 Test (org.testng.annotations.Test)31 ResourceException (org.forgerock.json.resource.ResourceException)28 ResourceResponse (org.forgerock.json.resource.ResourceResponse)27 Context (org.forgerock.services.context.Context)23 RealmContext (org.forgerock.openam.rest.RealmContext)15 Subject (javax.security.auth.Subject)13 ClientContext (org.forgerock.services.context.ClientContext)11 JsonSchema (com.fasterxml.jackson.databind.jsonschema.JsonSchema)8 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7 JsonValue (org.forgerock.json.JsonValue)6 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)6 RequestHandler (org.forgerock.json.resource.RequestHandler)4 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)3 LogicalSubject (com.sun.identity.entitlement.LogicalSubject)3 Map (java.util.Map)3 AuthorizationResult (org.forgerock.authz.filter.api.AuthorizationResult)3 Router (org.forgerock.json.resource.Router)3 EntitlementException (com.sun.identity.entitlement.EntitlementException)2 CreateRequest (org.forgerock.json.resource.CreateRequest)2