use of org.forgerock.json.resource.QueryResourceHandler 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.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldHandleApplicationFindFailure.
@Test(expectedExceptions = NotFoundException.class)
public void shouldHandleApplicationFindFailure() throws EntitlementException, ResourceException {
// 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);
EntitlementException exception = new EntitlementException(EntitlementException.APP_RETRIEVAL_ERROR);
given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willThrow(exception);
// When
Promise<QueryResponse, ResourceException> result = applicationsResource.queryCollection(serverContext, request, handler);
// Then
result.getOrThrowUninterruptibly();
}
use of org.forgerock.json.resource.QueryResourceHandler 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.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class ResourceSetResourceTest method queryShouldNotBeSupported.
@Test
public void queryShouldNotBeSupported() {
//Given
Context context = mock(Context.class);
QueryRequest request = mock(QueryRequest.class);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(request.getQueryFilter()).willReturn(QueryFilter.equalTo(new JsonPointer("/fred"), 5));
//When
Promise<QueryResponse, ResourceException> promise = resource.queryCollection(context, request, handler);
//Then
assertThat(promise).failedWithException().isInstanceOf(NotSupportedException.class);
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldQueryPoliciesWithHandler.
@Test
public void shouldQueryPoliciesWithHandler() throws ResourceException {
//Given
Context context = mock(Context.class);
QueryRequest request = mock(QueryRequest.class);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
//When
delegate.queryPolicies(context, request, handler);
//Then
verify(policyResource).handleQuery(context, request, handler);
}
Aggregations