### Question:
JsonSchemaValidator implements SchemaValidator { @Override public SchemaValidationResults validate(String rule) { try { JSONObject jsonRule = new JSONObject(rule); SchemaLoader.load(schema).validate(jsonRule); return SchemaValidationResults.success(); } catch (ValidationException e) { return mapAsSchemaValidationResults(e); } catch (Exception e) { return mapAsSchemaValidationResults(e); } }  @Override SchemaValidationResults validate(String rule);  }### Answer:
@Test void shouldReturnEmptyResultSetForValidRuleJson() { String json = JsonResourceUtils.getJsonResourceAsString("/json/validator/validRule.json"); SchemaValidationResults results = validator.validate(json); assertThat(results).isNotNull(); assertThat(results.hasErrors()).isFalse(); assertThat(results.getResults()).isEmpty(); }
@Test void shouldReturnSingleErrorResultForInvalidRuleJsonWithOneViolation() { String json = JsonResourceUtils.getJsonResourceAsString("/json/validator/invalidRuleWithOneViolation.json"); SchemaValidationResults results = validator.validate(json); assertThat(results).isNotNull(); assertThat(results.hasErrors()).isTrue(); assertThat(results.getResults()).containsExactly( SchemaValidationError.of("#: required key [actions] not found") ); }
@Test void shouldReturnErrorResultSetForInvalidRuleJsonWithMultipleViolations() { String json = JsonResourceUtils.getJsonResourceAsString("/json/validator/invalidRuleWithMultipleViolations.json"); SchemaValidationResults results = validator.validate(json); assertThat(results).isNotNull(); assertThat(results.hasErrors()).isTrue(); assertThat(results.getResults()).containsExactlyInAnyOrder( SchemaValidationError.of("#: required key [attributes] not found"), SchemaValidationError.of("#: required key [actions] not found"), SchemaValidationError.of("#: extraneous key [unknown] is not permitted"), SchemaValidationError.of("#/facts/0: required key [name] not found") ); }
@Test void shouldReturnSingleErrorResultWhenJsonCannotBeValidated() { String invalidJson = "!@#$"; SchemaValidationResults results = validator.validate(invalidJson); assertThat(results).isNotNull(); assertThat(results.getResults()).containsExactly( SchemaValidationError.of("Could not validate JSON. Exception JSONException has been thrown with message: " + "A JSONObject text must begin with '{' at 1 [character 2 line 1]") ); }