Search in sources :

Example 6 with PartialToken

use of org.forgerock.openam.sm.datalayer.api.query.PartialToken in project OpenAM by OpenRock.

the class PartialTokenTest method shouldSupportEmptyCollectionOfFields.

@Test
public void shouldSupportEmptyCollectionOfFields() {
    Map<CoreTokenField, Object> empty = Collections.emptyMap();
    assertThat(new PartialToken(empty).getFields()).isEmpty();
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Test(org.testng.annotations.Test)

Example 7 with PartialToken

use of org.forgerock.openam.sm.datalayer.api.query.PartialToken in project OpenAM by OpenRock.

the class SessionCount method getSessionsFromRepository.

private static Map<String, Long> getSessionsFromRepository(String uuid) throws Exception {
    CTSPersistentStore repo = sessionService.getRepository();
    try {
        // Filter and Query the CTS
        TokenFilter filter = new TokenFilterBuilder().returnAttribute(SessionTokenField.SESSION_ID.getField()).returnAttribute(CoreTokenField.EXPIRY_DATE).and().withAttribute(CoreTokenField.USER_ID, uuid).build();
        Collection<PartialToken> partialTokens = repo.attributeQuery(filter);
        if (debug.messageEnabled()) {
            debug.message(MessageFormat.format("getSessionsFromRepository query success:\n" + "Query: {0}\n" + "Count: {1}", filter, partialTokens.size()));
        }
        // Populate the return Map from the query results.
        Map<String, Long> sessions = new HashMap<String, Long>();
        for (PartialToken partialToken : partialTokens) {
            // Session ID
            String sessionId = partialToken.getValue(SessionTokenField.SESSION_ID.getField());
            // Expiration Date converted to Unix Time
            Calendar timestamp = partialToken.getValue(CoreTokenField.EXPIRY_DATE);
            long unixTime = TimeUtils.toUnixTime(timestamp);
            sessions.put(sessionId, unixTime);
        }
        if (debug.messageEnabled()) {
            debug.message(MessageFormat.format("getSessionsFromRepository query results:\n" + "{0}", sessions));
        }
        return sessions;
    } catch (Exception e) {
        debug.error("SessionCount.getSessionsFromRepository: " + "Session repository is not available", e);
        throw e;
    }
}
Also used : CTSPersistentStore(org.forgerock.openam.cts.CTSPersistentStore) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) HashMap(java.util.HashMap) Calendar(java.util.Calendar) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) SessionException(com.iplanet.dpro.session.SessionException) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Example 8 with PartialToken

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

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

use of org.forgerock.openam.sm.datalayer.api.query.PartialToken in project OpenAM by OpenRock.

the class DeleteOnQueryResultHandlerTest method shouldInvokeDeleteForEachHitEvenIfThereWasAnException.

@Test
public void shouldInvokeDeleteForEachHitEvenIfThereWasAnException() throws Exception {
    Collection<PartialToken> hits = getHits(5);
    handler.processResults(hits);
    willThrow(new CoreTokenException("")).willNothing().given(mockTaskDispatcher).delete(anyString(), any(ResultHandler.class));
    for (PartialToken hit : hits) {
        verify(mockTaskDispatcher).delete(eq(hit.<String>getValue(CoreTokenField.TOKEN_ID)), any(ResultHandler.class));
    }
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) Test(org.testng.annotations.Test)

Aggregations

PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)13 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)5 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)5 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)2 SessionException (com.iplanet.dpro.session.SessionException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CTSPersistentStore (org.forgerock.openam.cts.CTSPersistentStore)1 DeleteFailedException (org.forgerock.openam.cts.exceptions.DeleteFailedException)1 TokenStrategyFailedException (org.forgerock.openam.cts.utils.blob.TokenStrategyFailedException)1 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)1 CTSTokenPersistenceException (org.forgerock.openam.sts.CTSTokenPersistenceException)1 STSIssuedTokenState (org.forgerock.openam.sts.user.invocation.STSIssuedTokenState)1 Connection (org.forgerock.opendj.ldap.Connection)1