* 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
This commit is contained in:
Grzegorz Piwowarek 2018-05-21 09:37:44 +04:00 committed by Predrag Maric
parent 73935e65f2
commit fb04fc4588
14 changed files with 323 additions and 22 deletions

View File

@ -18,10 +18,6 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
@ -66,7 +62,6 @@
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
@ -89,12 +84,6 @@
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@ -133,16 +122,26 @@
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava-version}</version>
</dependency>
</dependencies>
<build>
@ -178,9 +177,10 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.jupiter.version>5.0.2</junit.jupiter.version>
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
<rxjava-version>2.1.12</rxjava-version>
<johnzon.version>1.1.3</johnzon.version>
<jsonb-api.version>1.0</jsonb-api.version>
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>

View File

@ -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");
}
}

View File

@ -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;
}

View File

@ -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<Account, String> {
public Flux<Account> findAllByValue(Double value);
public Mono<Account> findFirstByOwner(Mono<String> owner);
}

View File

@ -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<Account, String> {
}

View File

@ -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<Account, String>{
public Observable<Account> findAllByValue(Double value);
public Single<Account> findFirstByOwner(Single<String> owner);
}

View File

@ -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<Account> findById(String id) {
return template.findById(id, Account.class);
}
public Flux<Account> findAll() {
return template.findAll(Account.class);
}
public Mono<Account> save(Mono<Account> account) {
return template.save(account);
}
public ReactiveRemoveOperation.ReactiveRemove<Account> deleteAll() {
return template.remove(Account.class);
}
}

View File

@ -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<Foo> flux = Flux.<Foo> 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();

View File

@ -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;

View File

@ -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;

View File

@ -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<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());
}
@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());
}
@Test
public void givenAccount_whenSave_thenSaveAccount() {
Mono<Account> accountMono = repository.save(new Account(null, "Bill", 12.3));
assertNotNull(accountMono.block().getId());
}
}

View File

@ -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<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")));
}
@Test
public void givenAccount_whenSave_thenSave() {
Mono<Account> 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<Account> accountMono = repository.findById(inserted.getId());
assertEquals("john", accountMono.block().getOwner());
assertEquals(Double.valueOf(12.3), accountMono.block().getValue());
assertNotNull(accountMono.block().getId());
}
}

View File

@ -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<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());
}
@Test
public void givenOwner_whenFindFirstByOwner_thenFindAccount() {
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());
}
}

View File

@ -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<Account> accountMono = accountTemplate.save(Mono.just(new Account(null, "Raul", 12.3)));
Mono<Account> 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<Account> accountFlux = accountTemplate.findAll();
List<Account> accounts = accountFlux.collectList().block();
assertTrue(accounts.stream().anyMatch(x -> account1.getId().equals(x.getId()) ));
assertTrue(accounts.stream().anyMatch(x -> account2.getId().equals(x.getId()) ));
}
}