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();
}
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;
}
}
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));
}
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);
}
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));
}
}
Aggregations