Skip to content

Commit 955597b

Browse files
committed
DATAMONGO-1559 - Migrate reactive tests from TestSubscriber to StepVerifier.
1 parent f59bbd3 commit 955597b

File tree

7 files changed

+673
-1928
lines changed

7 files changed

+673
-1928
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateExecuteTests.java

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mongodb.core;
1817

1918
import static com.sun.prism.impl.Disposer.*;
2019
import static org.hamcrest.Matchers.*;
2120
import static org.junit.Assert.*;
2221
import static org.junit.Assume.*;
2322

24-
import java.util.List;
23+
import reactor.core.publisher.Flux;
24+
import reactor.test.StepVerifier;
2525

2626
import org.bson.Document;
2727
import org.junit.After;
@@ -39,11 +39,9 @@
3939

4040
import com.mongodb.MongoException;
4141
import com.mongodb.ReadPreference;
42+
import com.mongodb.reactivestreams.client.MongoCollection;
4243
import com.mongodb.reactivestreams.client.MongoDatabase;
4344

44-
import reactor.core.publisher.Flux;
45-
import reactor.test.TestSubscriber;
46-
4745
/**
4846
* Integration test for {@link ReactiveMongoTemplate} execute methods.
4947
*
@@ -55,111 +53,116 @@ public class ReactiveMongoTemplateExecuteTests {
5553

5654
private static final Version THREE = Version.parse("3.0");
5755

56+
@Rule public ExpectedException thrown = ExpectedException.none();
57+
5858
@Autowired SimpleReactiveMongoDatabaseFactory factory;
5959
@Autowired ReactiveMongoOperations operations;
6060

61-
@Rule public ExpectedException thrown = ExpectedException.none();
62-
6361
Version mongoVersion;
6462

6563
@Before
6664
public void setUp() {
6765
cleanUp();
6866

6967
if (mongoVersion == null) {
70-
Document result = operations.executeCommand("{ buildInfo: 1 }").block();
71-
mongoVersion = Version.parse(result.get("version").toString());
68+
mongoVersion = operations.executeCommand("{ buildInfo: 1 }") //
69+
.map(it -> it.get("version").toString())//
70+
.map(Version::parse) //
71+
.block();
7272
}
7373
}
7474

7575
@After
7676
public void tearDown() {
7777

78-
operations.dropCollection("person").block();
79-
operations.dropCollection(Person.class).block();
80-
operations.dropCollection("execute_test").block();
81-
operations.dropCollection("execute_test1").block();
82-
operations.dropCollection("execute_test2").block();
83-
operations.dropCollection("execute_index_test").block();
78+
Flux<Void> cleanup = operations.dropCollection("person") //
79+
.mergeWith(operations.dropCollection(Person.class)) //
80+
.mergeWith(operations.dropCollection("execute_test")) //
81+
.mergeWith(operations.dropCollection("execute_test1")) //
82+
.mergeWith(operations.dropCollection("execute_test2")) //
83+
.mergeWith(operations.dropCollection("execute_index_test"));
84+
85+
StepVerifier.create(cleanup).verifyComplete();
8486
}
8587

8688
@Test // DATAMONGO-1444
87-
public void executeCommandJsonCommandShouldReturnSingleResponse() throws Exception {
89+
public void executeCommandJsonCommandShouldReturnSingleResponse() {
8890

89-
Document document = operations.executeCommand("{ buildInfo: 1 }").block();
91+
StepVerifier.create(operations.executeCommand("{ buildInfo: 1 }")).consumeNextWith(actual -> {
9092

91-
assertThat(document, hasKey("version"));
93+
assertThat(actual, hasKey("version"));
94+
}).verifyComplete();
9295
}
9396

9497
@Test // DATAMONGO-1444
95-
public void executeCommandDocumentCommandShouldReturnSingleResponse() throws Exception {
98+
public void executeCommandDocumentCommandShouldReturnSingleResponse() {
9699

97-
Document document = operations.executeCommand(new Document("buildInfo", 1)).block();
100+
StepVerifier.create(operations.executeCommand(new Document("buildInfo", 1))).consumeNextWith(actual -> {
98101

99-
assertThat(document, hasKey("version"));
102+
assertThat(actual, hasKey("version"));
103+
}).verifyComplete();
100104
}
101105

102106
@Test // DATAMONGO-1444
103-
public void executeCommandJsonCommandShouldReturnMultipleResponses() throws Exception {
107+
public void executeCommandJsonCommandShouldReturnMultipleResponses() {
104108

105109
assumeTrue(mongoVersion.isGreaterThan(THREE));
106110

107-
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
111+
StepVerifier.create(operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}"))
112+
.expectNextCount(1).verifyComplete();
108113

109-
TestSubscriber<Document> subscriber = TestSubscriber.create();
110-
operations.executeCommand("{ find: 'execute_test'}").subscribe(subscriber);
114+
StepVerifier.create(operations.executeCommand("{ find: 'execute_test'}")) //
115+
.consumeNextWith(actual -> {
111116

112-
subscriber.awaitAndAssertNextValueCount(1);
113-
subscriber.assertValuesWith(document -> {
114-
115-
assertThat(document.get("ok", Double.class), is(closeTo(1D, 0D)));
116-
assertThat(document, hasKey("cursor"));
117-
});
117+
assertThat(actual.get("ok", Double.class), is(closeTo(1D, 0D)));
118+
assertThat(actual, hasKey("cursor"));
119+
}) //
120+
.verifyComplete();
118121
}
119122

120123
@Test // DATAMONGO-1444
121-
public void executeCommandJsonCommandShouldTranslateExceptions() throws Exception {
124+
public void executeCommandJsonCommandShouldTranslateExceptions() {
122125

123-
TestSubscriber<Document> testSubscriber = TestSubscriber.subscribe(operations.executeCommand("{ unknown: 1 }"));
124-
125-
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
126+
StepVerifier.create(operations.executeCommand("{ unknown: 1 }")) //
127+
.expectError(InvalidDataAccessApiUsageException.class) //
128+
.verify();
126129
}
127130

128131
@Test // DATAMONGO-1444
129-
public void executeCommandDocumentCommandShouldTranslateExceptions() throws Exception {
132+
public void executeCommandDocumentCommandShouldTranslateExceptions() {
130133

131-
TestSubscriber<Document> testSubscriber = TestSubscriber
132-
.subscribe(operations.executeCommand(new Document("unknown", 1)));
134+
StepVerifier.create(operations.executeCommand(new Document("unknown", 1))) //
135+
.expectError(InvalidDataAccessApiUsageException.class) //
136+
.verify();
133137

134-
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
135138
}
136139

137140
@Test // DATAMONGO-1444
138-
public void executeCommandWithReadPreferenceCommandShouldTranslateExceptions() throws Exception {
139-
140-
TestSubscriber<Document> testSubscriber = TestSubscriber
141-
.subscribe(operations.executeCommand(new Document("unknown", 1), ReadPreference.nearest()));
141+
public void executeCommandWithReadPreferenceCommandShouldTranslateExceptions() {
142142

143-
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
143+
StepVerifier.create(operations.executeCommand(new Document("unknown", 1), ReadPreference.nearest())) //
144+
.expectError(InvalidDataAccessApiUsageException.class) //
145+
.verify();
144146
}
145147

146148
@Test // DATAMONGO-1444
147-
public void executeOnDatabaseShouldExecuteCommand() throws Exception {
149+
public void executeOnDatabaseShouldExecuteCommand() {
148150

149-
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
150-
operations.executeCommand("{ insert: 'execute_test1', documents: [{},{},{}]}").block();
151-
operations.executeCommand("{ insert: 'execute_test2', documents: [{},{},{}]}").block();
151+
Flux<Document> documentFlux = operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}")
152+
.mergeWith(operations.executeCommand("{ insert: 'execute_test1', documents: [{},{},{}]}"))
153+
.mergeWith(operations.executeCommand("{ insert: 'execute_test2', documents: [{},{},{}]}"));
152154

153-
Flux<Document> execute = operations.execute(MongoDatabase::listCollections);
155+
StepVerifier.create(documentFlux).expectNextCount(3).verifyComplete();
154156

155-
List<Document> documents = execute.filter(document -> document.getString("name").startsWith("execute_test"))
156-
.collectList().block();
157+
Flux<Document> execute = operations.execute(MongoDatabase::listCollections);
157158

158-
assertThat(documents, hasSize(3));
159+
StepVerifier.create(execute.filter(document -> document.getString("name").startsWith("execute_test"))) //
160+
.expectNextCount(3) //
161+
.verifyComplete();
159162
}
160163

161164
@Test // DATAMONGO-1444
162-
public void executeOnDatabaseShouldDeferExecution() throws Exception {
165+
public void executeOnDatabaseShouldDeferExecution() {
163166

164167
operations.execute(db -> {
165168
throw new MongoException(50, "hi there");
@@ -169,42 +172,34 @@ public void executeOnDatabaseShouldDeferExecution() throws Exception {
169172
}
170173

171174
@Test // DATAMONGO-1444
172-
public void executeOnDatabaseShouldShouldTranslateExceptions() throws Exception {
173-
174-
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
175+
public void executeOnDatabaseShouldShouldTranslateExceptions() {
175176

176177
Flux<Document> execute = operations.execute(db -> {
177178
throw new MongoException(50, "hi there");
178179
});
179180

180-
execute.subscribe(testSubscriber);
181-
182-
testSubscriber.await().assertError(UncategorizedMongoDbException.class);
181+
StepVerifier.create(execute).expectError(UncategorizedMongoDbException.class).verify();
183182
}
184183

185184
@Test // DATAMONGO-1444
186-
public void executeOnCollectionWithTypeShouldReturnFindResults() throws Exception {
187-
188-
operations.executeCommand("{ insert: 'person', documents: [{},{},{}]}").block();
189-
190-
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
185+
public void executeOnCollectionWithTypeShouldReturnFindResults() {
191186

192-
Flux<Document> execute = operations.execute(Person.class, collection -> collection.find());
193-
execute.subscribe(testSubscriber);
187+
StepVerifier.create(operations.executeCommand("{ insert: 'person', documents: [{},{},{}]}")) //
188+
.expectNextCount(1) //
189+
.verifyComplete();
194190

195-
testSubscriber.awaitAndAssertNextValueCount(3).assertComplete();
191+
StepVerifier.create(operations.execute(Person.class, MongoCollection::find)).expectNextCount(3).verifyComplete();
196192
}
197193

198194
@Test // DATAMONGO-1444
199-
public void executeOnCollectionWithNameShouldReturnFindResults() throws Exception {
200-
201-
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
202-
203-
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
195+
public void executeOnCollectionWithNameShouldReturnFindResults() {
204196

205-
Flux<Document> execute = operations.execute("execute_test", collection -> collection.find());
206-
execute.subscribe(testSubscriber);
197+
StepVerifier.create(operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}")) //
198+
.expectNextCount(1) //
199+
.verifyComplete();
207200

208-
testSubscriber.awaitAndAssertNextValueCount(3).assertComplete();
201+
StepVerifier.create(operations.execute("execute_test", MongoCollection::find)) //
202+
.expectNextCount(3) //
203+
.verifyComplete();
209204
}
210205
}

0 commit comments

Comments
 (0)