2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. 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.dcaegen2.services.sdk.services.external.schema.manager.service;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.junit.jupiter.api.Test;
26 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
27 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
29 import java.io.IOException;
31 import static org.junit.jupiter.api.Assertions.assertFalse;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
35 class StndDefinedValidatorTest {
37 private static final String TEST_RESOURCES = "src/main/test/resources/";
38 private static final String MAPPING_FILE_PATH = TEST_RESOURCES + "externalRepo/schema-map.json";
39 private static final String SCHEMAS_PATH = TEST_RESOURCES + "externalRepo";
41 private static final String VALID_EVENT_PATH = TEST_RESOURCES + "externalRepo/validEvent.json";
42 private static final String INVALID_EVENT_PATH = TEST_RESOURCES + "externalRepo/invalidEvent.json";
43 private static final String VALID_NO_HASH_EVENT_PATH = TEST_RESOURCES + "externalRepo/validNoHashEvent.json";
44 private static final String INCORRECT_INTERNAL_REF_EVENT_PATH = TEST_RESOURCES + "externalRepo/incorrectHashEvent.json";
46 private final ObjectMapper objectMapper = new ObjectMapper();
47 private final StndDefinedValidator validator = new StndDefinedValidator.ValidatorBuilder()
48 .mappingFilePath(MAPPING_FILE_PATH)
49 .schemasPath(SCHEMAS_PATH)
53 void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidEventIsGiven()
56 FileReader fileReader = new FileReader(VALID_EVENT_PATH);
57 JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
60 boolean validationResult = validator.validate(validEventNode);
63 assertTrue(validationResult);
67 void shouldValidateStndDefinedFieldsInEventAndReturnFalseWhenInvalidEventIsGiven()
70 FileReader fileReader = new FileReader(INVALID_EVENT_PATH);
71 JsonNode invalidEventNode = objectMapper.readTree(fileReader.readFile());
74 boolean validationResult = validator.validate(invalidEventNode);
77 assertFalse(validationResult);
81 void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidSchemaReferenceHasNoHash()
84 FileReader fileReader = new FileReader(VALID_NO_HASH_EVENT_PATH);
85 JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
88 boolean validationResult = validator.validate(validEventNode);
91 assertTrue(validationResult);
95 void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToNotExistingLocalSchema()
98 String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-no-local-resource.json";
99 StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath);
100 FileReader fileReader = new FileReader(VALID_EVENT_PATH);
101 JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
105 assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
109 void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToEmptySchema()
112 String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-empty-content.json";
113 StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath);
114 FileReader fileReader = new FileReader(VALID_EVENT_PATH);
115 JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
119 assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
123 void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectYamlFormatSchema()
126 String noLocalResourceMappingFilePath = TEST_RESOURCES + "externalRepo/schema-map-incorrect-yaml-format.json";
127 StndDefinedValidator validator = getValidator(noLocalResourceMappingFilePath);
128 JsonNode validEventNode = objectMapper.readTree(new FileReader(VALID_EVENT_PATH).readFile());
132 assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
136 void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectInternalFileReference()
139 JsonNode validEventNode = objectMapper.readTree(new FileReader(INCORRECT_INTERNAL_REF_EVENT_PATH).readFile());
143 assertThrows(IncorrectInternalFileReferenceException.class, () -> validator.validate(validEventNode));
147 private StndDefinedValidator getValidator(String noLocalResourceMappingFilePath) {
148 return new StndDefinedValidator.ValidatorBuilder()
149 .mappingFilePath(noLocalResourceMappingFilePath)
150 .schemasPath(SCHEMAS_PATH)