Search in sources :

Example 1 with Task

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

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

the class TokenDataStoreTest method testCreateExecutorError.

@Test(expectedExceptions = ServerException.class)
public void testCreateExecutorError() throws Exception {
    // Given
    Token token = new Token("123", TokenType.GENERIC);
    final Task task = mock(Task.class);
    when(adapter.toToken(anyObject())).thenReturn(token);
    when(taskFactory.create(any(Token.class), any(ResultHandler.class))).thenReturn(task);
    doThrow(DataLayerException.class).when(taskExecutor).execute("123", task);
    // When
    store.create(new Object());
// 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) Test(org.testng.annotations.Test)

Example 3 with Task

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

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

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

Aggregations

Task (org.forgerock.openam.sm.datalayer.api.Task)28 Test (org.testng.annotations.Test)26 Token (org.forgerock.openam.cts.api.tokens.Token)17 ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)17 InvocationOnMock (org.mockito.invocation.InvocationOnMock)15 DataLayerException (org.forgerock.openam.sm.datalayer.api.DataLayerException)7 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)5 Debug (com.sun.identity.shared.debug.Debug)1 Closeable (java.io.Closeable)1 Collection (java.util.Collection)1 Semaphore (java.util.concurrent.Semaphore)1 ConnectionConfig (org.forgerock.openam.sm.ConnectionConfig)1 ConnectionConfigFactory (org.forgerock.openam.sm.ConnectionConfigFactory)1 ConnectionType (org.forgerock.openam.sm.datalayer.api.ConnectionType)1 TaskExecutor (org.forgerock.openam.sm.datalayer.api.TaskExecutor)1