Search in sources :

Example 6 with QueryFilter

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();
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) QueryFilter(org.forgerock.util.query.QueryFilter) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 7 with QueryFilter

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");
}
Also used : JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) QueryFilter(org.forgerock.util.query.QueryFilter) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with QueryFilter

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");
}
Also used : QueryFilter(org.forgerock.util.query.QueryFilter) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Test(org.testng.annotations.Test)

Example 9 with QueryFilter

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");
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Token(org.forgerock.openam.cts.api.tokens.Token) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) QueryBuilder(org.forgerock.openam.sm.datalayer.api.query.QueryBuilder) Filter(org.forgerock.opendj.ldap.Filter) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) QueryFilter(org.forgerock.util.query.QueryFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 10 with QueryFilter

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);
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Connection(org.forgerock.opendj.ldap.Connection) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) QueryBuilder(org.forgerock.openam.sm.datalayer.api.query.QueryBuilder) Filter(org.forgerock.opendj.ldap.Filter) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) QueryFilter(org.forgerock.util.query.QueryFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Aggregations

QueryFilter (org.forgerock.util.query.QueryFilter)16 Test (org.testng.annotations.Test)10 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)9 HashSet (java.util.HashSet)8 ResourceException (org.forgerock.json.resource.ResourceException)7 Collection (java.util.Collection)6 QueryResponse (org.forgerock.json.resource.QueryResponse)6 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)6 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)6 Pair (org.forgerock.util.Pair)6 RealmContext (org.forgerock.openam.rest.RealmContext)5 Context (org.forgerock.services.context.Context)5 RootContext (org.forgerock.services.context.RootContext)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JsonValue (org.forgerock.json.JsonValue)3 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)3 ResourceType (org.forgerock.openam.entitlement.ResourceType)3 Filter (org.forgerock.opendj.ldap.Filter)3