From 8fb3688e92321634e9aaae527e8114e35cd397b5 Mon Sep 17 00:00:00 2001 From: Robin Beer Date: Wed, 3 Jul 2024 15:56:41 +0100 Subject: [PATCH] refactor(Updated the process to define the number of unique devices): Unique devices is now based on --- .../middleware/models/Device.java | 14 ++++++++++ .../middleware/utils/NotificationUtils.java | 22 +++++++++++++-- .../utils/NotificationUtilsTest.java | 28 +++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/opentripplanner/middleware/models/Device.java diff --git a/src/main/java/org/opentripplanner/middleware/models/Device.java b/src/main/java/org/opentripplanner/middleware/models/Device.java new file mode 100644 index 000000000..7ff94435f --- /dev/null +++ b/src/main/java/org/opentripplanner/middleware/models/Device.java @@ -0,0 +1,14 @@ +package org.opentripplanner.middleware.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class Device { + @JsonProperty("DeviceName") + public String deviceName; + + public String getDeviceName() { + return deviceName; + } +} diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index 0cae5f69f..0c88b2fad 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -14,6 +14,7 @@ import org.eclipse.jetty.util.StringUtil; import org.opentripplanner.middleware.bugsnag.BugsnagReporter; import org.opentripplanner.middleware.models.AdminUser; +import org.opentripplanner.middleware.models.Device; import org.opentripplanner.middleware.models.OtpUser; import org.opentripplanner.middleware.persistence.Persistence; import org.slf4j.Logger; @@ -22,7 +23,10 @@ import java.io.IOException; import java.net.URI; import java.net.URLEncoder; +import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsText; @@ -352,9 +356,9 @@ public static int getPushInfo(String toUser) { null ); if (httpResponse.status == 200) { - // We don't use any of this information, we only care how many devices are registered. - var devices = JsonUtils.getPOJOFromHttpBodyAsList(httpResponse, Object.class); - return devices.size(); + return getNumberOfUniqueDevices( + Objects.requireNonNull(JsonUtils.getPOJOFromHttpBodyAsList(httpResponse, Device.class)) + ); } else { LOG.error("Error {} while getting info on push notification devices", httpResponse.status); } @@ -364,6 +368,18 @@ public static int getPushInfo(String toUser) { return 0; } + /** + * Return the number of unique, non null, device names. + */ + public static int getNumberOfUniqueDevices(List devices) { + return devices + .stream() + .map(Device::getDeviceName) + .filter(Objects::nonNull) + .collect(Collectors.toSet()) + .size(); + } + static String getPushDevicesUrl(String baseUrl, String toUser) { return baseUrl + URLEncoder.encode(toUser, UTF_8); } diff --git a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java index a691c8a93..ec3a48b2a 100644 --- a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java +++ b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java @@ -7,7 +7,9 @@ import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; +import org.opentripplanner.middleware.models.Device; +import java.util.List; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -54,6 +56,32 @@ void testGetPushDevicesUrl() { ); } + @Test + void testNumberOfUniqueDevices() { + String payload = + "[" + + " {" + + " \"DeviceName\": null" + + " }," + + " {" + + " \"DeviceName\": null" + + " }," + + " {" + + " \"DeviceName\": \"iPhone\"" + + " }," + + " {" + + " \"DeviceName\": null" + + " }," + + " {" + + " \"DeviceName\": \"Android\"" + + " }" + + "]"; + + List devices = JsonUtils.getPOJOFromJSONAsList(payload, Device.class); + assertEquals(5, devices.size()); + assertEquals(2, NotificationUtils.getNumberOfUniqueDevices(devices)); + } + @ParameterizedTest @MethodSource("createNotificationInfoCases") void testTruncateNotificationPayload(String originalTitle, String expectedTitle, String originalMessage, String expectedMessage, String message) {