use of org.forgerock.openam.rest.resource.SSOTokenContext 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();
}
use of org.forgerock.openam.rest.resource.SSOTokenContext 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);
}
use of org.forgerock.openam.rest.resource.SSOTokenContext 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();
}
use of org.forgerock.openam.rest.resource.SSOTokenContext 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();
}
use of org.forgerock.openam.rest.resource.SSOTokenContext 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();
}
Aggregations