use of org.forgerock.json.resource.QueryRequest 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.QueryRequest in project OpenAM by OpenRock.
the class AuditFilterTest method shouldReturnNullForQuerySuccess.
@Test
public void shouldReturnNullForQuerySuccess() {
QueryRequest queryRequest = mock(QueryRequest.class);
QueryResponse queryResponse = mock(QueryResponse.class);
JsonValue filterResponse = auditFilter.getQuerySuccessDetail(queryRequest, queryResponse);
assertThat(filterResponse).isEqualTo(null);
}
use of org.forgerock.json.resource.QueryRequest 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.QueryRequest 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.QueryRequest in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldQueryUmaPoliciesBySubjectOrUnknownResourceServer.
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectOrUnknownResourceServer() throws Exception {
//Given
Context context = createContext();
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.or(QueryFilter.equalTo(new JsonPointer("permissions/subject"), "SUBJECT_ONE"), QueryFilter.equalTo(new JsonPointer("resourceServer"), "OTHER_CLIENT_ID")));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request).getOrThrowUninterruptibly();
//Then
assertThat(queryResult.getSecond()).hasSize(1);
}
Aggregations