From 25d68c8900c2cc791f03ea3db173955ca237fd55 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Fri, 12 Jul 2019 15:40:05 +0200 Subject: Allow App To Choose Between MsgResponse Sinks - MZS Schema Change: Add "MsgResponseSinks" element to mzs:DeliveryRequest/Config that allows sender to configure how MsgResponses should be archived. - ConfigUtil: Interpret MsgResponseSink parameters from Spring Environment and merge with ConfigType. - MsgResponseBackend: Send responses to sinks according to MsgResponseSinks in Config - application.yaml: Add MsgResponseSinks parameter to configuration. - Uncouple Sink implementations from java.util.function.Function, because the sink interfaces are going to differ and there is no need to unite them under one interface. - Add and test LogResponseSink, which logs responses to it's logger. - MsgResponse: Add JAXB getter for response. Reason: Can be passed to marshaller. --- .../java/at/gv/egiz/moazs/LogResponseSinkTest.java | 78 ++++++++++++++++++++++ .../gv/egiz/moazs/SaveResponseToFileSinkTest.java | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/test/java/at/gv/egiz/moazs/LogResponseSinkTest.java (limited to 'src/test/java/at/gv') diff --git a/src/test/java/at/gv/egiz/moazs/LogResponseSinkTest.java b/src/test/java/at/gv/egiz/moazs/LogResponseSinkTest.java new file mode 100644 index 0000000..9e7c49d --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/LogResponseSinkTest.java @@ -0,0 +1,78 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.backend.LogResponseSink; +import at.gv.egiz.moazs.scheme.Marshaller; +import at.gv.egiz.moazs.scheme.NotificationResponse; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.slf4j.LoggerFactory; + +import static at.gv.zustellung.msg.xsd.DeliveryNotificationType.deliveryNotificationTypeBuilder; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class LogResponseSinkTest { + + private LogResponseSink logResponseSink; + + @Mock + private Appender appender; + private ch.qos.logback.classic.Logger logger; + + @Before + public void setup() { + logResponseSink = new LogResponseSink(new Marshaller(false)); + logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(LogResponseSink.class); + logger.addAppender(appender); + } + + @Test + public void logNotification() { + setLogLevel(Level.INFO); + var appDeliveryID = "log-notification-id"; + var notification = setupNotification(appDeliveryID); + + logResponseSink.log(notification) + .thenRun(expectSubstringSentToAppender(appDeliveryID)); + } + + @Test + public void warnWhenLogDisabled() { + setLogLevel(Level.WARN); + var appDeliveryID = "log-info-disabled"; + var notification = setupNotification(appDeliveryID); + + logResponseSink.log(notification) + .thenRun(expectSubstringSentToAppender("WARN")); + } + + private Runnable expectSubstringSentToAppender(String expectedSubstring) { + return ()-> verify(appender).doAppend(argThat(argument -> { + var message = ((ILoggingEvent) argument).getFormattedMessage(); + return message.contains(expectedSubstring); + })); + } + + private NotificationResponse setupNotification(String appDeliveryID) { + + var notification = deliveryNotificationTypeBuilder() + .withAppDeliveryID(appDeliveryID) + .build(); + + return new NotificationResponse(notification); + + } + + private void setLogLevel(Level level) { + logger.setLevel(level); + } + + +} diff --git a/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java b/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java index 9ddd37e..58efe6a 100644 --- a/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java +++ b/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java @@ -63,7 +63,7 @@ public class SaveResponseToFileSinkTest { var fileContent = "some content"; var status = setupMocks(fileContent); - sink.apply(status) + sink.save(status) .thenRun(() -> assertFilesCreatedAndContentMatches(fileContent)); } -- cgit v1.2.3