From 2b229332b966a006d0f2edb3b7d165a0510a08ff Mon Sep 17 00:00:00 2001 From: k0l0ssus Date: Fri, 10 Aug 2018 08:24:46 -0400 Subject: [PATCH] Add files via upload --- jersey-client-rx/pom.xml | 2 +- .../samples/jerseyrx/ClientOrchestration.java | 82 +++++++++---------- .../samples/jerseyrx/EmployeeDTO.java | 33 ++++++++ .../jerseyrx/ClientOrchestrationTest.java | 63 ++++++++------ 4 files changed, 107 insertions(+), 73 deletions(-) diff --git a/jersey-client-rx/pom.xml b/jersey-client-rx/pom.xml index fb7494fab1..4e35be31b4 100644 --- a/jersey-client-rx/pom.xml +++ b/jersey-client-rx/pom.xml @@ -40,7 +40,7 @@ org.glassfish.jersey.media jersey-media-json-jackson - 2.22 + 2.25 com.fasterxml.jackson.jaxrs 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 6e184876cb..f2687c8c8d 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 @@ -1,7 +1,6 @@ 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; @@ -27,28 +26,22 @@ import rx.Observable; * @author baeldung */ public class ClientOrchestration { - + Client client = ClientBuilder.newClient(); + 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"); - - public static void main(String[] args) { - ClientOrchestration orchestrator = new ClientOrchestration(); - - orchestrator.callBackOrchestrate(); - orchestrator.rxOrchestrate(); - orchestrator.observableJavaOrchestrate(); - orchestrator.flowableJavaOrchestrate(); - - } - + public void callBackOrchestrate() { logger.info("Orchestrating with the pyramid of doom"); - userIdService.request().accept(MediaType.APPLICATION_JSON) + userIdService.request() + .accept(MediaType.APPLICATION_JSON) .async() .get(new InvocationCallback() { @Override @@ -61,7 +54,7 @@ public class ClientOrchestration { nameService.resolveTemplate("empId", id).request() .async() .get(new InvocationCallback() { - + @Override public void completed(String response) { completionTracker.countDown(); @@ -70,7 +63,7 @@ public class ClientOrchestration { public void completed(String response) { logger.log(Level.INFO, "[InvocationCallback] The hash output {0}", response); } - + @Override public void failed(Throwable throwable) { completionTracker.countDown(); @@ -79,7 +72,7 @@ public class ClientOrchestration { } }); } - + @Override public void failed(Throwable throwable) { completionTracker.countDown(); @@ -88,7 +81,7 @@ public class ClientOrchestration { } }); }); - + try { if (!completionTracker.await(10, TimeUnit.SECONDS)) { //wait for inner requests to complete in 10 seconds logger.warning("[InvocationCallback] Some requests didn't complete within the timeout"); @@ -97,9 +90,9 @@ public class ClientOrchestration { failures.add(ex); Logger.getLogger(ClientOrchestration.class.getName()).log(Level.SEVERE, null, ex); } - + } - + @Override public void failed(Throwable throwable) { failures.add(throwable); @@ -107,7 +100,7 @@ public class ClientOrchestration { } }); } - + public void rxOrchestrate() { logger.info("Orchestrating with a CompletionStage"); CompletionStage userIdStage = userIdService.request().accept(MediaType.APPLICATION_JSON) @@ -119,7 +112,7 @@ public class ClientOrchestration { logger.warning("[CompletionStage] An error has occurred"); return null; }); - + userIdStage.thenAcceptAsync(empIdDto -> { logger.info("[CompletionStage] Got all the IDs " + empIdDto.getEmpIds()); empIdDto.getEmpIds().stream().forEach((Long id) -> { @@ -128,7 +121,7 @@ public class ClientOrchestration { .rx() .get(String.class) .toCompletableFuture(); - + completable.thenAccept((String userName) -> { hashService.resolveTemplate("comboIDandName", userName + id) .request() @@ -141,24 +134,24 @@ public class ClientOrchestration { logger.log(Level.WARNING, "[CompletionStage] Hash computation failed for {0}", id); return null; }); - + }); - + }); }); - + } - + public void observableJavaOrchestrate() { - + logger.info("Orchestrating with Observables"); - Observable userIdObservable = userIdService.register(RxObservableInvokerProvider.class).request() + Observable observableUserIdService = userIdService.register(RxObservableInvokerProvider.class).request() .accept(MediaType.APPLICATION_JSON) .rx(RxObservableInvoker.class) .get(new GenericType() { - }); - - userIdObservable.subscribe((EmployeeDTO empIdList) -> { + }).asObservable(); + + observableUserIdService.subscribe((EmployeeDTO empIdList) -> { logger.info("[Observable] Got all the IDs " + empIdList.getEmpIds()); Observable.from(empIdList.getEmpIds()).subscribe(id -> nameService.register(RxObservableInvokerProvider.class) @@ -171,8 +164,7 @@ public class ClientOrchestration { 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) + .subscribe(userName -> hashService.register(RxObservableInvokerProvider.class) .resolveTemplate("comboIDandName", userName + id) .request() .rx(RxObservableInvoker.class) @@ -184,21 +176,21 @@ public class ClientOrchestration { }) .subscribe(hashValue -> logger.log(Level.INFO, "[Observable] The hash output {0}", hashValue)))); }); - + } - + public void flowableJavaOrchestrate() { - - logger.info("Orchestrating with Flowable"); - Flowable userIdObservable = userIdService.register(RxFlowableInvokerProvider.class) + + Flowable userIdFlowable = userIdService.register(RxFlowableInvokerProvider.class) .request() .rx(RxFlowableInvoker.class) .get(new GenericType() { }); - - Disposable subscribe = userIdObservable.subscribe((EmployeeDTO dto) -> { + + userIdFlowable.subscribe((EmployeeDTO dto) -> { + logger.info("Orchestrating with Flowable"); List listOfIds = dto.getEmpIds(); - Observable.from(listOfIds).map(id + Flowable.just(listOfIds).subscribe(id -> nameService.register(RxFlowableInvokerProvider.class) .resolveTemplate("empId", id) .request() @@ -219,7 +211,7 @@ public class ClientOrchestration { }) .subscribe(hashValue -> logger.log(Level.INFO, "[Flowable] The hash output {0}", hashValue)))); }); - + } - + } diff --git a/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/EmployeeDTO.java b/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/EmployeeDTO.java index a161448bb7..3a818f979e 100644 --- a/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/EmployeeDTO.java +++ b/jersey-client-rx/src/main/java/com/baeldung/samples/jerseyrx/EmployeeDTO.java @@ -1,6 +1,7 @@ package com.baeldung.samples.jerseyrx; import java.util.List; +import java.util.Objects; /** * @@ -18,4 +19,36 @@ public class EmployeeDTO { this.empIds = empIds; } + @Override + public int hashCode() { + int hash = 5; + hash = 59 * hash + Objects.hashCode(this.empIds); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final EmployeeDTO other = (EmployeeDTO) obj; + if (!Objects.equals(this.empIds, other.empIds)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "EmployeeDTO{" + "empIds=" + empIds + '}'; + } + + + } 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 2158f29a61..6df0e1c110 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 @@ -1,15 +1,18 @@ package com.baeldung.samples.jerseyrx; -import com.github.tomakehurst.wiremock.WireMockServer; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; -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 com.github.tomakehurst.wiremock.junit.WireMockRule; +import java.util.LinkedList; +import java.util.logging.Logger; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import static junit.framework.Assert.assertTrue; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; /** @@ -17,53 +20,59 @@ import org.junit.Test; * @author baeldung */ public class ClientOrchestrationTest { - + + Client client = ClientBuilder.newClient(); + + 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"); + 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(); - + + @Rule + public WireMockRule wireMockServer = new WireMockRule(); + @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()); + + assertTrue(orchestrator.failures.isEmpty()); } - - @After - public void tearDown() { - - } - + + }