use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class PrivilegeAuthzModuleTest method crestQueryIsAllowed.
@Test
public void crestQueryIsAllowed() throws SSOException, DelegationException, ResourceException {
// Given...
final Set<String> actions = new HashSet<>(Arrays.asList("READ"));
final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS, DUMB_FUNC);
given(factory.newInstance("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS)).willReturn(permission);
given(subjectContext.getCallerSSOToken()).willReturn(token);
given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(true);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
Promise<QueryResponse, ResourceException> promise = Promises.newResultPromise(Responses.newQueryResponse("abc-def"));
given(provider.queryCollection(isA(Context.class), isA(QueryRequest.class), isA(QueryResourceHandler.class))).willReturn(promise);
// When...
final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
final Router router = new Router();
router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
final RealmContext context = new RealmContext(subjectContext);
context.setSubRealm("abc", "abc");
final QueryRequest request = Requests.newQueryRequest("/policies");
Promise<QueryResponse, ResourceException> result = router.handleQuery(context, request, handler);
// Then...
QueryResponse response = result.getOrThrowUninterruptibly();
assertThat(response.getPagedResultsCookie()).isEqualTo("abc-def");
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class ScriptResourceTest method shouldQueryScriptConfigurationWithPaging.
@Test
public void shouldQueryScriptConfigurationWithPaging() throws ScriptException, ResourceException {
// given
scriptConfigSet.clear();
for (int i = 0; i < 9; i++) {
ScriptConfiguration sc = ScriptConfiguration.builder().generateId().setName("MyJavaScript" + i).setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build();
scriptConfigSet.put(sc.getId(), sc);
}
QueryResourceHandler resultHandler = mock(QueryResourceHandler.class);
given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
QueryRequest queryRequest = mock(QueryRequest.class);
when(queryRequest.getPageSize()).thenReturn(5);
// when
when(queryRequest.getPagedResultsOffset()).thenReturn(0);
scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
// then
ArgumentCaptor<ResourceResponse> resources = ArgumentCaptor.forClass(ResourceResponse.class);
verify(resultHandler, times(5)).handleResource(resources.capture());
List<ResourceResponse> responses = resources.getAllValues();
assertThat(responses).isNotNull().hasSize(5);
int count = 0;
for (ResourceResponse resource : responses) {
assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
}
// when
Mockito.reset(resultHandler);
given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
resources = ArgumentCaptor.forClass(ResourceResponse.class);
when(queryRequest.getPagedResultsOffset()).thenReturn(5);
scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
verify(resultHandler, times(4)).handleResource(resources.capture());
// then
responses = resources.getAllValues();
assertThat(responses).isNotNull().hasSize(4);
for (ResourceResponse resource : responses) {
assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
}
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class UmaPolicyResourceTest method shouldSuccessfullyQueryPolicies.
@Test
public void shouldSuccessfullyQueryPolicies() {
//Given
Context context = mock(Context.class);
QueryRequest request = Requests.newQueryRequest("/policies");
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
QueryResponse queryResult = newQueryResponse();
Collection<UmaPolicy> umaPolicies = new HashSet<UmaPolicy>();
UmaPolicy policy1 = mock(UmaPolicy.class);
UmaPolicy policy2 = mock(UmaPolicy.class);
umaPolicies.add(policy1);
umaPolicies.add(policy2);
Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> promise = Promises.newResultPromise(Pair.of(queryResult, umaPolicies));
given(policyService.queryPolicies(context, request)).willReturn(promise);
//When
policyResource.queryCollection(context, 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 UmaEnabledFilterTest method testFilterQuery.
@Test(dataProvider = "factories")
public void testFilterQuery(UmaProviderSettingsFactory factory, boolean expectFailure) throws Exception {
// Given
UmaEnabledFilter filter = new UmaEnabledFilter(factory);
QueryResourceHandler resultHandler = mock(QueryResourceHandler.class);
QueryRequest request = Requests.newQueryRequest("test");
// When
Promise<QueryResponse, ResourceException> promise = filter.filterQuery(context, request, resultHandler, requestHandler);
// Then
checkResult(expectFailure, promise);
if (expectFailure) {
verifyNoMoreInteractions(requestHandler);
} else {
verify(requestHandler).handleQuery(context, request, resultHandler);
}
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class OathDevicesResourceTest method shouldQueryTrustedDevices.
@Test
public void shouldQueryTrustedDevices() throws ResourceException, SSOException {
// Given
QueryRequest request = Requests.newQueryRequest("");
QueryResourceHandler handler = mock(QueryResourceHandler.class);
List<JsonValue> devices = new ArrayList<JsonValue>();
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
Promise<QueryResponse, ResourceException> actual = resource.queryCollection(ctx(), request, handler);
QueryResponse response = actual.getOrThrowUninterruptibly();
// Then
assertThat(response).isNotNull();
verify(dao, times(1)).getDeviceProfiles(USER_ID, "/");
verify(handler, times(2)).handleResource(any(ResourceResponse.class));
}
Aggregations