14
14
* limitations under the License.
15
15
*/
16
16
17
- package com .mongodb .hibernate .query . select ;
17
+ package com .mongodb .hibernate .query ;
18
18
19
19
import static com .mongodb .hibernate .MongoTestAssertions .assertIterableEq ;
20
20
import static org .assertj .core .api .Assertions .assertThat ;
21
21
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
22
22
23
+ import com .mongodb .client .MongoCollection ;
23
24
import com .mongodb .hibernate .TestCommandListener ;
24
25
import com .mongodb .hibernate .junit .MongoExtension ;
25
26
import java .util .List ;
26
27
import java .util .function .Consumer ;
27
28
import org .assertj .core .api .InstanceOfAssertFactories ;
28
29
import org .bson .BsonDocument ;
30
+ import org .hibernate .query .MutationQuery ;
29
31
import org .hibernate .query .SelectionQuery ;
30
32
import org .hibernate .testing .orm .junit .ServiceRegistryScope ;
31
33
import org .hibernate .testing .orm .junit .ServiceRegistryScopeAware ;
36
38
37
39
@ SessionFactory (exportSchema = false )
38
40
@ ExtendWith (MongoExtension .class )
39
- abstract class AbstractSelectionQueryIntegrationTests implements SessionFactoryScopeAware , ServiceRegistryScopeAware {
41
+ public abstract class AbstractQueryIntegrationTests implements SessionFactoryScopeAware , ServiceRegistryScopeAware {
40
42
41
43
private SessionFactoryScope sessionFactoryScope ;
42
44
43
45
private TestCommandListener testCommandListener ;
44
46
45
- SessionFactoryScope getSessionFactoryScope () {
47
+ protected SessionFactoryScope getSessionFactoryScope () {
46
48
return sessionFactoryScope ;
47
49
}
48
50
49
- TestCommandListener getTestCommandListener () {
51
+ protected TestCommandListener getTestCommandListener () {
50
52
return testCommandListener ;
51
53
}
52
54
@@ -60,7 +62,7 @@ public void injectServiceRegistryScope(ServiceRegistryScope serviceRegistryScope
60
62
this .testCommandListener = serviceRegistryScope .getRegistry ().requireService (TestCommandListener .class );
61
63
}
62
64
63
- <T > void assertSelectionQuery (
65
+ protected <T > void assertSelectionQuery (
64
66
String hql ,
65
67
Class <T > resultType ,
66
68
Consumer <SelectionQuery <T >> queryPostProcessor ,
@@ -74,11 +76,12 @@ <T> void assertSelectionQuery(
74
76
resultList -> assertIterableEq (expectedResultList , resultList ));
75
77
}
76
78
77
- <T > void assertSelectionQuery (String hql , Class <T > resultType , String expectedMql , List <T > expectedResultList ) {
79
+ protected <T > void assertSelectionQuery (
80
+ String hql , Class <T > resultType , String expectedMql , List <T > expectedResultList ) {
78
81
assertSelectionQuery (hql , resultType , null , expectedMql , expectedResultList );
79
82
}
80
83
81
- <T > void assertSelectionQuery (
84
+ protected <T > void assertSelectionQuery (
82
85
String hql ,
83
86
Class <T > resultType ,
84
87
Consumer <SelectionQuery <T >> queryPostProcessor ,
@@ -97,12 +100,12 @@ <T> void assertSelectionQuery(
97
100
});
98
101
}
99
102
100
- <T > void assertSelectionQuery (
103
+ protected <T > void assertSelectionQuery (
101
104
String hql , Class <T > resultType , String expectedMql , Consumer <List <T >> resultListVerifier ) {
102
105
assertSelectionQuery (hql , resultType , null , expectedMql , resultListVerifier );
103
106
}
104
107
105
- <T > void assertSelectQueryFailure (
108
+ protected <T > void assertSelectQueryFailure (
106
109
String hql ,
107
110
Class <T > resultType ,
108
111
Consumer <SelectionQuery <T >> queryPostProcessor ,
@@ -120,7 +123,7 @@ <T> void assertSelectQueryFailure(
120
123
.hasMessage (expectedExceptionMessage , expectedExceptionMessageParameters ));
121
124
}
122
125
123
- <T > void assertSelectQueryFailure (
126
+ protected <T > void assertSelectQueryFailure (
124
127
String hql ,
125
128
Class <T > resultType ,
126
129
Class <? extends Exception > expectedExceptionType ,
@@ -135,12 +138,39 @@ <T> void assertSelectQueryFailure(
135
138
expectedExceptionMessageParameters );
136
139
}
137
140
138
- void assertActualCommand (BsonDocument expectedCommand ) {
141
+ protected void assertActualCommand (BsonDocument expectedCommand ) {
139
142
var capturedCommands = testCommandListener .getStartedCommands ();
140
143
141
144
assertThat (capturedCommands )
142
145
.singleElement ()
143
146
.asInstanceOf (InstanceOfAssertFactories .MAP )
144
147
.containsAllEntriesOf (expectedCommand );
145
148
}
149
+
150
+ protected void assertMutateQuery (
151
+ String hql ,
152
+ Consumer <MutationQuery > queryPostProcessor ,
153
+ int expectedMutatedCount ,
154
+ String expectedMql ,
155
+ MongoCollection <BsonDocument > collection ,
156
+ Iterable <BsonDocument > expectedDocuments ) {
157
+ sessionFactoryScope .inTransaction (session -> {
158
+ var mutationQuery = session .createMutationQuery (hql );
159
+ if (queryPostProcessor != null ) {
160
+ queryPostProcessor .accept (mutationQuery );
161
+ }
162
+ assertThat (mutationQuery .executeUpdate ()).isEqualTo (expectedMutatedCount );
163
+ assertActualCommand (BsonDocument .parse (expectedMql ));
164
+ });
165
+ assertThat (collection .find ()).containsExactlyElementsOf (expectedDocuments );
166
+ }
167
+
168
+ protected void assertMutateQuery (
169
+ String hql ,
170
+ int expectedMutatedCount ,
171
+ String expectedMql ,
172
+ MongoCollection <BsonDocument > collection ,
173
+ Iterable <BsonDocument > expectedDocuments ) {
174
+ assertMutateQuery (hql , null , expectedMutatedCount , expectedMql , collection , expectedDocuments );
175
+ }
146
176
}
0 commit comments