use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class ResourceSetServiceTest method getResourceSetsShouldReturnSetWhenResourceSetsExistQueryingByOr.
@Test
public void getResourceSetsShouldReturnSetWhenResourceSetsExistQueryingByOr() throws Exception {
//Given
Context context = createContext();
String realm = "REALM";
ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
query.setOperator(AggregateQuery.Operator.OR);
String resourceOwnerId = "RESOURCE_OWNER_ID";
boolean augmentWithPolicies = false;
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);
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);
queriedResourceSets.add(resourceSetOne);
queriedResourceSets.add(resourceSetTwo);
queriedPolicies.add(policyOne);
queriedPolicies.add(policyTwo);
mockResourceOwnerIdentity(resourceOwnerId, realm);
mockFilteredResourceSetsQueryVisitor(resourceSetQuery, queriedResourceSets);
given(policyOne.getResourceSet()).willReturn(resourceSetOne);
given(policyOne.getId()).willReturn("RS_ID_ONE");
given(policyTwo.getId()).willReturn("RS_ID_THREE");
given(policyTwo.getResourceSet()).willReturn(resourceSetTwo);
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");
//When
Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
//Then
assertThat(resourceSets).hasSize(3).contains(resourceSetOne, resourceSetTwo, resourceSetThree);
assertThat(resourceSetOne.getPolicy()).isNull();
assertThat(resourceSetTwo.getPolicy()).isNull();
assertThat(resourceSetThree.getPolicy()).isNull();
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpointTest method shouldListResourceSetDescriptions.
@Test
@SuppressWarnings("unchecked")
public void shouldListResourceSetDescriptions() throws Exception {
//Given
Set<ResourceSetDescription> resourceSetDescriptions = new HashSet<ResourceSetDescription>();
ResourceSetDescription resourceSetDescription = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", RESOURCE_SET_DESCRIPTION_CONTENT.asMap());
ResourceSetDescription resourceSetDescription2 = new ResourceSetDescription("RESOURCE_SET_ID_2", "CLIENT_ID", "RESOURCE_OWNER_ID", RESOURCE_SET_DESCRIPTION_UPDATED_CONTENT.asMap());
resourceSetDescriptions.add(resourceSetDescription);
resourceSetDescriptions.add(resourceSetDescription2);
noUriResourceSetId();
noConditions();
given(store.query(any(QueryFilter.class))).willReturn(resourceSetDescriptions);
//When
Representation responseRep = endpoint.readOrListResourceSet();
//Then
ArgumentCaptor<QueryFilter> queryParametersCaptor = ArgumentCaptor.forClass(QueryFilter.class);
verify(store).query(queryParametersCaptor.capture());
QueryFilter<String> query = queryParametersCaptor.getValue();
Map<String, String> params = query.accept(QUERY_PARAMS_EXTRACTOR, new HashMap<String, String>());
assertThat(params).contains(entry(ResourceSetTokenField.CLIENT_ID, "CLIENT_ID"), entry(ResourceSetTokenField.RESOURCE_OWNER_ID, "RESOURCE_OWNER_ID"));
List<String> responseBody = (List<String>) new ObjectMapper().readValue(responseRep.getText(), List.class);
assertThat(responseBody).contains("RESOURCE_SET_ID", "RESOURCE_SET_ID_2");
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpointTest method shouldNotCreateExistingResourceSetDescription.
@Test
@SuppressWarnings("unchecked")
public void shouldNotCreateExistingResourceSetDescription() throws Exception {
//Given
JsonRepresentation entity = createCreateRequestRepresentation();
when(store.query(any(QueryFilter.class))).thenReturn(asSet(new ResourceSetDescription("id", "CLIENT_ID", "RESOURCE_OWNER_ID", RESOURCE_SET_DESCRIPTION_CONTENT.asMap())));
noConditions();
//When
Representation result = endpoint.createResourceSet(entity);
//Then
ArgumentCaptor<QueryFilter> queryCaptor = ArgumentCaptor.forClass(QueryFilter.class);
verify(store).query(queryCaptor.capture());
verifyZeroInteractions(resourceRegistrationFilter);
String queryString = queryCaptor.getValue().toString();
assertThat(queryString).contains("name eq \"NAME\"").contains("clientId eq \"CLIENT_ID\"").contains("resourceOwnerId eq \"RESOURCE_OWNER_ID\"").doesNotContain(" or ");
verify(response).setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
assertThat(result).isInstanceOf(JsonRepresentation.class);
assertThat(((JsonRepresentation) result).getJsonObject().get("error")).isEqualTo("Bad Request");
assertThat(((JsonRepresentation) result).getJsonObject().getString("error_description")).contains("'NAME' already exists");
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldQuery.
@Test
public void shouldQuery() throws Exception {
// Given
final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class);
given(mockBuilder.withFilter(any(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
given(mockBuilder.execute(any(Connection.class))).willReturn(Arrays.asList((Collection<Token>) Arrays.asList(new Token("weasel", TokenType.OAUTH))).iterator());
given(mockQueryFactory.createInstance()).willReturn(mockBuilder);
QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class);
given(mockQueryFactory.createFilterConverter()).willReturn(visitor);
given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue());
// When
TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).build();
Collection<Token> result = adapter.query(mockConnection, filter);
// Then
verify(mockBuilder).withFilter(any(Filter.class));
verify(mockBuilder).execute(mockConnection);
assertThat(result.size()).isEqualTo(1);
assertThat(result.iterator().next().getTokenId()).isEqualTo("weasel");
}
use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldPartialQuery.
@Test
public void shouldPartialQuery() throws Exception {
// Given
final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class);
given(mockBuilder.withFilter(any(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
given(mockBuilder.returnTheseAttributes(anySetOf(CoreTokenField.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() {
@Override
public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable {
return mockBuilder;
}
});
PartialToken partialToken = new PartialToken(new HashMap<CoreTokenField, Object>());
given(mockBuilder.executeAttributeQuery(any(Connection.class))).willReturn(Arrays.asList((Collection<PartialToken>) Arrays.asList(partialToken)).iterator());
given(mockQueryFactory.createInstance()).willReturn(mockBuilder);
QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class);
given(mockQueryFactory.createFilterConverter()).willReturn(visitor);
given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue());
// When
TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).returnAttribute(CoreTokenField.STRING_ONE).build();
Collection<PartialToken> result = adapter.partialQuery(mockConnection, filter);
// Then
verify(mockBuilder).withFilter(any(Filter.class));
verify(mockBuilder).returnTheseAttributes(asSet(CoreTokenField.STRING_ONE));
verify(mockBuilder).executeAttributeQuery(mockConnection);
assertThat(result).containsOnly(partialToken);
}
Aggregations