Bug 1247489 - Add tests for mandatory field validation in TelemetryPingBuilder. r=grisha
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 11 Apr 2016 18:35:20 -0700
changeset 331069 9fdf15002094efa7092857de08608c36350b6413
parent 331068 c2ad1f090a2ee81dc50af2f0dd87151923329075
child 331070 9b10a8bbd6aebc90cbf72b85d0022bdc81365292
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgrisha
bugs1247489
milestone48.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 1247489 - Add tests for mandatory field validation in TelemetryPingBuilder. r=grisha MozReview-Commit-ID: 9AtwJ3SisFT
mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pings/TestTelemetryPingBuilder.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/telemetry/pings/TestTelemetryPingBuilder.java
@@ -0,0 +1,92 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.telemetry.pings;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+
+import static org.junit.Assert.*;
+
+/**
+ * Unit test methods of the {@link TelemetryPingBuilder} class.
+ */
+@RunWith(TestRunner.class)
+public class TestTelemetryPingBuilder {
+    @Test
+    public void testMandatoryFieldsNone() {
+        final NoMandatoryFieldsBuilder builder = new NoMandatoryFieldsBuilder();
+        builder.setNonMandatoryField();
+        assertNotNull("Builder does not throw and returns a non-null value", builder.build());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testMandatoryFieldsMissing() {
+        final MandatoryFieldsBuilder builder = new MandatoryFieldsBuilder();
+        builder.setNonMandatoryField()
+                .build(); // should throw
+    }
+
+    @Test
+    public void testMandatoryFieldsIncluded() {
+        final MandatoryFieldsBuilder builder = new MandatoryFieldsBuilder();
+        builder.setNonMandatoryField()
+                .setMandatoryField();
+        assertNotNull("Builder does not throw and returns non-null value", builder.build());
+    }
+
+    private static class NoMandatoryFieldsBuilder extends TelemetryPingBuilder {
+        public NoMandatoryFieldsBuilder() {
+            super("");
+        }
+
+        @Override
+        String getDocType() {
+            return "";
+        }
+
+        @Override
+        String[] getMandatoryFields() {
+            return new String[0];
+        }
+
+        public NoMandatoryFieldsBuilder setNonMandatoryField() {
+            payload.put("non-mandatory", true);
+            return this;
+        }
+    }
+
+    private static class MandatoryFieldsBuilder extends TelemetryPingBuilder {
+        private static final String MANDATORY_FIELD = "mandatory-field";
+
+        public MandatoryFieldsBuilder() {
+            super("");
+        }
+
+        @Override
+        String getDocType() {
+            return "";
+        }
+
+        @Override
+        String[] getMandatoryFields() {
+            return new String[] {
+                    MANDATORY_FIELD,
+            };
+        }
+
+        public MandatoryFieldsBuilder setNonMandatoryField() {
+            payload.put("non-mandatory", true);
+            return this;
+        }
+
+        public MandatoryFieldsBuilder setMandatoryField() {
+            payload.put(MANDATORY_FIELD, true);
+            return this;
+        }
+    }
+}