use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldCreateApplication.
@Test
public void shouldCreateApplication() throws ExecutionException, 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("newApplication");
//when
Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(realmContext, mockCreateRequest);
//then
assertThat(result.getOrThrowUninterruptibly()).isNotNull();
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldThrowBadRequestIfApplicationWrapperCannotBeCreated.
@Test(expectedExceptions = BadRequestException.class)
public void shouldThrowBadRequestIfApplicationWrapperCannotBeCreated() throws ResourceException {
//given
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSSOTokenContext);
realmContext.setSubRealm("REALM", "REALM");
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
CreateRequest mockCreateRequest = mock(CreateRequest.class);
Subject subject = new Subject();
given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {
@Override
protected ApplicationWrapper createApplicationWrapper(JsonValue jsonValue, Subject mySubject) throws EntitlementException {
throw new EntitlementException(317);
}
};
//when
Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(mockServerContext, mockCreateRequest);
//then
result.getOrThrowUninterruptibly();
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResourceTest method reservedInternalAppIsMappedDuringQuery.
@Test
public void reservedInternalAppIsMappedDuringQuery() throws EntitlementException, IllegalAccessException, InstantiationException {
// 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 = JsonValueBuilder.jsonValue().put("name", "agentProtectedApplication").build();
given(wrapper.toJsonValue()).willReturn(jsonValue);
} 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);
QueryRequest request = mock(QueryRequest.class);
given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
Subject subject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(subject);
Set<String> appNames = asSet("iPlanetAMWebAgentService");
given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
Application app = mock(Application.class);
given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq("iPlanetAMWebAgentService"))).willReturn(app);
given(app.getName()).willReturn("agentProtectedApplication");
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
// When...
applicationsResource.queryCollection(serverContext, request, handler);
// Then...
verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
verify(applicationManagerWrapper).getApplication(eq(subject), eq("/abc"), anyString());
ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
verify(handler).handleResource(resourceCapture.capture());
ResourceResponse resource = resourceCapture.getValue();
assertThat(resource.getId()).isEqualTo("agentProtectedApplication");
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResource method queryCollection.
/**
* Queries for a collection of resources.
*
* @param context {@inheritDoc}
* @param request {@inheritDoc}
* @param handler {@inheritDoc}
*/
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
//auth
final Subject mySubject = getContextSubject(context);
if (mySubject == null) {
debug.error("ApplicationsResource :: UPDATE : Unknown Subject");
return new BadRequestException().asPromise();
}
//select
final String realm = getRealm(context);
final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
try {
List<ResourceResponse> results = new ArrayList<>();
final Set<String> appNames = query(request, mySubject, realm);
for (String appName : appNames) {
final Application application = appManager.getApplication(mySubject, realm, appName);
if (application == null) {
debug.warning("Unable to find application " + appName);
continue;
}
ApplicationWrapper wrapper = createApplicationWrapper(application, appTypeManagerWrapper);
results.add(newResourceResponse(wrapper.getName(), null, wrapper.toJsonValue()));
}
QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
return QueryResponsePresentation.perform(handler, request, results);
} catch (EntitlementException e) {
if (debug.errorEnabled()) {
debug.error("ApplicationsResource :: QUERY by " + principalName + ": Failed to query resource.", e);
}
return exceptionMappingHandler.handleError(context, request, e).asPromise();
}
}
use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.
the class ApplicationsResource method updateInstance.
/**
* Updates an existing {@link Application}.
* The resourceId is the name of the application to update.
* The new Application may alter this name, but doing so will mean the original
* resourceId will no longer reference the new Application.
*
* @param context {@inheritDoc}
* @param resourceId {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId, UpdateRequest request) {
final Subject mySubject = getContextSubject(context);
if (mySubject == null) {
debug.error("ApplicationsResource :: UPDATE : Unknown Subject");
return new BadRequestException().asPromise();
}
final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
final ApplicationWrapper wrapp;
final Application oldApplication;
try {
wrapp = createApplicationWrapper(request.getContent(), mySubject);
if (wrapp.getName() == null) {
wrapp.setName(resourceId);
}
oldApplication = appManager.getApplication(mySubject, getRealm(context), resourceId);
if (oldApplication == null) {
throw new EntitlementException(EntitlementException.NOT_FOUND, new String[] { resourceId });
}
if (//return conflict
!resourceId.equals(wrapp.getName()) && appManager.getApplication(mySubject, getRealm(context), wrapp.getName()) != null) {
throw new EntitlementException(EntitlementException.APPLICATION_ALREADY_EXISTS);
}
appManager.updateApplication(oldApplication, wrapp.getApplication(), mySubject, getRealm(context));
ResourceResponse resource = newResourceResponse(wrapp.getName(), Long.toString(wrapp.getApplication().getLastModifiedDate()), wrapp.toJsonValue());
return newResultPromise(resource);
} catch (EntitlementException e) {
if (debug.errorEnabled()) {
debug.error("ApplicationsResource :: UPDATE by " + principalName + ": Error performing update operation.", e);
}
return exceptionMappingHandler.handleError(context, request, e).asPromise();
}
}
Aggregations