Search in sources :

Example 1 with QueryBuilder

use of org.forgerock.openam.sm.datalayer.api.query.QueryBuilder 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 2 with QueryBuilder

use of org.forgerock.openam.sm.datalayer.api.query.QueryBuilder 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

TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)2 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)2 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)2 Connection (org.forgerock.opendj.ldap.Connection)2 Filter (org.forgerock.opendj.ldap.Filter)2 QueryFilter (org.forgerock.util.query.QueryFilter)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Test (org.testng.annotations.Test)2 Token (org.forgerock.openam.cts.api.tokens.Token)1