Search in sources :

Example 6 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class CoreTokenAdapterTest method shouldUseTaskQueueForQuery.

@Test
public void shouldUseTaskQueueForQuery() throws CoreTokenException {
    // Given
    Collection<Token> tokens = new ArrayList<Token>();
    ResultHandler<Collection<Token>, CoreTokenException> mockResultHandler = mock(ResultHandler.class);
    given(mockResultHandler.getResults()).willReturn(tokens);
    given(mockResultHandlerFactory.getQueryHandler()).willReturn(mockResultHandler);
    TokenFilter filter = new TokenFilterBuilder().and().build();
    // When
    adapter.query(filter);
    // Then
    verify(mockTaskDispatcher).query(eq(filter), eq(mockResultHandler));
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) Token(org.forgerock.openam.cts.api.tokens.Token) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 7 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class CoreTokenAdapterTest method shouldUseTaskQueueForAttributeQuery.

@Test
public void shouldUseTaskQueueForAttributeQuery() throws CoreTokenException {
    // Given
    Collection<PartialToken> partialTokens = new ArrayList<PartialToken>();
    ResultHandler<Collection<PartialToken>, CoreTokenException> mockResultHandler = mock(ResultHandler.class);
    given(mockResultHandler.getResults()).willReturn(partialTokens);
    given(mockResultHandlerFactory.getPartialQueryHandler()).willReturn(mockResultHandler);
    TokenFilter filter = new TokenFilterBuilder().returnAttribute(CoreTokenField.BLOB).build();
    // When
    adapter.attributeQuery(filter);
    // Then
    verify(mockTaskDispatcher).partialQuery(eq(filter), eq(mockResultHandler));
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 8 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder 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 9 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder 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)

Example 10 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class CtsPersistenceOperationsDelegate method listDurationOfTokens.

/**
     * Gathers list of the durations of tokens in epoch'd seconds
     *
     * @param tokenType The type of token for which we are gathering results
     * @return A collection of longs, each of which represents the duration of a token inside the CTS
     * @throws CoreTokenException
     */
public Collection<Long> listDurationOfTokens(TokenType tokenType) throws CoreTokenException {
    final Collection<Long> results = new ArrayList<Long>();
    final long unixTime = TimeUtils.currentUnixTime();
    final TokenFilter filter = new TokenFilterBuilder().returnAttribute(CoreTokenField.CREATE_TIMESTAMP).and().withAttribute(CoreTokenField.TOKEN_TYPE, tokenType).build();
    for (PartialToken token : store.attributeQuery(filter)) {
        Calendar timestamp = token.getValue(CoreTokenField.CREATE_TIMESTAMP);
        results.add(unixTime - TimeUtils.toUnixTime(timestamp));
    }
    return results;
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Aggregations

TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)11 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)10 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)7 ArrayList (java.util.ArrayList)6 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 Test (org.testng.annotations.Test)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 Token (org.forgerock.openam.cts.api.tokens.Token)3 QueryFilter (org.forgerock.util.query.QueryFilter)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2 Connection (org.forgerock.opendj.ldap.Connection)2 Filter (org.forgerock.opendj.ldap.Filter)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 SessionException (com.iplanet.dpro.session.SessionException)1 SSOException (com.iplanet.sso.SSOException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1 Map (java.util.Map)1