Skip to content

Commit

Permalink
Enable metadata schema validation (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Nov 21, 2022
1 parent a1c0b25 commit 50444ea
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bin/test_registrar
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ cat "${TEST_SITE}/registration_summary.json"
echo

clean_devices=$(fgrep 'Z"' ${TEST_SITE}/registration_summary.json | wc -l)

EXPECTED_CLEAN=3 # AHU-1 has a bad site location, so does not match as clean.
exit_status=0

echo Found ${clean_devices} clean devices.
[ "${clean_devices}" == 4 ] || exit_status=1
[ "${clean_devices}" == $EXPECTED_CLEAN ] || exit_status=1

device_files=$(ls -d ${TEST_SITE}/devices/*)

Expand Down
11 changes: 9 additions & 2 deletions tests/downgrade.site/devices/DWN-1/expected/metadata_norm.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
{
"timestamp": "2020-05-01T13:39:07Z",
"version": "1.3.14",
"hash": "1064d284",
"hash": "c62d55be",
"cloud": {
"auth_type": "RS256"
},
"system": {
"location": {
"site": "AA-TEST-SITE",
"site": "US-SFO-XYY",
"section": "2-3N8C",
"position": {
"x": -111.0,
"y": 102.3
}
},
"physical_tag": {
"asset": {
"guid": "bim://04aEp5ymD_$u5IxhJN2aGi",
"site": "US-SFO-XYY",
"name": "AHU-1"
}
},
"min_loglevel": 300
},
"localnet": {
Expand Down
11 changes: 9 additions & 2 deletions tests/downgrade.site/devices/DWN-2/expected/metadata_norm.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
{
"timestamp": "2020-05-01T13:39:07Z",
"version": "1.3.14",
"hash": "1064d284",
"hash": "c62d55be",
"cloud": {
"auth_type": "RS256"
},
"system": {
"location": {
"site": "AA-TEST-SITE",
"site": "US-SFO-XYY",
"section": "2-3N8C",
"position": {
"x": -111.0,
"y": 102.3
}
},
"physical_tag": {
"asset": {
"guid": "bim://04aEp5ymD_$u5IxhJN2aGi",
"site": "US-SFO-XYY",
"name": "AHU-1"
}
},
"min_loglevel": 300
},
"localnet": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"timestamp" : "2020-05-01T13:39:07Z",
"version" : "1.4.0git ",
"version" : "1.4.0",
"system" : {
"min_loglevel" : 300,
"metrics_rate_sec" : 600
Expand Down
2 changes: 1 addition & 1 deletion tests/downgrade.site/registration_summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"DWN-2" : "devices/DWN-2"
},
"Version" : {
"main" : "1.3.14-90-g266b9b3"
"main" : "1.3.14-92-g6b250906"
}
}
9 changes: 8 additions & 1 deletion tests/downgrade.site/site_metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"system": {
"location": {
"site": "AA-TEST-SITE"
"site": "US-SFO-XYY"
},
"physical_tag": {
"asset": {
"guid": "bim://04aEp5ymD_$u5IxhJN2aGi",
"site": "US-SFO-XYY",
"name": "AHU-1"
}
}
}
}
41 changes: 41 additions & 0 deletions tests/metadata.tests/nosys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "1.4.0",
"timestamp": "2018-08-26T21:39:29.364Z",
"description": "Generic test example metadata file",
"cloud": {
"auth_type": "ES256"
},
"pointset": {
"points": {
"return_air_temperature_sensor": {
"units": "Degrees-Celsius",
"baseline_value": 20,
"baseline_tolerance": 2
},
"room_setpoint": {
"writable": true,
"units": "Degrees-Celsius",
"baseline_value": 20,
"baseline_state": "applied"
}
}
},
"discovery": {
"families": {
"bacnet": {
}
}
},
"testing": {
"targets": {
"invalid": {
"target_point": "return_air_temperature_sensor",
"target_value": -20
},
"failure": {
"target_point": "room_setpoint",
"target_value": -20
}
}
}
}
2 changes: 2 additions & 0 deletions tests/metadata.tests/nosys.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 schema violations found
object has missing required properties (["system"])
2 changes: 1 addition & 1 deletion validator/.idea/runConfigurations/Registrar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion validator/bin/registrar
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fi

error=0
echo java -cp $jarfile $mainclass -r $ROOT -s $site_dir $project_opt $*
java -cp $jarfile $mainclass -r $ROOT -s $site_dir $project_opt $* || error=$?
java -cp $jarfile $mainclass -r $ROOT -t -s $site_dir $project_opt $* || error=$?

echo Registrar complete, exit $error
exit $error
Original file line number Diff line number Diff line change
Expand Up @@ -301,23 +301,28 @@ private Metadata readMetadataWithValidation(boolean validate) {
return null;
}

JsonNode mergedMetadata = getMergedMetadata(instance);

try {
ProcessingReport report = schemas.get(METADATA_JSON).validate(instance);
ProcessingReport report = schemas.get(METADATA_JSON).validate(mergedMetadata);
if (validate) {
parseMetadataValidateProcessingReport(report);
}
} catch (ProcessingException | ValidationException e) {
exceptionMap.put(EXCEPTION_VALIDATING, e);
}
return JsonUtil.convertTo(Metadata.class, mergedMetadata);
}

JsonNode getMergedMetadata(JsonNode instance) {
try {
String intermediary = JsonUtil.stringify(instance);
if (siteMetadata == null) {
return JsonUtil.fromString(Metadata.class, intermediary);
return instance;
} else {
Map<String, Object> mergedMetadata = GeneralUtils.deepCopy(siteMetadata);
GeneralUtils.mergeObject(mergedMetadata, asMap(intermediary));
return JsonUtil.convertTo(Metadata.class, mergedMetadata);
return JsonUtil.convertTo(JsonNode.class, mergedMetadata);
}
} catch (Exception e) {
exceptionMap.put(EXCEPTION_READING, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.google.daq.mqtt.registrar;

import static com.google.udmi.util.JsonUtil.convertTo;
import static com.google.udmi.util.JsonUtil.stringify;
import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.main.JsonSchema;
import com.google.udmi.util.GeneralUtils;
import com.google.udmi.util.JsonUtil;
import java.util.Date;
import java.util.Map;
import org.junit.Test;
import udmi.schema.Metadata;

/**
* Unit tests for LocalDevice.
*/
public class LocalDeviceTest {

public static final String DEVICE_ID = "test_device";
public static final Map<String, JsonSchema> SCHEMAS = null;
public static final int FAKE_TIME = 1298213;
public static String MERGE_DATA = "{ \"testing\": 10 }";

@Test
public void getMergedEmptyMetadata() {
LocalDevice localDevice = getTestInstance(null);
JsonNode toMerge = JsonUtil.fromString(JsonNode.class, MERGE_DATA);
String original = stringify(toMerge);
JsonNode result = localDevice.getMergedMetadata(toMerge);
assertEquals("merged results with no side defaults", original, stringify(result));
}

@Test
public void getMergedSiteMetadata() {
Metadata siteMetadata = getSiteMetadata();
Metadata augmentedData = GeneralUtils.deepCopy(siteMetadata);
Metadata toMerge = new Metadata();
toMerge.timestamp = new Date(FAKE_TIME + 1);
augmentedData.timestamp = toMerge.timestamp;
toMerge.description = "testing";
augmentedData.description = toMerge.description;
LocalDevice localDevice = getTestInstance(siteMetadata);
Metadata result = convertTo(Metadata.class,
localDevice.getMergedMetadata(convertTo(JsonNode.class, toMerge)));
assertEquals("merged results with no side defaults", stringify(augmentedData),
stringify(result));
}

private Metadata getSiteMetadata() {
Metadata metadata = new Metadata();
metadata.timestamp = new Date(FAKE_TIME);
metadata.version = "testing";
return metadata;
}

private LocalDevice getTestInstance(Metadata siteMetadata) {
return new LocalDevice(null, null, DEVICE_ID, SCHEMAS, null, siteMetadata);
}
}

0 comments on commit 50444ea

Please sign in to comment.