Search in sources :

Example 36 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)

Example 37 with CreateRequest

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

the class PolicyResourceWithCopyMoveSupport method copyPolicy.

private ActionResponse copyPolicy(Context context, String resourceId, ActionRequest request) throws ResourceException {
    String sourceRealm = RealmContext.getRealm(context);
    JsonValue payload = request.getContent().get("to");
    if (payload.isNull()) {
        throw new BadRequestException("to definition is missing");
    }
    String destinationRealm = payload.get("realm").defaultTo(sourceRealm).asString();
    ReadRequest readRequest = Requests.newReadRequest("policies", resourceId);
    JsonValue policy = router.handleRead(context, readRequest).getOrThrowUninterruptibly().getContent();
    String sourceApplication = policy.get("applicationName").asString();
    String sourceResourceType = policy.get("resourceTypeUuid").asString();
    String destinationApplication = payload.get("application").defaultTo(sourceApplication).asString();
    String destinationResourceTypeId = payload.get("resourceType").defaultTo(sourceResourceType).asString();
    String copiedName = payload.get("name").defaultTo(resourceId).asString();
    if (sourceRealm.equals(destinationRealm) && resourceId.equals(copiedName)) {
        throw new BadRequestException("policy name already exists within the realm");
    }
    policy.put("name", copiedName);
    policy.put("applicationName", destinationApplication);
    policy.put("resourceTypeUuid", destinationResourceTypeId);
    RealmContext updatedContext = new RealmContext(context);
    updatedContext.setOverrideRealm(destinationRealm);
    CreateRequest createRequest = Requests.newCreateRequest("policies", policy);
    JsonValue copiedPolicy = router.handleCreate(updatedContext, createRequest).getOrThrowUninterruptibly().getContent();
    return Responses.newActionResponse(copiedPolicy);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 38 with CreateRequest

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

the class ApplicationsResourceTest method shouldReturnBadRequestIfCannotReturnResource.

@Test(expectedExceptions = BadRequestException.class)
public void shouldReturnBadRequestIfCannotReturnResource() throws EntitlementException, ResourceException {
    //given
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    realmContext.setSubRealm("/", "/");
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    Subject subject = null;
    given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
    doThrow(new EntitlementException(1)).when(applicationWrapper).toJsonValue();
    //when
    Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(realmContext, mockCreateRequest);
    //then
    result.getOrThrowUninterruptibly();
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 39 with CreateRequest

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

the class ApplicationV1FilterTest method createFailsWhenNoActionsDefined.

/**
     * Verifies that creation fails when no actions are defined.
     */
@Test(expectedExceptions = BadRequestException.class)
public void createFailsWhenNoActionsDefined() throws ResourceException {
    // Given
    // Build application JSON representation.
    JsonValue jsonValue = json(object(TestData.DATA_SET_1.getResources().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 40 with CreateRequest

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

the class ApplicationV1FilterTest method resourceTypeAssociationOnCreate.

/**
     * Verifies that the appropriate resource type is associated with the application being created.
     */
@Test
public void resourceTypeAssociationOnCreate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    // Build application JSON representation.
    JsonValue jsonValue = json(object(TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
    CreateRequest createRequest = mock(CreateRequest.class);
    given(createRequest.getContent()).willReturn(jsonValue);
    Set<ResourceType> resourceTypes = CollectionUtils.asSet(ResourceType.builder().setName("test").setUUID(TestData.DATA_SET_1.getResourceTypeUuid()).build());
    given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
    given(requestHandler.handleCreate(eq(context), eq(createRequest))).willReturn(mockPromise);
    // When
    Promise<ResourceResponse, ResourceException> result = filter.filterCreate(context, createRequest, requestHandler);
    // Then
    assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly(TestData.DATA_SET_1.getResourceTypeUuid());
    verify(requestHandler).handleCreate(eq(context), eq(createRequest));
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) 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