Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a RestClient base HermesSender #1831

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pl.allegro.tech.hermes.client.restclient;

import org.springframework.web.client.RestClient;
import pl.allegro.tech.hermes.client.HermesMessage;
import pl.allegro.tech.hermes.client.HermesResponse;
import pl.allegro.tech.hermes.client.HermesSender;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toMap;
import static pl.allegro.tech.hermes.client.HermesResponseBuilder.hermesResponse;


public class RestClientHermesSender implements HermesSender {

private final RestClient restClient;


public RestClientHermesSender(RestClient restClient) {
this.restClient = restClient;
}

@Override
public CompletableFuture<HermesResponse> send(URI uri, HermesMessage message) {
CompletableFuture<HermesResponse> future = new CompletableFuture<>();

var received = restClient
.post()
.uri(uri)
.headers(httpHeaders -> httpHeaders.setAll(message.getHeaders()))
.body(message.getBody())
.exchange((request, response) -> {
if (response.getStatusCode().is2xxSuccessful()) {
return hermesResponse(message)
.withBody(new String(response.getBody().readAllBytes(), StandardCharsets.UTF_8))
.withHttpStatus(response.getStatusCode().value())
.withHeaderSupplier(header -> convertToCaseInsensitiveMap(response.getHeaders().toSingleValueMap()).get(header))
.build();
} else {
return hermesResponse(message)
.withBody("")
.withHttpStatus(response.getStatusCode().value())
.withHeaderSupplier(header -> convertToCaseInsensitiveMap(response.getHeaders().toSingleValueMap()).get(header))
.build();
}
});

future.complete(received);
return future;
}

private TreeMap<String, String> convertToCaseInsensitiveMap(Map<String, String> hashMap) {
return hashMap.entrySet().stream()
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldVal, newVal) -> newVal,
() -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)
));
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package pl.allegro.tech.hermes.client

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockClassRule
import jakarta.ws.rs.client.ClientBuilder
import okhttp3.OkHttpClient
import org.springframework.web.reactive.function.client.WebClient
import pl.allegro.tech.hermes.client.webclient.WebClientHermesSender
import org.junit.ClassRule
import org.springframework.web.client.RestClient
import org.springframework.web.reactive.function.client.WebClient
import pl.allegro.tech.hermes.client.jersey.JerseyHermesSender
import pl.allegro.tech.hermes.client.okhttp.OkHttpHermesSender
import pl.allegro.tech.hermes.client.restclient.RestClientHermesSender
import pl.allegro.tech.hermes.client.webclient.WebClientHermesSender
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import jakarta.ws.rs.client.ClientBuilder

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.containing
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo
import static com.github.tomakehurst.wiremock.client.WireMock.post
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import static com.github.tomakehurst.wiremock.client.WireMock.*
import static pl.allegro.tech.hermes.client.HermesMessage.hermesMessage

class HermesSenderTest extends Specification {
Expand All @@ -29,7 +24,7 @@ class HermesSenderTest extends Specification {
WireMockClassRule service = new WireMockClassRule(14523)

void setup() {
WireMock.reset()
reset()
}

@Unroll
Expand All @@ -54,10 +49,11 @@ class HermesSenderTest extends Specification {
.withRequestBody(containing("Hello!")))

where:
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
new RestClientHermesSender(RestClient.create()) | 'RestClient'
}

@Unroll
Expand All @@ -81,10 +77,11 @@ class HermesSenderTest extends Specification {
.withRequestBody(containing("Hello!")))

where:
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
new RestClientHermesSender(RestClient.create()) | 'RestClient'
}

@Unroll
Expand All @@ -108,10 +105,11 @@ class HermesSenderTest extends Specification {
.withRequestBody(containing("Hello!")))

where:
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
sender | name
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender'
new RestClientHermesSender(RestClient.create()) | 'RestClient'
}

@Unroll
Expand All @@ -126,9 +124,9 @@ class HermesSenderTest extends Specification {

service.stubFor(
post(urlEqualTo('/topics/topic.test'))
.willReturn(aResponse()
.withStatus(201)
.withHeader(header, 'messageId'))
.willReturn(aResponse()
.withStatus(201)
.withHeader(header, 'messageId'))
)

when:
Expand All @@ -143,17 +141,23 @@ class HermesSenderTest extends Specification {
response.messageId == 'messageId'

where:
sender | name | header
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'Hermes-Message-Id'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'Hermes-Message-Id'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'Hermes-Message-Id'

new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'hermes-message-id'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'hermes-message-id'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'hermes-message-id'

new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'HERMES-MESSAGE-ID'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'HERMES-MESSAGE-ID'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'HERMES-MESSAGE-ID'
sender | name | header
new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'Hermes-Message-Id'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'Hermes-Message-Id'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'Hermes-Message-Id'
new RestClientHermesSender(RestClient.create()) | 'RestClient' | 'Hermes-Message-Id'


new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'hermes-message-id'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'hermes-message-id'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'hermes-message-id'
new RestClientHermesSender(RestClient.create()) | 'RestClient' | 'hermes-message-id'


new JerseyHermesSender(ClientBuilder.newClient()) | 'JerseySender' | 'HERMES-MESSAGE-ID'
new OkHttpHermesSender(new OkHttpClient()) | 'OkHttpSender' | 'HERMES-MESSAGE-ID'
new WebClientHermesSender(WebClient.create()) | 'WebClientSender' | 'HERMES-MESSAGE-ID'
new RestClientHermesSender(RestClient.create()) | 'RestClient' | 'HERMES-MESSAGE-ID'

}
}
Loading