### Question:
LdapAdapter implements TokenStorageAdapter<Connection> { @Override public Collection<Token> query(Connection connection, TokenFilter query) throws DataLayerException { try { return queryFactory.createInstance() .withFilter(query.getQuery().accept(queryConverter, null)) .execute(connection).next(); } catch (DataLayerRuntimeException e) { throw new DataLayerException("Error during partial query", e); } } @Inject  LdapAdapter(LdapTokenAttributeConversion conversion, LdapQueryFilterVisitor queryConverter,
            LdapQueryFactory queryFactory); void create(Connection connection, Token token); Token read(Connection connection,  String tokenId); boolean update(Connection connection, Token previous, Token updated); void delete(Connection connection, String tokenId); @Override Collection<Token> query(Connection connection, TokenFilter query); @Override Collection<PartialToken> partialQuery(Connection connection, TokenFilter query);  }### Answer:
@Test public void shouldQuery() throws Exception { final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class); given(mockBuilder.withFilter(nullable(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() { @Override public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable { return mockBuilder; } }); given(mockBuilder.execute(nullable(Connection.class))) .willReturn(Arrays.asList((Collection<Token>) Arrays.asList(new Token("weasel", TokenType.OAUTH))).iterator()); given(mockQueryFactory.createInstance()).willReturn(mockBuilder); QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class); given(mockQueryFactory.createFilterConverter()).willReturn(visitor); given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue()); TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).build(); Collection<Token> result = adapter.query(mockConnection, filter); verify(mockBuilder).withFilter(nullable(Filter.class)); verify(mockBuilder).execute(mockConnection); assertThat(result.size()).isEqualTo(1); assertThat(result.iterator().next().getTokenId()).isEqualTo("weasel"); }
@Test public void shouldQuery() throws Exception { final QueryBuilder<Connection, Filter> mockBuilder = mock(QueryBuilder.class); given(mockBuilder.withFilter(any(Filter.class))).willAnswer(new Answer<QueryBuilder<Connection, Filter>>() { @Override public QueryBuilder<Connection, Filter> answer(InvocationOnMock invocation) throws Throwable { return mockBuilder; } }); given(mockBuilder.execute(any(Connection.class))) .willReturn(Arrays.asList((Collection<Token>) Arrays.asList(new Token("weasel", TokenType.OAUTH))).iterator()); given(mockQueryFactory.createInstance()).willReturn(mockBuilder); QueryFilterVisitor<Filter, Void, CoreTokenField> visitor = mock(QueryFilterVisitor.class); given(mockQueryFactory.createFilterConverter()).willReturn(visitor); given(visitor.visitBooleanLiteralFilter(null, true)).willReturn(Filter.alwaysTrue()); TokenFilter filter = new TokenFilterBuilder().withQuery(QueryFilter.<CoreTokenField>alwaysTrue()).build(); Collection<Token> result = adapter.query(mockConnection, filter); verify(mockBuilder).withFilter(any(Filter.class)); verify(mockBuilder).execute(mockConnection); assertThat(result.size()).isEqualTo(1); assertThat(result.iterator().next().getTokenId()).isEqualTo("weasel"); }