Bug 1369891 - Improve sync ping tests and stop them from failing in automation r=nalexander
authorGrigory Kruglov <gkruglov@mozilla.com>
Fri, 02 Jun 2017 19:10:59 -0400
changeset 410241 4f5b93b0571a23c8314233534b06a1aeb476fb1e
parent 410240 eb1d5848152b5f0cb2eeff48bce5996410a3597b
child 410242 72ab73cbc5c7d6f18d55a855103e48d7a8bb7eae
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs1369891
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1369891 - Improve sync ping tests and stop them from failing in automation r=nalexander Switch to explicit checks of contents of json objects, vs checking against toString output. MozReview-Commit-ID: A9YqG3A2My5
mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBuilderTest.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBundleBuilderTest.java
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBuilderTest.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBuilderTest.java
@@ -1,16 +1,19 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.gecko.telemetry.pingbuilders;
 
 import android.os.Bundle;
 import android.os.Parcelable;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.simple.JSONArray;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 import org.mozilla.gecko.sync.ExtendedJSONObject;
 import org.mozilla.gecko.telemetry.TelemetryLocalPing;
 
 import java.util.ArrayList;
@@ -32,20 +35,34 @@ public class TelemetrySyncPingBuilderTes
     public void testGeneralShape() throws Exception {
         TelemetryLocalPing localPing = builder
                 .setDeviceID("device-id")
                 .setUID("uid")
                 .setTook(123L)
                 .setRestarted(false)
                 .build();
         ExtendedJSONObject payload = localPing.getPayload();
-        assertEquals(
-                "{\"uid\":\"uid\",\"took\":123,\"deviceID\":\"device-id\",\"version\":1}",
-                payload.toString()
-        );
+        assertEquals("uid", payload.getString("uid"));
+        assertEquals(Long.valueOf(123L), payload.getLong("took"));
+        assertEquals("device-id", payload.getString("deviceID"));
+        assertEquals(Integer.valueOf(1), payload.getIntegerSafely("version"));
+        assertFalse(payload.containsKey("restarted"));
+
+        localPing = builder
+                .setDeviceID("device-id")
+                .setUID("uid")
+                .setTook(123L)
+                .setRestarted(true)
+                .build();
+        payload = localPing.getPayload();
+        assertEquals("uid", payload.getString("uid"));
+        assertEquals(Long.valueOf(123L), payload.getLong("took"));
+        assertEquals("device-id", payload.getString("deviceID"));
+        assertEquals(Integer.valueOf(1), payload.getIntegerSafely("version"));
+        assertEquals(true, payload.getBoolean("restarted"));
     }
 
     @Test
     public void testDevices() throws Exception {
         ArrayList<Parcelable> devices = new ArrayList<>();
 
         TelemetryLocalPing localPing = builder
                 .setDevices(devices)
@@ -64,30 +81,35 @@ public class TelemetrySyncPingBuilderTes
         device.putString("id", "80daf12dsadsa4236914cff2cc6e9d0f80a965380e2cf8e976e4004ead887521b5d9");
         devices.add(device);
 
         // Test with only one device
         payload = builder
                 .setDevices(devices)
                 .build()
                 .getPayload();
-        assertEquals(
-                "{\"devices\":[{\"os\":\"Android\",\"id\":\"80daf12dsadsa4236914cff2cc6e9d0f80a965380e2cf8e976e4004ead887521b5d9\",\"version\":\"53.0a1\"}],\"version\":1}",
-                payload.toString()
-        );
+        JSONArray devicesJSON = payload.getArray("devices");
+        assertEquals(1, devicesJSON.size());
+        assertDevice((ExtendedJSONObject) devicesJSON.get(0), "Android", "53.0a1", "80daf12dsadsa4236914cff2cc6e9d0f80a965380e2cf8e976e4004ead887521b5d9");
 
         device = new Bundle();
         device.putString("os", "iOS");
         device.putString("version", "8.0");
         device.putString("id", "fa813452774b3cdc8f5f73290b5346df800f644b7b92a1ab94b6e2af748d261362");
         devices.add(device);
 
         // Test with more than one device
         payload = builder
                 .setDevices(devices)
                 .build()
                 .getPayload();
-        assertEquals(
-                "{\"devices\":[{\"os\":\"Android\",\"id\":\"80daf12dsadsa4236914cff2cc6e9d0f80a965380e2cf8e976e4004ead887521b5d9\",\"version\":\"53.0a1\"},{\"os\":\"iOS\",\"id\":\"fa813452774b3cdc8f5f73290b5346df800f644b7b92a1ab94b6e2af748d261362\",\"version\":\"8.0\"}],\"version\":1}",
-                payload.toString()
-        );
+        devicesJSON = payload.getArray("devices");
+        assertEquals(2, devicesJSON.size());
+        assertDevice((ExtendedJSONObject) devicesJSON.get(0), "Android", "53.0a1", "80daf12dsadsa4236914cff2cc6e9d0f80a965380e2cf8e976e4004ead887521b5d9");
+        assertDevice((ExtendedJSONObject) devicesJSON.get(1), "iOS", "8.0", "fa813452774b3cdc8f5f73290b5346df800f644b7b92a1ab94b6e2af748d261362");
+    }
+
+    private void assertDevice(ExtendedJSONObject device, String os, String version, String id) throws JSONException {
+        assertEquals(os, device.getString("os"));
+        assertEquals(version, device.getString("version"));
+        assertEquals(id, device.getString("id"));
     }
 }
\ No newline at end of file
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBundleBuilderTest.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pingbuilders/TelemetrySyncPingBundleBuilderTest.java
@@ -1,43 +1,45 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.gecko.telemetry.pingbuilders;
 
 import android.os.Parcel;
 import android.os.Parcelable;
 
+import org.json.JSONException;
 import org.json.simple.JSONArray;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 import org.mozilla.gecko.sync.ExtendedJSONObject;
 import org.mozilla.gecko.telemetry.TelemetryOutgoingPing;
 import org.mozilla.gecko.telemetry.TelemetryPing;
 import org.mozilla.gecko.telemetry.stores.TelemetryJSONFilePingStore;
 import org.mozilla.gecko.telemetry.stores.TelemetryPingStore;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Set;
 
 import static org.junit.Assert.*;
 
 @RunWith(TestRunner.class)
 public class TelemetrySyncPingBundleBuilderTest {
     public static class MockTelemetryPingStore extends TelemetryPingStore {
         public MockTelemetryPingStore() {
             super("default");
         }
 
-        private HashMap<String, TelemetryPing> pings = new HashMap<>();
+        // Stable ordering for the sake of easier testing.
+        private LinkedHashMap<String, TelemetryPing> pings = new LinkedHashMap<>();
 
         @Override
         public List<TelemetryPing> getAllPings() {
             return new ArrayList<>(pings.values());
         }
 
         @Override
         public int getCount() {
@@ -142,37 +144,46 @@ public class TelemetrySyncPingBundleBuil
                 .build()
         );
         builder.setSyncStore(syncPings);
 
         TelemetryOutgoingPing outgoingPing = builder.build();
 
         // Ensure we have that one ping.
         ExtendedJSONObject payload = outgoingPing.getPayload().getObject("payload");
-        assertEquals(
-                "{\"syncs\":[{\"took\":123,\"uid\":\"test-uid\",\"restarted\":true,\"deviceID\":\"test-device-id\",\"version\":1}],\"why\":\"schedule\"}",
-                payload.toString()
-        );
+        assertEquals("schedule", payload.getString("why"));
+        JSONArray syncs = payload.getArray("syncs");
+        assertEquals(1, syncs.size());
+        assertSync((ExtendedJSONObject) syncs.get(0), "test-uid", 123L, "test-device-id", 1, true);
 
         // Add another ping.
         syncPings.storePing(new TelemetrySyncPingBuilder()
                 .setDeviceID("test-device-id")
-                .setRestarted(true)
+                .setRestarted(false)
                 .setTook(321L)
                 .setUID("test-uid")
                 .build()
         );
         builder.setSyncStore(syncPings);
 
         // We should have two pings now.
         outgoingPing = builder.build();
-        JSONArray syncs = outgoingPing.getPayload()
+        syncs = outgoingPing.getPayload()
                 .getObject("payload")
                 .getArray("syncs");
         assertEquals(2, syncs.size());
-        assertTrue(
-                syncs.toString().contains("{\"took\":123,\"uid\":\"test-uid\",\"restarted\":true,\"deviceID\":\"test-device-id\",\"version\":1}")
-        );
-        assertTrue(
-                syncs.toString().contains("{\"took\":321,\"uid\":\"test-uid\",\"restarted\":true,\"deviceID\":\"test-device-id\",\"version\":1}")
-        );
+        assertSync((ExtendedJSONObject) syncs.get(0), "test-uid", 123L, "test-device-id", 1, true);
+        assertSync((ExtendedJSONObject) syncs.get(1), "test-uid", 321L, "test-device-id", 1, false);
+    }
+
+    private void assertSync(ExtendedJSONObject sync, String uid, long took, String deviceID, int version, boolean restarted) throws JSONException {
+        assertEquals(uid, sync.getString("uid"));
+        assertEquals(Long.valueOf(took), sync.getLong("took"));
+        assertEquals(deviceID, sync.getString("deviceID"));
+        assertEquals(Integer.valueOf(version), sync.getIntegerSafely("version"));
+        if (restarted) {
+            assertEquals(true, sync.getBoolean("restarted"));
+        } else {
+            assertFalse(sync.containsKey("restarted"));
+        }
+
     }
 }
\ No newline at end of file