Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / pnf-sim-lightweight / src / test / java / org / onap / pnfsimulator / simulator / validation / JSONValidatorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator.validation;
22
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import com.github.fge.jsonschema.core.exceptions.InvalidSchemaException;
26 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
27 import java.io.IOException;
28 import java.net.URL;
29 import org.json.JSONObject;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32
33 class JSONValidatorTest {
34
35     private final static String VALID_SCHEMA_NAME = "valid-test-schema.json";
36     private final static String INVALID_SCHEMA_NAME = "invalid-test-schema.json";
37
38     private JSONValidator validator;
39
40     @BeforeEach
41     void setUp() {
42         validator = new JSONValidator();
43     }
44
45     @Test
46     void validate_should_not_throw_given_valid_json() throws ProcessingException, IOException, ValidationException {
47         validator.validate(getValidJsonString(), getResourcePath(VALID_SCHEMA_NAME));
48     }
49
50     @Test
51     void validate_should_not_throw_when_optional_parameter_missing()
52         throws ProcessingException, IOException, ValidationException {
53
54         String invalidJsonString = new JSONObject()
55             .put("key1", "value1")
56             .put("key2", "value2")
57             .toString();
58
59         validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME));
60     }
61
62     @Test
63     void validate_should_throw_when_mandatory_parameter_missing() {
64
65         String invalidJsonString = new JSONObject()
66             .put("key1", "value1")
67             .put("key3", "value3")
68             .toString();
69
70         assertThrows(
71             ValidationException.class,
72             () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
73     }
74
75     @Test
76     void validate_should_throw_when_invalid_json_format() {
77         String invalidJsonString = "{" +
78             "\"key1\": \"value1\"" +
79             "\"key2\": \"value2" +
80             "}";
81
82         assertThrows(
83             IOException.class,
84             () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
85     }
86
87     @Test
88     void validate_should_throw_when_invalid_schema_format() {
89         assertThrows(
90             InvalidSchemaException.class,
91             () -> validator.validate(getValidJsonString(), getResourcePath(INVALID_SCHEMA_NAME)));
92     }
93
94     @Test
95     void validate_should_throw_when_invalid_schema_path() {
96
97         assertThrows(
98             IOException.class,
99             () -> validator.validate(getValidJsonString(), "/not/existing/path/schema.json"));
100     }
101
102     private String getResourcePath(String schemaFileName) {
103         URL result = getClass()
104             .getClassLoader()
105             .getResource(schemaFileName);
106
107         if (result == null) {
108             throw new IllegalArgumentException("Given file doesn't exist");
109         } else {
110             return result
111                 .toString()
112                 .replace("file:", "");
113         }
114     }
115
116     private String getValidJsonString() {
117         return new JSONObject()
118             .put("key1", "value1")
119             .put("key2", "value2")
120             .put("key3", "value3")
121             .toString();
122     }
123 }