use of org.forgerock.openam.cts.api.filter.TokenFilter in project OpenAM by OpenRock.
the class CoreTokenAdapterTest method shouldAddTokenIDAsReturnFieldForDeleteOnQuery.
@Test
public void shouldAddTokenIDAsReturnFieldForDeleteOnQuery() throws CoreTokenException {
// Given
TokenFilter filter = new TokenFilterBuilder().build();
// When
adapter.deleteOnQuery(filter);
// Then
ArgumentCaptor<TokenFilter> captor = ArgumentCaptor.forClass(TokenFilter.class);
verify(mockTaskDispatcher).partialQuery(captor.capture(), any(ResultHandler.class));
TokenFilter capturedFilter = captor.getValue();
assertThat(capturedFilter).isSameAs(filter);
assertThat(capturedFilter.getReturnFields()).containsOnly(CoreTokenField.TOKEN_ID);
}
use of org.forgerock.openam.cts.api.filter.TokenFilter in project OpenAM by OpenRock.
the class TokenDataStoreTest method testQueryExecutorError.
@Test(expectedExceptions = ServerException.class)
public void testQueryExecutorError() throws Exception {
// Given
QueryFilter<String> query = QueryFilter.alwaysTrue();
TokenFilter tokenFilter = mock(TokenFilter.class);
when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
final Token token1 = new Token("123", TokenType.GENERIC);
final Token token2 = new Token("456", TokenType.GENERIC);
Object o1 = new Object();
Object o2 = new Object();
when(adapter.fromToken(token1)).thenReturn(o1);
when(adapter.fromToken(token2)).thenReturn(o2);
final Task task = mock(Task.class);
when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenReturn(task);
doThrow(DataLayerException.class).when(taskExecutor).execute(null, task);
// When
store.query(query);
// Then - exception;
}
use of org.forgerock.openam.cts.api.filter.TokenFilter in project OpenAM by OpenRock.
the class TokenDataStoreTest method testQueryError.
@Test(expectedExceptions = ServerException.class)
public void testQueryError() throws Exception {
// Given
QueryFilter<String> query = QueryFilter.alwaysTrue();
TokenFilter tokenFilter = mock(TokenFilter.class);
when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
final Token token1 = new Token("123", TokenType.GENERIC);
final Token token2 = new Token("456", TokenType.GENERIC);
Object o1 = new Object();
Object o2 = new Object();
when(adapter.fromToken(token1)).thenReturn(o1);
when(adapter.fromToken(token2)).thenReturn(o2);
final Task task = mock(Task.class);
when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
((ResultHandler) invocation.getArguments()[1]).processError(new Exception());
return task;
}
});
// When
store.query(query);
// Then - exception;
}
use of org.forgerock.openam.cts.api.filter.TokenFilter in project OpenAM by OpenRock.
the class TokenDataStoreTest method testQuery.
@Test
public void testQuery() throws Exception {
// Given
QueryFilter<String> query = QueryFilter.alwaysTrue();
TokenFilter tokenFilter = mock(TokenFilter.class);
when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
final Token token1 = new Token("123", TokenType.GENERIC);
final Token token2 = new Token("456", TokenType.GENERIC);
Object o1 = new Object();
Object o2 = new Object();
when(adapter.fromToken(token1)).thenReturn(o1);
when(adapter.fromToken(token2)).thenReturn(o2);
final Task task = mock(Task.class);
when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
((ResultHandler) invocation.getArguments()[1]).processResults(asSet(token1, token2));
return task;
}
});
// When
Set<Object> result = store.query(query);
// Then
verify(taskFactory).query(eq(tokenFilter), any(ResultHandler.class));
verify(taskExecutor).execute(null, task);
assertThat(result).containsOnly(o1, o2);
}
use of org.forgerock.openam.cts.api.filter.TokenFilter in project OpenAM by OpenRock.
the class CTSPersistentStoreImpl method delete.
@Override
public int delete(Map<CoreTokenField, Object> query) throws DeleteFailedException {
TokenFilterBuilder.FilterAttributeBuilder builder = new TokenFilterBuilder().returnAttribute(CoreTokenField.TOKEN_ID).and();
for (Map.Entry<CoreTokenField, Object> entry : query.entrySet()) {
CoreTokenField key = entry.getKey();
Object value = entry.getValue();
builder = builder.withAttribute(key, value);
}
TokenFilter filter = builder.build();
Collection<String> failures = new ArrayList<String>();
try {
Collection<PartialToken> partialTokens = attributeQuery(filter);
debug("Found {0} partial Tokens for deletion", Integer.toString(partialTokens.size()));
for (PartialToken token : partialTokens) {
String tokenId = token.getValue(CoreTokenField.TOKEN_ID);
try {
delete(tokenId);
} catch (CoreTokenException e) {
failures.add(tokenId);
}
}
if (!failures.isEmpty()) {
error("Failed to delete {0} tokens.\n{1}", Integer.toString(failures.size()), StringUtils.join(failures, ","));
}
return partialTokens.size() - failures.size();
} catch (CoreTokenException e) {
throw new DeleteFailedException("Failed to delete Tokens", e);
}
}
Aggregations