|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.repository.support; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.Answers; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.junit.MockitoJUnitRunner; |
| 27 | +import org.springframework.data.mongodb.core.index.IndexOperationsProvider; |
| 28 | +import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery; |
| 29 | +import org.springframework.data.repository.query.parser.PartTree; |
| 30 | + |
| 31 | +/** |
| 32 | + * Unit tests for {@link IndexEnsuringQueryCreationListener}. |
| 33 | + * |
| 34 | + * @author Oliver Gierke |
| 35 | + */ |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class IndexEnsuringQueryCreationListenerUnitTests { |
| 38 | + |
| 39 | + IndexEnsuringQueryCreationListener listener; |
| 40 | + |
| 41 | + @Mock IndexOperationsProvider provider; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() { |
| 45 | + this.listener = new IndexEnsuringQueryCreationListener(provider); |
| 46 | + } |
| 47 | + |
| 48 | + @Test // DATAMONGO-1753 |
| 49 | + public void skipsQueryCreationForMethodWithoutPredicate() { |
| 50 | + |
| 51 | + PartTree tree = mock(PartTree.class); |
| 52 | + when(tree.hasPredicate()).thenReturn(false); |
| 53 | + |
| 54 | + PartTreeMongoQuery query = mock(PartTreeMongoQuery.class, Answers.RETURNS_MOCKS); |
| 55 | + when(query.getTree()).thenReturn(tree); |
| 56 | + |
| 57 | + listener.onCreation(query); |
| 58 | + |
| 59 | + verify(provider, times(0)).indexOps(any()); |
| 60 | + } |
| 61 | + |
| 62 | + interface SampleRepository { |
| 63 | + |
| 64 | + Object findAllBy(); |
| 65 | + } |
| 66 | +} |
0 commit comments