From d4a37b199806721fb296a4cfa4648c59ae0b89a6 Mon Sep 17 00:00:00 2001 From: Lukasz Gryzbon Date: Mon, 6 Mar 2023 14:14:22 -0400 Subject: [PATCH 1/3] refactor: #16 Move the Headers enum to the common package --- .../interceptors/{http => }/common/Headers.java | 6 +++--- .../interceptors/http/LsdFeignLoggerInterceptor.java | 2 +- .../interceptors/http/LsdOkHttpInterceptor.java | 2 +- .../http/common/DefaultHttpInteractionHandler.java | 1 + .../interceptors/messaging/EventConsumerInterceptor.java | 4 ++-- .../interceptors/messaging/EventPublisherInterceptor.java | 4 ++-- .../http/common/DefaultHttpInteractionHandlerTest.java | 1 + .../messaging/EventConsumerInterceptorTest.java | 4 ++-- .../messaging/EventPublisherInterceptorTest.java | 4 ++-- 9 files changed, 15 insertions(+), 13 deletions(-) rename lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/{http => }/common/Headers.java (78%) diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/Headers.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/common/Headers.java similarity index 78% rename from lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/Headers.java rename to lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/common/Headers.java index 82fd82d..ffc32db 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/Headers.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/common/Headers.java @@ -1,4 +1,4 @@ -package io.lsdconsulting.interceptors.http.common; +package io.lsdconsulting.interceptors.common; import java.util.Collection; import java.util.Map; @@ -11,7 +11,7 @@ public enum HeaderKeys { SOURCE_NAME("Source-Name"), TARGET_NAME("Target-Name"); - private String headerName; + private final String headerName; HeaderKeys(String headerName) { this.headerName = headerName; @@ -24,7 +24,7 @@ public String key() { public static Map singleValueMap(Map> headers) { return headers.entrySet().stream() - .collect(toMap(entry -> entry.getKey(), entry -> entry.getValue().stream() + .collect(toMap(Map.Entry::getKey, entry -> entry.getValue().stream() .findFirst() .orElse(""))); } diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdFeignLoggerInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdFeignLoggerInterceptor.java index 4474b12..42b9be6 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdFeignLoggerInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdFeignLoggerInterceptor.java @@ -4,7 +4,7 @@ import feign.Request; import feign.Response; import feign.Util; -import io.lsdconsulting.interceptors.http.common.Headers; +import io.lsdconsulting.interceptors.common.Headers; import io.lsdconsulting.interceptors.http.common.HttpInteractionHandler; import org.springframework.http.HttpStatus; diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdOkHttpInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdOkHttpInterceptor.java index 1e056a1..a74994b 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdOkHttpInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/LsdOkHttpInterceptor.java @@ -1,6 +1,6 @@ package io.lsdconsulting.interceptors.http; -import io.lsdconsulting.interceptors.http.common.Headers; +import io.lsdconsulting.interceptors.common.Headers; import io.lsdconsulting.interceptors.http.common.HttpInteractionHandler; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandler.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandler.java index 86b2df0..f4ae03f 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandler.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandler.java @@ -3,6 +3,7 @@ import com.lsd.LsdContext; import com.lsd.diagram.ValidComponentName; import com.lsd.events.Markup; +import io.lsdconsulting.interceptors.common.Headers; import io.lsdconsulting.interceptors.http.naming.DestinationNameMappings; import io.lsdconsulting.interceptors.http.naming.SourceNameMappings; import lombok.EqualsAndHashCode; diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java index 5c4eba3..254e2ee 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java @@ -7,8 +7,8 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.support.ChannelInterceptor; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString; import static lsd.format.PrettyPrinter.prettyPrint; diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java index 3cc525d..5e21e7e 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java @@ -7,8 +7,8 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.support.ChannelInterceptor; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString; import static lsd.format.PrettyPrinter.prettyPrint; diff --git a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandlerTest.java b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandlerTest.java index 4624b17..905d0d0 100644 --- a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandlerTest.java +++ b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/http/common/DefaultHttpInteractionHandlerTest.java @@ -1,6 +1,7 @@ package io.lsdconsulting.interceptors.http.common; import com.lsd.LsdContext; +import io.lsdconsulting.interceptors.common.Headers; import io.lsdconsulting.interceptors.http.naming.DestinationNameMappings; import io.lsdconsulting.interceptors.http.naming.SourceNameMappings; import org.junit.jupiter.api.Test; diff --git a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java index ccf4b9e..0ab7b46 100644 --- a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java +++ b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java @@ -8,8 +8,8 @@ import java.util.Map; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; diff --git a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java index 92928ab..5405d34 100644 --- a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java +++ b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java @@ -8,8 +8,8 @@ import java.util.Map; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME; -import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; +import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; From 57bcd8ed9577264ee146ea389dbae7bc0b9e6b92 Mon Sep 17 00:00:00 2001 From: Lukasz Gryzbon Date: Mon, 6 Mar 2023 18:55:33 -0400 Subject: [PATCH 2/3] fix: #16 Add message properties to the captured interactions --- .../messaging/EventConsumerInterceptor.java | 3 ++- .../messaging/EventPublisherInterceptor.java | 3 ++- .../interceptors/messaging/HtmlRenderer.java | 26 +++++++++++++++++++ .../EventConsumerInterceptorTest.java | 12 ++++++--- .../EventPublisherInterceptorTest.java | 12 ++++++--- 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/HtmlRenderer.java diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java index 254e2ee..1452282 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java @@ -9,6 +9,7 @@ import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.messaging.HtmlRenderer.renderHtmlFor; import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString; import static lsd.format.PrettyPrinter.prettyPrint; @@ -23,7 +24,7 @@ public Message preSend(Message message, MessageChannel channel) { String source = convertToString(message.getHeaders().get(SOURCE_NAME.key())); String target = convertToString(message.getHeaders().get(TARGET_NAME.key())); - lsdContext.capture("Consume event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload); + lsdContext.capture("Consume event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), renderHtmlFor(message.getHeaders(), payload)); return message; } diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java index 5e21e7e..a84cee8 100644 --- a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java @@ -9,6 +9,7 @@ import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME; import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME; +import static io.lsdconsulting.interceptors.messaging.HtmlRenderer.renderHtmlFor; import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString; import static lsd.format.PrettyPrinter.prettyPrint; @@ -23,7 +24,7 @@ public Message preSend(Message message, MessageChannel channel) { String source = convertToString(message.getHeaders().get(SOURCE_NAME.key())); String target = convertToString(message.getHeaders().get(TARGET_NAME.key())); - lsdContext.capture("Publish event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload); + lsdContext.capture("Publish event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), renderHtmlFor(message.getHeaders(), payload)); return message; } diff --git a/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/HtmlRenderer.java b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/HtmlRenderer.java new file mode 100644 index 0000000..2a4727f --- /dev/null +++ b/lsd-interceptors/src/main/java/io/lsdconsulting/interceptors/messaging/HtmlRenderer.java @@ -0,0 +1,26 @@ +package io.lsdconsulting.interceptors.messaging; + +import org.springframework.messaging.MessageHeaders; + +import static j2html.TagCreator.*; +import static java.lang.System.lineSeparator; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.lang3.StringUtils.isEmpty; + +public class HtmlRenderer { + public static String renderHtmlFor(MessageHeaders messageHeaders, String prettyBody) { + return p( + p(h4("Message Headers"), code(prettyPrintHeaders(messageHeaders))) + , isEmpty(prettyBody) + ? p() + : p(h4("Body"), code(prettyBody) + ) + ).render(); + } + + private static String prettyPrintHeaders(MessageHeaders requestHeaders) { + return requestHeaders.entrySet().stream().map(entry -> + entry.getKey() + ": " + entry.getValue()) + .collect(joining(lineSeparator())); + } +} diff --git a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java index 0ab7b46..fb4af3c 100644 --- a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java +++ b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import java.util.Map; @@ -30,14 +31,17 @@ void logInteraction() { given(message.getPayload()).willReturn("{\"key\":\"value\"}".getBytes(UTF_8)); given(message.getHeaders()).willReturn(new MessageHeaders(Map.of(SOURCE_NAME.key(), "Source", TARGET_NAME.key(), "Target"))); - underTest.preSend(message, null); + underTest.preSend(message, mock(MessageChannel.class)); verify(lsdContext).capture(patternCaptor.capture(), payloadCaptor.capture()); assertThat(patternCaptor.getValue()).isEqualTo("Consume event from Source to Target"); assertThat(payloadCaptor.getValue()).isEqualTo( - "{\n" + - " \"key\": \"value\"\n" + - "}" + "

