32
32
import org .bson .conversions .Bson ;
33
33
import org .junit .Test ;
34
34
import org .mockito .Mockito ;
35
+ import org .reactivestreams .Publisher ;
35
36
36
37
import org .springframework .beans .factory .BeanFactory ;
38
+ import org .springframework .context .ConfigurableApplicationContext ;
39
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
40
+ import org .springframework .context .annotation .Bean ;
41
+ import org .springframework .context .annotation .Configuration ;
37
42
import org .springframework .data .mongodb .ReactiveMongoDatabaseFactory ;
38
43
import org .springframework .data .mongodb .core .ReactiveMongoTemplate ;
39
44
import org .springframework .data .mongodb .core .convert .MappingMongoConverter ;
40
45
import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
41
46
import org .springframework .expression .Expression ;
42
47
import org .springframework .expression .common .LiteralExpression ;
48
+ import org .springframework .integration .channel .FluxMessageChannel ;
49
+ import org .springframework .integration .config .EnableIntegration ;
50
+ import org .springframework .integration .core .MessageSource ;
51
+ import org .springframework .integration .dsl .IntegrationFlow ;
52
+ import org .springframework .integration .dsl .IntegrationFlows ;
53
+ import org .springframework .integration .dsl .Pollers ;
43
54
import org .springframework .integration .mongodb .rules .MongoDbAvailable ;
44
55
import org .springframework .integration .mongodb .rules .MongoDbAvailableTests ;
45
56
50
61
51
62
/**
52
63
* @author David Turanski
64
+ * @author Artem Bilan
53
65
*
54
66
* @since 5.3
55
67
*/
@@ -83,7 +95,7 @@ public void validateSuccessfulQueryWithSingleElementFluxOfDbObject() {
83
95
ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = this .prepareReactiveMongoFactory ();
84
96
85
97
ReactiveMongoTemplate template = new ReactiveMongoTemplate (reactiveMongoDatabaseFactory );
86
- waitFor (template .save (this . createPerson (), "data" ));
98
+ waitFor (template .save (createPerson (), "data" ));
87
99
88
100
Expression queryExpression = new LiteralExpression ("{'name' : 'Oleg'}" );
89
101
ReactiveMongoDbMessageSource messageSource = new ReactiveMongoDbMessageSource (reactiveMongoDatabaseFactory ,
@@ -103,7 +115,7 @@ public void validateSuccessfulQueryWithSingleElementFluxOfPerson() {
103
115
ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = this .prepareReactiveMongoFactory ();
104
116
105
117
ReactiveMongoTemplate template = new ReactiveMongoTemplate (reactiveMongoDatabaseFactory );
106
- waitFor (template .save (this . createPerson (), "data" ));
118
+ waitFor (template .save (createPerson (), "data" ));
107
119
108
120
Expression queryExpression = new LiteralExpression ("{'name' : 'Oleg'}" );
109
121
ReactiveMongoDbMessageSource messageSource = new ReactiveMongoDbMessageSource (reactiveMongoDatabaseFactory ,
@@ -148,7 +160,7 @@ public void validateSuccessfulQueryWithEmptyReturn() {
148
160
@ MongoDbAvailable
149
161
@ SuppressWarnings ("unchecked" )
150
162
public void validateSuccessfulQueryWithCustomConverter () {
151
- MappingMongoConverter converter = new ReactiveTestMongoConverter (this . prepareReactiveMongoFactory (),
163
+ MappingMongoConverter converter = new ReactiveTestMongoConverter (prepareReactiveMongoFactory (),
152
164
new MongoMappingContext ());
153
165
converter .afterPropertiesSet ();
154
166
converter = spy (converter );
@@ -161,11 +173,28 @@ public void validateSuccessfulQueryWithCustomConverter() {
161
173
162
174
@ Test
163
175
@ MongoDbAvailable
164
- @ SuppressWarnings ("unchecked" )
165
- public void validatePipelineInModifyOut () {
176
+ public void validateWithConfiguredPollerFlow () {
177
+ ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = prepareReactiveMongoFactory ();
178
+ ReactiveMongoTemplate template = new ReactiveMongoTemplate (reactiveMongoDatabaseFactory );
166
179
167
- ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = this .prepareReactiveMongoFactory ();
180
+ waitFor (template .save (createPerson (), "data" ));
181
+
182
+ ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (TestContext .class );
183
+ FluxMessageChannel output = context .getBean (FluxMessageChannel .class );
184
+ StepVerifier .create (output )
185
+ .assertNext (
186
+ message -> assertThat (((Person ) message .getPayload ()).getName ()).isEqualTo ("Oleg" ))
187
+ .thenCancel ()
188
+ .verify ();
189
+
190
+ context .close ();
191
+ }
168
192
193
+ @ Test
194
+ @ MongoDbAvailable
195
+ @ SuppressWarnings ("unchecked" )
196
+ public void validatePipelineInModifyOut () {
197
+ ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = prepareReactiveMongoFactory ();
169
198
ReactiveMongoTemplate template = new ReactiveMongoTemplate (reactiveMongoDatabaseFactory );
170
199
171
200
waitFor (template .save (BasicDBObject .parse ("{'name' : 'Manny', 'id' : 1}" ), "data" ));
@@ -185,17 +214,17 @@ public void validatePipelineInModifyOut() {
185
214
}
186
215
187
216
private Flux <Person > queryMultipleElements (Expression queryExpression ) {
188
- return this . queryMultipleElements (queryExpression , Optional .empty ());
217
+ return queryMultipleElements (queryExpression , Optional .empty ());
189
218
}
190
219
191
220
@ SuppressWarnings ("unchecked" )
192
221
private Flux <Person > queryMultipleElements (Expression queryExpression , Optional <MappingMongoConverter > converter ) {
193
222
ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory = this .prepareReactiveMongoFactory ();
194
223
195
224
ReactiveMongoTemplate template = new ReactiveMongoTemplate (reactiveMongoDatabaseFactory );
196
- waitFor (template .save (this . createPerson ("Manny" ), "data" ));
197
- waitFor (template .save (this . createPerson ("Moe" ), "data" ));
198
- waitFor (template .save (this . createPerson ("Jack" ), "data" ));
225
+ waitFor (template .save (createPerson ("Manny" ), "data" ));
226
+ waitFor (template .save (createPerson ("Moe" ), "data" ));
227
+ waitFor (template .save (createPerson ("Jack" ), "data" ));
199
228
200
229
ReactiveMongoDbMessageSource messageSource = new ReactiveMongoDbMessageSource (reactiveMongoDatabaseFactory ,
201
230
queryExpression );
@@ -211,4 +240,38 @@ private static <T> T waitFor(Mono<T> mono) {
211
240
return mono .block (Duration .ofSeconds (10 ));
212
241
}
213
242
243
+ @ Configuration
244
+ @ EnableIntegration
245
+ static class TestContext {
246
+
247
+ @ Bean
248
+ FluxMessageChannel output () {
249
+ return new FluxMessageChannel ();
250
+ }
251
+
252
+ @ Bean
253
+ public MessageSource <Publisher <?>> mongodbMessageSource (ReactiveMongoDatabaseFactory mongoDatabaseFactory ) {
254
+ Expression queryExpression = new LiteralExpression ("{'name' : 'Oleg'}" );
255
+ ReactiveMongoDbMessageSource reactiveMongoDbMessageSource =
256
+ new ReactiveMongoDbMessageSource (mongoDatabaseFactory , queryExpression );
257
+ reactiveMongoDbMessageSource .setEntityClass (Person .class );
258
+ return reactiveMongoDbMessageSource ;
259
+ }
260
+
261
+ @ Bean
262
+ ReactiveMongoDatabaseFactory mongoDatabaseFactory () {
263
+ return MongoDbAvailableTests .REACTIVE_MONGO_DATABASE_FACTORY ;
264
+ }
265
+
266
+ @ Bean
267
+ public IntegrationFlow pollingFlow (MessageSource <Publisher <?>> mongodbMessageSource ) {
268
+ return IntegrationFlows
269
+ .from (mongodbMessageSource , c -> c .poller (Pollers .fixedDelay (100 ).maxMessagesPerPoll (1 )))
270
+ .split ()
271
+ .channel (output ())
272
+ .get ();
273
+ }
274
+
275
+ }
276
+
214
277
}
0 commit comments