Skip to content

Commit fb04fc4

Browse files
pivovaritpedja4
authored andcommitted
BAEL-1325 (eugenp#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
1 parent 73935e6 commit fb04fc4

14 files changed

Lines changed: 322 additions & 21 deletions

‎spring-5-reactive/pom.xml‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
</parent>
1919

2020
<dependencies>
21-
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-data-jpa</artifactId>
24-
</dependency>
2521
<dependency>
2622
<groupId>org.springframework.boot</groupId>
2723
<artifactId>spring-boot-starter-validation</artifactId>
@@ -66,7 +62,6 @@
6662
<artifactId>lombok</artifactId>
6763
<scope>compile</scope>
6864
</dependency>
69-
7065
<dependency>
7166
<groupId>org.apache.geronimo.specs</groupId>
7267
<artifactId>geronimo-json_1.1_spec</artifactId>
@@ -89,12 +84,6 @@
8984
<artifactId>spring-boot-devtools</artifactId>
9085
<scope>runtime</scope>
9186
</dependency>
92-
<dependency>
93-
<groupId>com.h2database</groupId>
94-
<artifactId>h2</artifactId>
95-
<scope>runtime</scope>
96-
</dependency>
97-
9887
<dependency>
9988
<groupId>org.springframework</groupId>
10089
<artifactId>spring-test</artifactId>
@@ -133,14 +122,24 @@
133122
<version>${junit.platform.version}</version>
134123
<scope>test</scope>
135124
</dependency>
125+
<dependency>
126+
<groupId>org.apache.commons</groupId>
127+
<artifactId>commons-lang3</artifactId>
128+
</dependency>
136129

137130
<dependency>
138-
<groupId>org.projectlombok</groupId>
139-
<artifactId>lombok</artifactId>
131+
<groupId>org.springframework.boot</groupId>
132+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
140133
</dependency>
141134
<dependency>
142-
<groupId>org.apache.commons</groupId>
143-
<artifactId>commons-lang3</artifactId>
135+
<groupId>de.flapdoodle.embed</groupId>
136+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>io.reactivex.rxjava2</groupId>
141+
<artifactId>rxjava</artifactId>
142+
<version>${rxjava-version}</version>
144143
</dependency>
145144

146145
</dependencies>
@@ -178,9 +177,10 @@
178177
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
179178
<java.version>1.8</java.version>
180179
<junit.platform.version>1.0.0</junit.platform.version>
181-
<junit.jupiter.version>5.0.0</junit.jupiter.version>
180+
<junit.jupiter.version>5.0.2</junit.jupiter.version>
182181
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
183182
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
183+
<rxjava-version>2.1.12</rxjava-version>
184184
<johnzon.version>1.1.3</johnzon.version>
185185
<jsonb-api.version>1.0</jsonb-api.version>
186186
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package com.baeldung.reactive;
22

3+
import com.mongodb.reactivestreams.client.MongoClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
59

610
@SpringBootApplication
7-
public class Spring5ReactiveApplication {
11+
public class Spring5ReactiveApplication{
812

913
public static void main(String[] args) {
1014
SpringApplication.run(Spring5ReactiveApplication.class, args);
1115
}
1216

17+
@Autowired
18+
MongoClient mongoClient;
19+
20+
@Bean
21+
public ReactiveMongoTemplate reactiveMongoTemplate() {
22+
return new ReactiveMongoTemplate(mongoClient, "test");
23+
}
24+
1325
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.reactive.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
import org.springframework.data.annotation.Id;
8+
import org.springframework.data.mongodb.core.mapping.Document;
9+
10+
@Document
11+
@Data
12+
@ToString
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
public class Account {
16+
17+
@Id
18+
private String id;
19+
private String owner;
20+
private Double value;
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.reactive.repository;
2+
3+
import com.baeldung.reactive.model.Account;
4+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
import reactor.core.publisher.Flux;
7+
import reactor.core.publisher.Mono;
8+
9+
@Repository
10+
public interface AccountCrudRepository extends ReactiveCrudRepository<Account, String> {
11+
12+
public Flux<Account> findAllByValue(Double value);
13+
14+
public Mono<Account> findFirstByOwner(Mono<String> owner);
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.reactive.repository;
2+
3+
import com.baeldung.reactive.model.Account;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
6+
public interface AccountMongoRepository extends ReactiveMongoRepository<Account, String> {
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.reactive.repository;
2+
3+
import com.baeldung.reactive.model.Account;
4+
import io.reactivex.Observable;
5+
import io.reactivex.Single;
6+
import org.springframework.data.repository.reactive.RxJava2CrudRepository;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Repository
10+
public interface AccountRxJavaRepository extends RxJava2CrudRepository<Account, String>{
11+
12+
public Observable<Account> findAllByValue(Double value);
13+
14+
public Single<Account> findFirstByOwner(Single<String> owner);
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.reactive.template;
2+
3+
import com.baeldung.reactive.model.Account;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
6+
import org.springframework.data.mongodb.core.ReactiveRemoveOperation;
7+
import org.springframework.stereotype.Service;
8+
import reactor.core.publisher.Flux;
9+
import reactor.core.publisher.Mono;
10+
11+
@Service
12+
public class AccountTemplateOperations {
13+
14+
@Autowired
15+
ReactiveMongoTemplate template;
16+
17+
public Mono<Account> findById(String id) {
18+
return template.findById(id, Account.class);
19+
}
20+
21+
public Flux<Account> findAll() {
22+
return template.findAll(Account.class);
23+
}
24+
25+
public Mono<Account> save(Mono<Account> account) {
26+
return template.save(account);
27+
}
28+
29+
public ReactiveRemoveOperation.ReactiveRemove<Account> deleteAll() {
30+
return template.remove(Account.class);
31+
}
32+
33+
}

‎spring-5-reactive/src/test/java/com/baeldung/reactive/FluxUnitTest.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
public class FluxUnitTest {
1616

17+
public static final Random RANDOM = new Random();
18+
1719
@Test
1820
public void whenFluxIsConstructed_thenCorrect() {
1921
final Flux<Foo> flux = Flux.<Foo> create(fluxSink -> {
20-
while (true) {
21-
fluxSink.next(new Foo(new Random().nextLong(), randomAlphabetic(6)));
22+
for (int i = 0 ; i < 100 ; i++) {
23+
fluxSink.next(new Foo(RANDOM.nextLong(), randomAlphabetic(6)));
2224
}
2325
}).sample(Duration.ofSeconds(1)).log();
2426

spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerTest.java renamed to spring-5-reactive/src/test/java/com/baeldung/reactive/filters/PlayerHandlerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@RunWith(SpringRunner.class)
1414
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
15-
public class PlayerHandlerTest {
15+
public class PlayerHandlerIntegrationTest {
1616

1717
@Autowired
1818
private WebTestClient webTestClient;

spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerTest.java renamed to spring-5-reactive/src/test/java/com/baeldung/reactive/filters/UserControllerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@RunWith(SpringRunner.class)
1414
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
15-
public class UserControllerTest {
15+
public class UserControllerIntegrationTest {
1616

1717
@Autowired
1818
private WebTestClient webTestClient;

0 commit comments

Comments
 (0)