Skip to content

Commit a9e855e

Browse files
authored
Merge pull request eugenp#8121 from alimate/BAEL-3272-2
BAEL-3272: Making Wiremock to Listen to a Random Port
2 parents c75e67d + 3cf3147 commit a9e855e

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

spring-jersey/src/test/java/com/baeldung/clientrx/ClientOrchestrationIntegrationTest.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public class ClientOrchestrationIntegrationTest {
4141

4242
private Client client = ClientBuilder.newClient();
4343

44-
private WebTarget userIdService = client.target("http://localhost:8080/id-service/ids");
45-
private WebTarget nameService = client.target("http://localhost:8080/name-service/users/{userId}/name");
46-
private WebTarget hashService = client.target("http://localhost:8080/hash-service/{rawValue}");
44+
private WebTarget userIdService = client.target("http://localhost:{port}/id-service/ids");
45+
private WebTarget nameService = client.target("http://localhost:{port}/name-service/users/{userId}/name");
46+
private WebTarget hashService = client.target("http://localhost:{port}/hash-service/{rawValue}");
4747

4848
private Logger logger = LoggerFactory.getLogger(ClientOrchestrationIntegrationTest.class);
4949

@@ -54,7 +54,7 @@ public class ClientOrchestrationIntegrationTest {
5454
private List<String> expectedHashValues = Arrays.asList("roht1", "kluh2", "WodiwKcalb3", "RehtnapKclab4", "kciteht5", "eyekwah6");
5555

5656
@Rule
57-
public WireMockRule wireMockServer = new WireMockRule();
57+
public WireMockRule wireMockServer = new WireMockRule(0);
5858

5959
@Before
6060
public void setup() {
@@ -83,19 +83,19 @@ public void callBackOrchestrate() throws InterruptedException {
8383

8484
final CountDownLatch completionTracker = new CountDownLatch(expectedHashValues.size()); // used to keep track of the progress of the subsequent calls
8585

86-
userIdService.request().accept(MediaType.APPLICATION_JSON).async().get(new InvocationCallback<List<Long>>() {
86+
getUserIdService().request().accept(MediaType.APPLICATION_JSON).async().get(new InvocationCallback<List<Long>>() {
8787
@Override
8888
public void completed(List<Long> employeeIds) {
8989
logger.info("[CallbackExample] id-service result: {}", employeeIds);
9090
employeeIds.forEach((id) -> {
9191
// for each employee ID, get the name
92-
nameService.resolveTemplate("userId", id).request().async().get(new InvocationCallback<String>() {
92+
getNameService().resolveTemplate("userId", id).request().async().get(new InvocationCallback<String>() {
9393

9494
@Override
9595
public void completed(String response) {
9696
logger.info("[CallbackExample] name-service result: {}", response);
9797

98-
hashService.resolveTemplate("rawValue", response + id).request().async().get(new InvocationCallback<String>() {
98+
getHashService().resolveTemplate("rawValue", response + id).request().async().get(new InvocationCallback<String>() {
9999
@Override
100100
public void completed(String response) {
101101
logger.info("[CallbackExample] hash-service result: {}", response);
@@ -144,7 +144,7 @@ public void rxOrchestrate() throws InterruptedException {
144144

145145
final CountDownLatch completionTracker = new CountDownLatch(expectedHashValues.size()); // used to keep track of the progress of the subsequent calls
146146

147-
CompletionStage<List<Long>> userIdStage = userIdService.request().accept(MediaType.APPLICATION_JSON).rx().get(new GenericType<List<Long>>() {
147+
CompletionStage<List<Long>> userIdStage = getUserIdService().request().accept(MediaType.APPLICATION_JSON).rx().get(new GenericType<List<Long>>() {
148148
}).exceptionally((throwable) -> {
149149
logger.warn("[CompletionStageExample] An error has occurred");
150150
return null;
@@ -153,11 +153,11 @@ public void rxOrchestrate() throws InterruptedException {
153153
userIdStage.thenAcceptAsync(employeeIds -> {
154154
logger.info("[CompletionStageExample] id-service result: {}", employeeIds);
155155
employeeIds.forEach((Long id) -> {
156-
CompletableFuture<String> completable = nameService.resolveTemplate("userId", id).request().rx().get(String.class).toCompletableFuture();
156+
CompletableFuture<String> completable = getNameService().resolveTemplate("userId", id).request().rx().get(String.class).toCompletableFuture();
157157

158158
completable.thenAccept((String userName) -> {
159159
logger.info("[CompletionStageExample] name-service result: {}", userName);
160-
hashService.resolveTemplate("rawValue", userName + id).request().rx().get(String.class).toCompletableFuture().thenAcceptAsync(hashValue -> {
160+
getHashService().resolveTemplate("rawValue", userName + id).request().rx().get(String.class).toCompletableFuture().thenAcceptAsync(hashValue -> {
161161
logger.info("[CompletionStageExample] hash-service result: {}", hashValue);
162162
receivedHashValues.add(hashValue);
163163
completionTracker.countDown();
@@ -191,18 +191,18 @@ public void observableJavaOrchestrate() throws InterruptedException {
191191

192192
final CountDownLatch completionTracker = new CountDownLatch(expectedHashValues.size()); // used to keep track of the progress of the subsequent calls
193193

194-
Observable<List<Long>> observableUserIdService = userIdService.register(RxObservableInvokerProvider.class).request().accept(MediaType.APPLICATION_JSON).rx(RxObservableInvoker.class).get(new GenericType<List<Long>>() {
194+
Observable<List<Long>> observableUserIdService = getUserIdService().register(RxObservableInvokerProvider.class).request().accept(MediaType.APPLICATION_JSON).rx(RxObservableInvoker.class).get(new GenericType<List<Long>>() {
195195
}).asObservable();
196196

197197
observableUserIdService.subscribe((List<Long> employeeIds) -> {
198198
logger.info("[ObservableExample] id-service result: {}", employeeIds);
199-
Observable.from(employeeIds).subscribe(id -> nameService.register(RxObservableInvokerProvider.class).resolveTemplate("userId", id).request().rx(RxObservableInvoker.class).get(String.class).asObservable() // gotten the name for the given
199+
Observable.from(employeeIds).subscribe(id -> getNameService().register(RxObservableInvokerProvider.class).resolveTemplate("userId", id).request().rx(RxObservableInvoker.class).get(String.class).asObservable() // gotten the name for the given
200200
// userId
201201
.doOnError((throwable) -> {
202202
logger.warn("[ObservableExample] An error has occurred in the username request step {}", throwable.getMessage());
203203
}).subscribe(userName -> {
204204
logger.info("[ObservableExample] name-service result: {}", userName);
205-
hashService.register(RxObservableInvokerProvider.class).resolveTemplate("rawValue", userName + id).request().rx(RxObservableInvoker.class).get(String.class).asObservable() // gotten the hash value for
205+
getHashService().register(RxObservableInvokerProvider.class).resolveTemplate("rawValue", userName + id).request().rx(RxObservableInvoker.class).get(String.class).asObservable() // gotten the hash value for
206206
// userId+username
207207
.doOnError((throwable) -> {
208208
logger.warn("[ObservableExample] An error has occurred in the hashing request step {}", throwable.getMessage());
@@ -233,18 +233,18 @@ public void flowableJavaOrchestrate() throws InterruptedException {
233233

234234
final CountDownLatch completionTracker = new CountDownLatch(expectedHashValues.size()); // used to keep track of the progress of the subsequent calls
235235

236-
Flowable<List<Long>> userIdFlowable = userIdService.register(RxFlowableInvokerProvider.class).request().rx(RxFlowableInvoker.class).get(new GenericType<List<Long>>() {
236+
Flowable<List<Long>> userIdFlowable = getUserIdService().register(RxFlowableInvokerProvider.class).request().rx(RxFlowableInvoker.class).get(new GenericType<List<Long>>() {
237237
});
238238

239239
userIdFlowable.subscribe((List<Long> employeeIds) -> {
240240
logger.info("[FlowableExample] id-service result: {}", employeeIds);
241241
Flowable.fromIterable(employeeIds).subscribe(id -> {
242-
nameService.register(RxFlowableInvokerProvider.class).resolveTemplate("userId", id).request().rx(RxFlowableInvoker.class).get(String.class) // gotten the name for the given userId
242+
getNameService().register(RxFlowableInvokerProvider.class).resolveTemplate("userId", id).request().rx(RxFlowableInvoker.class).get(String.class) // gotten the name for the given userId
243243
.doOnError((throwable) -> {
244244
logger.warn("[FlowableExample] An error has occurred in the username request step {}", throwable.getMessage());
245245
}).subscribe(userName -> {
246246
logger.info("[FlowableExample] name-service result: {}", userName);
247-
hashService.register(RxFlowableInvokerProvider.class).resolveTemplate("rawValue", userName + id).request().rx(RxFlowableInvoker.class).get(String.class) // gotten the hash value for userId+username
247+
getHashService().register(RxFlowableInvokerProvider.class).resolveTemplate("rawValue", userName + id).request().rx(RxFlowableInvoker.class).get(String.class) // gotten the hash value for userId+username
248248
.doOnError((throwable) -> {
249249
logger.warn(" [FlowableExample] An error has occurred in the hashing request step!", throwable);
250250
}).subscribe(hashValue -> {
@@ -269,4 +269,20 @@ public void flowableJavaOrchestrate() throws InterruptedException {
269269
assertThat(receivedHashValues).containsAll(expectedHashValues);
270270
}
271271

272+
private int getPort() {
273+
return wireMockServer.port();
274+
}
275+
276+
private WebTarget getUserIdService() {
277+
return userIdService.resolveTemplate("port", getPort());
278+
}
279+
280+
private WebTarget getNameService() {
281+
return nameService.resolveTemplate("port", getPort());
282+
}
283+
284+
private WebTarget getHashService() {
285+
return hashService.resolveTemplate("port", getPort());
286+
}
287+
272288
}

0 commit comments

Comments
 (0)