15
15
*/
16
16
package org .springframework .data .mongodb .repository .support ;
17
17
18
- import static org .mockito .ArgumentMatchers .*;
18
+ import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .ArgumentMatchers .any ;
19
20
import static org .mockito .Mockito .*;
21
+ import static org .mockito .Mockito .anyString ;
20
22
23
+ import org .bson .Document ;
21
24
import org .junit .Before ;
22
25
import org .junit .Test ;
23
26
import org .junit .runner .RunWith ;
24
27
import org .mockito .Answers ;
28
+ import org .mockito .ArgumentCaptor ;
25
29
import org .mockito .Mock ;
26
30
import org .mockito .junit .MockitoJUnitRunner ;
31
+ import org .springframework .data .domain .Sort ;
32
+ import org .springframework .data .mongodb .core .index .IndexDefinition ;
33
+ import org .springframework .data .mongodb .core .index .IndexOperations ;
27
34
import org .springframework .data .mongodb .core .index .IndexOperationsProvider ;
35
+ import org .springframework .data .mongodb .repository .query .MongoEntityMetadata ;
36
+ import org .springframework .data .mongodb .repository .query .MongoQueryMethod ;
28
37
import org .springframework .data .mongodb .repository .query .PartTreeMongoQuery ;
29
38
import org .springframework .data .repository .query .parser .PartTree ;
39
+ import org .springframework .data .util .Streamable ;
30
40
31
41
/**
32
42
* Unit tests for {@link IndexEnsuringQueryCreationListener}.
@@ -39,26 +49,109 @@ public class IndexEnsuringQueryCreationListenerUnitTests {
39
49
IndexEnsuringQueryCreationListener listener ;
40
50
41
51
@ Mock IndexOperationsProvider provider ;
52
+ @ Mock PartTree partTree ;
53
+ @ Mock PartTreeMongoQuery partTreeQuery ;
54
+ @ Mock MongoQueryMethod queryMethod ;
55
+ @ Mock IndexOperations indexOperations ;
56
+ @ Mock MongoEntityMetadata entityInformation ;
42
57
43
58
@ Before
44
59
public void setUp () {
60
+
45
61
this .listener = new IndexEnsuringQueryCreationListener (provider );
62
+
63
+ partTreeQuery = mock (PartTreeMongoQuery .class , Answers .RETURNS_MOCKS );
64
+ when (partTreeQuery .getTree ()).thenReturn (partTree );
65
+ when (provider .indexOps (anyString ())).thenReturn (indexOperations );
66
+ when (queryMethod .getEntityInformation ()).thenReturn (entityInformation );
67
+ when (entityInformation .getCollectionName ()).thenReturn ("persons" );
46
68
}
47
69
48
70
@ Test // DATAMONGO-1753
49
71
public void skipsQueryCreationForMethodWithoutPredicate () {
50
72
51
- PartTree tree = mock (PartTree .class );
52
- when (tree .hasPredicate ()).thenReturn (false );
73
+ when (partTree .hasPredicate ()).thenReturn (false );
53
74
54
- PartTreeMongoQuery query = mock (PartTreeMongoQuery .class , Answers .RETURNS_MOCKS );
55
- when (query .getTree ()).thenReturn (tree );
56
-
57
- listener .onCreation (query );
75
+ listener .onCreation (partTreeQuery );
58
76
59
77
verify (provider , times (0 )).indexOps (any ());
60
78
}
61
79
80
+ @ Test // DATAMONGO-1854
81
+ public void usesCollationWhenPresentAndFixedValue () {
82
+
83
+ when (partTree .hasPredicate ()).thenReturn (true );
84
+ when (partTree .getParts ()).thenReturn (Streamable .empty ());
85
+ when (partTree .getSort ()).thenReturn (Sort .unsorted ());
86
+ when (partTreeQuery .getQueryMethod ()).thenReturn (queryMethod );
87
+ when (queryMethod .hasAnnotatedCollation ()).thenReturn (true );
88
+ when (queryMethod .getAnnotatedCollation ()).thenReturn ("en_US" );
89
+
90
+ listener .onCreation (partTreeQuery );
91
+
92
+ ArgumentCaptor <IndexDefinition > indexArgumentCaptor = ArgumentCaptor .forClass (IndexDefinition .class );
93
+ verify (indexOperations ).ensureIndex (indexArgumentCaptor .capture ());
94
+
95
+ IndexDefinition indexDefinition = indexArgumentCaptor .getValue ();
96
+ assertThat (indexDefinition .getIndexOptions ()).isEqualTo (new Document ("collation" , new Document ("locale" , "en_US" )));
97
+ }
98
+
99
+ @ Test // DATAMONGO-1854
100
+ public void usesCollationWhenPresentAndFixedDocumentValue () {
101
+
102
+ when (partTree .hasPredicate ()).thenReturn (true );
103
+ when (partTree .getParts ()).thenReturn (Streamable .empty ());
104
+ when (partTree .getSort ()).thenReturn (Sort .unsorted ());
105
+ when (partTreeQuery .getQueryMethod ()).thenReturn (queryMethod );
106
+ when (queryMethod .hasAnnotatedCollation ()).thenReturn (true );
107
+ when (queryMethod .getAnnotatedCollation ()).thenReturn ("{ 'locale' : 'en_US' }" );
108
+
109
+ listener .onCreation (partTreeQuery );
110
+
111
+ ArgumentCaptor <IndexDefinition > indexArgumentCaptor = ArgumentCaptor .forClass (IndexDefinition .class );
112
+ verify (indexOperations ).ensureIndex (indexArgumentCaptor .capture ());
113
+
114
+ IndexDefinition indexDefinition = indexArgumentCaptor .getValue ();
115
+ assertThat (indexDefinition .getIndexOptions ()).isEqualTo (new Document ("collation" , new Document ("locale" , "en_US" )));
116
+ }
117
+
118
+ @ Test // DATAMONGO-1854
119
+ public void skipsCollationWhenPresentButDynamic () {
120
+
121
+ when (partTree .hasPredicate ()).thenReturn (true );
122
+ when (partTree .getParts ()).thenReturn (Streamable .empty ());
123
+ when (partTree .getSort ()).thenReturn (Sort .unsorted ());
124
+ when (partTreeQuery .getQueryMethod ()).thenReturn (queryMethod );
125
+ when (queryMethod .hasAnnotatedCollation ()).thenReturn (true );
126
+ when (queryMethod .getAnnotatedCollation ()).thenReturn ("{ 'locale' : '?0' }" );
127
+
128
+ listener .onCreation (partTreeQuery );
129
+
130
+ ArgumentCaptor <IndexDefinition > indexArgumentCaptor = ArgumentCaptor .forClass (IndexDefinition .class );
131
+ verify (indexOperations ).ensureIndex (indexArgumentCaptor .capture ());
132
+
133
+ IndexDefinition indexDefinition = indexArgumentCaptor .getValue ();
134
+ assertThat (indexDefinition .getIndexOptions ()).isEmpty ();
135
+ }
136
+
137
+ @ Test // DATAMONGO-1854
138
+ public void skipsCollationWhenNotPresent () {
139
+
140
+ when (partTree .hasPredicate ()).thenReturn (true );
141
+ when (partTree .getParts ()).thenReturn (Streamable .empty ());
142
+ when (partTree .getSort ()).thenReturn (Sort .unsorted ());
143
+ when (partTreeQuery .getQueryMethod ()).thenReturn (queryMethod );
144
+ when (queryMethod .hasAnnotatedCollation ()).thenReturn (false );
145
+
146
+ listener .onCreation (partTreeQuery );
147
+
148
+ ArgumentCaptor <IndexDefinition > indexArgumentCaptor = ArgumentCaptor .forClass (IndexDefinition .class );
149
+ verify (indexOperations ).ensureIndex (indexArgumentCaptor .capture ());
150
+
151
+ IndexDefinition indexDefinition = indexArgumentCaptor .getValue ();
152
+ assertThat (indexDefinition .getIndexOptions ()).isEmpty ();
153
+ }
154
+
62
155
interface SampleRepository {
63
156
64
157
Object findAllBy ();
0 commit comments