Mholloway has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316469

Change subject: WIP: Add tests for field checking with the new @Require 
annotation
......................................................................

WIP: Add tests for field checking with the new @Require annotation

Not yet passing.

Bug: T145075
Change-Id: I2f5add17352ae234ea860be1c7f291909eced67d
---
A 
app/src/test/java/org/wikipedia/json/RequiredFieldsCheckOnReadTypeAdapterTest.java
1 file changed, 161 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/69/316469/1

diff --git 
a/app/src/test/java/org/wikipedia/json/RequiredFieldsCheckOnReadTypeAdapterTest.java
 
b/app/src/test/java/org/wikipedia/json/RequiredFieldsCheckOnReadTypeAdapterTest.java
new file mode 100644
index 0000000..ef1e4ba
--- /dev/null
+++ 
b/app/src/test/java/org/wikipedia/json/RequiredFieldsCheckOnReadTypeAdapterTest.java
@@ -0,0 +1,161 @@
+package org.wikipedia.json;
+
+import android.net.Uri;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+
+import org.junit.Test;
+import org.wikipedia.Constants;
+import org.wikipedia.json.annotations.Required;
+import org.wikipedia.model.BaseModel;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.wikipedia.json.GsonMarshaller.marshal;
+import static org.wikipedia.json.GsonUnmarshaller.unmarshal;
+
+public class RequiredFieldsCheckOnReadTypeAdapterTest {
+    @Test
+    public void testRequireNonNull() {
+        RequiredModel expected = new RequiredModel();
+        expected.field = 1;
+
+        RequiredModel result = unmarshal(RequiredModel.class, 
marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testRequireMissing() {
+        RequiredModel expected = new RequiredModel();
+        RequiredModel result = unmarshal(RequiredModel.class, 
marshal(expected));
+        assertThat(result, nullValue());
+    }
+
+    @Test
+    public void testRequireNull() {
+        RequiredModel model = new RequiredModel();
+        model.field = null;
+
+        RequiredModel result = unmarshal(RequiredModel.class, marshal(model));
+        assertThat(result, nullValue());
+    }
+
+    @Test
+    public void testOptionalNonNull() {
+        OptionalModel expected = new OptionalModel();
+        expected.field = 1;
+        OptionalModel result = unmarshal(OptionalModel.class, 
marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testOptionalMissing() {
+        OptionalModel expected = new OptionalModel();
+        OptionalModel result = unmarshal(OptionalModel.class, 
marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testOptionalNull() {
+        OptionalModel expected = new OptionalModel();
+        expected.field = null;
+        OptionalModel result = unmarshal(OptionalModel.class, 
marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testRequiredTypeAdapterNonNull() {
+        RequiredTypeAdapterModel expected = new RequiredTypeAdapterModel();
+        expected.uri = Uri.parse(Constants.WIKIPEDIA_URL);
+        RequiredTypeAdapterModel result = 
unmarshal(RequiredTypeAdapterModel.class, marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testRequiredTypeAdapterMissing() {
+        RequiredTypeAdapterModel expected = new RequiredTypeAdapterModel();
+        RequiredTypeAdapterModel result = 
unmarshal(RequiredTypeAdapterModel.class, marshal(expected));
+        assertThat(result, nullValue());
+    }
+
+    @Test
+    public void testRequiredTypeAdapterNull() {
+        RequiredTypeAdapterModel expected = new RequiredTypeAdapterModel();
+        expected.uri = null;
+        RequiredTypeAdapterModel result = 
unmarshal(RequiredTypeAdapterModel.class, marshal(expected));
+        assertThat(result, nullValue());
+    }
+
+    @Test
+    public void testOptionalTypeAdapterNonNull() {
+        OptionalTypeAdapterModel expected = new OptionalTypeAdapterModel();
+        expected.uri = Uri.parse(Constants.WIKIPEDIA_URL);
+        OptionalTypeAdapterModel result = 
unmarshal(OptionalTypeAdapterModel.class, marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    //TODO: Wny does this test fail?
+    //@Test
+    public void testOptionalTypeAdapterMissing() {
+        OptionalTypeAdapterModel expected = new OptionalTypeAdapterModel();
+        OptionalTypeAdapterModel result = 
unmarshal(OptionalTypeAdapterModel.class, marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    //TODO: Wny does this test fail?
+    //@Test
+    public void testOptionalTypeAdapterNull() {
+        OptionalTypeAdapterModel expected = new OptionalTypeAdapterModel();
+        expected.uri = null;
+        OptionalTypeAdapterModel result = 
unmarshal(OptionalTypeAdapterModel.class, marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    //TODO: Wny does this test fail?
+    //@Test
+    public void testValidAggregated() {
+        RequiredModel required = new RequiredModel();
+        required.field = 1;
+        OptionalModel optional = new OptionalModel();
+        AggregatedModel expected = new AggregatedModel();
+        expected.required = required;
+        expected.optional = optional;
+
+        AggregatedModel result = unmarshal(AggregatedModel.class, 
marshal(expected));
+        assertThat(result, is(expected));
+    }
+
+    @Test
+    public void testInvalidAggregated() {
+        RequiredModel required = new RequiredModel();
+        OptionalModel optional = new OptionalModel();
+        AggregatedModel aggregated = new AggregatedModel();
+        aggregated.required = required;
+        aggregated.optional = optional;
+
+        AggregatedModel result = unmarshal(AggregatedModel.class, 
marshal(aggregated));
+        assertThat(result, nullValue());
+    }
+
+    private static class RequiredModel extends BaseModel {
+        @SuppressWarnings("NullableProblems") @Required @NonNull private 
Integer field;
+    }
+
+    private static class OptionalModel extends BaseModel {
+        @Nullable private Integer field;
+    }
+
+    private static class AggregatedModel {
+        @SuppressWarnings("NullableProblems") @Required @NonNull RequiredModel 
required;
+        @Nullable OptionalModel optional;
+    }
+
+    private static class RequiredTypeAdapterModel {
+        @SuppressWarnings("NullableProblems") @Required @NonNull private Uri 
uri;
+    }
+
+    private static class OptionalTypeAdapterModel {
+        @Nullable private Uri uri;
+    }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/316469
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2f5add17352ae234ea860be1c7f291909eced67d
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <mhollo...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to