Skip to content

Commit 18bfcd9

Browse files
committed
identing and reorering imports
1 parent 8ed270c commit 18bfcd9

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/completablefuturefeignclient/PurchaseService.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.baeldung.cloud.openfeign.completablefuturefeignclient;
22

3-
import feign.FeignException;
4-
import feign.RetryableException;
5-
import org.springframework.stereotype.Service;
6-
73
import java.util.concurrent.CompletableFuture;
84
import java.util.concurrent.ExecutionException;
95
import java.util.concurrent.TimeUnit;
106
import java.util.concurrent.TimeoutException;
117

8+
import org.springframework.stereotype.Service;
9+
10+
import feign.FeignException;
11+
import feign.RetryableException;
12+
1213
@Service
1314
public class PurchaseService {
1415

@@ -22,27 +23,27 @@ public PurchaseService(PaymentMethodClient paymentMethodClient, ReportClient rep
2223

2324
public String executePurchase(String siteId) throws ExecutionException, InterruptedException {
2425
CompletableFuture<String> paymentMethodsFuture = CompletableFuture.supplyAsync(() -> paymentMethodClient.getAvailablePaymentMethods(siteId))
25-
.orTimeout(400, TimeUnit.MILLISECONDS)
26-
.exceptionally(ex -> {
27-
if (ex.getCause() instanceof FeignException && ((FeignException) ex.getCause()).status() == 404) {
28-
return "cash";
29-
}
26+
.orTimeout(400, TimeUnit.MILLISECONDS)
27+
.exceptionally(ex -> {
28+
if (ex.getCause() instanceof FeignException && ((FeignException) ex.getCause()).status() == 404) {
29+
return "cash";
30+
}
3031

31-
if (ex.getCause() instanceof RetryableException) {
32-
// handle REST timeout
33-
throw new RuntimeException("REST call network timeout!");
34-
}
32+
if (ex.getCause() instanceof RetryableException) {
33+
// handle REST timeout
34+
throw new RuntimeException("REST call network timeout!");
35+
}
3536

36-
if (ex instanceof TimeoutException) {
37-
// handle thread timeout
38-
throw new RuntimeException("Thread timeout!", ex);
39-
}
37+
if (ex instanceof TimeoutException) {
38+
// handle thread timeout
39+
throw new RuntimeException("Thread timeout!", ex);
40+
}
4041

41-
throw new RuntimeException("Unrecoverable error!", ex);
42-
});
42+
throw new RuntimeException("Unrecoverable error!", ex);
43+
});
4344

4445
CompletableFuture.runAsync(() -> reportClient.sendReport("Purchase Order Report"))
45-
.orTimeout(400, TimeUnit.MILLISECONDS);
46+
.orTimeout(400, TimeUnit.MILLISECONDS);
4647

4748
return String.format("Purchase executed with payment method %s", paymentMethodsFuture.get());
4849
}

spring-cloud-modules/spring-cloud-openfeign-2/src/test/java/com/baeldung/cloud/openfeign/completablefuturefeignclient/PurchaseServiceIntegrationTest.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baeldung.cloud.openfeign.ExampleApplication;
44
import com.github.tomakehurst.wiremock.WireMockServer;
5+
56
import org.junit.jupiter.api.AfterEach;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Disabled;
@@ -43,8 +44,7 @@ public void startWireMockServer() {
4344
configureFor("localhost", 8083);
4445
wireMockServer.start();
4546

46-
stubFor(post(urlEqualTo("/reports"))
47-
.willReturn(aResponse().withStatus(HttpStatus.OK.value())));
47+
stubFor(post(urlEqualTo("/reports")).willReturn(aResponse().withStatus(HttpStatus.OK.value())));
4848
}
4949

5050
@AfterEach
@@ -53,10 +53,9 @@ public void stopWireMockServer() {
5353
}
5454

5555
@Test
56-
void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult()
57-
throws ExecutionException, InterruptedException {
58-
stubFor(get(urlEqualTo("/payment_methods?site_id=BR"))
59-
.willReturn(aResponse().withStatus(HttpStatus.OK.value()).withBody("credit_card")));
56+
void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult() throws ExecutionException, InterruptedException {
57+
stubFor(get(urlEqualTo("/payment_methods?site_id=BR")).willReturn(aResponse().withStatus(HttpStatus.OK.value())
58+
.withBody("credit_card")));
6059

6160
String result = purchaseService.executePurchase("BR");
6261

@@ -65,10 +64,8 @@ void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult()
6564
}
6665

6766
@Test
68-
void givenRestCalls_whenPurchaseReturns404_thenReturnDefault()
69-
throws ExecutionException, InterruptedException {
70-
stubFor(get(urlEqualTo("/payment_methods?site_id=BR"))
71-
.willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value())));
67+
void givenRestCalls_whenPurchaseReturns404_thenReturnDefault() throws ExecutionException, InterruptedException {
68+
stubFor(get(urlEqualTo("/payment_methods?site_id=BR")).willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value())));
7269

7370
String result = purchaseService.executePurchase("BR");
7471

@@ -79,8 +76,7 @@ void givenRestCalls_whenPurchaseReturns404_thenReturnDefault()
7976
@Test
8077
@Disabled
8178
void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenThrowNewException() {
82-
stubFor(get(urlEqualTo("/payment_methods?site_id=BR"))
83-
.willReturn(aResponse().withFixedDelay(550)));
79+
stubFor(get(urlEqualTo("/payment_methods?site_id=BR")).willReturn(aResponse().withFixedDelay(550)));
8480

8581
Throwable error = assertThrows(ExecutionException.class, () -> purchaseService.executePurchase("BR"));
8682

@@ -89,8 +85,7 @@ void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenThrowNewException()
8985

9086
@Test
9187
void givenRestCalls_whenPurchaseRequestWebTimeout_thenThrowNewException() {
92-
stubFor(get(urlEqualTo("/payment_methods?site_id=BR"))
93-
.willReturn(aResponse().withFixedDelay(250)));
88+
stubFor(get(urlEqualTo("/payment_methods?site_id=BR")).willReturn(aResponse().withFixedDelay(250)));
9489

9590
Throwable error = assertThrows(ExecutionException.class, () -> purchaseService.executePurchase("BR"));
9691

0 commit comments

Comments
 (0)