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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.simulator.validation;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import com.github.fge.jsonschema.core.exceptions.InvalidSchemaException;
26 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
27 import java.io.IOException;
29 import org.json.JSONObject;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
33 class JSONValidatorTest {
35 private final static String VALID_SCHEMA_NAME = "valid-test-schema.json";
36 private final static String INVALID_SCHEMA_NAME = "invalid-test-schema.json";
38 private JSONValidator validator;
42 validator = new JSONValidator();
46 void validate_should_not_throw_given_valid_json() throws ProcessingException, IOException, ValidationException {
47 validator.validate(getValidJsonString(), getResourcePath(VALID_SCHEMA_NAME));
51 void validate_should_not_throw_when_optional_parameter_missing()
52 throws ProcessingException, IOException, ValidationException {
54 String invalidJsonString = new JSONObject()
55 .put("key1", "value1")
56 .put("key2", "value2")
59 validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME));
63 void validate_should_throw_when_mandatory_parameter_missing() {
65 String invalidJsonString = new JSONObject()
66 .put("key1", "value1")
67 .put("key3", "value3")
71 ValidationException.class,
72 () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
76 void validate_should_throw_when_invalid_json_format() {
77 String invalidJsonString = "{" +
78 "\"key1\": \"value1\"" +
79 "\"key2\": \"value2" +
84 () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)));
88 void validate_should_throw_when_invalid_schema_format() {
90 InvalidSchemaException.class,
91 () -> validator.validate(getValidJsonString(), getResourcePath(INVALID_SCHEMA_NAME)));
95 void validate_should_throw_when_invalid_schema_path() {
99 () -> validator.validate(getValidJsonString(), "/not/existing/path/schema.json"));
102 private String getResourcePath(String schemaFileName) {
103 URL result = getClass()
105 .getResource(schemaFileName);
107 if (result == null) {
108 throw new IllegalArgumentException("Given file doesn't exist");
112 .replace("file:", "");
116 private String getValidJsonString() {
117 return new JSONObject()
118 .put("key1", "value1")
119 .put("key2", "value2")
120 .put("key3", "value3")