@Test public void equalsHashCode() throws JSONException { /* Empty objects. */ Data a = new Data(); Data b = new Data(); checkEquals(a, b); /* Properties. */ a.getProperties().put("a", "b"); checkNotEquals(a, b); b.getProperties().put("a", "b"); checkEquals(a, b); }
@Test public void serializeOrder() throws JSONException { /* When properties not inserted in order. */ Data d = new Data(); d.getProperties().put("a", "b"); d.getProperties().put("baseData", new JSONObject()); d.getProperties().put("baseType", "type"); /* When serializing properties. */ JSONStringer writer = mock(JSONStringer.class); when(writer.key(anyString())).thenReturn(writer); d.write(writer); /* Then baseType is written, then baseData, then Part C. */ InOrder inOrder = inOrder(writer); inOrder.verify(writer).key("baseType"); inOrder.verify(writer).key("baseData"); inOrder.verify(writer).key("a"); } }
@SuppressWarnings("SimplifiableIfStatement") @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; CommonSchemaLog that = (CommonSchemaLog) o; if (ver != null ? !ver.equals(that.ver) : that.ver != null) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; if (popSample != null ? !popSample.equals(that.popSample) : that.popSample != null) return false; if (iKey != null ? !iKey.equals(that.iKey) : that.iKey != null) return false; if (flags != null ? !flags.equals(that.flags) : that.flags != null) return false; if (cV != null ? !cV.equals(that.cV) : that.cV != null) return false; if (ext != null ? !ext.equals(that.ext) : that.ext != null) return false; return data != null ? data.equals(that.data) : that.data == null; }
@Override public void read(JSONObject object) throws JSONException { /* Override abstract log JSON since it's Common Schema and not App Center schema. */ /* Read top level PART A simple fields. */ setVer(object.getString(VER)); setName(object.getString(NAME)); setTimestamp(JSONDateUtils.toDate(object.getString(TIME))); if (object.has(POP_SAMPLE)) { setPopSample(object.getDouble(POP_SAMPLE)); } setIKey(object.optString(IKEY, null)); setFlags(JSONUtils.readLong(object, FLAGS)); setCV(object.optString(CV, null)); /* Read extensions. */ if (object.has(EXT)) { Extensions extensions = new Extensions(); extensions.read(object.getJSONObject(EXT)); setExt(extensions); } /* Read Parts B&C. */ if (object.has(DATA)) { Data data = new Data(); data.read(object.getJSONObject(DATA)); setData(data); } }
@Test() public void filterInvalidProperties() throws JSONException { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("a", "b")); properties.add(typedProperty(null, "c")); properties.add(typedProperty("d", null)); CommonSchemaDataUtils.addCommonSchemaData(properties, log); assertEquals(1, log.getData().getProperties().length()); assertEquals("b", log.getData().getProperties().getString("a")); assertNull(log.getExt()); }
@Test public void compareDifferentType() { TestUtils.compareSelfNullClass(new Data()); }
@Override public void write(JSONStringer writer) throws JSONException { /* Override abstract log JSON since it's Common Schema and not App Center schema. */ /* Part A. */ writer.key(VER).value(getVer()); writer.key(NAME).value(getName()); writer.key(TIME).value(JSONDateUtils.toString(getTimestamp())); JSONUtils.write(writer, POP_SAMPLE, getPopSample()); JSONUtils.write(writer, IKEY, getIKey()); JSONUtils.write(writer, FLAGS, getFlags()); JSONUtils.write(writer, CV, getCV()); /* Part A extensions. */ if (getExt() != null) { writer.key(EXT).object(); getExt().write(writer); writer.endObject(); } /* Parts B & C. */ if (getData() != null) { writer.key(DATA).object(); getData().write(writer); writer.endObject(); } }
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + (ver != null ? ver.hashCode() : 0); result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (popSample != null ? popSample.hashCode() : 0); result = 31 * result + (iKey != null ? iKey.hashCode() : 0); result = 31 * result + (flags != null ? flags.hashCode() : 0); result = 31 * result + (cV != null ? cV.hashCode() : 0); result = 31 * result + (ext != null ? ext.hashCode() : 0); result = 31 * result + (data != null ? data.hashCode() : 0); return result; } }
@Test public void emptyStringsAreAllowed() throws JSONException { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("", "")); CommonSchemaDataUtils.addCommonSchemaData(properties, log); assertEquals(1, log.getData().getProperties().length()); assertEquals("", log.getData().getProperties().getString("")); assertNull(log.getExt()); }
@Test public void booleanTypedProperty() throws JSONException { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); BooleanTypedProperty property = new BooleanTypedProperty(); property.setName("a"); property.setValue(true); properties.add(property); CommonSchemaDataUtils.addCommonSchemaData(properties, log); assertEquals(1, log.getData().getProperties().length()); assertTrue(log.getData().getProperties().getBoolean("a")); }
Data data = new Data(); dest.setData(data); JSONObject destProperties = data.getProperties(); JSONObject destMetadata = metadata.getMetadata(); for (int i = 0; i < lastIndex; i++) { JSONObject dataObject = data.getProperties(); String baseType = dataObject.optString(BASE_TYPE, null); JSONObject baseData = dataObject.optJSONObject(BASE_DATA);
@Test public void overrideProperty() { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("a.b", "1")); properties.add(typedProperty("a.b.c.d", "2")); properties.add(typedProperty("a.b.c", "3")); CommonSchemaDataUtils.addCommonSchemaData(properties, log); JSONObject b = log.getData().getProperties().optJSONObject("a").optJSONObject("b"); assertNotNull(b); assertEquals("3", b.optString("c", null)); assertNull(log.getExt()); }
log.setTimestamp(new Date()); log.addTransmissionTarget("token"); Data data = new Data(); log.setData(data); data.getProperties().put("key", largeValue.toString());
@Test public void deepNestingProperty() { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("a.b", "1")); properties.add(typedProperty("a.c.d", "2")); properties.add(typedProperty("a.c.e", "3")); CommonSchemaDataUtils.addCommonSchemaData(properties, log); assertEquals(1, log.getData().getProperties().length()); JSONObject a = log.getData().getProperties().optJSONObject("a"); assertNotNull(a); assertEquals("1", a.optString("b", null)); JSONObject c = a.optJSONObject("c"); assertNotNull(c); assertEquals("2", c.optString("d", null)); assertEquals("3", c.optString("e", null)); assertNull(log.getExt()); }
log.setData(new Data()); checkSerialization(serializer, log); log.getData().getProperties().put("baseType", "custom"); log.getData().getProperties().put("baseData", new JSONObject()); log.getData().getProperties().put("a", "b"); Log copy = serializer.deserializeLog(serializer.serializeLog(log), MockCommonSchemaLog.TYPE); assertEquals(log.getData().getProperties().remove("baseType"), csCopy.getData().getProperties().remove("baseType")); assertEquals(log.getData().getProperties().remove("baseData").toString(), csCopy.getData().getProperties().remove("baseData").toString()); assertEquals(log, copy);
@Test public void emptyProperties() { MockCommonSchemaLog log = new MockCommonSchemaLog(); CommonSchemaDataUtils.addCommonSchemaData(Collections.<TypedProperty>emptyList(), log); assertEquals(0, log.getData().getProperties().length()); assertNull(log.getExt()); }
Data data = new Data(); data.getProperties().put("a", "b"); a.setData(data); checkNotEquals(a, b);
@Test public void coverJSONException() throws Exception { /* Fake JSON exception to cover the checked exception that never happens. */ JSONObject value = mock(JSONObject.class); whenNew(JSONObject.class).withNoArguments().thenReturn(value); when(value.put(anyString(), any())).thenThrow(new JSONException("mock")); CommonSchemaLog commonSchemaLog = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); StringTypedProperty stringTypedProperty = new StringTypedProperty(); stringTypedProperty.setName("a"); stringTypedProperty.setValue("b"); properties.add(stringTypedProperty); CommonSchemaDataUtils.addCommonSchemaData(properties, commonSchemaLog); assertEquals(0, commonSchemaLog.getData().getProperties().length()); } }
expectedData.put("a", aData); expectedData.put("b", bData); assertEquals(expectedData.toString(), log.getData().getProperties().toString());
expectedData.put("a", aData); expectedData.put("b", bData); assertEquals(expectedData.toString(), log.getData().getProperties().toString());