Search in sources :

Example 21 with Application

use of com.sun.identity.entitlement.Application in project OpenAM by OpenRock.

the class ApplicationsResourceTest method shouldHandleJsonParsingFailure.

@Test(expectedExceptions = InternalServerErrorException.class)
public void shouldHandleJsonParsingFailure() throws EntitlementException, 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 {
                // Throws an EntitlementException when attempting to parse the json.
                EntitlementException entitlementException = new EntitlementException(1);
                given(wrapper.toJsonValue()).willThrow(entitlementException);
            } catch (EntitlementException e) {
                fail();
            }
            return wrapper;
        }
    };
    // Given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    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.getPageSize()).willReturn(3);
    given(request.getPagedResultsOffset()).willReturn(1);
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    Set<String> appNames = asOrderedSet("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));
    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) Set(java.util.Set) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ApplicationTypeManagerWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper) 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)

Example 22 with Application

use of com.sun.identity.entitlement.Application in project OpenAM by OpenRock.

the class ApplicationsResourceTest method updateInstanceShouldReturnServerInternalExceptionWhenApplicationToJson.

@Test(expectedExceptions = InternalServerErrorException.class)
public void updateInstanceShouldReturnServerInternalExceptionWhenApplicationToJson() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    String resourceId = "iPlanetAMWebAgentService";
    UpdateRequest request = mock(UpdateRequest.class);
    Subject subject = new Subject();
    JsonValue content = mock(JsonValue.class);
    Application application = mock(Application.class);
    Application newApplication = mock(Application.class);
    given(subjectContext.getCallerSubject()).willReturn(subject);
    given(request.getContent()).willReturn(content);
    given(applicationManagerWrapper.getApplication(subject, "/REALM", resourceId)).willReturn(application);
    given(applicationWrapper.getName()).willReturn("APP_NAME");
    given(applicationWrapper.getApplication()).willReturn(newApplication);
    given(newApplication.getLastModifiedDate()).willReturn(1000L);
    doThrow(new EntitlementException(1)).when(applicationWrapper).toJsonValue();
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.updateInstance(context, resourceId, request);
    //Then
    verify(applicationManagerWrapper).updateApplication(application, applicationWrapper.getApplication(), subject, "/REALM");
    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) 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) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 23 with Application

use of com.sun.identity.entitlement.Application 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)

Example 24 with Application

use of com.sun.identity.entitlement.Application in project OpenAM by OpenRock.

the class ApplicationsResourceTest method updateInstanceShouldReturnConflictWhenUpdatingFailsDueToNeedToDeletePolicies.

@Test(expectedExceptions = ConflictException.class)
public void updateInstanceShouldReturnConflictWhenUpdatingFailsDueToNeedToDeletePolicies() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    String resourceId = "iPlanetAMWebAgentService";
    UpdateRequest request = mock(UpdateRequest.class);
    Subject subject = new Subject();
    JsonValue content = mock(JsonValue.class);
    Application mockApplication = mock(Application.class);
    given(subjectContext.getCallerSubject()).willReturn(subject);
    given(request.getContent()).willReturn(content);
    given(applicationWrapper.getApplication()).willReturn(mockApplication);
    given(applicationManagerWrapper.getApplication(subject, "/REALM", resourceId)).willReturn(mockApplication);
    doThrow(new EntitlementException(404)).when(applicationManagerWrapper).updateApplication(any(Application.class), any(Application.class), any(Subject.class), anyString());
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.updateInstance(context, resourceId, request);
    //Then
    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) 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) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 25 with Application

use of com.sun.identity.entitlement.Application in project OpenAM by OpenRock.

the class ApplicationsResourceTest method shouldUseResourceIDForFetchingApplicationOnRead.

@Test
public void shouldUseResourceIDForFetchingApplicationOnRead() throws EntitlementException {
    // Given
    String resourceID = "iPlanetAMWebAgentService";
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    realmContext.setSubRealm("badger", "badger");
    Context serverContext = ClientContext.newInternalClientContext(realmContext);
    Subject subject = new Subject();
    given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
    Application mockApplication = mock(Application.class);
    given(applicationManagerWrapper.getApplication(any(Subject.class), anyString(), anyString())).willReturn(mockApplication);
    // When
    applicationsResource.readInstance(serverContext, resourceID, null);
    // Then
    verify(applicationManagerWrapper).getApplication(any(Subject.class), anyString(), eq(resourceID));
}
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) RealmContext(org.forgerock.openam.rest.RealmContext) Matchers.anyString(org.mockito.Matchers.anyString) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

Application (com.sun.identity.entitlement.Application)65 EntitlementException (com.sun.identity.entitlement.EntitlementException)37 Subject (javax.security.auth.Subject)29 ResourceResponse (org.forgerock.json.resource.ResourceResponse)22 Test (org.testng.annotations.Test)22 HashSet (java.util.HashSet)20 JsonValue (org.forgerock.json.JsonValue)18 Set (java.util.Set)16 ResourceException (org.forgerock.json.resource.ResourceException)16 RealmContext (org.forgerock.openam.rest.RealmContext)16 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)16 HashMap (java.util.HashMap)15 ClientContext (org.forgerock.services.context.ClientContext)13 Context (org.forgerock.services.context.Context)13 Matchers.anyString (org.mockito.Matchers.anyString)13 UpgradeException (org.forgerock.openam.upgrade.UpgradeException)10 UpdateRequest (org.forgerock.json.resource.UpdateRequest)9 ApplicationWrapper (org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper)9 Map (java.util.Map)8 BadRequestException (org.forgerock.json.resource.BadRequestException)7