Skip to content

Commit

Permalink
Merge pull request #42 from logchange/simple-http-connection-config
Browse files Browse the repository at this point in the history
Added `SimpleHofundHttpConnection` to simplified configuration of HTT…
  • Loading branch information
marwin1991 authored Jul 13, 2024
2 parents 70169e2 + a0b6210 commit 17b2a75
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 3 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions changelog/unreleased/000001-simple_http_connections.yml
Original file line number Diff line number Diff line change
@@ -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:
- 42
7 changes: 7 additions & 0 deletions hofund-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${mockwebserver.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spring-boot.version>3.3.1</spring-boot.version>

<!-- TESTS -->
<mockwebserver.version>4.12.0</mockwebserver.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit 17b2a75

Please sign in to comment.