BAEL-1325 - Spring Data Reactive Repositories with MongoDB (#4373)
* refactor: use StepVerifier * refactor: use test observer
This commit is contained in:
parent
3f9cf77272
commit
c7b2aded99
|
@ -141,6 +141,12 @@
|
|||
<artifactId>rxjava</artifactId>
|
||||
<version>${rxjava-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<version>${project-reactor-test}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -185,6 +191,7 @@
|
|||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<project-reactor-test>3.1.6.RELEASE</project-reactor-test>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -25,26 +26,44 @@ public class AccountCrudRepositoryIntegrationTest {
|
|||
public void givenValue_whenFindAllByValue_thenFindAccount() {
|
||||
repository.save(new Account(null, "Bill", 12.3)).block();
|
||||
Flux<Account> accountFlux = repository.findAllByValue(12.3);
|
||||
Account account = accountFlux.next().block();
|
||||
assertEquals("Bill", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3) , account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
|
||||
StepVerifier.create(accountFlux)
|
||||
.assertNext(account -> {
|
||||
assertEquals("Bill", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3) , account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOwner_whenFindFirstByOwner_thenFindAccount() {
|
||||
repository.save(new Account(null, "Bill", 12.3)).block();
|
||||
Mono<Account> accountMono = repository.findFirstByOwner(Mono.just("Bill"));
|
||||
Account account = accountMono.block();
|
||||
assertEquals("Bill", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3) , account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
|
||||
StepVerifier.create(accountMono)
|
||||
.assertNext(account -> {
|
||||
assertEquals("Bill", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3) , account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccount_whenSave_thenSaveAccount() {
|
||||
Mono<Account> accountMono = repository.save(new Account(null, "Bill", 12.3));
|
||||
assertNotNull(accountMono.block().getId());
|
||||
|
||||
StepVerifier
|
||||
.create(accountMono)
|
||||
.assertNext(account -> assertNotNull(account.getId()))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.baeldung.reactive.repository;
|
|||
|
||||
import com.baeldung.reactive.Spring5ReactiveApplication;
|
||||
import com.baeldung.reactive.model.Account;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -13,10 +11,10 @@ import org.springframework.data.domain.ExampleMatcher;
|
|||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
|
@ -32,23 +30,38 @@ public class AccountMongoRepositoryIntegrationTest {
|
|||
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("owner", startsWith());
|
||||
Example<Account> example = Example.of(new Account(null, "jo", null), matcher);
|
||||
Flux<Account> accountFlux = repository.findAll(example);
|
||||
List<Account> accounts = accountFlux.collectList().block();
|
||||
|
||||
assertTrue(accounts.stream().anyMatch(x -> x.getOwner().equals("john")));
|
||||
StepVerifier
|
||||
.create(accountFlux)
|
||||
.assertNext(account -> assertEquals("john", account.getOwner()))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccount_whenSave_thenSave() {
|
||||
Mono<Account> accountMono = repository.save(new Account(null, "john", 12.3));
|
||||
assertNotNull(accountMono.block().getId());
|
||||
|
||||
StepVerifier
|
||||
.create(accountMono)
|
||||
.assertNext(account -> assertNotNull(account.getId()))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenId_whenFindById_thenFindAccount() {
|
||||
Account inserted = repository.save(new Account(null, "john", 12.3)).block();
|
||||
Mono<Account> accountMono = repository.findById(inserted.getId());
|
||||
assertEquals("john", accountMono.block().getOwner());
|
||||
assertEquals(Double.valueOf(12.3), accountMono.block().getValue());
|
||||
assertNotNull(accountMono.block().getId());
|
||||
|
||||
StepVerifier
|
||||
.create(accountMono)
|
||||
.assertNext(account -> {
|
||||
assertEquals("john", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3), account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
}
|
|
@ -21,23 +21,38 @@ public class AccountRxJavaRepositoryIntegrationTest {
|
|||
AccountRxJavaRepository repository;
|
||||
|
||||
@Test
|
||||
public void givenValue_whenFindAllByValue_thenFindAccounts() {
|
||||
public void givenValue_whenFindAllByValue_thenFindAccounts() throws InterruptedException {
|
||||
repository.save(new Account(null, "bruno", 12.3)).blockingGet();
|
||||
Observable<Account> accountObservable = repository.findAllByValue(12.3);
|
||||
Account account = accountObservable.filter(x -> x.getOwner().equals("bruno")).blockingFirst();
|
||||
assertEquals("bruno", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3), account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
|
||||
accountObservable
|
||||
.test()
|
||||
.await()
|
||||
.assertComplete()
|
||||
.assertValueAt(0, account -> {
|
||||
assertEquals("bruno", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3), account.getValue());
|
||||
return true;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOwner_whenFindFirstByOwner_thenFindAccount() {
|
||||
public void givenOwner_whenFindFirstByOwner_thenFindAccount() throws InterruptedException {
|
||||
repository.save(new Account(null, "bruno", 12.3)).blockingGet();
|
||||
Single<Account> accountSingle = repository.findFirstByOwner(Single.just("bruno"));
|
||||
Account account = accountSingle.blockingGet();
|
||||
assertEquals("bruno", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3), account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
|
||||
accountSingle
|
||||
.test()
|
||||
.await()
|
||||
.assertComplete()
|
||||
.assertValueAt(0, account -> {
|
||||
assertEquals("bruno", account.getOwner());
|
||||
assertEquals(Double.valueOf(12.3), account.getValue());
|
||||
assertNotNull(account.getId());
|
||||
return true;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue