diff --git a/src/main/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQuery.java b/src/main/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQuery.java index 6eabb9c60..4adc6453f 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQuery.java +++ b/src/main/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQuery.java @@ -57,7 +57,6 @@ public abstract class AbstractN1qlBasedQuery implements RepositoryQuery { protected final CouchbaseQueryMethod queryMethod; private final CouchbaseOperations couchbaseOperations; - private Class returnedType; protected AbstractN1qlBasedQuery(CouchbaseQueryMethod queryMethod, CouchbaseOperations couchbaseOperations) { this.queryMethod = queryMethod; @@ -87,11 +86,9 @@ public Object execute(Object[] parameters) { ResultProcessor processor = this.queryMethod.getResultProcessor().withDynamicProjection(accessor); ReturnedType returnedType = processor.getReturnedType(); - this.returnedType = returnedType.getReturnedType(); - - if (this.returnedType.isInterface()) { - this.returnedType = returnedType.getDomainType(); - } + + Class typeToRead = returnedType.getTypeToRead(); + typeToRead = typeToRead == null ? returnedType.getDomainType() : typeToRead; Statement statement = getStatement(accessor, parameters, returnedType); JsonValue queryPlaceholderValues = getPlaceholderValues(accessor); @@ -104,8 +101,7 @@ public Object execute(Object[] parameters) { Statement countStatement = getCount(accessor, parameters); N1qlQuery countQuery = buildQuery(countStatement, queryPlaceholderValues, getCouchbaseOperations().getDefaultConsistency().n1qlConsistency()); - return processor.processResult(executeDependingOnType(query, countQuery, queryMethod, accessor.getPageable(), - queryMethod.isPageQuery(), queryMethod.isSliceQuery(), queryMethod.isModifyingQuery())); + return processor.processResult(executeDependingOnType(query, countQuery, queryMethod, accessor.getPageable(), typeToRead)); } protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) { @@ -122,22 +118,23 @@ protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceh return query; } - protected Object executeDependingOnType(N1qlQuery query, N1qlQuery countQuery, QueryMethod queryMethod, Pageable pageable, - boolean isPage, boolean isSlice, boolean isModifying) { - if (isModifying) { + protected Object executeDependingOnType(N1qlQuery query, N1qlQuery countQuery, QueryMethod queryMethod, + Pageable pageable, Class typeToRead) { + + if (queryMethod.isModifyingQuery()) { throw new UnsupportedOperationException("Modifying queries not yet supported"); } - if (isPage) { - return executePaged(query, countQuery, pageable); - } else if (isSlice) { - return executeSliced(query, countQuery, pageable); + if (queryMethod.isPageQuery()) { + return executePaged(query, countQuery, pageable, typeToRead); + } else if (queryMethod.isSliceQuery()) { + return executeSliced(query, countQuery, pageable, typeToRead); } else if (queryMethod.isCollectionQuery()) { - return executeCollection(query); - } else if (queryMethod.isQueryForEntity()) { - return executeEntity(query); + return executeCollection(query, typeToRead); } else if (queryMethod.isStreamQuery()){ - return executeStream(query); + return executeStream(query, typeToRead); + } else if (queryMethod.isQueryForEntity()) { + return executeEntity(query, typeToRead); } else if (queryMethod.getReturnedObjectType().isPrimitive() && useGeneratedCountQuery()) { //attempt to execute the created COUNT query @@ -156,24 +153,24 @@ private void logIfNecessary(N1qlQuery query) { } } - protected List executeCollection(N1qlQuery query) { + protected List executeCollection(N1qlQuery query, Class typeToRead) { logIfNecessary(query); - List result = couchbaseOperations.findByN1QL(query, this.returnedType); + List result = couchbaseOperations.findByN1QL(query, typeToRead); return result; } - protected Object executeEntity(N1qlQuery query) { + protected Object executeEntity(N1qlQuery query, Class typeToRead) { logIfNecessary(query); - List result = executeCollection(query); + List result = executeCollection(query, typeToRead); return result.isEmpty() ? null : result.get(0); } - protected Object executeStream(N1qlQuery query) { + protected Object executeStream(N1qlQuery query, Class typeToRead) { logIfNecessary(query); - return StreamUtils.createStreamFromIterator(executeCollection(query).iterator()); + return StreamUtils.createStreamFromIterator(executeCollection(query, typeToRead).iterator()); } - protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable) { + protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class typeToRead) { Assert.notNull(pageable); long total = 0L; logIfNecessary(countQuery); @@ -183,14 +180,14 @@ protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pa } logIfNecessary(query); - List result = couchbaseOperations.findByN1QL(query, this.returnedType); + List result = couchbaseOperations.findByN1QL(query, typeToRead); return new PageImpl(result, pageable, total); } - protected Object executeSliced(N1qlQuery query, N1qlQuery countQuery, Pageable pageable) { + protected Object executeSliced(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class typeToRead) { Assert.notNull(pageable); logIfNecessary(query); - List result = couchbaseOperations.findByN1QL(query, this.returnedType); + List result = couchbaseOperations.findByN1QL(query, typeToRead); int pageSize = pageable.getPageSize(); boolean hasNext = result.size() > pageSize; diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQueryTest.java b/src/test/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQueryTest.java index 318cb4393..b376fbaa6 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQueryTest.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/AbstractN1qlBasedQueryTest.java @@ -1,12 +1,30 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.couchbase.repository.query; import static com.couchbase.client.java.query.Select.select; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.stream.Stream; import com.couchbase.client.java.document.json.JsonArray; import com.couchbase.client.java.document.json.JsonObject; @@ -18,13 +36,29 @@ import com.couchbase.client.java.query.consistency.ScanConsistency; import org.junit.Test; import org.mockito.Mockito; - -import org.springframework.data.couchbase.core.mapping.event.User; +import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.data.repository.query.QueryCreationException; +import org.springframework.data.domain.Slice; +import org.springframework.data.projection.ProjectionFactory; +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; import org.springframework.data.repository.query.QueryMethod; +/** + * Unit tests for {@link AbstractN1qlBasedQuery}. + * + * @author Simon Basle + * @author Subhashni Balakrishnan + * @author Oliver Gierke + */ public class AbstractN1qlBasedQueryTest { + + CouchbaseMappingContext context = new CouchbaseMappingContext(); + ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory(); + RepositoryMetadata metadata = DefaultRepositoryMetadata.getMetadata(SampleRepository.class); @Test public void testEmptyArgumentsShouldProduceSimpleN1qlQuery() throws Exception { @@ -77,162 +111,201 @@ public void testMultipleArgumentsShouldProduceParametrizedQuery() throws Excepti } @Test - public void shouldChooseCollectionExecutionWhenCollectionType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldChooseCollectionExecutionWhenCollectionType() throws Exception { + + Method method = SampleRepository.class.getMethod("findAll"); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); - when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) + when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), any(Class.class))) .thenCallRealMethod(); - when(queryMethod.isCollectionQuery()).thenReturn(true); - - mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, false); - verify(mock).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Object.class); + verify(mock).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldChooseEntityExecutionWhenEntityType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldChooseEntityExecutionWhenEntityType() throws Exception { + + Method method = SampleRepository.class.getMethod("findById", Integer.class); + + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); - when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) + when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), any(Class.class))) .thenCallRealMethod(); - when(queryMethod.isQueryForEntity()).thenReturn(true); - - mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldChooseStreamExecutionWhenStreamType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldChooseStreamExecutionWhenStreamType() throws Exception { + + Method method = SampleRepository.class.getMethod("streamAll"); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) + any(Class.class))) .thenCallRealMethod(); - when(queryMethod.isStreamQuery()).thenReturn(true); - - mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldChoosePagedExecutionWhenPageType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldChoosePagedExecutionWhenPageType() throws Exception { + + Method method = SampleRepository.class.getMethod("findAllPaged", Pageable.class); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) + any(Class.class))) .thenCallRealMethod(); - mock.executeDependingOnType(query, query, queryMethod, pageable, true, false, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); + + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldChooseSlicedExecutionWhenSliceType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldChooseSlicedExecutionWhenSliceType() throws Exception { + + Method method = SampleRepository.class.getMethod("findAllSliced", Pageable.class); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) - .thenCallRealMethod(); - when(queryMethod.isSliceQuery()).thenReturn(true); - - mock.executeDependingOnType(query, query, queryMethod, pageable, false, true, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + any(Class.class))).thenCallRealMethod(); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); + + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldThrowWhenModifyingType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); + public void shouldThrowWhenModifyingType() throws Exception { + + Method method = SampleRepository.class.getMethod("modifyingMethod"); + CouchbaseQueryMethod queryMethod = spy(new CouchbaseQueryMethod(method, metadata, projectionFactory, context)); + when(queryMethod.isModifyingQuery()).thenReturn(true); + N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) - .thenCallRealMethod(); + any(Class.class))).thenCallRealMethod(); - try { mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, true); fail(); } catch (UnsupportedOperationException e) { } - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + try { mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); fail(); } catch (UnsupportedOperationException e) { } + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock, never()).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldExecuteSingleProjectionWhenRandomObjectReturnType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); - doReturn(CountDownLatch.class).when(queryMethod).getReturnedObjectType(); + public void shouldExecuteSingleProjectionWhenRandomObjectReturnType() throws Exception { + + Method method = SampleRepository.class.getMethod("countDown"); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) - .thenCallRealMethod(); + any(Class.class))).thenCallRealMethod(); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); - mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock).executeSingleProjection(any(N1qlQuery.class)); } @Test - public void shouldExecuteSingleProjectionWhenPrimitiveReturnType() { - CouchbaseQueryMethod queryMethod = Mockito.mock(CouchbaseQueryMethod.class); - doReturn(long.class).when(queryMethod).getReturnedObjectType(); + public void shouldExecuteSingleProjectionWhenPrimitiveReturnType() throws Exception { + + Method method = SampleRepository.class.getMethod("longMethod"); + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, projectionFactory, context); N1qlQuery query = Mockito.mock(N1qlQuery.class); Pageable pageable = Mockito.mock(Pageable.class); AbstractN1qlBasedQuery mock = mock(AbstractN1qlBasedQuery.class); when(mock.executeDependingOnType(any(N1qlQuery.class), any(N1qlQuery.class), any(QueryMethod.class), any(Pageable.class), - anyBoolean(), anyBoolean(), anyBoolean())) - .thenCallRealMethod(); + any(Class.class))).thenCallRealMethod(); + + mock.executeDependingOnType(query, query, queryMethod, pageable, Sample.class); - mock.executeDependingOnType(query, query, queryMethod, pageable, false, false, false); - verify(mock, never()).executeCollection(any(N1qlQuery.class)); - verify(mock, never()).executeEntity(any(N1qlQuery.class)); - verify(mock, never()).executeStream(any(N1qlQuery.class)); - verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); - verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class)); + verify(mock, never()).executeCollection(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeEntity(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executeStream(any(N1qlQuery.class), any(Class.class)); + verify(mock, never()).executePaged(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); + verify(mock, never()).executeSliced(any(N1qlQuery.class), any(N1qlQuery.class), any(Pageable.class), any(Class.class)); verify(mock).executeSingleProjection(any(N1qlQuery.class)); } + + static class Sample { + Integer id; + } + + interface SampleRepository extends Repository { + + Collection findAll(); + + Sample findById(Integer id); + + Stream streamAll(); + + Page findAllPaged(Pageable pageable); + + Slice findAllSliced(Pageable pageable); + + void modifyingMethod(); + + CountDownLatch countDown(); + + long longMethod(); + } }