Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable metadata schema validation #504

Merged
merged 7 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this automatic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by automatic?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic = Can we derive the 4 from a calculation based on the test inputs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm semi ok with this if we make it less of a hunt to figure out why it's 3, instead of it just being a bare int by itself in a file that calculates the rest. For example

Clean devices are: SNS-4 GAT-123 AHU-22

[ "${clean_devices}" == 3 ].....

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not sure it's worth it in this case. The logic to calculate that
for just this one case would be relatively complicated (find all the files
that don't match a very specific pattern that just matches one file), and
then likely would end up bring more fragile than just having the raw
expected value -- and then you'd also have to worry about an inadvertent
change in the input data changing the expected and actual value for the
test...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok -- added named constant with detail about why it is what it is!

[ "${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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests for this function please. It might need to be broken up into pieces?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added class LocalDeviceTest with coverage for the two major caes


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);
}
}