Skip to content

Commit

Permalink
test: clarify an assertion failure
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Mar 12, 2020
1 parent baffa22 commit 20d9444
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions java/test/jmri/server/json/JsonHttpServiceTestBase.java
Expand Up @@ -67,7 +67,7 @@ public void testDoDelete() throws JsonException {
assertEquals("ID is 42", 42, ex.getId());
}
}

/**
* Validate a JSON schema request exists and is schema valid for the type.
*
Expand All @@ -78,10 +78,10 @@ public final void testDoSchema(String type) throws JsonException {
// protect against JUnit tests in Eclipse that test this class directly
assumeNotNull(service);
JsonNode schema = service.doSchema(type, false, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema);
validate(type, schema);
assertEquals("Type is in client schema", "jmri-json-" + type + "-client-message", schema.path(JSON.DATA).path(JSON.SCHEMA).path("title").asText());
schema = service.doSchema(type, true, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema);
validate(type, schema);
assertEquals("Type is in server schema", "jmri-json-" + type + "-server-message", schema.path(JSON.DATA).path(JSON.SCHEMA).path("title").asText());
// Suppress a warning message (see networknt/json-schema-validator#79)
JUnitAppender.checkForMessageStartingWith(
Expand All @@ -108,14 +108,14 @@ public final void testDoSchema(String type1, String type2) throws JsonException
// protect against JUnit tests in Eclipse that test this class directly
assumeNotNull(service);
JsonNode schema1 = service.doSchema(type1, false, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema1);
validate(type1, schema1);
JsonNode schema2 = service.doSchema(type2, false, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema2);
validate(type2, schema2);
assertEquals("Client schema objects are the same", schema1.path(JSON.DATA).path(JSON.SCHEMA), schema1.path(JSON.DATA).path(JSON.SCHEMA));
schema1 = service.doSchema(type1, true, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema1);
validate(type1, schema1);
schema2 = service.doSchema(type2, true, new JsonRequest(locale, JSON.V5, JSON.GET, 42));
validate(schema2);
validate(type2, schema2);
assertEquals("Server schema objects are the same", schema1.path(JSON.DATA).path(JSON.SCHEMA), schema1.path(JSON.DATA).path(JSON.SCHEMA));
// Suppress a warning message (see networknt/json-schema-validator#79)
JUnitAppender.checkForMessageStartingWith(
Expand All @@ -125,28 +125,41 @@ public final void testDoSchema(String type1, String type2) throws JsonException
/**
* Validate a JsonNode message produced by the JMRI JSON server against
* published JMRI JSON service schema. Asserts a failure if the node is not
* schema valid.
* schema valid. Assertion message will contain the type {@code contextual}
*
* @param node the node to validate
*/
public final void validate(JsonNode node) {
validate(node, true);
validate("contextual", node, true);
}

/**
* Validate a JsonNode message produced by the JMRI JSON server against
* published JMRI JSON service schema. Asserts a failure if the node is not
* schema valid.
*
* @param type the JSON object type
* @param node the node to validate
*/
public final void validate(String type, JsonNode node) {
validate(type, node, true);
}

/**
* Validate a JsonNode message against published JMRI JSON service schema.
* Asserts a failure if the node is not schema valid.
*
* @param type the JSON object type
* @param node the node to validate
* @param server true if the node is generated by a server; false if node is
* a client node
*/
public final void validate(JsonNode node, boolean server) {
public final void validate(String type, JsonNode node, boolean server) {
Assert.assertNotNull("Node is not null.", node);
try {
InstanceManager.getDefault(JsonSchemaServiceCache.class).validateMessage(node, server, new JsonRequest(locale, JSON.V5, JSON.GET, 0));
} catch (JsonException ex) {
Assert.fail("Unable to validate schema.");
Assert.fail("Unable to validate " + ((server) ? "server" : "client") + " schema for " + type + ".");
}
}

Expand Down

0 comments on commit 20d9444

Please sign in to comment.