Commit b31dfd2f authored by devesh bajpai's avatar devesh bajpai
Browse files

Merge branch 'master' into global-status-monitoring

parents 0923aa59 139f563d
Pipeline #47381 passed with stages
in 5 minutes and 48 seconds
......@@ -15,14 +15,12 @@
limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.11.0-SNAPSHOT</version>
<packaging>jar</packaging>
......
......@@ -23,6 +23,7 @@ import org.opengroup.osdu.core.common.model.units.ReferenceConverter;
import org.opengroup.osdu.core.common.model.crs.ConversionRecord;
import org.opengroup.osdu.core.common.model.crs.ConvertStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
......@@ -119,49 +120,58 @@ public class UnitConversionImpl {
JsonObject data = record.getAsJsonObject(DATA);
for(int i = 0; i < propertyArray.size(); i++) {
String name = propertyArray.get(i).getAsString();
JsonElement valueElement = getJsonPropertyValueFromJsonObject(name, data);
if((null == valueElement) || (valueElement instanceof JsonNull)) {
String message = String.format(MISSING_PROPERTY, name);
conversionMessages.add(message);
continue;
}
try {
double value = valueElement.getAsDouble();
value = unit.convertToSI(value);
overrideNumberPropertyOfJsonObject(name, value, data);
String[] nameArray = splitJsonPropertiesByDots(name);
if (nameArray.length > 0 && PROPERTY_NAME_VALUE.equals(nameArray[nameArray.length - 1])) {
String unitKeyProperty = createUnitKeyPropertyName(nameArray);
if (isJsonPropertyPresentedInJsonObject(unitKeyProperty, data)) {
overrideStringPropertyOfJsonObject(unitKeyProperty, unit.getBaseSymbol(), data);
}
List<JsonElement> valueElements = getJsonPropertyValueFromJsonObject(name, data);
List<Number> convertedValues = new ArrayList<>();
for (int j = 0; j < valueElements.size(); j++) {
JsonElement valueElement = valueElements.get(j);
if((null == valueElement) || (valueElement instanceof JsonNull)) {
String message = String.format(MISSING_PROPERTY, constructPropertyName(name, j));
conversionMessages.add(message);
convertedValues.add(null);
continue;
}
try {
double value = valueElement.getAsDouble();
value = unit.convertToSI(value);
convertedValues.add(value);
}
catch(ClassCastException ccEx){
hasFailure = true;
String message = String.format(PROPERTY_VALUE_CAST_ERROR, constructPropertyName(name, j));
conversionMessages.add(message);
break;
}
catch(IllegalStateException isEx) {
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, constructPropertyName(name, j));
conversionMessages.add(message);
break;
}
catch(NumberFormatException nfEx){
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, constructPropertyName(name, j));
conversionMessages.add(message);
break;
}
catch(Exception ex) {
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, constructPropertyName(name, j));
conversionMessages.add(message);
break;
}
unitConverted = true;
}
catch(ClassCastException ccEx){
hasFailure = true;
String message = String.format(PROPERTY_VALUE_CAST_ERROR, name);
conversionMessages.add(message);
break;
}
catch(IllegalStateException isEx) {
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, name);
conversionMessages.add(message);
break;
}
catch(NumberFormatException nfEx){
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, name);
conversionMessages.add(message);
if (hasFailure) {
break;
}
catch(Exception ex){
hasFailure = true;
String message = String.format(ILLEGAL_PROPERTY_VALUE, name);
conversionMessages.add(message);
break;
overrideNumberPropertyOfJsonObject(name, convertedValues, data);
String[] nameArray = splitJsonPropertiesByDots(name);
if (nameArray.length > 0 && PROPERTY_NAME_VALUE.equals(nameArray[nameArray.length - 1])) {
String unitKeyProperty = createUnitKeyPropertyName(nameArray);
if (isJsonPropertyPresentedInJsonObject(unitKeyProperty, data)) {
overrideStringPropertyOfJsonObject(unitKeyProperty, unit.getBaseSymbol(), data);
}
}
unitConverted = true;
}
if(unitConverted && !hasFailure){
String basePersistableReference = unit.getBaseUnit();
......@@ -185,4 +195,15 @@ public class UnitConversionImpl {
unitKeyPropertyArray[unitKeyPropertyArray.length - 1] = PROPERTY_POSTFIX_UNIT_KEY;
return String.join(".", unitKeyPropertyArray);
}
private String constructPropertyName(String name, int index) {
if (!name.contains("[")) {
return name;
} else {
String target = name.substring(name.indexOf("["), name.indexOf("]") + 1);
String position = "[" + index + "]";
String result = name.replace(target, position);
return result;
}
}
}
......@@ -18,9 +18,13 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import static java.util.Optional.ofNullable;
public class JsonUtils {
private static final String PN_END = "]";
public static String jsonElementToString(JsonElement jsonElement) {
if (jsonElement == null) {
......@@ -41,24 +45,51 @@ public class JsonUtils {
/**
* @param propertyName - property name with path split by dots e.g. depth.value
* @param jsonObject - JsonObject which presumably contains the property
* @return JsonElement with property, if it found and null if not
* @return JsonElement list with property, if it found and null if not
*/
public static JsonElement getJsonPropertyValueFromJsonObject(String propertyName, JsonObject jsonObject) {
public static List<JsonElement> getJsonPropertyValueFromJsonObject(String propertyName, JsonObject jsonObject) {
List<JsonElement> result = new ArrayList<>();
String[] propertiesHierarchy = splitJsonPropertiesByDots(propertyName);
if (propertiesHierarchy[0].endsWith(PN_END)) {
return getNestedJsonPropertyValueFromJsonObject(propertiesHierarchy, jsonObject);
}
result.add(getNestedJsonElement(propertiesHierarchy, jsonObject));
return result;
}
private static List<JsonElement> getNestedJsonPropertyValueFromJsonObject(String[] propertyNestedNames, JsonObject jsonObject) {
List<JsonElement> result = new ArrayList<>();
String[] innerNestedNames = new String[propertyNestedNames.length - 1];
System.arraycopy(propertyNestedNames, 1, innerNestedNames, 0, propertyNestedNames.length - 1);
JsonArray elementArray = jsonObject.getAsJsonArray(propertyNestedNames[0].substring(0, propertyNestedNames[0].length() - 2 )) ;
for (int i = 0; i < elementArray.size(); i++) {
JsonObject element = elementArray.get(i).getAsJsonObject();
JsonElement elementValue = getNestedJsonElement(innerNestedNames, element);
result.add(elementValue);
}
return result;
}
private static JsonElement getNestedJsonElement(String[] nestedNames, JsonObject jsonObject) {
JsonObject json = jsonObject;
for (int i = 0; i < propertiesHierarchy.length; i++) {
if (i == propertiesHierarchy.length - 1) {
return json.get(propertiesHierarchy[i]);
} else {
JsonElement element = json.get(propertiesHierarchy[i]);
if (element == null || !element.isJsonObject()) {
return null;
for (int i = 0; i < nestedNames.length; i++) {
if (i == nestedNames.length - 1) {
return json.get(nestedNames[i]);
} else {
JsonElement element = json.get(nestedNames[i]);
if (element == null || !element.isJsonObject()) {
return null;
}
json = element.getAsJsonObject();
}
}
return null;
return json;
}
/**
......@@ -88,13 +119,35 @@ public class JsonUtils {
* @param value - the value of the property with Number type
* @param jsonObject - JsonObject which presumably contains the property
*/
public static void overrideNumberPropertyOfJsonObject(String propertyName, Number value, JsonObject jsonObject) {
public static void overrideNumberPropertyOfJsonObject(String propertyName, List<Number> value, JsonObject jsonObject) {
String[] nestedNames = splitJsonPropertiesByDots(propertyName);
if (nestedNames[0].endsWith(PN_END)) {
overrideNestedNumberPropertyOfJsonObject(nestedNames, value, jsonObject);
return;
}
JsonObject targetJsonObject = buildNewJsonObject(nestedNames, jsonObject);
ofNullable(targetJsonObject)
.ifPresent(json -> json.addProperty(nestedNames[nestedNames.length - 1], value));
.ifPresent(json -> json.addProperty(nestedNames[nestedNames.length - 1], value.get(0)));
}
private static void overrideNestedNumberPropertyOfJsonObject(String[] nestedNames, List<Number> values, JsonObject jsonObject) {
JsonArray elementArray = jsonObject.getAsJsonArray(nestedNames[0].substring(0, nestedNames[0].length() - 2 )) ;
String[] innerNestedNames = new String[nestedNames.length - 1];
System.arraycopy(nestedNames, 1, innerNestedNames, 0, nestedNames.length - 1);
for (int i = 0; i < elementArray.size(); i++) {
JsonObject element = elementArray.get(i).getAsJsonObject();
JsonObject targetJsonObject = buildNewJsonObject(innerNestedNames, element);
if (targetJsonObject != null) {
targetJsonObject.addProperty(innerNestedNames[innerNestedNames.length - 1], values.get(i));
}
}
}
/**
......
......@@ -14,10 +14,8 @@
package org.opengroup.osdu.core.common.crs;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -26,24 +24,27 @@ import org.opengroup.osdu.core.common.model.crs.ConversionRecord;
import org.opengroup.osdu.core.common.model.crs.ConvertStatus;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
@RunWith(PowerMockRunner.class)
public class UnitConversionTests {
private JsonParser jsonParser = new JsonParser();
private UnitConversionImpl unitConversion = new UnitConversionImpl();
private JsonObject testData;
@Before
public void setup() {
this.unitConversion = new UnitConversionImpl();
this.testData = getTestData();
}
@Test
public void shouldReturnOriginalRecordWhenMetaIsMissing() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"acl\": {\"viewers\": [\"viewers@unittest.com\"],\"owners\": [\"owners@unittest.com\"]},\"legal\": {\"legaltags\": [\"unit-test-legal\"],\"otherRelevantDataCountries\": [\"AA\"]},\"data\": {\"msg\": \"testing record 1\",\"X\": 16.00,\"Y\": 10.00,\"Z\": 0}}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("metaIsMissing");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -58,8 +59,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenUnitIsMissingInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"acl\": {\"viewers\": [\"viewers@unittest.com\"],\"owners\": [\"owners@unittest.com\"]},\"legal\": {\"legaltags\": [\"unit-test-legal\"],\"otherRelevantDataCountries\": [\"AA\"]},\"data\": {\"msg\": \"testing record 1\",\"X\": 16.00,\"Y\": 10.00,\"Z\": 0},\"meta\": [{\"path\": \"\",\"kind\": \"CRS\",\"persistableReference\": \"reference\",\"propertyNames\": [\"X\",\"Y\",\"Z\"],\"name\": \"name\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("unitMissingInMeta");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -73,8 +73,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenReferenceIsMissingInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("persistableReferenceMissing");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -89,8 +88,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenReferenceIsInvalidInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"reference\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("persistableReferenceInvalid");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -105,8 +103,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyNamesAreMissingInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("propertyNamesMissingInMeta");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -121,8 +118,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenMetaDataKindIsMissingInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("metaDataKindIsMissing");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -137,8 +133,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyNamesAreNotArrayInMeta() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": \"MD\",\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("propertyNameAreNotArrayInMeta");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -153,8 +148,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyIsMissingInData() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"TVD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("propertyMissingInData");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -171,8 +165,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyValueIsNullInData() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": null},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("nullPropertyValueInData");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -189,8 +182,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyIsBadInData() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": \"Bad\"},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("badPropertyValueInData");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject) metaArray.get(0);
......@@ -215,8 +207,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnUpdatedRecordWhenUnitMetaAndDataAreValid() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("validDataAndMeta");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject) metaArray.get(0);
......@@ -243,8 +234,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnUpdatedRecordWhenUnitMetaAndDataAreValidAndNested() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": {\"value\": 10.0}},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD.value\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("validNestedDataAndMeta");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject) metaArray.get(0);
......@@ -271,8 +261,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnOriginalRecordWhenPropertyValueIsNullInDataAndNested() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": {\"value\": null}},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"MD.value\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("nullNestedPropertyValueInData");
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
......@@ -282,15 +271,14 @@ public class UnitConversionTests {
Assert.assertEquals(1, conversionRecords.size());
Assert.assertTrue(conversionRecords.get(0).getConvertStatus() == ConvertStatus.SUCCESS);
String message = String.format(UnitConversionImpl.MISSING_PROPERTY, "MD.value");
Assert.assertTrue(conversionRecords.get(0).getConversionMessages().get(0).equalsIgnoreCase(message));
Assert.assertEquals(message, conversionRecords.get(0).getConversionMessages().get(0));
JsonObject resultRecord = conversionRecords.get(0).getRecordJsonObject();
Assert.assertEquals(record, resultRecord);
}
@Test
public void shouldReturnUpdatedRecordWhenPersistableReferenceIsJsonObject() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"MD\": 10.0},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": { \"scaleOffset\": {\"scale\": 0.3048, \"offset\": 0 }, \"symbol\": \"ft/s\", \"baseMeasurement\": { \"type\": \"UM\", \"ancestry\": \"Velocity\" }, \"type\": \"USO\" },\"propertyNames\": [\"MD\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("persistableAsJsonObject");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject) metaArray.get(0);
......@@ -317,8 +305,7 @@ public class UnitConversionTests {
@Test
public void shouldReturnUpdatedRecordWhenDataContainsDepthWitUnitKey() {
String stringRecord = "{\"id\": \"unit-test-1\",\"kind\": \"unit:test:1.0.0\",\"data\": {\"depth\":{ \"inner\" : { \"unitKey\":\"ft\", \"value\":10.0}}},\"meta\": [{\"path\": \"\",\"kind\": \"UNIT\",\"persistableReference\": \"%7B%22ScaleOffset%22%3A%7B%22Scale%22%3A0.3048%2C%22Offset%22%3A0.0%7D%2C%22Symbol%22%3A%22ft%22%2C%22BaseMeasurement%22%3A%22%257B%2522Ancestry%2522%253A%2522Length%2522%257D%22%7D\",\"propertyNames\": [\"depth.inner.value\"],\"name\": \"ft\"}]}";
JsonObject record = (JsonObject) this.jsonParser.parse(stringRecord);
JsonObject record = testData.getAsJsonObject("dataContainsDepthUnitKey");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject)metaArray.get(0);
......@@ -346,4 +333,83 @@ public class UnitConversionTests {
String resultName = resultMeta.get("name").getAsString();
Assert.assertEquals("m", resultName);
}
@Test
public void shouldReturnUpdatedRecordWhenDataContainsNestedArrayProperties() {
JsonObject record = testData.getAsJsonObject("validNestedArray");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
JsonObject meta = (JsonObject) metaArray.get(0);
String persistableReference = meta.get("persistableReference").getAsString();
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
conversionRecords.add(conversionRecord);
this.unitConversion.convertUnitsToSI(conversionRecords);
Assert.assertEquals(1, conversionRecords.size());
Assert.assertTrue(conversionRecords.get(0).getConversionMessages().size() == 0);
JsonObject resultRecord = conversionRecords.get(0).getRecordJsonObject();
JsonElement data = resultRecord.get("data");
JsonArray markers = data.getAsJsonObject().getAsJsonArray("markers");
JsonObject item1 = markers.get(0).getAsJsonObject();
double actualConvertedMeasuredDepthValue1 = item1.get("measuredDepth").getAsDouble();
Assert.assertEquals(3.048, actualConvertedMeasuredDepthValue1, 0.00001);
JsonObject item2 = markers.get(1).getAsJsonObject();
double actualConvertedMeasuredDepthValue2 = item2.get("measuredDepth").getAsDouble();
Assert.assertEquals(6.096, actualConvertedMeasuredDepthValue2, 0.00001);
JsonArray resultMetaArray = resultRecord.getAsJsonArray("meta");
Assert.assertEquals(1, resultMetaArray.size());
JsonObject resultMeta = (JsonObject) resultMetaArray.get(0);
String resultPersistableReference = resultMeta.get("persistableReference").getAsString();
Assert.assertTrue(persistableReference != resultPersistableReference);
String resultName = resultMeta.get("name").getAsString();
Assert.assertEquals("m", resultName);
}
@Test
public void shouldReturnOriginalRecordWhenNestedArrayPropertyValueTypeIsInvalidInDataAndNested() {
JsonObject record = testData.getAsJsonObject("nestedArrayWithInvalidValueType");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
conversionRecord.setConvertStatus(ConvertStatus.SUCCESS);
conversionRecords.add(conversionRecord);
this.unitConversion.convertUnitsToSI(conversionRecords);
Assert.assertEquals(1, conversionRecords.size());
Assert.assertTrue(conversionRecords.get(0).getConvertStatus() == ConvertStatus.ERROR);
String message = String.format(UnitConversionImpl.ILLEGAL_PROPERTY_VALUE, "markers[1].measuredDepth");
Assert.assertEquals(message, conversionRecords.get(0).getConversionMessages().get(0));
JsonObject resultRecord = conversionRecords.get(0).getRecordJsonObject();
Assert.assertEquals(record, resultRecord);
}
@Test
public void shouldReturnOriginalRecordWhenIOneOfNestedArrayPropertyValueIsMissingInDataAndNested() {
JsonObject record = testData.getAsJsonObject("nestedArrayWithMissingValue");
JsonArray metaArray = record.getAsJsonArray("meta");
Assert.assertEquals(1, metaArray.size());
List<ConversionRecord> conversionRecords = new ArrayList<>();
ConversionRecord conversionRecord = new ConversionRecord();
conversionRecord.setRecordJsonObject(record);
conversionRecord.setConvertStatus(ConvertStatus.SUCCESS);
conversionRecords.add(conversionRecord);
this.unitConversion.convertUnitsToSI(conversionRecords);
Assert.assertEquals(1, conversionRecords.size());
Assert.assertTrue(conversionRecords.get(0).getConvertStatus() == ConvertStatus.SUCCESS);
String message = String.format(UnitConversionImpl.MISSING_PROPERTY, "markers[1].measuredDepth");
Assert.assertEquals(message, conversionRecords.get(0).getConversionMessages().get(0));
JsonObject resultRecord = conversionRecords.get(0).getRecordJsonObject();
Assert.assertEquals(record, resultRecord);
}
private JsonObject getTestData() {
InputStream inStream = this.getClass().getResourceAsStream("/testdata/nested-data.json");
BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
Gson gson = new Gson();
JsonReader reader = new JsonReader(br);
JsonObject result = gson.fromJson(reader, JsonObject.class);
return result;
}
}
\ No newline at end of file
......@@ -22,6 +22,9 @@ import org.opengroup.osdu.core.common.util.JsonUtils;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.opengroup.osdu.core.common.util.JsonUtils.*;
......@@ -111,8 +114,8 @@ public class JsonUtilsTest {
setupMocksForJsonPropertyTests(internalJsonObject);
when(internalJsonObject.get("value")).thenReturn(mockJsonPrimitive);
JsonElement jsonElement = getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject);
assertSame(mockJsonPrimitive, jsonElement);
List<JsonElement> jsonElement = getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject);
assertSame(mockJsonPrimitive, jsonElement.get(0));
verify(mockJsonObject, times(1)).get("depth");
verify(internalJsonObject, times(1)).get("value");
......@@ -126,7 +129,7 @@ public class JsonUtilsTest {
setupMocksForJsonPropertyTests(internalJsonObject);
when(internalJsonObject.get("value")).thenReturn(null);
assertNull(getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject));
assertNull(getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject).get(0));
verify(mockJsonObject, times(1)).get("depth");
verify(internalJsonObject, times(1)).get("value");
......@@ -139,12 +142,45 @@ public class JsonUtilsTest {
setupMocksForJsonPropertyTests(internalJsonObject);
assertNull(getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject));
assertNull(getJsonPropertyValueFromJsonObject(propertyName, mockJsonObject).get(0));
verify(mockJsonObject, times(1)).get("depth");
verify(internalJsonObject, times(1)).get("deeper");