d7398d0891e84832a409a149368c17f397d2d48c
[integration.git] /
1 package org.onap.pnfsimulator.simulator.validation;
2
3 import static org.junit.jupiter.api.Assertions.assertThrows;
4
5 import com.github.fge.jsonschema.core.exceptions.InvalidSchemaException;
6 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
7 import java.io.IOException;
8 import java.net.URL;
9 import org.json.JSONObject;
10 import org.junit.jupiter.api.BeforeEach;
11 import org.junit.jupiter.api.Test;
12
13 class JSONValidatorTest {
14
15     private final static String VALID_SCHEMA_NAME = "valid-test-schema.json";
16     private final static String INVALID_SCHEMA_NAME = "invalid-test-schema.json";
17
18     private JSONValidator validator;
19
20     @BeforeEach
21     void setUp() {
22         validator = new JSONValidator();
23     }
24
25     @Test
26     void validate_should_not_throw_given_valid_json() throws ProcessingException, IOException, ValidationException {
27         validator.validate(getValidJsonString(), getResourcePath(VALID_SCHEMA_NAME));
28     }
29
30     @Test
31     void validate_should_not_throw_when_optional_parameter_missing()
32         throws ProcessingException, IOException, ValidationException {
33
34         String invalidJsonString = new JSONObject()
35             .put("key1", "value1")
36             .put("key2", "value2")
37             .toString();
38
39         validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME));
40     }
41
42     @Test
43     void validate_should_throw_when_mandatory_parameter_missing() {
44
45         String invalidJsonString = new JSONObject()
46             .put("key1", "value1")
47             .put("key3", "value3")
48             .toString();
49
50         assertThrows(
51             ValidationException.class,
52             () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
53     }
54
55     @Test
56     void validate_should_throw_when_invalid_json_format() {
57         String invalidJsonString = "{" +
58             "\"key1\": \"value1\"" +
59             "\"key2\": \"value2" +
60             "}";
61
62         assertThrows(
63             IOException.class,
64             () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
65     }
66
67     @Test
68     void validate_should_throw_when_invalid_schema_format() {
69         assertThrows(
70             InvalidSchemaException.class,
71             () -> validator.validate(getValidJsonString(), getResourcePath(INVALID_SCHEMA_NAME)));
72     }
73
74     @Test
75     void validate_should_throw_when_invalid_schema_path() {
76
77         assertThrows(
78             IOException.class,
79             () -> validator.validate(getValidJsonString(), "/not/existing/path/schema.json"));
80     }
81
82     private String getResourcePath(String schemaFileName) {
83         URL result = getClass()
84             .getClassLoader()
85             .getResource(schemaFileName);
86
87         if (result == null) {
88             throw new IllegalArgumentException("Given file doesn't exist");
89         } else {
90             return result
91                 .toString()
92                 .replace("file:", "");
93         }
94     }
95
96     private String getValidJsonString() {
97         return new JSONObject()
98             .put("key1", "value1")
99             .put("key2", "value2")
100             .put("key3", "value3")
101             .toString();
102     }
103 }