### Question:
TokenDataStore { public Set<T> query(QueryFilter<String> query) throws ServerException { SyncResultHandler<Collection<Token>> handler = new SyncResultHandler<Collection<Token>>(); try { Task task = taskFactory.query(adapter.toTokenQuery(query), handler); taskExecutor.execute(null, task); return convertResults(handler.getResults()); } catch (ServerException e) { throw e; } catch (DataLayerException e) { if (debug.warningEnabled()) { debug.warning("Unable to read objects corresponding to query: " + query, e); } throw new ServerException("Could not query tokens from data store: " + e.getMessage()); } } TokenDataStore(JavaBeanAdapter<T> adapter, TaskExecutor taskExecutor, TaskFactory taskFactory); @VisibleForTesting  TokenDataStore(JavaBeanAdapter<T> adapter, TaskExecutor taskExecutor, TaskFactory taskFactory, Debug debug); void create(T obj); T read(String id); void update(T obj); void delete(String id); Set<T> query(QueryFilter<String> query);  }### Answer:
@Test public void testQuery() throws Exception { 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; } }); Set<Object> result = store.query(query); verify(taskFactory).query(eq(tokenFilter), any(ResultHandler.class)); verify(taskExecutor).execute(null, task); assertThat(result).containsOnly(o1, o2); }
@Test(expectedExceptions = ServerException.class) public void testQueryError() throws Exception { 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; } }); store.query(query); }
@Test(expectedExceptions = ServerException.class) public void testQueryExecutorError() throws Exception { 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); store.query(query); }