diff --git a/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/ClientOrchestration.java b/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/ClientOrchestration.java index 5c145ca5d9..6e184876cb 100644 --- a/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/ClientOrchestration.java +++ b/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/ClientOrchestration.java @@ -2,6 +2,7 @@ package com.baeldung.samples.jerseyrx; import io.reactivex.Flowable; import io.reactivex.disposables.Disposable; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; @@ -31,6 +32,7 @@ public class ClientOrchestration { WebTarget userIdService = client.target("http://localhost:8080/serviceA/id"); WebTarget nameService = client.target("http://localhost:8080/serviceA/{empId}/name"); WebTarget hashService = client.target("http://localhost:8080/serviceA/{comboIDandName}/hash"); + LinkedList failures = new LinkedList<>(); Logger logger = Logger.getLogger("ClientOrchestrator"); @@ -72,6 +74,7 @@ public class ClientOrchestration { @Override public void failed(Throwable throwable) { completionTracker.countDown(); + failures.add(throwable); logger.log(Level.WARNING, "[InvocationCallback] An error has occurred in the hashing request step {0}", throwable.getMessage()); } }); @@ -80,6 +83,7 @@ public class ClientOrchestration { @Override public void failed(Throwable throwable) { completionTracker.countDown(); + failures.add(throwable); logger.log(Level.WARNING, "[InvocationCallback] An error has occurred in the username request step {0}", throwable.getMessage()); } }); @@ -90,6 +94,7 @@ public class ClientOrchestration { logger.warning("[InvocationCallback] Some requests didn't complete within the timeout"); } } catch (InterruptedException ex) { + failures.add(ex); Logger.getLogger(ClientOrchestration.class.getName()).log(Level.SEVERE, null, ex); } @@ -97,6 +102,7 @@ public class ClientOrchestration { @Override public void failed(Throwable throwable) { + failures.add(throwable); logger.warning("Couldn't get the list of IDs"); } }); @@ -108,7 +114,8 @@ public class ClientOrchestration { .rx() .get(new GenericType() { }) - .exceptionally((Throwable throwable) -> { + .exceptionally((throwable) -> { + failures.add(throwable); logger.warning("[CompletionStage] An error has occurred"); return null; }); @@ -129,7 +136,8 @@ public class ClientOrchestration { .get(String.class) .toCompletableFuture() .thenAcceptAsync(hashValue -> logger.log(Level.INFO, "[CompletionFuture] The hash output {0}", hashValue)) - .exceptionally((Throwable throwable) -> { + .exceptionally((throwable) -> { + failures.add(throwable); logger.log(Level.WARNING, "[CompletionStage] Hash computation failed for {0}", id); return null; }); @@ -159,7 +167,10 @@ public class ClientOrchestration { .rx(RxObservableInvoker.class) .get(String.class) .asObservable() //gotten the name for the given empId - .doOnError(throwable -> logger.log(Level.WARNING, " [Observable] An error has occurred in the username request step {0}", throwable.getMessage())) + .doOnError((throwable) -> { + failures.add(throwable); + logger.log(Level.WARNING, " [Observable] An error has occurred in the username request step {0}", throwable.getMessage()); + }) .subscribe(userName -> hashService .register(RxObservableInvokerProvider.class) .resolveTemplate("comboIDandName", userName + id) @@ -167,13 +178,15 @@ public class ClientOrchestration { .rx(RxObservableInvoker.class) .get(String.class) .asObservable() //gotten the hash value for empId+username - .doOnError(throwable -> logger.log(Level.WARNING, " [Observable]An error has occurred in the hashing request step {0}", throwable.getMessage())) + .doOnError((throwable) -> { + failures.add(throwable); + logger.log(Level.WARNING, " [Observable]An error has occurred in the hashing request step {0}", throwable.getMessage()); + }) .subscribe(hashValue -> logger.log(Level.INFO, "[Observable] The hash output {0}", hashValue)))); }); } - public void flowableJavaOrchestrate() { logger.info("Orchestrating with Flowable"); @@ -191,13 +204,19 @@ public class ClientOrchestration { .request() .rx(RxFlowableInvoker.class) .get(String.class) //gotten the name for the given empId - .doOnError(throwable -> logger.log(Level.WARNING, "[Flowable] An error has occurred in the username request step {0}", throwable.getMessage())) + .doOnError((throwable) -> { + failures.add(throwable); + logger.log(Level.WARNING, "[Flowable] An error has occurred in the username request step {0}", throwable.getMessage()); + }) .subscribe(userName -> hashService.register(RxFlowableInvokerProvider.class) .resolveTemplate("comboIDandName", userName + id) .request() .rx(RxFlowableInvoker.class) .get(String.class) //gotten the hash value for empId+username - .doOnError(throwable -> logger.warning(" [Flowable] An error has occurred in the hashing request step " + throwable.getMessage())) + .doOnError((throwable) -> { + failures.add(throwable); + logger.warning(" [Flowable] An error has occurred in the hashing request step " + throwable.getMessage()); + }) .subscribe(hashValue -> logger.log(Level.INFO, "[Flowable] The hash output {0}", hashValue)))); }); diff --git a/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationTest.java b/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationTest.java index 4286e192c0..2158f29a61 100644 --- a/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationTest.java +++ b/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationTest.java @@ -7,6 +7,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -16,52 +17,53 @@ import org.junit.Test; * @author baeldung */ public class ClientOrchestrationTest { - + ClientOrchestration orchestrator = new ClientOrchestration(); - + String jsonIdList = "{\"empIds\":[1,2,3,4,5,6]}"; - + String[] nameList = new String[]{"n/a", "Thor", "Hulk", "BlackWidow", "BlackPanther", "TheTick", "Hawkeye"}; - + String[] hashResultList = new String[]{"roht1", "kluh2", "WodiwKcalb3", "RehtnapKclab4", "kciteht5", "eyekwah6"}; - + WireMockServer wireMockServer = new WireMockServer(); - + @Before public void setup() { wireMockServer.start(); configureFor("localhost", 8080); stubFor(get(urlEqualTo("/serviceA/id")).willReturn(aResponse().withBody(jsonIdList).withHeader("Content-Type", "application/json"))); - + stubFor(get(urlEqualTo("/serviceA/1/name")).willReturn(aResponse().withBody(nameList[1]))); stubFor(get(urlEqualTo("/serviceA/2/name")).willReturn(aResponse().withBody(nameList[2]))); stubFor(get(urlEqualTo("/serviceA/3/name")).willReturn(aResponse().withBody(nameList[3]))); stubFor(get(urlEqualTo("/serviceA/4/name")).willReturn(aResponse().withBody(nameList[4]))); stubFor(get(urlEqualTo("/serviceA/5/name")).willReturn(aResponse().withBody(nameList[5]))); stubFor(get(urlEqualTo("/serviceA/6/name")).willReturn(aResponse().withBody(nameList[6]))); - + stubFor(get(urlEqualTo("/serviceA/Thor1/hash")).willReturn(aResponse().withBody(hashResultList[0]))); stubFor(get(urlEqualTo("/serviceA/Hulk2/hash")).willReturn(aResponse().withBody(hashResultList[1]))); stubFor(get(urlEqualTo("/serviceA/BlackWidow3/hash")).willReturn(aResponse().withBody(hashResultList[2]))); stubFor(get(urlEqualTo("/serviceA/BlackPanther4/hash")).willReturn(aResponse().withBody(hashResultList[3]))); stubFor(get(urlEqualTo("/serviceA/TheTick5/hash")).willReturn(aResponse().withBody(hashResultList[4]))); stubFor(get(urlEqualTo("/serviceA/Hawkeye6/hash")).willReturn(aResponse().withBody(hashResultList[5]))); - + } - + @Test public void hits() { - + orchestrator.callBackOrchestrate(); orchestrator.rxOrchestrate(); orchestrator.observableJavaOrchestrate(); orchestrator.flowableJavaOrchestrate(); - + + Assert.assertTrue(orchestrator.failures.isEmpty()); } - + @After public void tearDown() { - + } - + }