1 package org.onap.pnfsimulator.simulator.validation;
3 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import com.github.fge.jsonschema.core.exceptions.InvalidSchemaException;
6 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
7 import java.io.IOException;
9 import org.json.JSONObject;
10 import org.junit.jupiter.api.BeforeEach;
11 import org.junit.jupiter.api.Test;
13 class JSONValidatorTest {
15 private final static String VALID_SCHEMA_NAME = "valid-test-schema.json";
16 private final static String INVALID_SCHEMA_NAME = "invalid-test-schema.json";
18 private JSONValidator validator;
22 validator = new JSONValidator();
26 void validate_should_not_throw_given_valid_json() throws ProcessingException, IOException, ValidationException {
27 validator.validate(getValidJsonString(), getResourcePath(VALID_SCHEMA_NAME));
31 void validate_should_not_throw_when_optional_parameter_missing()
32 throws ProcessingException, IOException, ValidationException {
34 String invalidJsonString = new JSONObject()
35 .put("key1", "value1")
36 .put("key2", "value2")
39 validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME));
43 void validate_should_throw_when_mandatory_parameter_missing() {
45 String invalidJsonString = new JSONObject()
46 .put("key1", "value1")
47 .put("key3", "value3")
51 ValidationException.class,
52 () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
56 void validate_should_throw_when_invalid_json_format() {
57 String invalidJsonString = "{" +
58 "\"key1\": \"value1\"" +
59 "\"key2\": \"value2" +
64 () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
68 void validate_should_throw_when_invalid_schema_format() {
70 InvalidSchemaException.class,
71 () -> validator.validate(getValidJsonString(), getResourcePath(INVALID_SCHEMA_NAME)));
75 void validate_should_throw_when_invalid_schema_path() {
79 () -> validator.validate(getValidJsonString(), "/not/existing/path/schema.json"));
82 private String getResourcePath(String schemaFileName) {
83 URL result = getClass()
85 .getResource(schemaFileName);
88 throw new IllegalArgumentException("Given file doesn't exist");
92 .replace("file:", "");
96 private String getValidJsonString() {
97 return new JSONObject()
98 .put("key1", "value1")
99 .put("key2", "value2")
100 .put("key3", "value3")