From dc6115b8350878821443865bdd5e40b879842604 Mon Sep 17 00:00:00 2001 From: pz2 Date: Sat, 13 Jul 2024 13:57:00 +0200 Subject: [PATCH 1/2] Added `SimpleHofundHttpConnection` to simplified configuration of HTTP connections and added ability to override `getRequestMethod()` in `AbstractHofundBasicHttpConnection` to allow using `POST` method. --- README.md | 32 ++++- .../000001-simple_http_connections.yml | 9 ++ hofund-core/pom.xml | 7 ++ .../AbstractHofundBasicHttpConnection.java | 5 + .../hofund/connection/RequestMethod.java | 20 +++ .../SimpleHofundHttpConnection.java | 57 +++++++++ ...AbstractHofundBasicHttpConnectionTest.java | 117 ++++++++++++++++++ .../HofundConnectionAutoConfiguration.java | 1 - 8 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 changelog/unreleased/000001-simple_http_connections.yml create mode 100644 hofund-core/src/main/java/dev/logchange/hofund/connection/RequestMethod.java create mode 100644 hofund-core/src/main/java/dev/logchange/hofund/connection/SimpleHofundHttpConnection.java create mode 100644 hofund-core/src/test/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnectionTest.java diff --git a/README.md b/README.md index eb30ca6..f7f4d6b 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,31 @@ hofund_git_info{branch="master",build_host="DESKTOP-AAAAA",build_time="2023-02-1 ### 4. Testing connections You can define your own `HofundConnectionsProvider` but if you want to test HTTP connection the easiest way -is to extend `AbstractHofundBasicHttpConnection`. If your project is based on spring you can extend it like below: +is to configure `SimpleHofundHttpConnection` or extend `AbstractHofundBasicHttpConnection`. If your project is based on spring you can extend it like below: + +```java +package dev.logchange.hofund.testapp.stats.health; +import dev.logchange.hofund.connection.SimpleHofundHttpConnection; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Bean; + +@Configuration +public class SimpleHofundHttpConnectionConfiguration { + + @Bean + public SimpleHofundHttpConnection paymentApiSimpleHofundHttpConnection(){ + return new SimpleHofundHttpConnection("payment-api", "http://host.docker.internal:18083/actuator/health/info"); + } + + @Bean + public SimpleHofundHttpConnection cartApiSimpleHofundHttpConnection(){ + return new SimpleHofundHttpConnection("car-api", "http://host.docker.internal:18084/actuator/health/info"); + } +} + + +``` ```java @@ -205,7 +229,7 @@ public class PaymentsHealthCheck extends AbstractHofundBasicHttpConnection { @Value("${hofund.connection.payment.url:http://host.docker.internal:18083/}") private String basicUrl; - @Value("${hofund.connection.payment.url.suffix:actuator/health}") + @Value("${hofund.connection.payment.url.suffix:actuator/health/info}") private String urlSuffix; @Override @@ -225,6 +249,10 @@ public class PaymentsHealthCheck extends AbstractHofundBasicHttpConnection { Extending `AbstractHofundBasicHttpConnection` is really simple, you only have to overrider `getTarget()` and `getUrl()` methods. The example above allows you to change values through spring application properties. +If you want to use f.e `POST` method, you can use `new SimpleHofundHttpConnection("abc", "some_url", RequestMethod.POST)` or override `getRquestMethod()`. + +If you don't want to test connection in some conditions, you can use `new SimpleHofundHttpConnection("abc", "some_url", CheckingStatus.INACTIVE)` or override `getCheckingStatus()`. + ### 5. Metrics description - `hofund_info` - used to detect if application is running and what version is used. Application name and id diff --git a/changelog/unreleased/000001-simple_http_connections.yml b/changelog/unreleased/000001-simple_http_connections.yml new file mode 100644 index 0000000..c8df898 --- /dev/null +++ b/changelog/unreleased/000001-simple_http_connections.yml @@ -0,0 +1,9 @@ +title: Added `SimpleHofundHttpConnection` to simplified configuration of HTTP connections and added ability to override + `getRequestMethod()` in `AbstractHofundBasicHttpConnection` to allow using `POST` method. +authors: + - name: Peter Zmilczak + nick: marwin1991 + url: https://github.com/marwin1991 +type: added #[added/changed/deprecated/removed/fixed/security/other] +merge_requests: + - 33 \ No newline at end of file diff --git a/hofund-core/pom.xml b/hofund-core/pom.xml index 435bf7b..08083cd 100644 --- a/hofund-core/pom.xml +++ b/hofund-core/pom.xml @@ -30,5 +30,12 @@ org.slf4j slf4j-api + + + com.squareup.okhttp3 + mockwebserver + 4.12.0 + test + diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnection.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnection.java index ed78989..5bb2b93 100644 --- a/hofund-core/src/main/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnection.java +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnection.java @@ -38,6 +38,10 @@ protected int getReadTimeout() { return 1000; } + protected RequestMethod getRequestMethod() { + return RequestMethod.GET; + } + /** * If your connection can be disabled f.e. by parameter or should be * active between 9am to 5pm you can override this method and implement it as you wish. @@ -85,6 +89,7 @@ private StatusFunction testConnection() { HttpURLConnection urlConn = (HttpURLConnection) getURL().openConnection(); urlConn.setConnectTimeout(getConnectTimeout()); urlConn.setReadTimeout(getReadTimeout()); + urlConn.setRequestMethod(getRequestMethod().getName()); urlConn.connect(); int responseCode = urlConn.getResponseCode(); diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/RequestMethod.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/RequestMethod.java new file mode 100644 index 0000000..9b8a450 --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/RequestMethod.java @@ -0,0 +1,20 @@ +package dev.logchange.hofund.connection; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum RequestMethod { + + GET("GET"), + POST("POST"), + PATCH("PATH"), + HEAD("HEAD"), + OPTIONS("OPTIONS"), + PUT("PUT"), + DELETE("DELETE"), + TRACE("TRACE"); + + private final String name; +} diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/SimpleHofundHttpConnection.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/SimpleHofundHttpConnection.java new file mode 100644 index 0000000..15514d3 --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/SimpleHofundHttpConnection.java @@ -0,0 +1,57 @@ +package dev.logchange.hofund.connection; + +public class SimpleHofundHttpConnection extends AbstractHofundBasicHttpConnection { + + private final String target; + private final String url; + private final CheckingStatus checkingStatus; + private final RequestMethod requestMethod; + + public SimpleHofundHttpConnection(String target, String url) { + this.target = target; + this.url = url; + this.checkingStatus = CheckingStatus.ACTIVE; + this.requestMethod = RequestMethod.GET; + } + + public SimpleHofundHttpConnection(String target, String url, CheckingStatus checkingStatus) { + this.target = target; + this.url = url; + this.checkingStatus = checkingStatus; + this.requestMethod = RequestMethod.GET; + } + + public SimpleHofundHttpConnection(String target, String url, RequestMethod requestMethod) { + this.target = target; + this.url = url; + this.checkingStatus = CheckingStatus.ACTIVE; + this.requestMethod = requestMethod; + } + + public SimpleHofundHttpConnection(String target, String url, CheckingStatus checkingStatus, RequestMethod requestMethod) { + this.target = target; + this.url = url; + this.checkingStatus = checkingStatus; + this.requestMethod = requestMethod; + } + + @Override + protected String getTarget() { + return target; + } + + @Override + protected String getUrl() { + return url; + } + + @Override + protected RequestMethod getRequestMethod() { + return requestMethod; + } + + @Override + protected CheckingStatus getCheckingStatus() { + return checkingStatus; + } +} diff --git a/hofund-core/src/test/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnectionTest.java b/hofund-core/src/test/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnectionTest.java new file mode 100644 index 0000000..c7bbef1 --- /dev/null +++ b/hofund-core/src/test/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnectionTest.java @@ -0,0 +1,117 @@ +package dev.logchange.hofund.connection; + +import lombok.RequiredArgsConstructor; +import okhttp3.HttpUrl; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AbstractHofundBasicHttpConnectionTest { + + /** + * AbstractHofundBasicHttpConnection is an abstract class so to test it + * we need to create below fake class. + */ + @RequiredArgsConstructor + private static class TestableAbstractHofundBasicHttpConnection extends AbstractHofundBasicHttpConnection { + + private final String target; + private final String url; + private final CheckingStatus checkingStatus; + private final RequestMethod requestMethod; + + @Override + protected String getTarget() { + return target; + } + + @Override + protected String getUrl() { + return url; + } + + @Override + protected CheckingStatus getCheckingStatus() { + return checkingStatus; + } + + @Override + protected RequestMethod getRequestMethod() { + return requestMethod; + } + } + + @Test + void testGetMethod() { + try (MockWebServer server = new MockWebServer()) { + + // given: + server.enqueue(new MockResponse() + .setBody("hello, world!") + ); + + HttpUrl url = server.url("/api/some/"); + + TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.GET); + + HofundConnection hofundConnection = connection.toHofundConnection(); + + // when: + Status status = hofundConnection.getFun().get().getStatus(); + + // then: + RecordedRequest request = server.takeRequest(); + assertEquals(Status.UP, status); + assertEquals(request.getMethod(), "GET"); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + @Test + void testPostMethod() { + try (MockWebServer server = new MockWebServer()) { + + // given: + server.enqueue(new MockResponse() + .setBody("hello, world!") + ); + + HttpUrl url = server.url("/api/some/"); + + TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.POST); + + HofundConnection hofundConnection = connection.toHofundConnection(); + + // when: + Status status = hofundConnection.getFun().get().getStatus(); + + // then: + RecordedRequest request = server.takeRequest(); + assertEquals(Status.UP, status); + assertEquals(request.getMethod(), "POST"); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + @Test + void testCheckingStatusInactive() { + // given: + TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", "https://a.b.c.d/", CheckingStatus.INACTIVE, RequestMethod.GET); + + HofundConnection hofundConnection = connection.toHofundConnection(); + + // when: + Status status = hofundConnection.getFun().get().getStatus(); + + // then: + assertEquals(Status.INACTIVE, status); + } + +} \ No newline at end of file diff --git a/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/HofundConnectionAutoConfiguration.java b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/HofundConnectionAutoConfiguration.java index b032bdf..38c94ea 100644 --- a/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/HofundConnectionAutoConfiguration.java +++ b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/HofundConnectionAutoConfiguration.java @@ -11,7 +11,6 @@ import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; From a0b62108cd654c3748839e90d584fd7018180853 Mon Sep 17 00:00:00 2001 From: pz2 Date: Sat, 13 Jul 2024 14:00:54 +0200 Subject: [PATCH 2/2] Added `SimpleHofundHttpConnection` to simplified configuration of HTTP connections and added ability to override `getRequestMethod()` in `AbstractHofundBasicHttpConnection` to allow using `POST` method. --- changelog/unreleased/000001-simple_http_connections.yml | 2 +- hofund-core/pom.xml | 2 +- pom.xml | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog/unreleased/000001-simple_http_connections.yml b/changelog/unreleased/000001-simple_http_connections.yml index c8df898..89caabe 100644 --- a/changelog/unreleased/000001-simple_http_connections.yml +++ b/changelog/unreleased/000001-simple_http_connections.yml @@ -6,4 +6,4 @@ authors: url: https://github.com/marwin1991 type: added #[added/changed/deprecated/removed/fixed/security/other] merge_requests: - - 33 \ No newline at end of file + - 42 \ No newline at end of file diff --git a/hofund-core/pom.xml b/hofund-core/pom.xml index 08083cd..8a80407 100644 --- a/hofund-core/pom.xml +++ b/hofund-core/pom.xml @@ -34,7 +34,7 @@ com.squareup.okhttp3 mockwebserver - 4.12.0 + ${mockwebserver.version} test diff --git a/pom.xml b/pom.xml index 8593414..1e77a16 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,9 @@ UTF-8 3.3.1 + + + 4.12.0