CommonSchemaDataUtils { public static void addCommonSchemaData(List<TypedProperty> properties, CommonSchemaLog dest) { if (properties == null) { return; } try { Data data = new Data(); dest.setData(data); MetadataExtension metadata = new MetadataExtension(); for (TypedProperty property : properties) { Object value; try { value = validateProperty(property); } catch (IllegalArgumentException e) { AppCenterLog.warn(LOG_TAG, e.getMessage()); continue; } Integer metadataType = getMetadataType(property); String key = property.getName(); String[] keys = key.split("\\.", -1); int lastIndex = keys.length - 1; JSONObject destProperties = data.getProperties(); JSONObject destMetadata = metadata.getMetadata(); for (int i = 0; i < lastIndex; i++) { String subKey = keys[i]; JSONObject subDataObject = destProperties.optJSONObject(subKey); if (subDataObject == null) { if (destProperties.has(subKey)) { AppCenterLog.warn(LOG_TAG, "Property key '" + subKey + "' already has a value, the old value will be overridden."); } subDataObject = new JSONObject(); destProperties.put(subKey, subDataObject); } destProperties = subDataObject; destMetadata = addIntermediateMetadata(destMetadata, subKey); } String lastKey = keys[lastIndex]; if (destProperties.has(lastKey)) { AppCenterLog.warn(LOG_TAG, "Property key '" + lastKey + "' already has a value, the old value will be overridden."); } destProperties.put(lastKey, value); addLeafMetadata(metadataType, destMetadata, lastKey); } JSONObject dataObject = data.getProperties(); String baseType = dataObject.optString(BASE_TYPE, null); JSONObject baseData = dataObject.optJSONObject(BASE_DATA); if (baseType == null && baseData != null) { AppCenterLog.warn(LOG_TAG, "baseData was set but baseType is missing."); dataObject.remove(BASE_DATA); JSONObject baseMetaData = metadata.getMetadata().optJSONObject(METADATA_FIELDS); baseMetaData.remove(BASE_DATA); } if (baseType != null && baseData == null) { AppCenterLog.warn(LOG_TAG, "baseType was set but baseData is missing."); dataObject.remove(BASE_TYPE); } if (!cleanUpEmptyObjectsInMetadata(metadata.getMetadata())) { if (dest.getExt() == null) { dest.setExt(new Extensions()); } dest.getExt().setMetadata(metadata); } } catch (JSONException ignore) { } }  static void addCommonSchemaData(List<TypedProperty> properties, CommonSchemaLog dest);  }
@Test public void coverJSONException() throws Exception { 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()); }