use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.
the class ResourceSetServiceTest method shouldRevokeAllResourceSetPolicies.
@Test
public void shouldRevokeAllResourceSetPolicies() throws Exception {
//Given
String realm = "REALM";
Context context = mockContext(realm);
String resourceOwnerId = "RESOURCE_OWNER_ID";
Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
ResourceSetDescription resourceSetOne = new ResourceSetDescription("RS_ID_ONE", "CLIENT_ID_ONE", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
ResourceSetDescription resourceSetTwo = new ResourceSetDescription("RS_ID_TWO", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
Collection<UmaPolicy> queriedPolicies = new HashSet<>();
Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
mockResourceOwnerIdentity(resourceOwnerId, realm);
queriedResourceSets.add(resourceSetOne);
queriedResourceSets.add(resourceSetTwo);
given(resourceSetStore.query(Matchers.<QueryFilter<String>>anyObject())).willReturn(queriedResourceSets);
given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
given(policyService.deletePolicy(context, "RS_ID_ONE")).willReturn(Promises.<Void, ResourceException>newResultPromise(null));
given(policyService.deletePolicy(context, "RS_ID_TWO")).willReturn(Promises.<Void, ResourceException>newResultPromise(null));
//When
service.revokeAllPolicies(context, realm, resourceOwnerId).getOrThrowUninterruptibly();
//Then
verify(policyService).deletePolicy(context, "RS_ID_ONE");
verify(policyService).deletePolicy(context, "RS_ID_TWO");
}
use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method queryShouldHandleQueryException.
@Test
public void queryShouldHandleQueryException() throws Exception {
//given
setupExistingResourceTypeFromJson(jsonResourceType);
QueryRequest queryRequest = mock(QueryRequest.class);
QueryResponseHandler queryHandler = makeQueryResponseHandler();
Throwable t = new QueryException(QueryException.QueryErrorCode.FILTER_BOOLEAN_LITERAL_FALSE);
when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenThrow(t);
//when
Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
//then
assertThat(promise).failedWithResourceException().withCode(ResourceException.BAD_REQUEST);
}
use of org.forgerock.json.resource.QueryResponse 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));
}
use of org.forgerock.json.resource.QueryResponse 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.json.resource.QueryResponse 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();
}
Aggregations