Skip to content

Commit

Permalink
Update Spring to v3.x.x (#106)
Browse files Browse the repository at this point in the history
* WIP bumped spring

* Bumped http4k, jetty reactive httpclient and logback

* Enabled tests

* Corrected imports

* Removed redundant beant

* Bumped to Java 17

* Removed netty dependencies

* Revert "Removed netty dependencies"

This reverts commit 297ea61.
  • Loading branch information
Hakky54 authored Jul 16, 2024
1 parent f8bb438 commit 2a80755
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Also [GitHub - SSLContext Kickstart](https://github.com/Hakky54/sslcontext-kicks
# Tutorial
## Starting the server
**Minimum requirements:**
1. Java 11
1. Java 17
2. Maven 3.5.0
3. Eclipse, Intellij IDEA (or any other text editor like VIM)
4. A terminal
Expand Down
8 changes: 8 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-reactive-httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
Expand Down
17 changes: 9 additions & 8 deletions client/src/main/java/nl/altindag/client/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,15 @@ public HttpClient jdkHttpClient(SSLFactory sslFactory) {
}

@Bean
public RestTemplate restTemplate(org.apache.http.impl.client.CloseableHttpClient httpClient) {
public RestTemplate restTemplate(org.apache.hc.client5.http.impl.classic.CloseableHttpClient httpClient) {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
}

@Bean
@Bean("okHttpClient")
@Scope("prototype")
public OkHttpClient okHttpClient(SSLFactory sslFactory) {
return new OkHttpClient.Builder()
.sslSocketFactory(sslFactory.getSslSocketFactory(), sslFactory.getTrustManager().orElseThrow())
.hostnameVerifier(sslFactory.getHostnameVerifier())
.build();
}

Expand All @@ -155,17 +154,19 @@ public reactor.netty.http.client.HttpClient nettyHttpClient(SSLFactory sslFactor
@Scope("prototype")
public org.eclipse.jetty.client.HttpClient jettyHttpClient(SSLFactory sslFactory) {
var sslContextFactory = JettySslUtils.forClient(sslFactory);
return new org.eclipse.jetty.client.HttpClient(sslContextFactory);
org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient();
httpClient.setSslContextFactory(sslContextFactory);
return httpClient;
}

@Bean
@Bean("webClientWithNetty")
public WebClient webClientWithNetty(reactor.netty.http.client.HttpClient httpClient) {
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}

@Bean
@Bean("webClientWithJetty")
public WebClient webClientWithJetty(org.eclipse.jetty.client.HttpClient httpClient) {
return WebClient.builder()
.clientConnector(new JettyClientHttpConnector(httpClient))
Expand Down Expand Up @@ -235,7 +236,7 @@ public void unirest(SSLFactory sslFactory) {
}

@Bean
public Retrofit retrofit(OkHttpClient okHttpClient) {
public Retrofit retrofit(@Qualifier("okHttpClient") OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Constants.getServerUrl())
Expand Down Expand Up @@ -288,7 +289,7 @@ public Feign.Builder feignWithOldJdkHttpClient(SSLFactory sslFactory) {
}

@Bean
public Feign.Builder feignWithOkHttpClient(OkHttpClient okHttpClient) {
public Feign.Builder feignWithOkHttpClient(@Qualifier("okHttpClient") OkHttpClient okHttpClient) {
return Feign.builder()
.client(new feign.okhttp.OkHttpClient(okHttpClient));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package nl.altindag.client.service
import nl.altindag.client.Constants
import nl.altindag.client.model.ClientResponse
import org.awaitility.Awaitility.await
import org.http4k.client.AsyncHttpClient
import org.http4k.client.AsyncHttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import java.util.concurrent.TimeUnit

abstract class Http4kAsyncClientService(val client: AsyncHttpClient): RequestService {
abstract class Http4kAsyncClientService(val client: AsyncHttpHandler): RequestService {

override fun executeRequest(url: String): ClientResponse {
var response: ClientResponse? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ package nl.altindag.client.service

import nl.altindag.client.ClientType
import nl.altindag.client.ClientType.HTTP4K_JETTY_HTTP_CLIENT
import nl.altindag.ssl.SSLFactory
import nl.altindag.ssl.jetty.util.JettySslUtils
import org.eclipse.jetty.client.HttpClient
import org.http4k.client.JettyClient
import org.springframework.stereotype.Service

@Service
class Http4kJettyHttpClientService(
sslFactory: SSLFactory
jettyClient: HttpClient
) : Http4kClientService(
JettyClient(
client = HttpClient(JettySslUtils.forClient(sslFactory))
client = jettyClient
)
) {

override fun getClientType(): ClientType = HTTP4K_JETTY_HTTP_CLIENT

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ClientResponse executeRequest(String url) throws Exception {

var contentResponse = httpClient.newRequest(url)
.method(HttpMethod.GET)
.header(HEADER_KEY_CLIENT_TYPE, getClientType().getValue())
.headers(header -> header.add(HEADER_KEY_CLIENT_TYPE, getClientType().getValue()))
.send();

httpClient.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import nl.altindag.client.model.ClientResponse;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -32,7 +33,7 @@ public class OkHttpClientService implements RequestService {

private final OkHttpClient okHttpClient;

public OkHttpClientService(OkHttpClient okHttpClient) {
public OkHttpClientService(@Qualifier("okHttpClient") OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static nl.altindag.client.ClientType.SPRING_WEB_CLIENT_JETTY;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

Expand All @@ -25,7 +26,7 @@
@Service
public class SpringWebClientJettyService extends SpringWebClientService {

public SpringWebClientJettyService(WebClient webClientWithJetty) {
public SpringWebClientJettyService(@Qualifier("webClientWithJetty") WebClient webClientWithJetty) {
super(webClientWithJetty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static nl.altindag.client.ClientType.SPRING_WEB_CLIENT_NETTY;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

Expand All @@ -25,7 +26,7 @@
@Service
public class SpringWebClientNettyService extends SpringWebClientService {

public SpringWebClientNettyService(WebClient webClientWithNetty) {
public SpringWebClientNettyService(@Qualifier("webClientWithNetty") WebClient webClientWithNetty) {
super(webClientWithNetty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void createJdkHttpClient() {

@Test
void createRestTemplate() {
CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
org.apache.hc.client5.http.impl.classic.CloseableHttpClient httpClient = mock(org.apache.hc.client5.http.impl.classic.CloseableHttpClient.class);

RestTemplate restTemplate = victim.restTemplate(httpClient);

Expand All @@ -127,12 +127,10 @@ void createOkHttpClient() {

assertThat(okHttpClient).isNotNull();
verify(sslFactory, times(1)).getTrustManager();
verify(sslFactory, times(1)).getHostnameVerifier();
verify(sslFactory, times(1)).getSslSocketFactory();

assertThat(sslFactory.getTrustManager()).isPresent();
assertThat(okHttpClient.x509TrustManager()).isEqualTo(sslFactory.getTrustManager().get());
assertThat(okHttpClient.hostnameVerifier()).isEqualTo(sslFactory.getHostnameVerifier());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import nl.altindag.client.ClientType.HTTP4K_JETTY_HTTP_CLIENT
import nl.altindag.client.TestConstants
import nl.altindag.client.util.MockServerTestHelper
import nl.altindag.client.util.SSLFactoryTestHelper
import nl.altindag.ssl.jetty.util.JettySslUtils
import org.assertj.core.api.Assertions.assertThat
import org.eclipse.jetty.client.HttpClient
import org.junit.jupiter.api.Test
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
Expand All @@ -31,7 +33,11 @@ class Http4kJettyHttpClientServiceShould {
MockServerTestHelper.mockResponseForClient(HTTP4K_JETTY_HTTP_CLIENT)
val sslFactory = SSLFactoryTestHelper.createSSLFactory(false, true)

val client = Http4kJettyHttpClientService(sslFactory)
val sslContextFactory = JettySslUtils.forClient(sslFactory)
val httpClient = HttpClient()
httpClient.sslContextFactory = sslContextFactory

val client = Http4kJettyHttpClientService(httpClient)
val response = client.executeRequest(TestConstants.HTTP_URL)

assertThat(response.responseBody).isEqualTo("Hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package nl.altindag.client.service;

import nl.altindag.client.model.ClientResponse;
import org.eclipse.jetty.client.ContentResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.http.HttpMethod;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -27,8 +27,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static nl.altindag.client.ClientType.JETTY_REACTIVE_HTTP_CLIENT;
import static nl.altindag.client.TestConstants.HEADER_KEY_CLIENT_TYPE;
import java.util.function.Consumer;

import static nl.altindag.client.TestConstants.HTTP_URL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -38,6 +38,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("unchecked")
@ExtendWith(MockitoExtension.class)
class JettyReactiveHttpClientServiceShould {

Expand All @@ -53,25 +54,19 @@ void executeRequest() throws Exception {

when(httpClient.newRequest(anyString())).thenReturn(request);
when(request.method(any(HttpMethod.class))).thenReturn(request);
when(request.header(anyString(), anyString())).thenReturn(request);
when(request.headers(any(Consumer.class))).thenReturn(request);
when(request.send()).thenReturn(contentResponse);
when(contentResponse.getContentAsString()).thenReturn("hello");
when(contentResponse.getStatus()).thenReturn(200);

ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> headerKeyCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> headerValueCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<HttpMethod> httpMethodCaptor = ArgumentCaptor.forClass(HttpMethod.class);

ClientResponse clientResponse = victim.executeRequest(HTTP_URL);

assertThat(clientResponse.getStatusCode()).isEqualTo(200);
assertThat(clientResponse.getResponseBody()).isEqualTo("hello");

verify(request, times(1)).header(headerKeyCaptor.capture(), headerValueCaptor.capture());
assertThat(headerKeyCaptor.getValue()).isEqualTo(HEADER_KEY_CLIENT_TYPE);
assertThat(headerValueCaptor.getValue()).isEqualTo(JETTY_REACTIVE_HTTP_CLIENT.getValue());

verify(request, times(1)).method(httpMethodCaptor.capture());
assertThat(httpMethodCaptor.getValue()).isEqualTo(HttpMethod.GET);

Expand Down
Loading

0 comments on commit 2a80755

Please sign in to comment.