Skip to content

Commit

Permalink
fix: push integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Sep 28, 2023
1 parent 188541d commit 8e84a14
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/src/main/java/io/ably/lib/rest/DeviceDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public boolean equals(Object o) {
thisJson.remove("deviceSecret");
otherJson.remove("deviceSecret");

normalizeRecipientField(thisJson);
normalizeRecipientField(otherJson);

if ((this.metadata == null || this.metadata.entrySet().isEmpty()) && (other.metadata == null || other.metadata.entrySet().isEmpty())) {
// Empty metadata == null metadata.
thisJson.remove("metadata");
Expand Down Expand Up @@ -170,4 +173,20 @@ public DeviceDetails fromJsonElement(JsonElement e) {
public static HttpCore.ResponseHandler<DeviceDetails> httpResponseHandler = new Serialisation.HttpResponseHandler<DeviceDetails>(DeviceDetails.class, fromJsonElement);

public static HttpCore.BodyHandler<DeviceDetails> httpBodyHandler = new Serialisation.HttpBodyHandler<DeviceDetails>(DeviceDetails[].class, fromJsonElement);

/**
* Push recipient can contain some additional field, but `transportType`, `deviceToken`, `registrationToken` only matters for equals
*/
private static void normalizeRecipientField(JsonObject deviceDetailsJson) {
JsonElement push = deviceDetailsJson.get("push");
if (push == null) return;
JsonElement recipient = push.getAsJsonObject().get("recipient");
if (recipient == null) return;
JsonObject normalizedRecipient = JsonUtils.object()
.add("transportType", recipient.getAsJsonObject().get("transportType"))
.add("deviceToken", recipient.getAsJsonObject().get("deviceToken"))
.add("registrationToken", recipient.getAsJsonObject().get("registrationToken"))
.toJson();
push.getAsJsonObject().add("recipient", normalizedRecipient);
}
}
37 changes: 37 additions & 0 deletions lib/src/test/java/io/ably/lib/rest/DeviceDetailsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.ably.lib.rest;

import io.ably.lib.util.JsonUtils;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class DeviceDetailsTest {

@Test
public void shouldIgnoreUnrelatedRecipientFields() {
DeviceDetails details = DeviceDetails.fromJsonObject(JsonUtils.object()
.add("id", "testDeviceDetails")
.add("platform", "ios")
.add("formFactor", "phone")
.add("metadata", JsonUtils.object())
.add("push", JsonUtils.object()
.add("recipient", JsonUtils.object()
.add("transportType", "apns")
.add("deviceToken", "foo")
.add("apnsDeviceTokens", JsonUtils.object().add("default", "foo"))))
.toJson());

DeviceDetails otherDetails = DeviceDetails.fromJsonObject(JsonUtils.object()
.add("id", "testDeviceDetails")
.add("platform", "ios")
.add("formFactor", "phone")
.add("metadata", JsonUtils.object())
.add("push", JsonUtils.object()
.add("recipient", JsonUtils.object()
.add("transportType", "apns")
.add("deviceToken", "foo")))
.toJson());

assertTrue("Should ignore `apnsDeviceTokens` field", details.equals(otherDetails));
}
}

0 comments on commit 8e84a14

Please sign in to comment.