Message Headers

Target-Name: Target\n" + + "Source-Name: Source\n" + + "id: " + message.getHeaders().get("id") + "\n" + + "timestamp: " + message.getHeaders().get("timestamp") + "

Body

{\n" + + " "key": "value"\n" + + "}

" ); } } \ No newline at end of file diff --git a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java index 5405d34..944ccc3 100644 --- a/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java +++ b/lsd-interceptors/src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import java.util.Map; @@ -30,14 +31,17 @@ void logInteraction() { given(message.getPayload()).willReturn("{\"key\":\"value\"}".getBytes(UTF_8)); given(message.getHeaders()).willReturn(new MessageHeaders(Map.of(SOURCE_NAME.key(), "Source", TARGET_NAME.key(), "Target"))); - underTest.preSend(message, null); + underTest.preSend(message, mock(MessageChannel.class)); verify(lsdContext).capture(patternCaptor.capture(), payloadCaptor.capture()); assertThat(patternCaptor.getValue()).isEqualTo("Publish event from Source to Target"); assertThat(payloadCaptor.getValue()).isEqualTo( - "{\n" + - " \"key\": \"value\"\n" + - "}" + "

Message Headers

Target-Name: Target\n" + + "Source-Name: Source\n" + + "id: " + message.getHeaders().get("id") + "\n" + + "timestamp: " + message.getHeaders().get("timestamp") + "

Body

{\n" + + " "key": "value"\n" + + "}

" ); } } From c4b2ad6e22245d2caacf387e7eaa2d94db89ceba Mon Sep 17 00:00:00 2001 From: Lukasz Gryzbon Date: Tue, 7 Mar 2023 06:30:50 -0400 Subject: [PATCH 3/3] fix: #16 Update formatting library version to 1.0.7 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index eae55e8..1276c99 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ allprojects { plugins.withId('java') { dependencies { implementation 'io.github.lsd-consulting:lsd-core:1.2.6' - implementation 'io.github.lsd-consulting:lsd-formatting-library:1.0.6' + implementation 'io.github.lsd-consulting:lsd-formatting-library:1.0.7' implementation 'org.projectlombok:lombok:1.18.22'