use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class ResourceSetServiceTest method getResourceSetsShouldReturnEmptySetWhenResourceSetsExistQueryingByAndWithPolicies.
@Test
public void getResourceSetsShouldReturnEmptySetWhenResourceSetsExistQueryingByAndWithPolicies() throws Exception {
//Given
Context context = createContext();
String realm = "REALM";
ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
String resourceOwnerId = "RESOURCE_OWNER_ID";
boolean augmentWithPolicies = true;
QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
QueryFilter policyQuery = QueryFilter.alwaysFalse();
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());
ResourceSetDescription resourceSetThree = new ResourceSetDescription("RS_ID_THREE", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
Collection<UmaPolicy> queriedPolicies = new HashSet<>();
UmaPolicy policyOne = mock(UmaPolicy.class);
UmaPolicy policyTwo = mock(UmaPolicy.class);
UmaPolicy policyThree = mock(UmaPolicy.class);
JsonValue policyOneJson = mock(JsonValue.class);
JsonValue policyTwoJson = mock(JsonValue.class);
JsonValue policyThreeJson = mock(JsonValue.class);
Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
query.setResourceSetQuery(resourceSetQuery);
query.setPolicyQuery(policyQuery);
query.setOperator(AggregateQuery.Operator.AND);
queriedResourceSets.add(resourceSetOne);
queriedResourceSets.add(resourceSetTwo);
queriedPolicies.add(policyOne);
queriedPolicies.add(policyThree);
given(policyOne.getId()).willReturn("RS_ID_ONE");
given(policyOne.asJson()).willReturn(policyOneJson);
given(policyOne.getResourceSet()).willReturn(resourceSetOne);
given(policyTwo.getId()).willReturn("RS_ID_TWO");
given(policyTwo.asJson()).willReturn(policyTwoJson);
given(policyTwo.getResourceSet()).willReturn(resourceSetTwo);
given(policyThree.getId()).willReturn("RS_ID_THREE");
given(policyThree.asJson()).willReturn(policyThreeJson);
given(policyThree.getResourceSet()).willReturn(resourceSetThree);
given(resourceSetStore.query(resourceSetQuery)).willReturn(queriedResourceSets);
given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
given(resourceSetStore.read("RS_ID_THREE", resourceOwnerId)).willReturn(resourceSetThree);
mockPolicyEvaluator("RS_CLIENT_ID");
AMIdentity amIdentity = mock(AMIdentity.class);
given(amIdentity.getUniversalId()).willReturn("UNIVERSAL_ID");
given(coreWrapper.getIdentity("RESOURCE_OWNER_ID", realm)).willReturn(amIdentity);
given(resourceSetQuery.accept(any(QueryFilterVisitor.class), eq(queriedResourceSets))).willReturn(queriedResourceSets);
//When
Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
//Then
assertThat(resourceSets).hasSize(1).contains(resourceSetOne);
assertThat(resourceSetOne.getPolicy()).isEqualTo(policyOneJson);
assertThat(resourceSetTwo.getPolicy()).isNull();
assertThat(resourceSetThree.getPolicy()).isNull();
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class ResourceSetServiceTest method getResourceSetsShouldReturnEmptySetWhenNoResourceSetsExist.
@Test
public void getResourceSetsShouldReturnEmptySetWhenNoResourceSetsExist() throws Exception {
//Given
String realm = "REALM";
Context context = mockContext(realm);
ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
String resourceOwnerId = "RESOURCE_OWNER_ID";
boolean augmentWithPolicies = false;
QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
QueryFilter<JsonPointer> policyQuery = QueryFilter.alwaysFalse();
Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
Collection<UmaPolicy> queriedPolicies = new HashSet<>();
Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
query.setResourceSetQuery(resourceSetQuery);
query.setPolicyQuery(policyQuery);
given(resourceSetStore.query(any(QueryFilter.class))).willReturn(queriedResourceSets);
given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
mockResourceOwnerIdentity(resourceOwnerId, realm);
mockPolicyEvaluator("RS_CLIENT_ID");
mockFilteredResourceSetsQueryVisitor(resourceSetQuery, queriedResourceSets);
//When
Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
//Then
assertThat(resourceSets).isEmpty();
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class TokenResource method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest queryRequest, QueryResourceHandler handler) {
try {
JsonValue response;
Collection<QueryFilter<CoreTokenField>> query = new ArrayList<QueryFilter<CoreTokenField>>();
//get uid of submitter
AMIdentity uid;
try {
uid = getUid(context);
if (!uid.equals(adminUserId)) {
query.add(QueryFilter.equalTo(USERNAME_FIELD, uid.getName()));
query.add(QueryFilter.equalTo(REALM_FIELD, DNMapper.orgNameToRealmName(uid.getRealm())));
}
} catch (Exception e) {
if (debug.errorEnabled()) {
debug.error("TokenResource :: QUERY : Unable to query collection as no UID discovered " + "for requesting user.");
}
return new PermanentException(401, "Unauthorized", e).asPromise();
}
String id = queryRequest.getQueryId();
String queryString;
if (id.equals("access_token")) {
queryString = "tokenName=access_token";
} else {
queryString = id;
}
String[] constraints = queryString.split(",");
boolean userNamePresent = false;
for (String constraint : constraints) {
String[] params = constraint.split("=");
if (params.length == 2) {
if (OAuthTokenField.USER_NAME.getOAuthField().equals(params[0])) {
userNamePresent = true;
}
query.add(QueryFilter.equalTo(getOAuth2TokenField(params[0]), params[1]));
}
}
if (adminUserId.equals(uid)) {
if (!userNamePresent) {
return new BadRequestException("userName field MUST be set in _queryId").asPromise();
}
} else if (userNamePresent) {
return new BadRequestException("userName field MUST NOT be set in _queryId").asPromise();
}
response = tokenStore.query(QueryFilter.and(query));
return handleResponse(handler, response, context);
} catch (UnauthorizedClientException e) {
debug.error("TokenResource :: QUERY : Unable to query collection as the client is not authorized.", e);
return new PermanentException(401, e.getMessage(), e).asPromise();
} catch (CoreTokenException e) {
debug.error("TokenResource :: QUERY : Unable to query collection as the token store is not available.", e);
return new ServiceUnavailableException(e.getMessage(), e).asPromise();
} catch (InternalServerErrorException e) {
debug.error("TokenResource :: QUERY : Unable to query collection as writing the response failed.", e);
return e.asPromise();
} catch (NotFoundException e) {
debug.error("TokenResource :: QUERY : Unable to query collection as realm does not have OAuth 2 provider.", e);
return e.asPromise();
}
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class JavaBeanAdapter method toTokenQuery.
/**
* Use the bean mappings that have been parsed to turn a query keyed by bean property names into a query keyed by
* token property names.
* @param filter The query keyed by bean property names.
* @return The transformed query keyed by token field names.
*/
public TokenFilter toTokenQuery(QueryFilter<String> filter) {
TokenFilterBuilder builder = new TokenFilterBuilder();
List<QueryFilter<CoreTokenField>> tokenFilter = new ArrayList<QueryFilter<CoreTokenField>>();
tokenFilter.add(filter.accept(TOKEN_QUERY_TRANSLATOR, null));
tokenFilter.add(QueryFilter.equalTo(CoreTokenField.TOKEN_TYPE, tokenType));
return builder.withQuery(QueryFilter.and(tokenFilter)).build();
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class OpenAMResourceSetStoreTest method shouldQueryResourceSetToken.
@Test
public void shouldQueryResourceSetToken() throws Exception {
//Given
Map<String, Object> queryParameters = new HashMap<String, Object>();
queryParameters.put(ResourceSetTokenField.CLIENT_ID, "CLIENT_ID");
ResourceSetDescription resourceSet1 = new ResourceSetDescription("123", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
ResourceSetDescription resourceSet2 = new ResourceSetDescription("456", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
given(dataStore.query(Matchers.<QueryFilter<String>>anyObject())).willReturn(asSet(resourceSet1, resourceSet2));
resourceSet1.setRealm("REALM");
resourceSet2.setRealm("REALM");
//When
QueryFilter<String> query = QueryFilter.alwaysTrue();
Set<ResourceSetDescription> resourceSetDescriptions = store.query(query);
//Then
assertThat(resourceSetDescriptions).contains(resourceSet1, resourceSet2);
ArgumentCaptor<QueryFilter> tokenFilterCaptor = ArgumentCaptor.forClass(QueryFilter.class);
verify(dataStore).query(tokenFilterCaptor.capture());
assertThat(tokenFilterCaptor.getValue()).isEqualTo(QueryFilter.and(query, QueryFilter.equalTo(ResourceSetTokenField.REALM, "REALM")));
}
Aggregations