Search in sources :

Example 1 with TokenFilter

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);
}
Also used : TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 2 with TokenFilter

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;
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 3 with TokenFilter

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;
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) DataLayerException(org.forgerock.openam.sm.datalayer.api.DataLayerException) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 4 with TokenFilter

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);
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 5 with TokenFilter

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);
    }
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) ArrayList(java.util.ArrayList) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) DeleteFailedException(org.forgerock.openam.cts.exceptions.DeleteFailedException) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) Map(java.util.Map) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Aggregations

TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)15 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)10 Test (org.testng.annotations.Test)10 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)7 Token (org.forgerock.openam.cts.api.tokens.Token)6 ArrayList (java.util.ArrayList)5 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 Task (org.forgerock.openam.sm.datalayer.api.Task)5 ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)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 QueryFilter (org.forgerock.util.query.QueryFilter)2 SessionException (com.iplanet.dpro.session.SessionException)1 SSOException (com.iplanet.sso.SSOException)1