-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from lsd-consulting/14
Add support for Spring Cloud Stream
- Loading branch information
Showing
7 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ptors/src/main/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.lsdconsulting.interceptors.messaging; | ||
|
||
import com.lsd.LsdContext; | ||
import com.lsd.diagram.ValidComponentName; | ||
import lombok.RequiredArgsConstructor; | ||
import lsd.format.PrettyPrinter; | ||
import org.springframework.messaging.Message; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
public class EventConsumerInterceptor implements ChannelInterceptor { | ||
|
||
private final LsdContext lsdContext; | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
String payload = PrettyPrinter.prettyPrint(new String((byte[]) message.getPayload())); | ||
String source = (String) message.getHeaders().get(SOURCE_NAME.key()); | ||
String target = (String) message.getHeaders().get(TARGET_NAME.key()); | ||
|
||
lsdContext.capture("Consume event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload); | ||
|
||
return message; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...tors/src/main/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.lsdconsulting.interceptors.messaging; | ||
|
||
import com.lsd.LsdContext; | ||
import com.lsd.diagram.ValidComponentName; | ||
import lombok.RequiredArgsConstructor; | ||
import lsd.format.PrettyPrinter; | ||
import org.springframework.messaging.Message; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
public class EventPublisherInterceptor implements ChannelInterceptor { | ||
|
||
private final LsdContext lsdContext; | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
String payload = PrettyPrinter.prettyPrint(new String((byte[]) message.getPayload())); | ||
String source = (String) message.getHeaders().get(SOURCE_NAME.key()); | ||
String target = (String) message.getHeaders().get(TARGET_NAME.key()); | ||
|
||
lsdContext.capture("Publish event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload); | ||
|
||
return message; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...java/io/lsdconsulting/interceptors/messaging/autoconfigure/LsdMessagingConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.lsdconsulting.interceptors.messaging.autoconfigure; | ||
|
||
import com.lsd.LsdContext; | ||
import io.lsdconsulting.interceptors.messaging.EventConsumerInterceptor; | ||
import io.lsdconsulting.interceptors.messaging.EventPublisherInterceptor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.integration.config.GlobalChannelInterceptor; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
|
||
/** | ||
* <p> | ||
* If a {@link LsdContext} and ChannelInterceptor classes is available it will automatically autoconfig a {@link LsdMessagingConfiguration} | ||
* </p> | ||
*/ | ||
@Configuration | ||
@PropertySource(ignoreResourceNotFound = true, value = "classpath:lsd.properties") | ||
@ConditionalOnClass(value = {LsdContext.class, ChannelInterceptor.class}) | ||
@ConditionalOnProperty(name = "lsd.interceptors.autoconfig.enabled", havingValue = "true", matchIfMissing = true) | ||
public class LsdMessagingConfiguration { | ||
|
||
private final LsdContext lsdContext = LsdContext.getInstance(); | ||
|
||
@Bean | ||
@GlobalChannelInterceptor(patterns = "*-in-*", order = 100) | ||
public EventConsumerInterceptor eventConsumerInterceptor() { | ||
return new EventConsumerInterceptor(lsdContext); | ||
} | ||
|
||
@Bean | ||
@GlobalChannelInterceptor(patterns = "*-out-*", order = 101) | ||
public EventPublisherInterceptor eventPublisherInterceptor() { | ||
return new EventPublisherInterceptor(lsdContext); | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
.../src/test/java/io/lsdconsulting/interceptors/aop/SpringDataRepositoryInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...s/src/test/java/io/lsdconsulting/interceptors/messaging/EventConsumerInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.lsdconsulting.interceptors.messaging; | ||
|
||
import com.lsd.LsdContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageHeaders; | ||
|
||
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 java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class EventConsumerInterceptorTest { | ||
private final ArgumentCaptor<String> patternCaptor = ArgumentCaptor.forClass(String.class); | ||
private final ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class); | ||
|
||
private final LsdContext lsdContext = mock(LsdContext.class); | ||
private final Message<byte[]> message = mock(Message.class); | ||
|
||
private final EventConsumerInterceptor underTest = new EventConsumerInterceptor(lsdContext); | ||
|
||
@Test | ||
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); | ||
|
||
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" + | ||
"}" | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
.../src/test/java/io/lsdconsulting/interceptors/messaging/EventPublisherInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.lsdconsulting.interceptors.messaging; | ||
|
||
import com.lsd.LsdContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageHeaders; | ||
|
||
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 java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class EventPublisherInterceptorTest { | ||
private final ArgumentCaptor<String> patternCaptor = ArgumentCaptor.forClass(String.class); | ||
private final ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class); | ||
|
||
private final LsdContext lsdContext = mock(LsdContext.class); | ||
private final Message<byte[]> message = mock(Message.class); | ||
|
||
private final EventPublisherInterceptor underTest = new EventPublisherInterceptor(lsdContext); | ||
|
||
@Test | ||
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); | ||
|
||
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" + | ||
"}" | ||
); | ||
} | ||
} |