Skip to content

Commit bd0b328

Browse files
committed
complete quote service
1 parent 3509fa5 commit bd0b328

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

src/main/java/org/learning/by/example/reactive/microservices/services/QuoteServiceImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.springframework.web.reactive.function.client.WebClient;
99
import reactor.core.publisher.Mono;
1010

11-
import java.util.function.Function;
12-
1311
public class QuoteServiceImpl implements QuoteService {
1412

1513
private static final String ERROR_GETTING_QUOTE = "ERROR GETTING_QUOTE";
@@ -32,14 +30,14 @@ Mono<Quote[]> request() {
3230
.flatMap(clientResponse -> clientResponse.bodyToMono(Quote[].class));
3331
}
3432

35-
Function<Mono<Quote[]>, Mono<Quote>> chooseFirst() {
36-
return mono -> mono.flatMap(quotes -> Mono.just(quotes[0]))
33+
Mono<Quote> chooseFirst(Mono<Quote[]> monoQuotes) {
34+
return monoQuotes.flatMap(quotes -> Mono.just(quotes[0]))
3735
.onErrorResume(throwable ->Mono.error(new GetQuoteException(ERROR_GETTING_QUOTE, throwable)));
3836
}
3937

4038
@Override
4139
public Mono<Quote> get() {
42-
return Mono.defer(this::request).publish(chooseFirst())
40+
return this.request().publish(this::chooseFirst)
4341
.onErrorResume(throwable ->Mono.error(new GetQuoteException(ERROR_GETTING_QUOTE, throwable)));
4442
}
4543

src/test/java/org/learning/by/example/reactive/microservices/services/QuoteServiceImplTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.learning.by.example.reactive.microservices.exceptions.GetQuoteException;
88
import org.learning.by.example.reactive.microservices.model.Quote;
99
import org.learning.by.example.reactive.microservices.test.categories.UnitTest;
10+
import org.mockito.Mockito;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.mock.mockito.SpyBean;
1213
import org.springframework.test.context.ActiveProfiles;
@@ -16,7 +17,6 @@
1617
import static org.hamcrest.CoreMatchers.instanceOf;
1718
import static org.hamcrest.MatcherAssert.assertThat;
1819
import static org.hamcrest.Matchers.is;
19-
import static org.mockito.BDDMockito.given;
2020
import static org.mockito.Mockito.*;
2121

2222
@RunWith(SpringRunner.class)
@@ -49,7 +49,7 @@ public void mockedRequest() {
4949

5050
verify(quoteService, times(1)).get();
5151
verify(quoteService, times(1)).request();
52-
verify(quoteService, times(1)).chooseFirst();
52+
verify(quoteService, times(1)).chooseFirst(Mockito.any());
5353

5454
reset(quoteService);
5555
}
@@ -68,7 +68,6 @@ private Mono<Quote[]> createMockedResponse(final Integer ID, final String title,
6868

6969
@Test
7070
public void requestErrorShouldBeHandle() {
71-
7271
doReturn(Mono.error(new RuntimeException(BAD_EXCEPTION))).when(quoteService).request();
7372

7473
quoteService.get().subscribe(quote -> {
@@ -79,14 +78,14 @@ public void requestErrorShouldBeHandle() {
7978

8079
verify(quoteService, times(1)).get();
8180
verify(quoteService, times(1)).request();
82-
verify(quoteService, times(1)).chooseFirst();
81+
verify(quoteService, times(1)).chooseFirst(Mockito.any());
8382

8483
reset(quoteService);
8584
}
8685

8786
@Test
8887
public void chooseFirstErrorShouldBeHandle() {
89-
given(quoteService.chooseFirst()).willReturn(mono -> Mono.error(new RuntimeException(BAD_EXCEPTION)));
88+
doReturn(Mono.error(new RuntimeException(BAD_EXCEPTION))).when(quoteService).chooseFirst(Mockito.any());
9089

9190
quoteService.get().subscribe(quote -> {
9291
throw new UnsupportedOperationException(SHOULD_NOT_RETURN_OBJECT);
@@ -96,7 +95,7 @@ public void chooseFirstErrorShouldBeHandle() {
9695

9796
verify(quoteService, times(1)).get();
9897
verify(quoteService, times(1)).request();
99-
verify(quoteService, times(1)).chooseFirst();
98+
verify(quoteService, times(1)).chooseFirst(Mockito.any());
10099

101100
reset(quoteService);
102101
}

0 commit comments

Comments
 (0)