From 2efe2c7987e399c75e52c0eb528e47a81838ed4d Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Tue, 17 Oct 2023 13:50:31 +0700 Subject: [PATCH 1/2] using json body for sample --- .../CompletableFutureTimeoutUnitTest.java | 93 ++++++++++++++++++- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java b/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java index c1de38ad37..62cf00930b 100644 --- a/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java +++ b/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java @@ -1,7 +1,9 @@ package com.baeldung.concurrent.completablefuture; +import com.fasterxml.jackson.databind.JsonNode; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; +import net.minidev.json.writer.JsonReader; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -13,6 +15,7 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.*; +import java.util.function.Supplier; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -23,8 +26,93 @@ class CompletableFutureTimeoutUnitTest { private WireMockServer wireMockServer; private ScheduledExecutorService executorService; private static final int DEFAULT_TIMEOUT = 1000; //1 seconds - private static final String DEFAULT_PRODUCT = "default_product"; - private static final String PRODUCT_OFFERS = "product_offers"; + private static final String DEFAULT_PRODUCT = "{\n" + + " \"products\": [\n" + + " {\n" + + " \"id\": 2,\n" + + " \"title\": \"iPhone X\",\n" + + " \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + + " \"price\": 899,\n" + + " \"discountPercentage\": 0.0,\n" + + " \"rating\": 4.44,\n" + + " \"stock\": 34,\n" + + " \"brand\": \"Apple\",\n" + + " \"category\": \"smartphones\"\n" + + " },\n" + + " {\n" + + " \"id\": 3,\n" + + " \"title\": \"Samsung Universe 9\",\n" + + " \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + + " \"price\": 1249,\n" + + " \"discountPercentage\": 0.0,\n" + + " \"rating\": 4.09,\n" + + " \"stock\": 36,\n" + + " \"brand\": \"Samsung\",\n" + + " \"category\": \"smartphones\"\n" + + " }\n" + + " ],\n" + + " \"total\": 2\n" + + "}"; + private static final String PRODUCT_OFFERS = "{\n" + + " \"products\": [\n" + + " {\n" + + " \"id\": 1,\n" + + " \"title\": \"iPhone 9\",\n" + + " \"description\": \"An apple mobile which is nothing like apple\",\n" + + " \"price\": 549,\n" + + " \"discountPercentage\": 12.96,\n" + + " \"rating\": 4.69,\n" + + " \"stock\": 94,\n" + + " \"brand\": \"Apple\",\n" + + " \"category\": \"smartphones\"\n" + + " },\n" + + " {\n" + + " \"id\": 2,\n" + + " \"title\": \"iPhone X\",\n" + + " \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + + " \"price\": 899,\n" + + " \"discountPercentage\": 17.94,\n" + + " \"rating\": 4.44,\n" + + " \"stock\": 34,\n" + + " \"brand\": \"Apple\",\n" + + " \"category\": \"smartphones\"\n" + + " },\n" + + " {\n" + + " \"id\": 3,\n" + + " \"title\": \"Samsung Universe 9\",\n" + + " \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + + " \"price\": 1249,\n" + + " \"discountPercentage\": 15.46,\n" + + " \"rating\": 4.09,\n" + + " \"stock\": 36,\n" + + " \"brand\": \"Samsung\",\n" + + " \"category\": \"smartphones\"\n" + + " },\n" + + " {\n" + + " \"id\": 4,\n" + + " \"title\": \"OPPOF19\",\n" + + " \"description\": \"OPPO F19 is officially announced on April 2021.\",\n" + + " \"price\": 280,\n" + + " \"discountPercentage\": 17.91,\n" + + " \"rating\": 4.3,\n" + + " \"stock\": 123,\n" + + " \"brand\": \"OPPO\",\n" + + " \"category\": \"smartphones\"\n" + + " },\n" + + " {\n" + + " \"id\": 5,\n" + + " \"title\": \"Huawei P30\",\n" + + " \"description\": \"Huawei’s re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.\",\n" + + " \"price\": 499,\n" + + " \"discountPercentage\": 10.58,\n" + + " \"rating\": 4.09,\n" + + " \"stock\": 32,\n" + + " \"brand\": \"Huawei\",\n" + + " \"category\": \"smartphones\"\n" + + " }\n" + + " ],\n" + + " \"total\": 5\n" + + "}"; @BeforeAll void setUp() { @@ -40,7 +128,6 @@ class CompletableFutureTimeoutUnitTest { executorService = Executors.newScheduledThreadPool(1); } - @AfterAll void tearDown() { executorService.shutdown(); From 38d4d8f621f8fc91f72979d0ad5790b1e585a654 Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Tue, 17 Oct 2023 18:24:02 +0700 Subject: [PATCH 2/2] text block --- .../CompletableFutureTimeoutUnitTest.java | 181 +++++++++--------- 1 file changed, 90 insertions(+), 91 deletions(-) diff --git a/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java b/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java index 62cf00930b..bb538f2584 100644 --- a/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java +++ b/core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureTimeoutUnitTest.java @@ -1,9 +1,7 @@ package com.baeldung.concurrent.completablefuture; -import com.fasterxml.jackson.databind.JsonNode; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; -import net.minidev.json.writer.JsonReader; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -15,7 +13,6 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.*; -import java.util.function.Supplier; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -26,100 +23,102 @@ class CompletableFutureTimeoutUnitTest { private WireMockServer wireMockServer; private ScheduledExecutorService executorService; private static final int DEFAULT_TIMEOUT = 1000; //1 seconds - private static final String DEFAULT_PRODUCT = "{\n" + - " \"products\": [\n" + - " {\n" + - " \"id\": 2,\n" + - " \"title\": \"iPhone X\",\n" + - " \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + - " \"price\": 899,\n" + - " \"discountPercentage\": 0.0,\n" + - " \"rating\": 4.44,\n" + - " \"stock\": 34,\n" + - " \"brand\": \"Apple\",\n" + - " \"category\": \"smartphones\"\n" + - " },\n" + - " {\n" + - " \"id\": 3,\n" + - " \"title\": \"Samsung Universe 9\",\n" + - " \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + - " \"price\": 1249,\n" + - " \"discountPercentage\": 0.0,\n" + - " \"rating\": 4.09,\n" + - " \"stock\": 36,\n" + - " \"brand\": \"Samsung\",\n" + - " \"category\": \"smartphones\"\n" + - " }\n" + - " ],\n" + - " \"total\": 2\n" + - "}"; - private static final String PRODUCT_OFFERS = "{\n" + - " \"products\": [\n" + - " {\n" + - " \"id\": 1,\n" + - " \"title\": \"iPhone 9\",\n" + - " \"description\": \"An apple mobile which is nothing like apple\",\n" + - " \"price\": 549,\n" + - " \"discountPercentage\": 12.96,\n" + - " \"rating\": 4.69,\n" + - " \"stock\": 94,\n" + - " \"brand\": \"Apple\",\n" + - " \"category\": \"smartphones\"\n" + - " },\n" + - " {\n" + - " \"id\": 2,\n" + - " \"title\": \"iPhone X\",\n" + - " \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + - " \"price\": 899,\n" + - " \"discountPercentage\": 17.94,\n" + - " \"rating\": 4.44,\n" + - " \"stock\": 34,\n" + - " \"brand\": \"Apple\",\n" + - " \"category\": \"smartphones\"\n" + - " },\n" + - " {\n" + - " \"id\": 3,\n" + - " \"title\": \"Samsung Universe 9\",\n" + - " \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + - " \"price\": 1249,\n" + - " \"discountPercentage\": 15.46,\n" + - " \"rating\": 4.09,\n" + - " \"stock\": 36,\n" + - " \"brand\": \"Samsung\",\n" + - " \"category\": \"smartphones\"\n" + - " },\n" + - " {\n" + - " \"id\": 4,\n" + - " \"title\": \"OPPOF19\",\n" + - " \"description\": \"OPPO F19 is officially announced on April 2021.\",\n" + - " \"price\": 280,\n" + - " \"discountPercentage\": 17.91,\n" + - " \"rating\": 4.3,\n" + - " \"stock\": 123,\n" + - " \"brand\": \"OPPO\",\n" + - " \"category\": \"smartphones\"\n" + - " },\n" + - " {\n" + - " \"id\": 5,\n" + - " \"title\": \"Huawei P30\",\n" + - " \"description\": \"Huawei’s re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.\",\n" + - " \"price\": 499,\n" + - " \"discountPercentage\": 10.58,\n" + - " \"rating\": 4.09,\n" + - " \"stock\": 32,\n" + - " \"brand\": \"Huawei\",\n" + - " \"category\": \"smartphones\"\n" + - " }\n" + - " ],\n" + - " \"total\": 5\n" + - "}"; + private static final String DEFAULT_PRODUCT = """ + { + "products": [ + { + "id": 2, + "title": "iPhone X", + "description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...", + "price": 899, + "discountPercentage": 0.0, + "rating": 4.44, + "stock": 34, + "brand": "Apple", + "category": "smartphones" + }, + { + "id": 3, + "title": "Samsung Universe 9", + "description": "Samsung's new variant which goes beyond Galaxy to the Universe", + "price": 1249, + "discountPercentage": 0.0, + "rating": 4.09, + "stock": 36, + "brand": "Samsung", + "category": "smartphones" + } + ], + "total": 2 + }"""; + private static final String PRODUCT_OFFERS = """ + { + "products": [ + { + "id": 1, + "title": "iPhone 9", + "description": "An apple mobile which is nothing like apple", + "price": 549, + "discountPercentage": 12.96, + "rating": 4.69, + "stock": 94, + "brand": "Apple", + "category": "smartphones" + }, + { + "id": 2, + "title": "iPhone X", + "description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...", + "price": 899, + "discountPercentage": 17.94, + "rating": 4.44, + "stock": 34, + "brand": "Apple", + "category": "smartphones" + }, + { + "id": 3, + "title": "Samsung Universe 9", + "description": "Samsung's new variant which goes beyond Galaxy to the Universe", + "price": 1249, + "discountPercentage": 15.46, + "rating": 4.09, + "stock": 36, + "brand": "Samsung", + "category": "smartphones" + }, + { + "id": 4, + "title": "OPPOF19", + "description": "OPPO F19 is officially announced on April 2021.", + "price": 280, + "discountPercentage": 17.91, + "rating": 4.3, + "stock": 123, + "brand": "OPPO", + "category": "smartphones" + }, + { + "id": 5, + "title": "Huawei P30", + "description": "Huawei’s re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.", + "price": 499, + "discountPercentage": 10.58, + "rating": 4.09, + "stock": 32, + "brand": "Huawei", + "category": "smartphones" + } + ], + "total": 5 + }"""; @BeforeAll void setUp() { wireMockServer = new WireMockServer(8080); wireMockServer.start(); WireMock.configureFor("localhost", 8080); - + System.out.println(PRODUCT_OFFERS); stubFor(get(urlEqualTo("/api/dummy")) .willReturn(aResponse() .withFixedDelay(5000) // must be > DEFAULT_TIMEOUT for a timeout to occur.