Search in sources :

Example 21 with CreateRequest

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

the class ApplicationV1FilterTest method resourceTypeCreationOnCreate.

/**
     * Verifies that the appropriate resource type is created for the application being created.
     */
@Test
public void resourceTypeCreationOnCreate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    // Build application JSON representation.
    JsonValue jsonValue = json(object(field("name", "testApplication"), 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 = Collections.emptySet();
    given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
    ResourceType resourceType = ResourceType.builder().setName("test").setUUID("some-test-uuid").setActions(TestData.DATA_SET_1.getActions().getUnderlyingMap()).setPatterns(TestData.DATA_SET_1.getResources().getUnderlyingSet()).build();
    given(resourceTypeService.saveResourceType(eq(subject), eq("/abc"), resourceTypeCaptor.capture())).willReturn(resourceType);
    // When
    filter.filterCreate(context, createRequest, requestHandler);
    // Then
    assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly("some-test-uuid");
    verify(requestHandler).handleCreate(eq(context), eq(createRequest));
    ResourceType capturedResourceType = resourceTypeCaptor.getValue();
    assertThat(capturedResourceType.getName()).startsWith("testApplicationResourceType");
    assertThat(capturedResourceType.getActions()).isEqualTo(TestData.DATA_SET_1.getActions().getUnderlyingMap());
    assertThat(capturedResourceType.getPatterns()).isEqualTo(TestData.DATA_SET_1.getResources().getUnderlyingSet());
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) Test(org.testng.annotations.Test)

Example 22 with CreateRequest

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

the class PolicyResourceDelegateTest method shouldHandleFailureToDeleteFailedCreationOfPolicies.

@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToDeleteFailedCreationOfPolicies() 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 createException = mock(ResourceException.class);
    ResourceException deleteException = mock(ResourceException.class);
    Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newExceptionPromise(createException);
    Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newExceptionPromise(deleteException);
    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");
        assertThat(e).isEqualTo(deleteException);
        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 23 with CreateRequest

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

the class ResourceSetResourceTest method createShouldNotBeSupported.

@Test
public void createShouldNotBeSupported() {
    //Given
    Context context = mock(Context.class);
    CreateRequest request = mock(CreateRequest.class);
    //When
    Promise<ResourceResponse, ResourceException> instancePromise = resource.createInstance(context, request);
    //Then
    assertThat(instancePromise).failedWithException().isInstanceOf(NotSupportedException.class);
}
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) Test(org.testng.annotations.Test)

Example 24 with CreateRequest

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

the class PrivilegeAuthzModuleTest method crestRequestNotAllowed.

@Test
public void crestRequestNotAllowed() throws SSOException, DelegationException {
    // Given...
    final Set<String> actions = new HashSet<>(Arrays.asList("MODIFY"));
    final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS, DUMB_FUNC);
    given(factory.newInstance("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS)).willReturn(permission);
    given(subjectContext.getCallerSSOToken()).willReturn(token);
    given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(false);
    // When...
    final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
    final Router router = new Router();
    router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
    final RealmContext context = new RealmContext(subjectContext);
    context.setSubRealm("abc", "abc");
    final CreateRequest request = Requests.newCreateRequest("/policies", JsonValue.json(new Object()));
    Promise<ResourceResponse, ResourceException> promise = router.handleCreate(context, request);
    // Then...
    assertThat(promise).failedWithException().isInstanceOf(ForbiddenException.class);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) FilterChain(org.forgerock.json.resource.FilterChain) Router(org.forgerock.json.resource.Router) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) DelegationPermission(com.sun.identity.delegation.DelegationPermission) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 25 with CreateRequest

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

the class RealmContextFilterTest method filterShouldConsumeRealmFromRequestWithUriRealmAndOverrideRealm.

@Test
public void filterShouldConsumeRealmFromRequestWithUriRealmAndOverrideRealm() throws Exception {
    //Given
    Context context = mockContext(SUB_REALM + "/" + ENDPOINT_PATH_ELEMENT);
    Request request = createRequest(HOSTNAME, SUB_REALM + "/" + ENDPOINT_PATH_ELEMENT + "?realm=" + OVERRIDE_REALM);
    mockDnsAlias(HOSTNAME, "/");
    mockRealmAlias("/" + SUB_REALM, "/" + SUB_REALM);
    mockRealmAlias(OVERRIDE_REALM_ALIAS, OVERRIDE_REALM);
    //When
    filter.filter(context, request, handler);
    //Then
    ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
    verify(handler).handle(contextCaptor.capture(), eq(request));
    verifyRealmContext(contextCaptor.getValue(), "", "/" + SUB_REALM, OVERRIDE_REALM);
    verifyUriRouterContext(contextCaptor.getValue(), SUB_REALM);
}
Also used : RootContext(org.forgerock.services.context.RootContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) AttributesContext(org.forgerock.services.context.AttributesContext) CreateRequest(org.forgerock.json.resource.CreateRequest) ActionRequest(org.forgerock.json.resource.ActionRequest) ReadRequest(org.forgerock.json.resource.ReadRequest) DeleteRequest(org.forgerock.json.resource.DeleteRequest) UpdateRequest(org.forgerock.json.resource.UpdateRequest) PatchRequest(org.forgerock.json.resource.PatchRequest) Request(org.forgerock.http.protocol.Request) QueryRequest(org.forgerock.json.resource.QueryRequest) 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