text block
This commit is contained in:
parent
2efe2c7987
commit
38d4d8f621
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue