15
15
*/
16
16
package org .springframework .data .mongodb .repository ;
17
17
18
- import static org .assertj .core .api .Assertions .assertThat ;
18
+ import static org .assertj .core .api .Assertions .* ;
19
19
20
20
import org .bson .Document ;
21
21
import org .bson .types .ObjectId ;
22
22
import org .junit .jupiter .api .BeforeEach ;
23
23
import org .junit .jupiter .api .Test ;
24
24
import org .junit .jupiter .api .extension .ExtendWith ;
25
+
25
26
import org .springframework .beans .factory .annotation .Autowired ;
26
27
import org .springframework .context .annotation .ComponentScan .Filter ;
27
28
import org .springframework .context .annotation .Configuration ;
40
41
import com .mongodb .client .MongoClient ;
41
42
42
43
/**
44
+ * Integration tests for Repositories using optimistic locking.
45
+ *
43
46
* @author Christoph Strobl
44
- * @since 2025/03
45
47
*/
46
48
@ ExtendWith ({ MongoClientExtension .class , SpringExtension .class })
47
49
@ ContextConfiguration
48
- public class VersionedPersonRepositoryIntegrationTests {
50
+ class VersionedPersonRepositoryIntegrationTests {
49
51
50
52
static @ Client MongoClient mongoClient ;
51
53
@@ -70,14 +72,15 @@ public MongoClient mongoClient() {
70
72
71
73
@ BeforeEach
72
74
void beforeEach () {
73
- MongoTestUtils .flushCollection ("versioned-person-tests" , template . getCollectionName ( VersionedPersonWithCounter . class ),
74
- mongoClient );
75
+ MongoTestUtils .flushCollection ("versioned-person-tests" ,
76
+ template . getCollectionName ( VersionedPersonWithCounter . class ), mongoClient );
75
77
}
76
78
77
79
@ Test // GH-4918
78
80
void updatesVersionedTypeCorrectly () {
79
81
80
- VersionedPerson person = template .insert (VersionedPersonWithCounter .class ).one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
82
+ VersionedPerson person = template .insert (VersionedPersonWithCounter .class )
83
+ .one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
81
84
82
85
int updateCount = versionedPersonRepository .findAndSetFirstnameToLastnameByLastname (person .getLastname ());
83
86
@@ -93,7 +96,8 @@ void updatesVersionedTypeCorrectly() {
93
96
@ Test // GH-4918
94
97
void updatesVersionedTypeCorrectlyWhenUpdateIsUsingInc () {
95
98
96
- VersionedPerson person = template .insert (VersionedPersonWithCounter .class ).one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
99
+ VersionedPerson person = template .insert (VersionedPersonWithCounter .class )
100
+ .one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
97
101
98
102
int updateCount = versionedPersonRepository .findAndIncCounterByLastname (person .getLastname ());
99
103
@@ -103,13 +107,15 @@ void updatesVersionedTypeCorrectlyWhenUpdateIsUsingInc() {
103
107
return collection .find (new Document ("_id" , new ObjectId (person .getId ()))).first ();
104
108
});
105
109
106
- assertThat (document ).containsEntry ("lastname" , "Duckling" ).containsEntry ("version" , 1L ).containsEntry ("counter" , 42 );
110
+ assertThat (document ).containsEntry ("lastname" , "Duckling" ).containsEntry ("version" , 1L ).containsEntry ("counter" ,
111
+ 42 );
107
112
}
108
113
109
114
@ Test // GH-4918
110
115
void updatesVersionedTypeCorrectlyWhenUpdateCoversVersionBump () {
111
116
112
- VersionedPerson person = template .insert (VersionedPersonWithCounter .class ).one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
117
+ VersionedPerson person = template .insert (VersionedPersonWithCounter .class )
118
+ .one (new VersionedPersonWithCounter ("Donald" , "Duckling" ));
113
119
114
120
int updateCount = versionedPersonRepository .findAndSetFirstnameToLastnameIncVersionByLastname (person .getLastname (),
115
121
10 );
@@ -123,7 +129,7 @@ void updatesVersionedTypeCorrectlyWhenUpdateCoversVersionBump() {
123
129
assertThat (document ).containsEntry ("firstname" , "Duckling" ).containsEntry ("version" , 10L );
124
130
}
125
131
126
- public interface VersionedPersonRepository extends CrudRepository <VersionedPersonWithCounter , String > {
132
+ interface VersionedPersonRepository extends CrudRepository <VersionedPersonWithCounter , String > {
127
133
128
134
@ Update ("{ '$set': { 'firstname' : ?0 } }" )
129
135
int findAndSetFirstnameToLastnameByLastname (String lastname );
@@ -156,5 +162,7 @@ public int getCounter() {
156
162
public void setCounter (int counter ) {
157
163
this .counter = counter ;
158
164
}
165
+
159
166
}
167
+
160
168
}
0 commit comments