Skip to content

Commit

Permalink
Add tests for each HTTP client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chavjoh committed Dec 31, 2023
1 parent 373e41a commit 33a6609
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 8 deletions.
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.16.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
Expand All @@ -58,9 +65,9 @@
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.16.1</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-bom</artifactId>
<version>5.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -145,6 +152,13 @@
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Abstract class implementing common parts for Apache HTTP.
*/
public class AbstractApacheHttpClient extends AbstractHttpClient implements AutoCloseable {
public abstract class AbstractApacheHttpClient extends AbstractHttpClient implements AutoCloseable {

protected final CloseableHttpAsyncClient client;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Abstract class implementing common parts for Java HTTP.
*/
public class AbstractJavaHttpClient extends AbstractHttpClient implements AutoCloseable {
public abstract class AbstractJavaHttpClient extends AbstractHttpClient implements AutoCloseable {

protected final HttpClient client;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Abstract class implementing common parts for OkHttp.
*/
public class AbstractOkHttpClient extends AbstractHttpClient implements AutoCloseable {
public abstract class AbstractOkHttpClient extends AbstractHttpClient implements AutoCloseable {

public static final RequestBody EMPTY_BODY = RequestBody.create(EMPTY, null);
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse(HEADER_CONTENT_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Abstract class implementing common parts for Vert.x HTTP.
*/
public class AbstractVertxHttpClient extends AbstractHttpClient implements AutoCloseable {
public abstract class AbstractVertxHttpClient extends AbstractHttpClient implements AutoCloseable {

protected final WebClient client;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.chavaillaz.client.common.apache;

import static com.chavaillaz.client.common.apache.ApacheHttpUtils.defaultHttpClientBuilder;
import static com.chavaillaz.client.common.model.UserApi.stubForUserApi;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.security.AnonymousAuthentication;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@WireMockTest
class ApacheHttpTest {

@BeforeEach
void setup() {
stubForUserApi();
}

@Test
void testApache(WireMockRuntimeInfo wiremock) throws Exception {
CloseableHttpAsyncClient client = defaultHttpClientBuilder(null).build();
try (var api = new ApacheHttpUserApi(client, wiremock.getHttpBaseUrl(), new AnonymousAuthentication())) {
User user = api.getUser().join();
assertNotNull(user);
assertEquals("WireMock", user.getUsername());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.chavaillaz.client.common.apache;

import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.get;

import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.model.UserApi;
import com.chavaillaz.client.common.security.Authentication;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;

public class ApacheHttpUserApi extends AbstractApacheHttpClient implements UserApi {

public ApacheHttpUserApi(CloseableHttpAsyncClient client, String baseUrl, Authentication authentication) {
super(client, baseUrl, authentication);
}

public CompletableFuture<User> getUser() {
return sendAsync(requestBuilder(get(), URL_USER), User.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void testExceptionMessageWithClass() {
}

@Test
void testExcepionMessageWithJavaType() {
void testExceptionMessageWithJavaType() {
JavaType type = TypeFactory.defaultInstance()
.constructSimpleType(String.class, new JavaType[]{});
Exception root = new Exception("Root");
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/chavaillaz/client/common/java/JavaHttpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.chavaillaz.client.common.java;

import static com.chavaillaz.client.common.java.JavaHttpUtils.defaultHttpClientBuilder;
import static com.chavaillaz.client.common.model.UserApi.stubForUserApi;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.net.http.HttpClient;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.security.AnonymousAuthentication;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@WireMockTest
class JavaHttpTest {

@BeforeEach
void setup() {
stubForUserApi();
}

@Test
void testJava(WireMockRuntimeInfo wiremock) {
HttpClient client = defaultHttpClientBuilder(null).build();
try (var api = new JavaHttpUserApi(client, wiremock.getHttpBaseUrl(), new AnonymousAuthentication())) {
User user = api.getUser().join();
assertNotNull(user);
assertEquals("WireMock", user.getUsername());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.chavaillaz.client.common.java;

import java.net.http.HttpClient;
import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.model.UserApi;
import com.chavaillaz.client.common.security.Authentication;

public class JavaHttpUserApi extends AbstractJavaHttpClient implements UserApi {

public JavaHttpUserApi(HttpClient client, String baseUrl, Authentication authentication) {
super(client, baseUrl, authentication);
}

@Override
public CompletableFuture<User> getUser() {
return sendAsync(requestBuilder(URL_USER).GET(), User.class);
}

}
10 changes: 10 additions & 0 deletions src/test/java/com/chavaillaz/client/common/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.chavaillaz.client.common.model;

import lombok.Data;

@Data
public class User {

private String username;

}
28 changes: 28 additions & 0 deletions src/test/java/com/chavaillaz/client/common/model/UserApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.chavaillaz.client.common.model;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.common.ContentTypes.APPLICATION_JSON;

import java.util.concurrent.CompletableFuture;

public interface UserApi extends AutoCloseable {

String URL_USER = "/user";

static void stubForUserApi() {
stubFor(get("/user").willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody("{\"username\": \"WireMock\"}")));
}

/**
* Gets the current user.
*
* @return A {@link CompletableFuture} with the user
*/
CompletableFuture<User> getUser();

}
34 changes: 34 additions & 0 deletions src/test/java/com/chavaillaz/client/common/okhttp/OkHttpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.chavaillaz.client.common.okhttp;

import static com.chavaillaz.client.common.model.UserApi.stubForUserApi;
import static com.chavaillaz.client.common.okhttp.OkHttpUtils.defaultHttpClientBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.security.AnonymousAuthentication;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@WireMockTest
class OkHttpTest {

@BeforeEach
void setup() {
stubForUserApi();
}

@Test
void testOkHttp(WireMockRuntimeInfo wiremock) throws Exception {
OkHttpClient client = defaultHttpClientBuilder(null).build();
try (var api = new OkHttpUserApi(client, wiremock.getHttpBaseUrl(), new AnonymousAuthentication())) {
User user = api.getUser().join();
assertNotNull(user);
assertEquals("WireMock", user.getUsername());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.chavaillaz.client.common.okhttp;

import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.model.UserApi;
import com.chavaillaz.client.common.security.Authentication;
import okhttp3.OkHttpClient;

public class OkHttpUserApi extends AbstractOkHttpClient implements UserApi {

public OkHttpUserApi(OkHttpClient client, String baseUrl, Authentication authentication) {
super(client, baseUrl, authentication);
}

@Override
public CompletableFuture<User> getUser() {
return sendAsync(requestBuilder(URL_USER).get(), User.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.chavaillaz.client.common.vertx;

import static com.chavaillaz.client.common.model.UserApi.stubForUserApi;
import static com.chavaillaz.client.common.vertx.VertxUtils.defaultWebClientOptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.security.AnonymousAuthentication;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@WireMockTest
class VertxHttpTest {

@BeforeEach
void setup() {
stubForUserApi();
}

@Test
void testVertx(WireMockRuntimeInfo wiremock) throws Exception {
WebClient client = WebClient.create(Vertx.vertx(), defaultWebClientOptions(null));
try (var api = new VertxHttpUserApi(client, wiremock.getHttpBaseUrl(), new AnonymousAuthentication())) {
User user = api.getUser().join();
assertNotNull(user);
assertEquals("WireMock", user.getUsername());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.chavaillaz.client.common.vertx;

import static io.vertx.core.http.HttpMethod.GET;

import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.common.model.User;
import com.chavaillaz.client.common.model.UserApi;
import com.chavaillaz.client.common.security.Authentication;
import io.vertx.ext.web.client.WebClient;

public class VertxHttpUserApi extends AbstractVertxHttpClient implements UserApi {

protected VertxHttpUserApi(WebClient client, String baseUrl, Authentication authentication) {
super(client, baseUrl, authentication);
}

@Override
public CompletableFuture<User> getUser() {
return handleAsync(requestBuilder(GET, URL_USER).send(), User.class);
}

}

0 comments on commit 33a6609

Please sign in to comment.