Skip to content

Commit

Permalink
Merge pull request #241 from ibi-group/feature/OTP-1214-push-device
Browse files Browse the repository at this point in the history
Updated the process to define the number of unique devices
  • Loading branch information
br648 authored Jul 4, 2024
2 parents a5e8c08 + 8fb3688 commit 55e24e4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/opentripplanner/middleware/models/Device.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<Device> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +56,32 @@ void testGetPushDevicesUrl() {
);
}

@Test
void testNumberOfUniqueDevices() {
String payload =
"[" +
" {" +
" \"DeviceName\": null" +
" }," +
" {" +
" \"DeviceName\": null" +
" }," +
" {" +
" \"DeviceName\": \"iPhone\"" +
" }," +
" {" +
" \"DeviceName\": null" +
" }," +
" {" +
" \"DeviceName\": \"Android\"" +
" }" +
"]";

List<Device> 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) {
Expand Down

0 comments on commit 55e24e4

Please sign in to comment.