@Override public Collection<CommonSchemaLog> toCommonSchemaLogs(Log log) { Collection<CommonSchemaLog> commonSchemaLogs = new LinkedList<>(); for (String transmissionTarget : log.getTransmissionTargetTokens()) { /* Part A common fields. */ CommonSchemaEventLog commonSchemaEventLog = new CommonSchemaEventLog(); /* Event name goes to Part A. */ EventLog eventLog = (EventLog) log; PartAUtils.setName(commonSchemaEventLog, eventLog.getName()); /* Add common Part A fields. */ PartAUtils.addPartAFromLog(log, commonSchemaEventLog, transmissionTarget); /* Part B, C and Part A metadata. */ CommonSchemaDataUtils.addCommonSchemaData(eventLog.getTypedProperties(), commonSchemaEventLog); commonSchemaLogs.add(commonSchemaEventLog); /* Copy tag. */ commonSchemaEventLog.setTag(log.getTag()); } return commonSchemaLogs; } }
@Test public void coverInit() { new CommonSchemaDataUtils(); }
value = validateProperty(property); } catch (IllegalArgumentException e) { AppCenterLog.warn(LOG_TAG, e.getMessage()); Integer metadataType = getMetadataType(property); destMetadata = addIntermediateMetadata(destMetadata, subKey); addLeafMetadata(metadataType, destMetadata, lastKey); if (!cleanUpEmptyObjectsInMetadata(metadata.getMetadata())) { if (dest.getExt() == null) { dest.setExt(new Extensions());
/** * Remove all empty children from JSON object. * <p> * For example, if a property is {"a.b.c": 3}, the metadata contains {"f": {"a": {"f": {"b": {"f": {"c":4}}}}}}. * When {"a.b": "a"} property overrides the metadata JSON object (since string type doesn't require metadata), * it would remove "b" but {"f": {"a": {"f": {}}}} remains in the metadata. * <p> * This method cleans up empty child objects in the given JSON object that were created while * building metadata but remained after its properties were overridden. * * @param object Parent JSON object. * @return true if the object has no children and safe to be removed from its parent. */ private static boolean cleanUpEmptyObjectsInMetadata(JSONObject object) { for (Iterator<String> iterator = object.keys(); iterator.hasNext(); ) { String childKey = iterator.next(); JSONObject child = object.optJSONObject(childKey); if (child != null) { if (cleanUpEmptyObjectsInMetadata(child)) { iterator.remove(); } } } return object.length() == 0; } }
@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()); }
@Test public void emptyProperties() { MockCommonSchemaLog log = new MockCommonSchemaLog(); CommonSchemaDataUtils.addCommonSchemaData(Collections.<TypedProperty>emptyList(), log); assertEquals(0, log.getData().getProperties().length()); assertNull(log.getExt()); }
b.setValue(2.2); properties.add(b); CommonSchemaDataUtils.addCommonSchemaData(properties, log);
b.setValue("2"); properties.add(b); CommonSchemaDataUtils.addCommonSchemaData(properties, log);
@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 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()); } }
@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")); }
properties.add(baseDataD); properties.add(typedProperty("baseData.e", "5")); CommonSchemaDataUtils.addCommonSchemaData(properties, log);
c.setValue(3.14); properties.add(c); CommonSchemaDataUtils.addCommonSchemaData(properties, log);
@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 invalidBaseDataRemovesBaseType() { MockCommonSchemaLog log = new MockCommonSchemaLog(); log.setExt(new Extensions()); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("baseType", "Some.Type")); properties.add(typedProperty("baseData", "value")); CommonSchemaDataUtils.addCommonSchemaData(properties, log); /* Check everything removed. */ assertEquals(0, log.getData().getProperties().length()); assertNull(log.getExt().getMetadata()); }
@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()); }
@Test public void unknownTypedProperty() { MockCommonSchemaLog log = new MockCommonSchemaLog(); TypedProperty typedProperty = new TypedProperty() { @Override public String getType() { return "unknown"; } }; typedProperty.setName("a"); CommonSchemaDataUtils.addCommonSchemaData(Collections.singletonList(typedProperty), log); /* Data is empty because the invalid property filtered out. */ assertEquals(0, log.getData().getProperties().length()); /* And we don't send metadata when using only standard types. */ assertNull(log.getExt()); }
@Test public void dateTimeTypedProperty() throws JSONException { MockCommonSchemaLog log = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); DateTimeTypedProperty property = new DateTimeTypedProperty(); property.setName("a"); property.setValue(new Date(100)); properties.add(property); CommonSchemaDataUtils.addCommonSchemaData(properties, log); /* Check data. */ assertEquals(1, log.getData().getProperties().length()); assertEquals(new Date(100), JSONDateUtils.toDate(log.getData().getProperties().getString("a"))); /* Check metadata. */ JSONObject expectedMetadata = new JSONObject(); JSONObject a = new JSONObject(); a.put("a", DATA_TYPE_DATETIME); expectedMetadata.put(METADATA_FIELDS, a); assertNotNull(log.getExt()); assertNotNull(log.getExt().getMetadata()); assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString()); }
@Test public void baseTypeOverriddenToBeInvalid() { MockCommonSchemaLog log = new MockCommonSchemaLog(); log.setExt(new Extensions()); List<TypedProperty> properties = new ArrayList<>(); properties.add(typedProperty("baseType", "Some.Type")); properties.add(typedProperty("baseType.something", "test")); properties.add(typedProperty("baseData.something", "test")); CommonSchemaDataUtils.addCommonSchemaData(properties, log); /* Check only string base type was kept. */ assertEquals(2, log.getData().getProperties().length()); assertEquals("Some.Type", log.getData().getProperties().optString("baseType")); assertNull(log.getExt().getMetadata()); } }
@Test public void overrideMetadataToNull() throws JSONException { MockCommonSchemaLog log = new MockCommonSchemaLog(); log.setExt(new Extensions()); List<TypedProperty> properties = new ArrayList<>(); LongTypedProperty a = new LongTypedProperty(); a.setName("a.b.c"); a.setValue(1); properties.add(a); StringTypedProperty b = new StringTypedProperty(); b.setName("a.b"); b.setValue("2"); properties.add(b); CommonSchemaDataUtils.addCommonSchemaData(properties, log); /* Check data. */ JSONObject aData = new JSONObject(); aData.put("b", "2"); JSONObject expectedData = new JSONObject(); expectedData.put("a", aData); assertEquals(expectedData.toString(), log.getData().getProperties().toString()); /* Check metadata is null */ assertNull(log.getExt().getMetadata()); }