Search in sources :

Example 11 with CreateRequest

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

the class PolicyResourceTest method mockCreateRequest.

private CreateRequest mockCreateRequest(String resourceName, JsonValue content) {
    CreateRequest mockRequest = mock(CreateRequest.class);
    given(mockRequest.getNewResourceId()).willReturn(resourceName);
    given(mockRequest.getContent()).willReturn(content);
    return mockRequest;
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest)

Example 12 with CreateRequest

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

the class RealmContextFilter method filterCreate.

@Override
public Promise<ResourceResponse, ResourceException> filterCreate(Context context, CreateRequest request, RequestHandler next) {
    try {
        Context newContext = evaluate(context, request);
        CreateRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfCreateRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
        return next.handleCreate(evaluate(context, request), evaluatedRequest);
    } catch (ResourceException e) {
        return newExceptionPromise(e);
    }
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) CreateRequest(org.forgerock.json.resource.CreateRequest) UriRouterContext(org.forgerock.http.routing.UriRouterContext) ResourceException(org.forgerock.json.resource.ResourceException)

Example 13 with CreateRequest

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

Example 14 with CreateRequest

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

the class ApplicationV1FilterTest method createFailsWhenNoResourcesDefined.

/**
     * Verifies that creation fails when no resources are defined.
     */
@Test(expectedExceptions = BadRequestException.class)
public void createFailsWhenNoResourcesDefined() throws ResourceException {
    // Given
    // Build application JSON representation.
    JsonValue jsonValue = json(object(TestData.DATA_SET_1.getActions().asJson()));
    CreateRequest createRequest = mock(CreateRequest.class);
    given(createRequest.getContent()).willReturn(jsonValue);
    // When
    Promise<ResourceResponse, ResourceException> result = filter.filterCreate(context, createRequest, requestHandler);
    // Then
    result.getOrThrowUninterruptibly();
}
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 15 with CreateRequest

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

the class ApplicationsResourceTest method shouldNotCreateApplicationWithInvalidCharactersInName.

@Test(expectedExceptions = BadRequestException.class)
public void shouldNotCreateApplicationWithInvalidCharactersInName() throws ResourceException {
    //given
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    realmContext.setSubRealm("/", "/");
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    Subject mockSubject = new Subject();
    Application mockApplication = mock(Application.class);
    applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {

        @Override
        protected ApplicationWrapper createApplicationWrapper(JsonValue jsonValue, Subject mySubject) throws EntitlementException {
            return applicationWrapper;
        }

        @Override
        protected ApplicationWrapper createApplicationWrapper(Application application, ApplicationTypeManagerWrapper type) {
            return applicationWrapper;
        }
    };
    given(mockSSOTokenContext.getCallerSubject()).willReturn(mockSubject);
    given(applicationWrapper.getApplication()).willReturn(mockApplication);
    given(mockApplication.getName()).willReturn("new+application");
    //when
    Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(realmContext, mockCreateRequest);
    // Then
    result.getOrThrowUninterruptibly();
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) ApplicationTypeManagerWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Subject(javax.security.auth.Subject) EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Application(com.sun.identity.entitlement.Application) 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