From ca824c47d96701dc95290c9a11cffc2a6694c9a7 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 10 Apr 2022 04:12:31 +0200 Subject: [PATCH] Feature/bael 5393 closable http client (#12009) * BAEL-5393: Inital commit * BAEL-5393: Update tests * BAEL-5393: Revert comment --- httpclient-2/pom.xml | 20 ++++- .../httpclient/ApacheHttpClientUnitTest.java | 78 +++++++++++++++++++ .../httpclient/GetRequestMockServer.java | 78 +++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java diff --git a/httpclient-2/pom.xml b/httpclient-2/pom.xml index 32f2e80b18..f2da238494 100644 --- a/httpclient-2/pom.xml +++ b/httpclient-2/pom.xml @@ -55,6 +55,22 @@ spring-boot-starter-webflux ${spring-boot.version} + + org.mock-server + mockserver-netty + ${mockserver.version} + + + org.mock-server + mockserver-client-java + ${mockserver.version} + + + org.assertj + assertj-core + ${assertj.version} + test + @@ -79,8 +95,10 @@ + 3.22.0 + 5.11.2 4.5.8 - 5.1 + 5.1.3 11 11 2.1.7.RELEASE diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java new file mode 100644 index 0000000000..0d45eedc12 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java @@ -0,0 +1,78 @@ +package com.baeldung.httpclient.httpclient; + +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +class ApacheHttpClientUnitTest extends GetRequestMockServer { + + @Test + void givenDeveloperUsedHttpClient_whenExecutingGetRequest_thenStatusIsOkButSonarReportsAnIssue() throws IOException { + HttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(serviceOneUrl); + HttpResponse response = httpClient.execute(httpGet); + assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK); + } + + @Test + void givenDeveloperUsedCloseableHttpClient_whenExecutingGetRequest_thenStatusIsOk() throws IOException { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpGet httpGet = new HttpGet(serviceOneUrl); + HttpResponse response = httpClient.execute(httpGet); + assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK); + } + } + + @Test + void givenDeveloperUsedHttpClientBuilder_whenExecutingGetRequest_thenStatusIsOk() throws IOException { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet(serviceOneUrl); + HttpResponse response = httpClient.execute(httpGet); + assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK); + } + } + + @Test + void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet(serviceOneUrl); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + HttpEntity entity = response.getEntity(); + EntityUtils.consume(entity); + assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK); + } + } + } + + @Test + void givenDeveloperUsedSingleClient_whenExecutingTwoGetRequest_thenStatusIsOk() throws IOException { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGetOne = new HttpGet(serviceOneUrl); + try (CloseableHttpResponse responseOne = httpClient.execute(httpGetOne)) { + HttpEntity entityOne = responseOne.getEntity(); + EntityUtils.consume(entityOne); + assertThat(responseOne.getCode()).isEqualTo(HttpStatus.SC_OK); + } + + HttpGet httpGetTwo = new HttpGet(serviceTwoUrl); + try (CloseableHttpResponse responseTwo = httpClient.execute(httpGetTwo)) { + HttpEntity entityTwo = responseTwo.getEntity(); + EntityUtils.consume(entityTwo); + assertThat(responseTwo.getCode()).isEqualTo(HttpStatus.SC_OK); + } + } + } + +} diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java b/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java new file mode 100644 index 0000000000..2c08cfa6e4 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java @@ -0,0 +1,78 @@ +package com.baeldung.httpclient.httpclient; + +import org.apache.hc.core5.http.HttpStatus; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.mockserver.client.MockServerClient; +import org.mockserver.integration.ClientAndServer; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.URISyntaxException; + +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +public class GetRequestMockServer { + + public static ClientAndServer mockServer; + public static String serviceOneUrl; + public static String serviceTwoUrl; + + private static int serverPort; + + public static final String SERVER_ADDRESS = "127.0.0.1"; + public static final String PATH_ONE = "/test1"; + public static final String PATH_TWO = "/test2"; + public static final String METHOD = "GET"; + + @BeforeAll + static void startServer() throws IOException, URISyntaxException { + serverPort = getFreePort(); + serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE; + serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO; + mockServer = startClientAndServer(serverPort); + mockGetRequest(); + } + + @AfterAll + static void stopServer() { + mockServer.stop(); + } + + private static void mockGetRequest() { + new MockServerClient(SERVER_ADDRESS, serverPort) + .when( + request() + .withPath(PATH_ONE) + .withMethod(METHOD), + exactly(5) + ) + .respond( + response() + .withStatusCode(HttpStatus.SC_OK) + .withBody("{\"status\":\"ok\"}") + ); + new MockServerClient(SERVER_ADDRESS, serverPort) + .when( + request() + .withPath(PATH_TWO) + .withMethod(METHOD), + exactly(1) + ) + .respond( + response() + .withStatusCode(HttpStatus.SC_OK) + .withBody("{\"status\":\"ok\"}") + ); + } + + private static int getFreePort () throws IOException { + try (ServerSocket serverSocket = new ServerSocket(0)) { + return serverSocket.getLocalPort(); + } + } + +}