DATAMONGO-1559 - Migrate reactive tests from TestSubscriber to StepVerifier.

This commit is contained in:
Mark Paluch
2017-03-24 17:42:31 +01:00
parent f59bbd351d
commit 955597bb54
7 changed files with 673 additions and 1928 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
import static com.sun.prism.impl.Disposer.*;
@@ -21,7 +20,8 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.List;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.bson.Document;
import org.junit.After;
@@ -39,11 +39,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.MongoException;
import com.mongodb.ReadPreference;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import reactor.core.publisher.Flux;
import reactor.test.TestSubscriber;
/**
* Integration test for {@link ReactiveMongoTemplate} execute methods.
*
@@ -55,11 +53,11 @@ public class ReactiveMongoTemplateExecuteTests {
private static final Version THREE = Version.parse("3.0");
@Rule public ExpectedException thrown = ExpectedException.none();
@Autowired SimpleReactiveMongoDatabaseFactory factory;
@Autowired ReactiveMongoOperations operations;
@Rule public ExpectedException thrown = ExpectedException.none();
Version mongoVersion;
@Before
@@ -67,99 +65,104 @@ public class ReactiveMongoTemplateExecuteTests {
cleanUp();
if (mongoVersion == null) {
Document result = operations.executeCommand("{ buildInfo: 1 }").block();
mongoVersion = Version.parse(result.get("version").toString());
mongoVersion = operations.executeCommand("{ buildInfo: 1 }") //
.map(it -> it.get("version").toString())//
.map(Version::parse) //
.block();
}
}
@After
public void tearDown() {
operations.dropCollection("person").block();
operations.dropCollection(Person.class).block();
operations.dropCollection("execute_test").block();
operations.dropCollection("execute_test1").block();
operations.dropCollection("execute_test2").block();
operations.dropCollection("execute_index_test").block();
Flux<Void> cleanup = operations.dropCollection("person") //
.mergeWith(operations.dropCollection(Person.class)) //
.mergeWith(operations.dropCollection("execute_test")) //
.mergeWith(operations.dropCollection("execute_test1")) //
.mergeWith(operations.dropCollection("execute_test2")) //
.mergeWith(operations.dropCollection("execute_index_test"));
StepVerifier.create(cleanup).verifyComplete();
}
@Test // DATAMONGO-1444
public void executeCommandJsonCommandShouldReturnSingleResponse() throws Exception {
public void executeCommandJsonCommandShouldReturnSingleResponse() {
Document document = operations.executeCommand("{ buildInfo: 1 }").block();
StepVerifier.create(operations.executeCommand("{ buildInfo: 1 }")).consumeNextWith(actual -> {
assertThat(document, hasKey("version"));
assertThat(actual, hasKey("version"));
}).verifyComplete();
}
@Test // DATAMONGO-1444
public void executeCommandDocumentCommandShouldReturnSingleResponse() throws Exception {
public void executeCommandDocumentCommandShouldReturnSingleResponse() {
Document document = operations.executeCommand(new Document("buildInfo", 1)).block();
StepVerifier.create(operations.executeCommand(new Document("buildInfo", 1))).consumeNextWith(actual -> {
assertThat(document, hasKey("version"));
assertThat(actual, hasKey("version"));
}).verifyComplete();
}
@Test // DATAMONGO-1444
public void executeCommandJsonCommandShouldReturnMultipleResponses() throws Exception {
public void executeCommandJsonCommandShouldReturnMultipleResponses() {
assumeTrue(mongoVersion.isGreaterThan(THREE));
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
StepVerifier.create(operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}"))
.expectNextCount(1).verifyComplete();
TestSubscriber<Document> subscriber = TestSubscriber.create();
operations.executeCommand("{ find: 'execute_test'}").subscribe(subscriber);
StepVerifier.create(operations.executeCommand("{ find: 'execute_test'}")) //
.consumeNextWith(actual -> {
subscriber.awaitAndAssertNextValueCount(1);
subscriber.assertValuesWith(document -> {
assertThat(document.get("ok", Double.class), is(closeTo(1D, 0D)));
assertThat(document, hasKey("cursor"));
});
assertThat(actual.get("ok", Double.class), is(closeTo(1D, 0D)));
assertThat(actual, hasKey("cursor"));
}) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void executeCommandJsonCommandShouldTranslateExceptions() throws Exception {
public void executeCommandJsonCommandShouldTranslateExceptions() {
TestSubscriber<Document> testSubscriber = TestSubscriber.subscribe(operations.executeCommand("{ unknown: 1 }"));
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
StepVerifier.create(operations.executeCommand("{ unknown: 1 }")) //
.expectError(InvalidDataAccessApiUsageException.class) //
.verify();
}
@Test // DATAMONGO-1444
public void executeCommandDocumentCommandShouldTranslateExceptions() throws Exception {
public void executeCommandDocumentCommandShouldTranslateExceptions() {
TestSubscriber<Document> testSubscriber = TestSubscriber
.subscribe(operations.executeCommand(new Document("unknown", 1)));
StepVerifier.create(operations.executeCommand(new Document("unknown", 1))) //
.expectError(InvalidDataAccessApiUsageException.class) //
.verify();
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
}
@Test // DATAMONGO-1444
public void executeCommandWithReadPreferenceCommandShouldTranslateExceptions() throws Exception {
public void executeCommandWithReadPreferenceCommandShouldTranslateExceptions() {
TestSubscriber<Document> testSubscriber = TestSubscriber
.subscribe(operations.executeCommand(new Document("unknown", 1), ReadPreference.nearest()));
testSubscriber.await().assertError(InvalidDataAccessApiUsageException.class);
StepVerifier.create(operations.executeCommand(new Document("unknown", 1), ReadPreference.nearest())) //
.expectError(InvalidDataAccessApiUsageException.class) //
.verify();
}
@Test // DATAMONGO-1444
public void executeOnDatabaseShouldExecuteCommand() throws Exception {
public void executeOnDatabaseShouldExecuteCommand() {
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
operations.executeCommand("{ insert: 'execute_test1', documents: [{},{},{}]}").block();
operations.executeCommand("{ insert: 'execute_test2', documents: [{},{},{}]}").block();
Flux<Document> documentFlux = operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}")
.mergeWith(operations.executeCommand("{ insert: 'execute_test1', documents: [{},{},{}]}"))
.mergeWith(operations.executeCommand("{ insert: 'execute_test2', documents: [{},{},{}]}"));
StepVerifier.create(documentFlux).expectNextCount(3).verifyComplete();
Flux<Document> execute = operations.execute(MongoDatabase::listCollections);
List<Document> documents = execute.filter(document -> document.getString("name").startsWith("execute_test"))
.collectList().block();
assertThat(documents, hasSize(3));
StepVerifier.create(execute.filter(document -> document.getString("name").startsWith("execute_test"))) //
.expectNextCount(3) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void executeOnDatabaseShouldDeferExecution() throws Exception {
public void executeOnDatabaseShouldDeferExecution() {
operations.execute(db -> {
throw new MongoException(50, "hi there");
@@ -169,42 +172,34 @@ public class ReactiveMongoTemplateExecuteTests {
}
@Test // DATAMONGO-1444
public void executeOnDatabaseShouldShouldTranslateExceptions() throws Exception {
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
public void executeOnDatabaseShouldShouldTranslateExceptions() {
Flux<Document> execute = operations.execute(db -> {
throw new MongoException(50, "hi there");
});
execute.subscribe(testSubscriber);
testSubscriber.await().assertError(UncategorizedMongoDbException.class);
StepVerifier.create(execute).expectError(UncategorizedMongoDbException.class).verify();
}
@Test // DATAMONGO-1444
public void executeOnCollectionWithTypeShouldReturnFindResults() throws Exception {
public void executeOnCollectionWithTypeShouldReturnFindResults() {
operations.executeCommand("{ insert: 'person', documents: [{},{},{}]}").block();
StepVerifier.create(operations.executeCommand("{ insert: 'person', documents: [{},{},{}]}")) //
.expectNextCount(1) //
.verifyComplete();
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
Flux<Document> execute = operations.execute(Person.class, collection -> collection.find());
execute.subscribe(testSubscriber);
testSubscriber.awaitAndAssertNextValueCount(3).assertComplete();
StepVerifier.create(operations.execute(Person.class, MongoCollection::find)).expectNextCount(3).verifyComplete();
}
@Test // DATAMONGO-1444
public void executeOnCollectionWithNameShouldReturnFindResults() throws Exception {
public void executeOnCollectionWithNameShouldReturnFindResults() {
operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}").block();
StepVerifier.create(operations.executeCommand("{ insert: 'execute_test', documents: [{},{},{}]}")) //
.expectNextCount(1) //
.verifyComplete();
TestSubscriber<Document> testSubscriber = TestSubscriber.create();
Flux<Document> execute = operations.execute("execute_test", collection -> collection.find());
execute.subscribe(testSubscriber);
testSubscriber.awaitAndAssertNextValueCount(3).assertComplete();
StepVerifier.create(operations.execute("execute_test", MongoCollection::find)) //
.expectNextCount(3) //
.verifyComplete();
}
}

View File

@@ -18,6 +18,10 @@ package org.springframework.data.mongodb.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import lombok.Data;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.List;
import org.bson.Document;
@@ -33,18 +37,12 @@ import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexField;
import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.util.Version;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.reactivestreams.client.ListIndexesPublisher;
import com.mongodb.reactivestreams.client.MongoCollection;
import lombok.Data;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
/**
* Integration test for {@link MongoTemplate}.
*
@@ -55,25 +53,19 @@ import reactor.test.TestSubscriber;
@ContextConfiguration("classpath:reactive-infrastructure.xml")
public class ReactiveMongoTemplateIndexTests {
@Rule public ExpectedException thrown = ExpectedException.none();
@Autowired SimpleReactiveMongoDatabaseFactory factory;
@Autowired ReactiveMongoTemplate template;
@Rule public ExpectedException thrown = ExpectedException.none();
Version mongoVersion;
@Before
public void setUp() {
cleanDb();
StepVerifier.create(template.dropCollection(Person.class)).verifyComplete();
}
@After
public void cleanUp() {}
private void cleanDb() {
template.dropCollection(Person.class).block();
}
@Test // DATAMONGO-1444
public void testEnsureIndexShouldCreateIndex() {
@@ -84,23 +76,28 @@ public class ReactiveMongoTemplateIndexTests {
p2.setAge(40);
template.insert(p2);
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique()).block();
StepVerifier
.create(template.indexOps(Person.class) //
.ensureIndex(new Index().on("age", Direction.DESC).unique())) //
.expectNextCount(1) //
.verifyComplete();
MongoCollection<Document> coll = template.getCollection(template.getCollectionName(Person.class));
List<Document> indexInfo = Flux.from(coll.listIndexes()).collectList().block();
StepVerifier.create(Flux.from(coll.listIndexes()).collectList()).consumeNextWith(indexInfo -> {
assertThat(indexInfo.size(), is(2));
Object indexKey = null;
boolean unique = false;
for (Document ix : indexInfo) {
assertThat(indexInfo.size(), is(2));
Object indexKey = null;
boolean unique = false;
for (Document ix : indexInfo) {
if ("age_-1".equals(ix.get("name"))) {
indexKey = ix.get("key");
unique = (Boolean) ix.get("unique");
if ("age_-1".equals(ix.get("name"))) {
indexKey = ix.get("key");
unique = (Boolean) ix.get("unique");
}
}
}
assertThat(((Document) indexKey), hasEntry("age", -1));
assertThat(unique, is(true));
assertThat(((Document) indexKey), hasEntry("age", -1));
assertThat(unique, is(true));
}).verifyComplete();
}
@Test // DATAMONGO-1444
@@ -108,22 +105,27 @@ public class ReactiveMongoTemplateIndexTests {
Person p1 = new Person("Oliver");
p1.setAge(25);
template.insert(p1).block();
StepVerifier.create(template.insert(p1)).expectNextCount(1).verifyComplete();
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique()).block();
StepVerifier
.create(template.indexOps(Person.class) //
.ensureIndex(new Index().on("age", Direction.DESC).unique())) //
.expectNextCount(1) //
.verifyComplete();
List<IndexInfo> indexInfoList = Flux.from(template.indexOps(Person.class).getIndexInfo()).collectList()
.block();
assertThat(indexInfoList.size(), is(2));
StepVerifier.create(template.indexOps(Person.class).getIndexInfo().collectList()).consumeNextWith(indexInfos -> {
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique(), is(true));
assertThat(ii.isSparse(), is(false));
assertThat(indexInfos.size(), is(2));
List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);
IndexInfo ii = indexInfos.get(1);
assertThat(ii.isUnique(), is(true));
assertThat(ii.isSparse(), is(false));
assertThat(field, is(IndexField.create("age", Direction.DESC)));
List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);
assertThat(field, is(IndexField.create("age", Direction.DESC)));
}).verifyComplete();
}
@Test // DATAMONGO-1444
@@ -131,41 +133,46 @@ public class ReactiveMongoTemplateIndexTests {
String command = "db." + template.getCollectionName(Person.class)
+ ".createIndex({'age':-1}, {'unique':true, 'sparse':true}), 1";
template.indexOps(Person.class).dropAllIndexes().block();
StepVerifier.create(template.indexOps(Person.class).dropAllIndexes()).verifyComplete();
TestSubscriber<IndexInfo> subscriber = TestSubscriber
.subscribe(template.indexOps(Person.class).getIndexInfo());
subscriber.await().assertComplete().assertNoValues();
StepVerifier.create(template.indexOps(Person.class).getIndexInfo()).verifyComplete();
Mono.from(factory.getMongoDatabase().runCommand(new org.bson.Document("eval", command))).block();
StepVerifier.create(factory.getMongoDatabase().runCommand(new org.bson.Document("eval", command))) //
.expectNextCount(1) //
.verifyComplete();
ListIndexesPublisher<Document> listIndexesPublisher = template
.getCollection(template.getCollectionName(Person.class)).listIndexes();
List<Document> indexInfo = Flux.from(listIndexesPublisher).collectList().block();
Document indexKey = null;
boolean unique = false;
for (Document document : indexInfo) {
StepVerifier.create(Flux.from(listIndexesPublisher).collectList()).consumeNextWith(indexInfos -> {
if ("age_-1".equals(document.get("name"))) {
indexKey = (org.bson.Document) document.get("key");
unique = (Boolean) document.get("unique");
Document indexKey = null;
boolean unique = false;
for (Document document : indexInfos) {
if ("age_-1".equals(document.get("name"))) {
indexKey = (org.bson.Document) document.get("key");
unique = (Boolean) document.get("unique");
}
}
}
assertThat(indexKey, hasEntry("age", -1D));
assertThat(unique, is(true));
assertThat(indexKey, hasEntry("age", -1D));
assertThat(unique, is(true));
}).verifyComplete();
List<IndexInfo> indexInfos = template.indexOps(Person.class).getIndexInfo().collectList().block();
StepVerifier.create(Flux.from(template.indexOps(Person.class).getIndexInfo().collectList()))
.consumeNextWith(indexInfos -> {
IndexInfo info = indexInfos.get(1);
assertThat(info.isUnique(), is(true));
assertThat(info.isSparse(), is(true));
IndexInfo info = indexInfos.get(1);
assertThat(info.isUnique(), is(true));
assertThat(info.isSparse(), is(true));
List<IndexField> indexFields = info.getIndexFields();
IndexField field = indexFields.get(0);
List<IndexField> indexFields = info.getIndexFields();
IndexField field = indexFields.get(0);
assertThat(field, is(IndexField.create("age", Direction.DESC)));
assertThat(field, is(IndexField.create("age", Direction.DESC)));
}).verifyComplete();
}
@Data

View File

@@ -13,12 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import rx.Observable;
import rx.Single;
import java.util.Arrays;
import java.util.List;
@@ -28,28 +35,17 @@ import org.junit.runner.RunWith;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
import org.springframework.data.repository.reactive.RxJava1SortingRepository;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
import rx.Observable;
import rx.Single;
/**
* Test for {@link ReactiveMongoRepository} using reactive wrapper type conversion.
*
@@ -59,11 +55,11 @@ import rx.Single;
@ContextConfiguration(classes = ConvertingReactiveMongoRepositoryTests.Config.class)
public class ConvertingReactiveMongoRepositoryTests {
@EnableReactiveMongoRepositories(includeFilters = @Filter(value = Repository.class), considerNestedRepositories = true)
@EnableReactiveMongoRepositories(includeFilters = @Filter(value = Repository.class),
considerNestedRepositories = true)
@ImportResource("classpath:reactive-infrastructure.xml")
static class Config {}
@Autowired ReactiveMongoTemplate template;
@Autowired MixedReactivePersonRepostitory reactiveRepository;
@Autowired ReactivePersonRepostitory reactivePersonRepostitory;
@Autowired RxJavaPersonRepostitory rxJavaPersonRepostitory;
@@ -71,9 +67,9 @@ public class ConvertingReactiveMongoRepositoryTests {
ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia;
@Before
public void setUp() throws Exception {
public void setUp() {
reactiveRepository.deleteAll().block();
StepVerifier.create(reactiveRepository.deleteAll()).verifyComplete();
dave = new ReactivePerson("Dave", "Matthews", 42);
oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@@ -83,123 +79,121 @@ public class ConvertingReactiveMongoRepositoryTests {
leroi = new ReactivePerson("Leroi", "Moore", 41);
alicia = new ReactivePerson("Alicia", "Keys", 30);
TestSubscriber<ReactivePerson> subscriber = TestSubscriber.create();
reactiveRepository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).subscribe(subscriber);
subscriber.await().assertComplete().assertNoError();
StepVerifier.create(reactiveRepository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia))) //
.expectNextCount(7) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void reactiveStreamsMethodsShouldWork() throws Exception {
TestSubscriber<Boolean> subscriber = TestSubscriber.subscribe(reactivePersonRepostitory.exists(dave.getId()));
subscriber.awaitAndAssertNextValueCount(1).assertValues(true);
public void reactiveStreamsMethodsShouldWork() {
StepVerifier.create(reactivePersonRepostitory.exists(dave.getId())).expectNext(true).verifyComplete();
}
@Test // DATAMONGO-1444
public void reactiveStreamsQueryMethodsShouldWork() throws Exception {
TestSubscriber<ReactivePerson> subscriber = TestSubscriber
.subscribe(reactivePersonRepostitory.findByLastname(boyd.getLastname()));
subscriber.awaitAndAssertNextValueCount(1).assertValues(boyd);
public void reactiveStreamsQueryMethodsShouldWork() {
StepVerifier.create(reactivePersonRepostitory.findByLastname(boyd.getLastname())).expectNext(boyd).verifyComplete();
}
@Test // DATAMONGO-1444
public void simpleRxJavaMethodsShouldWork() throws Exception {
public void simpleRxJavaMethodsShouldWork() {
rx.observers.TestSubscriber<Boolean> subscriber = new rx.observers.TestSubscriber<>();
rxJavaPersonRepostitory.exists(dave.getId()).subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValue(true);
rxJavaPersonRepostitory.exists(dave.getId()) //
.test() //
.awaitTerminalEvent() //
.assertValue(true) //
.assertNoErrors() //
.assertCompleted();
}
@Test // DATAMONGO-1444
public void existsWithSingleRxJavaIdMethodsShouldWork() throws Exception {
public void existsWithSingleRxJavaIdMethodsShouldWork() {
rx.observers.TestSubscriber<Boolean> subscriber = new rx.observers.TestSubscriber<>();
rxJavaPersonRepostitory.exists(Single.just(dave.getId())).subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValue(true);
rxJavaPersonRepostitory.exists(Single.just(dave.getId())) //
.test() //
.awaitTerminalEvent() //
.assertValue(true) //
.assertNoErrors() //
.assertCompleted();
}
@Test // DATAMONGO-1444
public void singleRxJavaQueryMethodShouldWork() throws Exception {
public void singleRxJavaQueryMethodShouldWork() {
rx.observers.TestSubscriber<ReactivePerson> subscriber = new rx.observers.TestSubscriber<>();
rxJavaPersonRepostitory.findByFirstnameAndLastname(dave.getFirstname(), dave.getLastname()).subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValue(dave);
rxJavaPersonRepostitory.findByFirstnameAndLastname(dave.getFirstname(), dave.getLastname()) //
.test() //
.awaitTerminalEvent() //
.assertValue(dave) //
.assertNoErrors() //
.assertCompleted();
}
@Test // DATAMONGO-1444
public void singleProjectedRxJavaQueryMethodShouldWork() throws Exception {
public void singleProjectedRxJavaQueryMethodShouldWork() {
rx.observers.TestSubscriber<ProjectedPerson> subscriber = new rx.observers.TestSubscriber<>();
rxJavaPersonRepostitory.findProjectedByLastname(carter.getLastname()).subscribe(subscriber);
List<ProjectedPerson> people = rxJavaPersonRepostitory.findProjectedByLastname(carter.getLastname()) //
.test() //
.awaitTerminalEvent() //
.assertValueCount(1) //
.assertNoErrors() //
.assertCompleted() //
.getOnNextEvents();
subscriber.awaitTerminalEvent();
subscriber.assertCompleted();
subscriber.assertNoErrors();
ProjectedPerson projectedPerson = subscriber.getOnNextEvents().get(0);
ProjectedPerson projectedPerson = people.get(0);
assertThat(projectedPerson.getFirstname(), is(equalTo(carter.getFirstname())));
}
@Test // DATAMONGO-1444
public void observableRxJavaQueryMethodShouldWork() throws Exception {
public void observableRxJavaQueryMethodShouldWork() {
rx.observers.TestSubscriber<ReactivePerson> subscriber = new rx.observers.TestSubscriber<>();
rxJavaPersonRepostitory.findByLastname(boyd.getLastname()).subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValue(boyd);
rxJavaPersonRepostitory.findByLastname(boyd.getLastname()) //
.test() //
.awaitTerminalEvent() //
.assertValue(boyd) //
.assertNoErrors() //
.assertCompleted() //
.getOnNextEvents();
}
@Test // DATAMONGO-1444
public void mixedRepositoryShouldWork() throws Exception {
public void mixedRepositoryShouldWork() {
ReactivePerson value = reactiveRepository.findByLastname(boyd.getLastname()).toBlocking().value();
assertThat(value, is(equalTo(boyd)));
reactiveRepository.findByLastname(boyd.getLastname()) //
.test() //
.awaitTerminalEvent() //
.assertValue(boyd) //
.assertNoErrors() //
.assertCompleted() //
.getOnNextEvents();
}
@Test // DATAMONGO-1444
public void shouldFindOneBySingleOfLastName() throws Exception {
public void shouldFindOneBySingleOfLastName() {
ReactivePerson carter = reactiveRepository.findByLastname(Single.just("Beauford")).block();
assertThat(carter.getFirstname(), is(equalTo("Carter")));
StepVerifier.create(reactiveRepository.findByLastname(Single.just(carter.getLastname()))) //
.expectNext(carter) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByObservableOfLastNameIn() throws Exception {
public void shouldFindByObservableOfLastNameIn() {
List<ReactivePerson> persons = reactiveRepository.findByLastnameIn(Observable.just("Beauford", "Matthews"))
.collectList().block();
assertThat(persons, hasItems(carter, dave, oliver));
StepVerifier.create(reactiveRepository.findByLastnameIn(Observable.just(carter.getLastname(), dave.getLastname()))) //
.expectNextCount(3) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByPublisherOfLastNameInAndAgeGreater() throws Exception {
public void shouldFindByPublisherOfLastNameInAndAgeGreater() {
List<ReactivePerson> persons = reactiveRepository
.findByLastnameInAndAgeGreaterThan(Flux.just("Beauford", "Matthews"), 41).toList().toBlocking().single();
List<ReactivePerson> people = reactiveRepository
.findByLastnameInAndAgeGreaterThan(Flux.just(carter.getLastname(), dave.getLastname()), 41).test() //
.awaitTerminalEvent() //
.assertValueCount(2) //
.assertNoErrors() //
.assertCompleted() //
.getOnNextEvents();
assertThat(persons, hasItems(carter, dave));
assertThat(people, hasItems(carter, dave));
}
@Repository

View File

@@ -19,8 +19,13 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.Sort.Direction.*;
import lombok.NoArgsConstructor;
import reactor.core.Cancellation;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
@@ -34,12 +39,9 @@ import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
@@ -55,13 +57,7 @@ import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.NoArgsConstructor;
import org.springframework.util.ClassUtils;
import reactor.core.Cancellation;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
/**
* Test for {@link ReactiveMongoRepository} query methods.
@@ -75,10 +71,10 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
@Autowired ReactiveMongoTemplate template;
ReactiveMongoRepositoryFactory factory;
private ClassLoader classLoader;
private BeanFactory beanFactory;
private ReactivePersonRepository repository;
private ReactiveCappedCollectionRepository cappedRepository;
ClassLoader classLoader;
BeanFactory beanFactory;
ReactivePersonRepository repository;
ReactiveCappedCollectionRepository cappedRepository;
Person dave, oliver, carter, boyd, stefan, leroi, alicia;
@@ -104,7 +100,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
repository = factory.getRepository(ReactivePersonRepository.class);
cappedRepository = factory.getRepository(ReactiveCappedCollectionRepository.class);
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave = new Person("Dave", "Matthews", 42);
oliver = new Person("Oliver August", "Matthews", 4);
@@ -118,78 +114,72 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
TestSubscriber<Person> subscriber = TestSubscriber.create();
repository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).subscribe(subscriber);
subscriber.await().assertComplete().assertNoError();
StepVerifier.create(repository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia))) //
.expectNextCount(7) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByLastName() {
List<Person> list = repository.findByLastname("Matthews").collectList().block();
assertThat(list, hasSize(2));
StepVerifier.create(repository.findByLastname(dave.getLastname())).expectNextCount(2).verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindOneByLastName() {
Person carter = repository.findOneByLastname("Beauford").block();
assertThat(carter.getFirstname(), is(equalTo("Carter")));
StepVerifier.create(repository.findOneByLastname(carter.getLastname())).expectNext(carter);
}
@Test // DATAMONGO-1444
public void shouldFindOneByPublisherOfLastName() {
Person carter = repository.findByLastname(Mono.just("Beauford")).block();
assertThat(carter.getFirstname(), is(equalTo("Carter")));
StepVerifier.create(repository.findByLastname(Mono.just(carter.getLastname()))).expectNext(carter);
}
@Test // DATAMONGO-1444
public void shouldFindByPublisherOfLastNameIn() {
List<Person> persons = repository.findByLastnameIn(Flux.just("Beauford", "Matthews")).collectList().block();
assertThat(persons, hasItems(carter, dave, oliver));
StepVerifier.create(repository.findByLastnameIn(Flux.just(carter.getLastname(), dave.getLastname()))) //
.expectNextCount(3) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByPublisherOfLastNameInAndAgeGreater() {
List<Person> persons = repository.findByLastnameInAndAgeGreaterThan(Flux.just("Beauford", "Matthews"), 41)
.collectList().block();
assertThat(persons, hasItems(carter, dave));
StepVerifier
.create(repository.findByLastnameInAndAgeGreaterThan(Flux.just(carter.getLastname(), dave.getLastname()), 41)) //
.expectNextCount(2) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindUsingPublishersInStringQuery() {
List<Person> persons = repository.findStringQuery(Flux.just("Beauford", "Matthews"), Mono.just(41)).collectList()
.block();
assertThat(persons, hasItems(carter, dave));
StepVerifier.create(repository.findStringQuery(Flux.just("Beauford", "Matthews"), Mono.just(41))) //
.expectNextCount(2) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByLastNameAndSort() {
StepVerifier.create(repository.findByLastname("Matthews", Sort.by(ASC, "age"))) //
.expectNext(oliver, dave) //
.verifyComplete();
List<Person> persons = repository.findByLastname("Matthews", new Sort(new Order(ASC, "age"))).collectList().block();
assertThat(persons, contains(oliver, dave));
persons = repository.findByLastname("Matthews", new Sort(new Order(DESC, "age"))).collectList().block();
assertThat(persons, contains(dave, oliver));
StepVerifier.create(repository.findByLastname("Matthews", Sort.by(DESC, "age"))) //
.expectNext(dave, oliver) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldUseInfiniteStream() throws Exception {
template.dropCollection(Capped.class).block();
template.createCollection(Capped.class, new CollectionOptions(1000, 100, true)).block();
template.insert(new Capped("value", Math.random())).block();
StepVerifier
.create(template.dropCollection(Capped.class) //
.then(template.createCollection(Capped.class, //
new CollectionOptions(1000, 100, true)))) //
.expectNextCount(1) //
.verifyComplete();
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
BlockingQueue<Capped> documents = new LinkedBlockingDeque<>(100);
@@ -197,7 +187,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
template.insert(new Capped("value", Math.random())).block();
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
assertThat(documents.isEmpty(), is(true));
@@ -207,9 +197,14 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
@Test // DATAMONGO-1444
public void shouldUseInfiniteStreamWithProjection() throws Exception {
template.dropCollection(Capped.class).block();
template.createCollection(Capped.class, new CollectionOptions(1000, 100, true)).block();
template.insert(new Capped("value", Math.random())).block();
StepVerifier
.create(template.dropCollection(Capped.class) //
.then(template.createCollection(Capped.class, //
new CollectionOptions(1000, 100, true)))) //
.expectNextCount(1) //
.verifyComplete();
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
BlockingQueue<CappedProjection> documents = new LinkedBlockingDeque<>(100);
@@ -219,7 +214,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
assertThat(projection1, is(notNullValue()));
assertThat(projection1.getRandom(), is(not(0)));
template.insert(new Capped("value", Math.random())).block();
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
CappedProjection projection2 = documents.poll(5, TimeUnit.SECONDS);
assertThat(projection2, is(notNullValue()));
@@ -235,11 +230,11 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
repository.save(dave).block();
StepVerifier.create(repository.save(dave)).expectNextCount(1).verifyComplete();
repository.findByLocationWithin(new Circle(-78.99171, 45.738868, 170)) //
.subscribeWith(TestSubscriber.create()) //
.awaitAndAssertNextValues(dave);
StepVerifier.create(repository.findByLocationWithin(new Circle(-78.99171, 45.738868, 170))) //
.expectNext(dave) //
.verifyComplete();
}
@Test // DATAMONGO-1444
@@ -247,11 +242,13 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
repository.save(dave).block();
StepVerifier.create(repository.save(dave)).expectNextCount(1).verifyComplete();
repository.findByLocationWithin(new Circle(-78.99171, 45.738868, 170), new PageRequest(0, 10)) //
.subscribeWith(TestSubscriber.create()) //
.awaitAndAssertNextValues(dave);
StepVerifier
.create(repository.findByLocationWithin(new Circle(-78.99171, 45.738868, 170), //
PageRequest.of(0, 10))) //
.expectNext(dave) //
.verifyComplete();
}
@Test // DATAMONGO-1444
@@ -259,15 +256,15 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
repository.save(dave).block();
StepVerifier.create(repository.save(dave)).expectNextCount(1).verifyComplete();
repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000, Metrics.KILOMETERS)) //
.subscribeWith(TestSubscriber.create()) //
.awaitAndAssertNextValuesWith(personGeoResult -> {
StepVerifier.create(repository.findByLocationNear(new Point(-73.99, 40.73), //
new Distance(2000, Metrics.KILOMETERS)) //
).consumeNextWith(actual -> {
assertThat(personGeoResult.getDistance().getValue(), is(closeTo(1, 1)));
assertThat(personGeoResult.getContent(), is(equalTo(dave)));
});
assertThat(actual.getDistance().getValue(), is(closeTo(1, 1)));
assertThat(actual.getContent(), is(equalTo(dave)));
}).verifyComplete();
}
@Test // DATAMONGO-1444
@@ -275,15 +272,17 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
repository.save(dave).block();
StepVerifier.create(repository.save(dave)).expectNextCount(1).verifyComplete();
repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000, Metrics.KILOMETERS), new PageRequest(0, 10)) //
.subscribeWith(TestSubscriber.create()) //
.awaitAndAssertNextValuesWith(personGeoResult -> {
StepVerifier
.create(repository.findByLocationNear(new Point(-73.99, 40.73), //
new Distance(2000, Metrics.KILOMETERS), //
PageRequest.of(0, 10))) //
.consumeNextWith(actual -> {
assertThat(personGeoResult.getDistance().getValue(), is(closeTo(1, 1)));
assertThat(personGeoResult.getContent(), is(equalTo(dave)));
});
assertThat(actual.getDistance().getValue(), is(closeTo(1, 1)));
assertThat(actual.getContent(), is(equalTo(dave)));
}).verifyComplete();
}
@Test // DATAMONGO-1444
@@ -291,11 +290,13 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
repository.save(dave).block();
StepVerifier.create(repository.save(dave)).expectNextCount(1).verifyComplete();
repository.findPersonByLocationNear(new Point(-73.99, 40.73), new Distance(2000, Metrics.KILOMETERS)) //
.subscribeWith(TestSubscriber.create()) //
.awaitAndAssertNextValues(dave);
StepVerifier
.create(repository.findPersonByLocationNear(new Point(-73.99, 40.73), //
new Distance(2000, Metrics.KILOMETERS))) //
.expectNext(dave) //
.verifyComplete();
}
interface ReactivePersonRepository extends ReactiveMongoRepository<Person, String> {

View File

@@ -13,15 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
@@ -32,8 +35,6 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
@@ -45,12 +46,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ClassUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
/**
* Test for {@link ReactiveMongoRepository}.
*
@@ -62,10 +57,10 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
@Autowired private ReactiveMongoTemplate template;
private ReactiveMongoRepositoryFactory factory;
private ClassLoader classLoader;
private BeanFactory beanFactory;
private ReactivePersonRepostitory repository;
ReactiveMongoRepositoryFactory factory;
ClassLoader classLoader;
BeanFactory beanFactory;
ReactivePersonRepostitory repository;
private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia;
@@ -90,7 +85,7 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
repository = factory.getRepository(ReactivePersonRepostitory.class);
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave = new ReactivePerson("Dave", "Matthews", 42);
oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@@ -100,134 +95,92 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
leroi = new ReactivePerson("Leroi", "Moore", 41);
alicia = new ReactivePerson("Alicia", "Keys", 30);
TestSubscriber<ReactivePerson> subscriber = TestSubscriber.create();
repository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).subscribe(subscriber);
subscriber.await().assertComplete().assertNoError();
StepVerifier.create(repository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia))) //
.expectNextCount(7) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void existsByIdShouldReturnTrueForExistingObject() {
Boolean exists = repository.exists(dave.id).block();
assertThat(exists, is(true));
StepVerifier.create(repository.exists(dave.id)).expectNext(true).verifyComplete();
}
@Test // DATAMONGO-1444
public void existsByIdShouldReturnFalseForAbsentObject() {
TestSubscriber<Boolean> testSubscriber = TestSubscriber.subscribe(repository.exists("unknown"));
testSubscriber.await().assertComplete().assertValues(false).assertNoError();
StepVerifier.create(repository.exists("unknown")).expectNext(false).verifyComplete();
}
@Test // DATAMONGO-1444
public void existsByMonoOfIdShouldReturnTrueForExistingObject() {
Boolean exists = repository.exists(Mono.just(dave.id)).block();
assertThat(exists, is(true));
StepVerifier.create(repository.exists(Mono.just(dave.id))).expectNext(true).verifyComplete();
}
@Test // DATAMONGO-1444
public void existsByEmptyMonoOfIdShouldReturnEmptyMono() {
TestSubscriber<Boolean> testSubscriber = TestSubscriber.subscribe(repository.exists(Mono.empty()));
testSubscriber.await().assertComplete().assertNoValues().assertNoError();
StepVerifier.create(repository.exists(Mono.empty())).verifyComplete();
}
@Test // DATAMONGO-1444
public void findOneShouldReturnObject() {
ReactivePerson person = repository.findOne(dave.id).block();
assertThat(person.getFirstname(), is(equalTo("Dave")));
StepVerifier.create(repository.findOne(dave.id)).expectNext(dave).verifyComplete();
}
@Test // DATAMONGO-1444
public void findOneShouldCompleteWithoutValueForAbsentObject() {
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findOne("unknown"));
testSubscriber.await().assertComplete().assertNoValues().assertNoError();
StepVerifier.create(repository.findOne("unknown")).verifyComplete();
}
@Test // DATAMONGO-1444
public void findOneByMonoOfIdShouldReturnTrueForExistingObject() {
ReactivePerson person = repository.findOne(Mono.just(dave.id)).block();
assertThat(person.id, is(equalTo(dave.id)));
StepVerifier.create(repository.findOne(Mono.just(dave.id))).expectNext(dave).verifyComplete();
}
@Test // DATAMONGO-1444
public void findOneByEmptyMonoOfIdShouldReturnEmptyMono() {
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findOne(Mono.empty()));
testSubscriber.await().assertComplete().assertNoValues().assertNoError();
StepVerifier.create(repository.findOne(Mono.empty())).verifyComplete();
}
@Test // DATAMONGO-1444
public void findAllShouldReturnAllResults() {
List<ReactivePerson> persons = repository.findAll().collectList().block();
assertThat(persons, hasSize(7));
StepVerifier.create(repository.findAll()).expectNextCount(7).verifyComplete();
}
@Test // DATAMONGO-1444
public void findAllByIterableOfIdShouldReturnResults() {
List<ReactivePerson> persons = repository.findAll(Arrays.asList(dave.id, boyd.id)).collectList().block();
assertThat(persons, hasSize(2));
StepVerifier.create(repository.findAll(Arrays.asList(dave.id, boyd.id))).expectNextCount(2).verifyComplete();
}
@Test // DATAMONGO-1444
public void findAllByPublisherOfIdShouldReturnResults() {
List<ReactivePerson> persons = repository.findAll(Flux.just(dave.id, boyd.id)).collectList().block();
assertThat(persons, hasSize(2));
StepVerifier.create(repository.findAll(Flux.just(dave.id, boyd.id))).expectNextCount(2).verifyComplete();
}
@Test // DATAMONGO-1444
public void findAllByEmptyPublisherOfIdShouldReturnResults() {
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findAll(Flux.empty()));
testSubscriber.await().assertComplete().assertNoValues().assertNoError();
StepVerifier.create(repository.findAll(Flux.empty())).verifyComplete();
}
@Test // DATAMONGO-1444
public void findAllWithSortShouldReturnResults() {
List<ReactivePerson> persons = repository.findAll(new Sort(new Order(Direction.ASC, "age"))).collectList().block();
assertThat(persons, hasSize(7));
assertThat(persons.get(0).getId(), is(equalTo(oliver.getId())));
StepVerifier.create(repository.findAll(new Sort(new Order(Direction.ASC, "age")))) //
.expectNextCount(7) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void countShouldReturnNumberOfRecords() {
TestSubscriber<Long> testSubscriber = TestSubscriber.subscribe(repository.count());
testSubscriber.await().assertComplete().assertValueCount(1).assertValues(7L).assertNoError();
StepVerifier.create(repository.count()).expectNext(7L).verifyComplete();
}
@Test // DATAMONGO-1444
public void insertEntityShouldInsertEntity() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
ReactivePerson person = new ReactivePerson("Homer", "Simpson", 36);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.insert(person));
testSubscriber.await().assertComplete().assertValueCount(1).assertValues(person);
StepVerifier.create(repository.insert(person)).expectNext(person).verifyComplete();
assertThat(person.getId(), is(notNullValue()));
}
@@ -245,16 +198,15 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
@Test // DATAMONGO-1444
public void insertIterableOfEntitiesShouldInsertEntity() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave.setId(null);
oliver.setId(null);
boyd.setId(null);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
.subscribe(repository.insert(Arrays.asList(dave, oliver, boyd)));
testSubscriber.await().assertComplete().assertValueCount(3).assertValues(dave, oliver, boyd);
StepVerifier.create(repository.insert(Arrays.asList(dave, oliver, boyd))) //
.expectNext(dave, oliver, boyd) //
.verifyComplete();
assertThat(dave.getId(), is(notNullValue()));
assertThat(oliver.getId(), is(notNullValue()));
@@ -264,16 +216,13 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
@Test // DATAMONGO-1444
public void insertPublisherOfEntitiesShouldInsertEntity() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave.setId(null);
oliver.setId(null);
boyd.setId(null);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
.subscribe(repository.insert(Flux.just(dave, oliver, boyd)));
testSubscriber.await().assertComplete().assertValueCount(3);
StepVerifier.create(repository.insert(Flux.just(dave, oliver, boyd))).expectNextCount(3).verifyComplete();
assertThat(dave.getId(), is(notNullValue()));
assertThat(oliver.getId(), is(notNullValue()));
@@ -286,19 +235,15 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
dave.setFirstname("Hello, Dave");
dave.setLastname("Bowman");
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.save(dave));
StepVerifier.create(repository.save(dave)).expectNext(dave).verifyComplete();
testSubscriber.await().assertComplete().assertValueCount(1).assertValues(dave);
StepVerifier.create(repository.findByLastname("Matthews")).expectNext(oliver).verifyComplete();
List<ReactivePerson> matthews = repository.findByLastname("Matthews").collectList().block();
assertThat(matthews, hasSize(1));
assertThat(matthews, contains(oliver));
assertThat(matthews, not(contains(dave)));
StepVerifier.create(repository.findOne(dave.id)).consumeNextWith(actual -> {
ReactivePerson reactivePerson = repository.findOne(dave.id).block();
assertThat(reactivePerson.getFirstname(), is(equalTo(dave.getFirstname())));
assertThat(reactivePerson.getLastname(), is(equalTo(dave.getLastname())));
assertThat(actual.getFirstname(), is(equalTo(dave.getFirstname())));
assertThat(actual.getLastname(), is(equalTo(dave.getLastname())));
}).verifyComplete();
}
@Test // DATAMONGO-1444
@@ -306,29 +251,25 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
ReactivePerson person = new ReactivePerson("Homer", "Simpson", 36);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.save(person));
StepVerifier.create(repository.save(person)).expectNext(person).verifyComplete();
testSubscriber.await().assertComplete().assertValueCount(1).assertValues(person);
StepVerifier.create(repository.findOne(person.id)).consumeNextWith(actual -> {
ReactivePerson reactivePerson = repository.findOne(person.id).block();
assertThat(reactivePerson.getFirstname(), is(equalTo(person.getFirstname())));
assertThat(reactivePerson.getLastname(), is(equalTo(person.getLastname())));
assertThat(actual.getFirstname(), is(equalTo(person.getFirstname())));
assertThat(actual.getLastname(), is(equalTo(person.getLastname())));
}).verifyComplete();
}
@Test // DATAMONGO-1444
public void saveIterableOfNewEntitiesShouldInsertEntity() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave.setId(null);
oliver.setId(null);
boyd.setId(null);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
.subscribe(repository.save(Arrays.asList(dave, oliver, boyd)));
testSubscriber.await().assertComplete().assertValueCount(3).assertValues(dave, oliver, boyd);
StepVerifier.create(repository.save(Arrays.asList(dave, oliver, boyd))).expectNextCount(3).verifyComplete();
assertThat(dave.getId(), is(notNullValue()));
assertThat(oliver.getId(), is(notNullValue()));
@@ -343,32 +284,24 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
dave.setFirstname("Hello, Dave");
dave.setLastname("Bowman");
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
.subscribe(repository.save(Arrays.asList(person, dave)));
StepVerifier.create(repository.save(Arrays.asList(person, dave))).expectNextCount(2).verifyComplete();
testSubscriber.await().assertComplete().assertValueCount(2);
ReactivePerson persistentDave = repository.findOne(dave.id).block();
assertThat(persistentDave, is(equalTo(dave)));
StepVerifier.create(repository.findOne(dave.id)).expectNext(dave).verifyComplete();
assertThat(person.id, is(notNullValue()));
ReactivePerson persistentHomer = repository.findOne(person.id).block();
assertThat(persistentHomer, is(equalTo(person)));
StepVerifier.create(repository.findOne(person.id)).expectNext(person).verifyComplete();
}
@Test // DATAMONGO-1444
public void savePublisherOfEntitiesShouldInsertEntity() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
dave.setId(null);
oliver.setId(null);
boyd.setId(null);
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
.subscribe(repository.save(Flux.just(dave, oliver, boyd)));
testSubscriber.await().assertComplete().assertValueCount(3);
StepVerifier.create(repository.save(Flux.just(dave, oliver, boyd))).expectNextCount(3).verifyComplete();
assertThat(dave.getId(), is(notNullValue()));
assertThat(oliver.getId(), is(notNullValue()));
@@ -378,67 +311,46 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
@Test // DATAMONGO-1444
public void deleteAllShouldRemoveEntities() {
repository.deleteAll().block();
StepVerifier.create(repository.deleteAll()).verifyComplete();
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findAll());
testSubscriber.await().assertComplete().assertValueCount(0);
StepVerifier.create(repository.findAll()).verifyComplete();
}
@Test // DATAMONGO-1444
public void deleteByIdShouldRemoveEntity() {
TestSubscriber<Void> testSubscriber = TestSubscriber.subscribe(repository.delete(dave.id));
StepVerifier.create(repository.delete(dave.id)).verifyComplete();
testSubscriber.await().assertComplete().assertNoValues();
TestSubscriber<ReactivePerson> verificationSubscriber = TestSubscriber.subscribe(repository.findOne(dave.id));
verificationSubscriber.await().assertComplete().assertNoValues();
StepVerifier.create(repository.findOne(dave.id)).verifyComplete();
}
@Test // DATAMONGO-1444
public void deleteShouldRemoveEntity() {
TestSubscriber<Void> testSubscriber = TestSubscriber.subscribe(repository.delete(dave));
StepVerifier.create(repository.delete(dave)).verifyComplete();
testSubscriber.await().assertComplete().assertNoValues();
StepVerifier.create(repository.findOne(dave.id)).verifyComplete();
TestSubscriber<ReactivePerson> verificationSubscriber = TestSubscriber.subscribe(repository.findOne(dave.id));
verificationSubscriber.await().assertComplete().assertNoValues();
}
@Test // DATAMONGO-1444
public void deleteIterableOfEntitiesShouldRemoveEntities() {
TestSubscriber<Void> testSubscriber = TestSubscriber.subscribe(repository.delete(Arrays.asList(dave, boyd)));
StepVerifier.create(repository.delete(Arrays.asList(dave, boyd))).verifyComplete();
testSubscriber.await().assertComplete().assertNoValues();
TestSubscriber<ReactivePerson> verificationSubscriber = TestSubscriber.subscribe(repository.findOne(boyd.id));
verificationSubscriber.await().assertComplete().assertNoValues();
List<ReactivePerson> matthews = repository.findByLastname("Matthews").collectList().block();
assertThat(matthews, hasSize(1));
assertThat(matthews, contains(oliver));
StepVerifier.create(repository.findOne(boyd.id)).verifyComplete();
StepVerifier.create(repository.findByLastname("Matthews")).expectNext(oliver).verifyComplete();
}
@Test // DATAMONGO-1444
public void deletePublisherOfEntitiesShouldRemoveEntities() {
TestSubscriber<Void> testSubscriber = TestSubscriber.subscribe(repository.delete(Flux.just(dave, boyd)));
StepVerifier.create(repository.delete(Flux.just(dave, boyd))).verifyComplete();
testSubscriber.await().assertComplete().assertNoValues();
TestSubscriber<ReactivePerson> verificationSubscriber = TestSubscriber.subscribe(repository.findOne(boyd.id));
verificationSubscriber.await().assertComplete().assertNoValues();
List<ReactivePerson> matthews = repository.findByLastname("Matthews").collectList().block();
assertThat(matthews, hasSize(1));
assertThat(matthews, contains(oliver));
StepVerifier.create(repository.findOne(boyd.id)).verifyComplete();
StepVerifier.create(repository.findByLastname("Matthews")).expectNext(oliver).verifyComplete();
}
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {