use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ScriptResourceTest method shouldQueryScriptConfigurationWithoutError.
@Test
public void shouldQueryScriptConfigurationWithoutError() throws Exception {
// given
scriptConfigSet.clear();
scriptConfigSet.put("1234567890", ScriptConfiguration.builder().setId("1234567890").setName("MyJavaScript").setDescription("A test JavaScript configuration").setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build());
scriptConfigSet.put("0987654321", ScriptConfiguration.builder().setId("0987654321").setName("MyGroovyScript").setDescription("A test Groovy script configuration").setScript(script).setLanguage(GROOVY).setContext(POLICY_CONDITION).build());
QueryRequest queryRequest = mock(QueryRequest.class);
QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
// when
Promise<QueryResponse, ResourceException> promise = scriptResource.queryCollection(context, queryRequest, mockHandler);
QueryResponse response = promise.getOrThrowUninterruptibly();
// then
assertNotNull(response);
ArgumentCaptor<ResourceResponse> responses = ArgumentCaptor.forClass(ResourceResponse.class);
verify(mockHandler, times(2)).handleResource(responses.capture());
Iterator<ResourceResponse> iterator = responses.getAllValues().iterator();
JsonValue responseJsonOne = iterator.next().getContent();
JsonValue responseJsonTwo = iterator.next().getContent();
assertEquals(responseJsonOne.get(JSON_UUID).asString(), "1234567890");
assertEquals(responseJsonOne.get(SCRIPT_NAME).asString(), "MyJavaScript");
assertEquals(responseJsonOne.get(SCRIPT_DESCRIPTION).asString(), "A test JavaScript configuration");
assertEquals(responseJsonOne.get(SCRIPT_TEXT).asString(), encodeScript);
assertEquals(getLanguageFromString(responseJsonOne.get(SCRIPT_LANGUAGE).asString()), JAVASCRIPT);
assertEquals(getContextFromString(responseJsonOne.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
assertEquals(responseJsonTwo.get(JSON_UUID).asString(), "0987654321");
assertEquals(responseJsonTwo.get(SCRIPT_NAME).asString(), "MyGroovyScript");
assertEquals(responseJsonTwo.get(SCRIPT_DESCRIPTION).asString(), "A test Groovy script configuration");
assertEquals(responseJsonTwo.get(SCRIPT_TEXT).asString(), encodeScript);
assertEquals(getLanguageFromString(responseJsonTwo.get(SCRIPT_LANGUAGE).asString()), GROOVY);
assertEquals(getContextFromString(responseJsonTwo.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class UmaPolicyApplicationListener method deletePolicies.
private void deletePolicies(String realm, String resourceServerId) {
RealmContext realmContext = new RealmContext(new RootContext());
realmContext.setDnsAlias("/", realm);
final Context context = new AdminSubjectContext(logger, sessionCache, realmContext);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), resourceServerId));
final List<ResourceResponse> resources = new ArrayList<>();
policyResource.handleQuery(context, request, new QueryResourceHandler() {
@Override
public boolean handleResource(ResourceResponse resource) {
resources.add(resource);
return true;
}
}).thenAsync(new AsyncFunction<QueryResponse, List<ResourceResponse>, ResourceException>() {
@Override
public Promise<List<ResourceResponse>, ResourceException> apply(QueryResponse response) {
List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
for (ResourceResponse policy : resources) {
DeleteRequest deleteRequest = Requests.newDeleteRequest("", policy.getId());
promises.add(policyResource.handleDelete(context, deleteRequest));
}
Promise<List<ResourceResponse>, ResourceException> when = Promises.when(promises);
return when;
}
}).thenOnException(new ExceptionHandler<ResourceException>() {
@Override
public void handleException(ResourceException error) {
logger.error(error.getReason());
}
});
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class UmaPolicyServiceImpl method internalReadPolicy.
/**
* {@inheritDoc}
*/
private Promise<UmaPolicy, ResourceException> internalReadPolicy(final Context context, final String resourceSetId) {
String resourceOwnerUid = getResourceOwnerUid(context);
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("resourceTypeUuid"), resourceSetId), QueryFilter.equalTo(new JsonPointer("createdBy"), resourceOwnerUid)));
return policyResourceDelegate.queryPolicies(context, request).thenAsync(new AsyncFunction<Pair<QueryResponse, List<ResourceResponse>>, UmaPolicy, ResourceException>() {
@Override
public Promise<UmaPolicy, ResourceException> apply(Pair<QueryResponse, List<ResourceResponse>> value) {
try {
if (value.getSecond().isEmpty()) {
return new NotFoundException("UMA Policy not found, " + resourceSetId).asPromise();
} else {
ResourceSetDescription resourceSet = getResourceSet(getRealm(context), resourceSetId);
UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, value.getSecond());
return newResultPromise(umaPolicy);
}
} catch (ResourceException e) {
return e.asPromise();
}
}
});
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldQueryPolicies.
@Test
public void shouldQueryPolicies() 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);
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ResourceSetResourceTest method nameQueryShouldBeSupported.
@Test
public void nameQueryShouldBeSupported() throws Exception {
//Given
Context context = mock(Context.class);
QueryRequest request = mock(QueryRequest.class);
given(request.getFields()).willReturn(Arrays.asList(new JsonPointer("/fred")));
QueryResourceHandler handler = mock(QueryResourceHandler.class);
ResourceSetDescription resourceSet = mock(ResourceSetDescription.class);
QueryFilter<JsonPointer> queryFilter = QueryFilter.and(QueryFilter.equalTo(new JsonPointer("/name"), "NAME"), QueryFilter.equalTo(new JsonPointer("/resourceServer"), "myclient"), QueryFilter.equalTo(new JsonPointer("/policy/permissions/subject"), "SUBJECT"));
Promise<Collection<ResourceSetDescription>, ResourceException> resourceSetsPromise = Promises.newResultPromise((Collection<ResourceSetDescription>) asSet(resourceSet));
given(contextHelper.getRealm(context)).willReturn("REALM");
given(contextHelper.getUserId(context)).willReturn("RESOURCE_OWNER_ID");
given(request.getQueryFilter()).willReturn(queryFilter);
given(resourceSetService.getResourceSets(eq(context), eq("REALM"), Matchers.<ResourceSetWithPolicyQuery>anyObject(), eq("RESOURCE_OWNER_ID"), eq(false))).willReturn(resourceSetsPromise);
//When
Promise<QueryResponse, ResourceException> promise = resource.queryCollection(context, request, handler);
//Then
ArgumentCaptor<ResourceSetWithPolicyQuery> queryCaptor = ArgumentCaptor.forClass(ResourceSetWithPolicyQuery.class);
verify(resourceSetService).getResourceSets(eq(context), eq("REALM"), queryCaptor.capture(), eq("RESOURCE_OWNER_ID"), eq(false));
assertThat(queryCaptor.getValue().getOperator()).isEqualTo(AggregateQuery.Operator.AND);
assertThat(queryCaptor.getValue().getPolicyQuery()).isEqualTo(QueryFilter.equalTo(new JsonPointer("/permissions/subject"), "SUBJECT"));
assertThat(queryCaptor.getValue().getResourceSetQuery()).isEqualTo(QueryFilter.and(QueryFilter.equalTo("name", "NAME"), QueryFilter.equalTo("clientId", "myclient")));
assertThat(promise).succeeded().withObject().isNotNull();
}
Aggregations