Search in sources :

Example 56 with ResourceException

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

the class SubjectTypesResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws Exception {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    given(mockRequest.getPageSize()).willReturn(2);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<QueryResponse, ResourceException> promise = testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    assertThat(promise).succeeded();
    verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) Subject(javax.security.auth.Subject) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) Test(org.testng.annotations.Test)

Example 57 with ResourceException

use of org.forgerock.json.resource.ResourceException 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 58 with ResourceException

use of org.forgerock.json.resource.ResourceException 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 59 with ResourceException

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

the class ApplicationsResourceTest method shouldNotDeleteInstanceWhenSubjectIsNull.

@Test(expectedExceptions = BadRequestException.class)
public void shouldNotDeleteInstanceWhenSubjectIsNull() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    Context context = ClientContext.newInternalClientContext(subjectContext);
    String resourceId = "RESOURCE_ID";
    DeleteRequest request = mock(DeleteRequest.class);
    Subject subject = null;
    given(subjectContext.getCallerSubject()).willReturn(subject);
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.deleteInstance(context, resourceId, request);
    //Then
    verify(applicationManagerWrapper, never()).deleteApplication(subject, "REALM", resourceId);
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 60 with ResourceException

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

the class ApplicationsResourceTest method shouldReturnThreeResultsOnQuery.

@Test
public void shouldReturnThreeResultsOnQuery() throws EntitlementException, IllegalAccessException, InstantiationException, ResourceException {
    // Override the creation of the application wrapper so to return a mocked version.
    applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {

        @Override
        protected ApplicationWrapper createApplicationWrapper(Application application, ApplicationTypeManagerWrapper type) {
            ApplicationWrapper wrapper = mock(ApplicationWrapper.class);
            String appName = application.getName();
            given(wrapper.getName()).willReturn(appName);
            try {
                JsonValue jsonValue = json(object(field("name", appName)));
                given(wrapper.toJsonValue()).willReturn(jsonValue);
            } catch (EntitlementException e) {
                fail();
            }
            return wrapper;
        }
    };
    // Given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    realmContext.setSubRealm("abc", "abc");
    Context serverContext = ClientContext.newInternalClientContext(realmContext);
    // Set the page size to be three starting from the second item.
    QueryRequest request = mock(QueryRequest.class);
    given(request.getAdditionalParameter(QueryResponsePresentation.REMAINING)).willReturn("true");
    given(request.getPageSize()).willReturn(3);
    given(request.getPagedResultsOffset()).willReturn(1);
    given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    Set<String> appNames = asSet("app1", "app2", "app3", "app4", "app5", "iPlanetAMWebAgentService");
    given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
    for (String appName : appNames) {
        Application app = mock(Application.class);
        given(app.getName()).willReturn(appName);
        given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq(appName))).willReturn(app);
    }
    // When
    Promise<QueryResponse, ResourceException> result = applicationsResource.queryCollection(serverContext, request, handler);
    // Then
    verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
    verify(applicationManagerWrapper, times(appNames.size())).getApplication(eq(subject), eq("/abc"), anyString());
    ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
    verify(handler, times(3)).handleResource(resourceCapture.capture());
    List<String> selectedApps = transformList(resourceCapture.getAllValues(), new ResourceToIdMapper());
    assertThat(selectedApps).containsOnly("app2", "app3", "app4");
    QueryResponse response = result.getOrThrowUninterruptibly();
    assertThat(response.getRemainingPagedResults()).isEqualTo(2);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) Set(java.util.Set) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ApplicationTypeManagerWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper) JsonValue(org.forgerock.json.JsonValue) Matchers.anyString(org.mockito.Matchers.anyString) 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) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Application(com.sun.identity.entitlement.Application) Test(org.testng.annotations.Test)

Aggregations

ResourceException (org.forgerock.json.resource.ResourceException)323 Test (org.testng.annotations.Test)233 ResourceResponse (org.forgerock.json.resource.ResourceResponse)179 JsonValue (org.forgerock.json.JsonValue)145 Context (org.forgerock.services.context.Context)145 RealmContext (org.forgerock.openam.rest.RealmContext)110 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)71 Subject (javax.security.auth.Subject)58 ClientContext (org.forgerock.services.context.ClientContext)56 NotFoundException (org.forgerock.json.resource.NotFoundException)47 BadRequestException (org.forgerock.json.resource.BadRequestException)44 QueryResponse (org.forgerock.json.resource.QueryResponse)43 HashSet (java.util.HashSet)42 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)42 CreateRequest (org.forgerock.json.resource.CreateRequest)40 SSOException (com.iplanet.sso.SSOException)38 ActionResponse (org.forgerock.json.resource.ActionResponse)37 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)37 Matchers.anyString (org.mockito.Matchers.anyString)37 ArrayList (java.util.ArrayList)35