use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class AuditRestRouteProvider method addResourceRoutes.
@Override
public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
rootRouter.route("global-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).toRequestHandler(STARTS_WITH, auditServiceProvider.getDefaultAuditService());
rootRouter.route("realm-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).through(RealmContextFilter.class).toRequestHandler(STARTS_WITH, new RequestHandler() {
@Override
public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest actionRequest) {
return getAuditService(context).handleAction(context, actionRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest createRequest) {
return getAuditService(context).handleCreate(context, createRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest deleteRequest) {
return getAuditService(context).handleDelete(context, deleteRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest patchRequest) {
return getAuditService(context).handlePatch(context, patchRequest);
}
@Override
public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest queryRequest, QueryResourceHandler queryResourceHandler) {
return getAuditService(context).handleQuery(context, queryRequest, queryResourceHandler);
}
@Override
public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest readRequest) {
return getAuditService(context).handleRead(context, readRequest);
}
@Override
public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest updateRequest) {
return getAuditService(context).handleUpdate(context, updateRequest);
}
private AMAuditService getAuditService(Context context) {
String realm = context.asContext(RealmContext.class).getResolvedRealm();
if (StringUtils.isEmpty(realm)) {
logger.warn("Context contained RealmContext but had an empty resolved realm");
return auditServiceProvider.getDefaultAuditService();
}
return auditServiceProvider.getAuditService(realm);
}
});
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class TrustedDevicesResourceTest method shouldQueryTrustedDevices.
@Test
public void shouldQueryTrustedDevices() throws ResourceException {
//Given
QueryRequest request = Requests.newQueryRequest("");
Connection connection = newInternalConnection(newCollection(resource));
QueryResourceHandler handler = mock(QueryResourceHandler.class);
List<JsonValue> devices = new ArrayList<>();
devices.add(json(object(field("name", "NAME_1"), field("lastSelectedDate", new Date().getTime()))));
devices.add(json(object(field("name", "NAME_2"), field("lastSelectedDate", new Date().getTime() + 1000))));
given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
//When
connection.query(ctx(), request, handler);
//Then
verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class PolicyResourceTest method shouldHandleQueryRequests.
@Test
public void shouldHandleQueryRequests() throws Exception {
// Given
QueryRequest request = mock(QueryRequest.class);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
List<Privilege> policies = Arrays.<Privilege>asList(new StubPrivilege("one"), new StubPrivilege("two"));
given(mockStore.query(request)).willReturn(policies);
given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
// When
policyResource.queryCollection(mockServerContext, request, handler);
// Then
verify(handler, times(policies.size())).handleResource(any(ResourceResponse.class));
}
use of org.forgerock.json.resource.QueryResourceHandler 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.QueryResourceHandler 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);
}
Aggregations