Search in sources :

Example 1 with ResultHandler

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

the class TokenDataStoreTest method testRead.

@Test
public void testRead() throws Exception {
    // Given
    final Token token = new Token("123", TokenType.GENERIC);
    Object returned = new Object();
    final Task task = mock(Task.class);
    when(adapter.fromToken(token)).thenReturn(returned);
    when(taskFactory.read(anyString(), any(ResultHandler.class))).thenAnswer(new Answer<Task>() {

        @Override
        public Task answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(token);
            return task;
        }
    });
    // When
    Object result = store.read("123");
    // Then
    verify(taskFactory).read(eq("123"), any(ResultHandler.class));
    verify(taskExecutor).execute("123", task);
    assertThat(result).isSameAs(returned);
}
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) Test(org.testng.annotations.Test)

Example 2 with ResultHandler

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

the class TokenDataStoreTest method testUpdateError.

@Test(expectedExceptions = ServerException.class)
public void testUpdateError() throws Exception {
    // Given
    final Token token = new Token("123", TokenType.GENERIC);
    Object returned = new Object();
    final Task readTask = mock(Task.class);
    final Task updateTask = mock(Task.class);
    when(adapter.toToken(any())).thenReturn(token);
    when(adapter.fromToken(token)).thenReturn(returned);
    when(taskFactory.read(eq("123"), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(token);
            return readTask;
        }
    });
    when(taskFactory.update(any(Token.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processError(new Exception());
            return updateTask;
        }
    });
    // When
    store.update(new Object());
// 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) Test(org.testng.annotations.Test)

Example 3 with ResultHandler

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

the class TokenDataStoreTest method testUpdateExecutorError.

@Test(expectedExceptions = ServerException.class)
public void testUpdateExecutorError() throws Exception {
    // Given
    final Token token = new Token("123", TokenType.GENERIC);
    Object returned = new Object();
    final Task readTask = mock(Task.class);
    final Task updateTask = mock(Task.class);
    when(adapter.toToken(any())).thenReturn(token);
    when(adapter.fromToken(token)).thenReturn(returned);
    when(taskFactory.read(eq("123"), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(token);
            return readTask;
        }
    });
    when(taskFactory.update(any(Token.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(token);
            return updateTask;
        }
    });
    doThrow(DataLayerException.class).when(taskExecutor).execute(anyString(), eq(updateTask));
    // When
    store.update(new Object());
// 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) Test(org.testng.annotations.Test)

Example 4 with ResultHandler

use of org.forgerock.openam.sm.datalayer.api.ResultHandler 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 5 with ResultHandler

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

Aggregations

ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)14 Task (org.forgerock.openam.sm.datalayer.api.Task)14 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 Test (org.testng.annotations.Test)14 Token (org.forgerock.openam.cts.api.tokens.Token)12 DataLayerException (org.forgerock.openam.sm.datalayer.api.DataLayerException)5 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)2