git » notification-sender.git » commit b0a3a91

omit empty notifications

author Thorsten Ortlepp
2024-05-06 22:07:36 UTC
committer Thorsten Ortlepp
2024-05-06 22:07:36 UTC
parent fdf19cdf6446815ed155bfb70564e5af8bdedd74

omit empty notifications

src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java +4 -2
src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java +1 -1
src/test/java/eu/ortlepp/notificationsender/NotificationHandlerTest.java +1 -1

diff --git a/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java b/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
index 17b4a46..6a93992 100644
--- a/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
+++ b/src/main/java/eu/ortlepp/notificationsender/NotificationHandler.java
@@ -78,7 +78,7 @@ public class NotificationHandler implements RequestHandler<Map<String, Object>,
 
 
   /**
-   * Extract the notifications from the input JSON.
+   * Extract the notifications from the input JSON. Omit empty notifications.
    *
    * @param body The input JSON
    * @return The extracted messages; empty list if an error occurred while parsing the JSON
@@ -87,7 +87,9 @@ public class NotificationHandler implements RequestHandler<Map<String, Object>,
     try {
       Notifications notifications =
           new ObjectMapper().readValue(body.toString(), Notifications.class);
-      return Arrays.asList(notifications.messages());
+      var messages = new ArrayList<>(Arrays.asList(notifications.messages()));
+      messages.removeIf(message -> message.trim().isEmpty());
+      return messages;
     } catch (JsonProcessingException ex) {
       logger.log("parsing input JSON failed : " + ex.getMessage());
       return new ArrayList<>();
diff --git a/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java b/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java
index 8c39c72..94d4462 100644
--- a/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java
+++ b/src/main/java/eu/ortlepp/notificationsender/service/NotificationSender.java
@@ -17,7 +17,7 @@ public interface NotificationSender {
 
 
   /**
-   * Format notifications for sending.
+   * Format notifications for sending (default implementation).
    * If only one notification is passed, it is returned as is.
    * If multiple notifications are passed, a numbered list of notifications is returned.
    *
diff --git a/src/test/java/eu/ortlepp/notificationsender/NotificationHandlerTest.java b/src/test/java/eu/ortlepp/notificationsender/NotificationHandlerTest.java
index 9777356..e5b2bfb 100644
--- a/src/test/java/eu/ortlepp/notificationsender/NotificationHandlerTest.java
+++ b/src/test/java/eu/ortlepp/notificationsender/NotificationHandlerTest.java
@@ -17,7 +17,7 @@ import org.junit.jupiter.api.Test;
 
 public class NotificationHandlerTest {
 
-  private static final Object INPUT_VALID = "{\"messages\":[\"first\",\"second\"]}";
+  private static final Object INPUT_VALID = "{\"messages\":[\"first\",\"\",\"second\"]}";
   private static final Object INPUT_INVALID = "{\"messages\":\"some text\"}";
 
   private final Context context = new FakeContext();