From fb04fc45885c33369ddb5e04a9644e625cd4a3b3 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 21 May 2018 09:37:44 +0400 Subject: [PATCH] BAEL-1325 (#4272) * NatsClient refactor * add: mongodb reactive repositories * fix: test names * fix: test names * fix: test running on maven * fix: test name * fix: test class names * Rename tests --- spring-5-reactive/pom.xml | 34 ++++++------ .../reactive/Spring5ReactiveApplication.java | 14 ++++- .../com/baeldung/reactive/model/Account.java | 21 ++++++++ .../repository/AccountCrudRepository.java | 15 ++++++ .../repository/AccountMongoRepository.java | 7 +++ .../repository/AccountRxJavaRepository.java | 15 ++++++ .../template/AccountTemplateOperations.java | 33 ++++++++++++ .../com/baeldung/reactive/FluxUnitTest.java | 6 ++- ...java => PlayerHandlerIntegrationTest.java} | 2 +- ...ava => UserControllerIntegrationTest.java} | 2 +- .../AccountCrudRepositoryIntegrationTest.java | 51 ++++++++++++++++++ ...AccountMongoRepositoryIntegrationTest.java | 54 +++++++++++++++++++ ...ccountRxJavaRepositoryIntegrationTest.java | 43 +++++++++++++++ ...ountTemplateOperationsIntegrationTest.java | 48 +++++++++++++++++ 14 files changed, 323 insertions(+), 22 deletions(-) create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java rename spring-5-reactive/src/test/java/com/baeldung/reactive/filters/{PlayerHandlerTest.java => PlayerHandlerIntegrationTest.java} (96%) rename spring-5-reactive/src/test/java/com/baeldung/reactive/filters/{UserControllerTest.java => UserControllerIntegrationTest.java} (96%) create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index d17026af47..fa86e9ca8a 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -18,10 +18,6 @@ - - org.springframework.boot - spring-boot-starter-data-jpa - org.springframework.boot spring-boot-starter-validation @@ -66,7 +62,6 @@ lombok compile - org.apache.geronimo.specs geronimo-json_1.1_spec @@ -89,12 +84,6 @@ spring-boot-devtools runtime - - com.h2database - h2 - runtime - - org.springframework spring-test @@ -133,16 +122,26 @@ ${junit.platform.version} test - - - org.projectlombok - lombok - org.apache.commons commons-lang3 + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + test + + + io.reactivex.rxjava2 + rxjava + ${rxjava-version} + + @@ -178,9 +177,10 @@ UTF-8 1.8 1.0.0 - 5.0.0 + 5.0.2 2.20 1.0.1.RELEASE + 2.1.12 1.1.3 1.0 1.0 diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java index a9308124fa..e96767145e 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java @@ -1,13 +1,25 @@ package com.baeldung.reactive; +import com.mongodb.reactivestreams.client.MongoClient; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @SpringBootApplication -public class Spring5ReactiveApplication { +public class Spring5ReactiveApplication{ public static void main(String[] args) { SpringApplication.run(Spring5ReactiveApplication.class, args); } + @Autowired + MongoClient mongoClient; + + @Bean + public ReactiveMongoTemplate reactiveMongoTemplate() { + return new ReactiveMongoTemplate(mongoClient, "test"); + } + } diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java new file mode 100644 index 0000000000..57abd80009 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java @@ -0,0 +1,21 @@ +package com.baeldung.reactive.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +@Data +@ToString +@AllArgsConstructor +@NoArgsConstructor +public class Account { + + @Id + private String id; + private String owner; + private Double value; +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java new file mode 100644 index 0000000000..8798c13772 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.reactive.repository; + +import com.baeldung.reactive.model.Account; +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import org.springframework.stereotype.Repository; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Repository +public interface AccountCrudRepository extends ReactiveCrudRepository { + + public Flux findAllByValue(Double value); + + public Mono findFirstByOwner(Mono owner); +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java new file mode 100644 index 0000000000..5c09e4a264 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.reactive.repository; + +import com.baeldung.reactive.model.Account; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; + +public interface AccountMongoRepository extends ReactiveMongoRepository { +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java new file mode 100644 index 0000000000..6afe92a21b --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.reactive.repository; + +import com.baeldung.reactive.model.Account; +import io.reactivex.Observable; +import io.reactivex.Single; +import org.springframework.data.repository.reactive.RxJava2CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AccountRxJavaRepository extends RxJava2CrudRepository{ + + public Observable findAllByValue(Double value); + + public Single findFirstByOwner(Single owner); +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java new file mode 100644 index 0000000000..9d32f34e3b --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java @@ -0,0 +1,33 @@ +package com.baeldung.reactive.template; + +import com.baeldung.reactive.model.Account; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.ReactiveMongoTemplate; +import org.springframework.data.mongodb.core.ReactiveRemoveOperation; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Service +public class AccountTemplateOperations { + + @Autowired + ReactiveMongoTemplate template; + + public Mono findById(String id) { + return template.findById(id, Account.class); + } + + public Flux findAll() { + return template.findAll(Account.class); + } + + public Mono save(Mono account) { + return template.save(account); + } + + public ReactiveRemoveOperation.ReactiveRemove deleteAll() { + return template.remove(Account.class); + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/FluxUnitTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/FluxUnitTest.java index bad5fc5f22..a63d2391a1 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/FluxUnitTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/FluxUnitTest.java @@ -14,11 +14,13 @@ import reactor.core.publisher.Flux; public class FluxUnitTest { + public static final Random RANDOM = new Random(); + @Test public void whenFluxIsConstructed_thenCorrect() { final Flux flux = Flux. create(fluxSink -> { - while (true) { - fluxSink.next(new Foo(new Random().nextLong(), randomAlphabetic(6))); + for (int i = 0 ; i < 100 ; i++) { + fluxSink.next(new Foo(RANDOM.nextLong(), randomAlphabetic(6))); } }).sample(Duration.ofSeconds(1)).log(); diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerIntegrationTest.java similarity index 96% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerTest.java rename to spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerIntegrationTest.java index 8189802248..bb2408ea79 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerIntegrationTest.java @@ -12,7 +12,7 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class PlayerHandlerTest { +public class PlayerHandlerIntegrationTest { @Autowired private WebTestClient webTestClient; diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerIntegrationTest.java similarity index 96% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerTest.java rename to spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerIntegrationTest.java index d32a9d478f..e7d289e5c4 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerIntegrationTest.java @@ -12,7 +12,7 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class UserControllerTest { +public class UserControllerIntegrationTest { @Autowired private WebTestClient webTestClient; diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java new file mode 100644 index 0000000000..86f995ed79 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java @@ -0,0 +1,51 @@ +package com.baeldung.reactive.repository; + + +import com.baeldung.reactive.Spring5ReactiveApplication; +import com.baeldung.reactive.model.Account; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class AccountCrudRepositoryIntegrationTest { + + @Autowired + AccountCrudRepository repository; + + @Test + public void givenValue_whenFindAllByValue_thenFindAccount() { + repository.save(new Account(null, "Bill", 12.3)).block(); + Flux accountFlux = repository.findAllByValue(12.3); + Account account = accountFlux.next().block(); + assertEquals("Bill", account.getOwner()); + assertEquals(Double.valueOf(12.3) , account.getValue()); + assertNotNull(account.getId()); + } + + @Test + public void givenOwner_whenFindFirstByOwner_thenFindAccount() { + repository.save(new Account(null, "Bill", 12.3)).block(); + Mono accountMono = repository.findFirstByOwner(Mono.just("Bill")); + Account account = accountMono.block(); + assertEquals("Bill", account.getOwner()); + assertEquals(Double.valueOf(12.3) , account.getValue()); + assertNotNull(account.getId()); + } + + @Test + public void givenAccount_whenSave_thenSaveAccount() { + Mono accountMono = repository.save(new Account(null, "Bill", 12.3)); + assertNotNull(accountMono.block().getId()); + } + + +} \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java new file mode 100644 index 0000000000..f95c443b7f --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java @@ -0,0 +1,54 @@ +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; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; +import org.springframework.test.context.junit4.SpringRunner; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; + +import static org.junit.Assert.*; +import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class AccountMongoRepositoryIntegrationTest { + + @Autowired + AccountMongoRepository repository; + + @Test + public void givenExample_whenFindAllWithExample_thenFindAllMacthings() { + repository.save(new Account(null, "john", 12.3)).block(); + ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("owner", startsWith()); + Example example = Example.of(new Account(null, "jo", null), matcher); + Flux accountFlux = repository.findAll(example); + List accounts = accountFlux.collectList().block(); + + assertTrue(accounts.stream().anyMatch(x -> x.getOwner().equals("john"))); + } + + @Test + public void givenAccount_whenSave_thenSave() { + Mono accountMono = repository.save(new Account(null, "john", 12.3)); + assertNotNull(accountMono.block().getId()); + } + + @Test + public void givenId_whenFindById_thenFindAccount() { + Account inserted = repository.save(new Account(null, "john", 12.3)).block(); + Mono accountMono = repository.findById(inserted.getId()); + assertEquals("john", accountMono.block().getOwner()); + assertEquals(Double.valueOf(12.3), accountMono.block().getValue()); + assertNotNull(accountMono.block().getId()); + } +} \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java new file mode 100644 index 0000000000..6199b460d0 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java @@ -0,0 +1,43 @@ +package com.baeldung.reactive.repository; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import com.baeldung.reactive.model.Account; +import io.reactivex.Observable; +import io.reactivex.Single; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class AccountRxJavaRepositoryIntegrationTest { + + @Autowired + AccountRxJavaRepository repository; + + @Test + public void givenValue_whenFindAllByValue_thenFindAccounts() { + repository.save(new Account(null, "bruno", 12.3)).blockingGet(); + Observable 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()); + } + + @Test + public void givenOwner_whenFindFirstByOwner_thenFindAccount() { + repository.save(new Account(null, "bruno", 12.3)).blockingGet(); + Single accountSingle = repository.findFirstByOwner(Single.just("bruno")); + Account account = accountSingle.blockingGet(); + assertEquals("bruno", account.getOwner()); + assertEquals(Double.valueOf(12.3), account.getValue()); + assertNotNull(account.getId()); + } + +} \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java new file mode 100644 index 0000000000..373da0e393 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java @@ -0,0 +1,48 @@ +package com.baeldung.reactive.template; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import com.baeldung.reactive.model.Account; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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 java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class AccountTemplateOperationsIntegrationTest { + + @Autowired + AccountTemplateOperations accountTemplate; + + @Test + public void givenAccount_whenSave_thenSave() { + Account account = accountTemplate.save(Mono.just(new Account(null, "Raul", 12.3))).block(); + assertNotNull( account.getId() ); + } + + @Test + public void givenId_whenFindById_thenFindAccount() { + Mono accountMono = accountTemplate.save(Mono.just(new Account(null, "Raul", 12.3))); + Mono accountMonoResult = accountTemplate.findById(accountMono.block().getId()); + assertNotNull(accountMonoResult.block().getId()); + assertEquals(accountMonoResult.block().getOwner(), "Raul"); + } + + @Test + public void whenFindAll_thenFindAllAccounts() { + Account account1 = accountTemplate.save(Mono.just(new Account(null, "Raul", 12.3))).block(); + Account account2 = accountTemplate.save(Mono.just(new Account(null, "Raul Torres", 13.3))).block(); + Flux accountFlux = accountTemplate.findAll(); + List accounts = accountFlux.collectList().block(); + assertTrue(accounts.stream().anyMatch(x -> account1.getId().equals(x.getId()) )); + assertTrue(accounts.stream().anyMatch(x -> account2.getId().equals(x.getId()) )); + } + +} \ No newline at end of file