f53b70c5bd968196549f5cd43a558241f07e16c5
[dcaegen2/services/sdk.git] /
1 /*
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
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.dcaegen2.services.sdk.services.external.schema.manager.service;
22
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;
28
29 import java.io.IOException;
30
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;
34
35 class StndDefinedValidatorTest {
36
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";
40
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";
45
46     private final ObjectMapper objectMapper = new ObjectMapper();
47     private final StndDefinedValidator validator = new StndDefinedValidator.ValidatorBuilder()
48             .mappingFilePath(MAPPING_FILE_PATH)
49             .schemasPath(SCHEMAS_PATH)
50             .build();
51
52     @Test
53     void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidEventIsGiven()
54             throws IOException {
55         //given
56         FileReader fileReader = new FileReader(VALID_EVENT_PATH);
57         JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
58
59         //when
60         boolean validationResult = validator.validate(validEventNode);
61
62         //then
63         assertTrue(validationResult);
64     }
65
66     @Test
67     void shouldValidateStndDefinedFieldsInEventAndReturnFalseWhenInvalidEventIsGiven()
68             throws IOException {
69         //given
70         FileReader fileReader = new FileReader(INVALID_EVENT_PATH);
71         JsonNode invalidEventNode = objectMapper.readTree(fileReader.readFile());
72
73         //when
74         boolean validationResult = validator.validate(invalidEventNode);
75
76         //then
77         assertFalse(validationResult);
78     }
79
80     @Test
81     void shouldValidateStndDefinedFieldsInEventAndReturnTrueWhenValidSchemaReferenceHasNoHash()
82             throws IOException {
83         //given
84         FileReader fileReader = new FileReader(VALID_NO_HASH_EVENT_PATH);
85         JsonNode validEventNode = objectMapper.readTree(fileReader.readFile());
86
87         //when
88         boolean validationResult = validator.validate(validEventNode);
89
90         //then
91         assertTrue(validationResult);
92     }
93
94     @Test
95     void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToNotExistingLocalSchema()
96             throws IOException {
97         //given
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());
102
103         //when
104         //then
105         assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
106     }
107
108     @Test
109     void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToEmptySchema()
110             throws IOException {
111         //given
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());
116
117         //when
118         //then
119         assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
120     }
121
122     @Test
123     void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectYamlFormatSchema()
124             throws IOException {
125         //given
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());
129
130         //when
131         //then
132         assertThrows(NoLocalReferenceException.class, () -> validator.validate(validEventNode));
133     }
134
135     @Test
136     void shouldValidateStndDefinedFieldsInEventAndReturnExceptionWhenEventReferToIncorrectInternalFileReference()
137             throws IOException {
138         //given
139         JsonNode validEventNode = objectMapper.readTree(new FileReader(INCORRECT_INTERNAL_REF_EVENT_PATH).readFile());
140
141         //when
142         //then
143         assertThrows(IncorrectInternalFileReferenceException.class, () -> validator.validate(validEventNode));
144     }
145
146
147     private StndDefinedValidator getValidator(String noLocalResourceMappingFilePath) {
148         return new StndDefinedValidator.ValidatorBuilder()
149                 .mappingFilePath(noLocalResourceMappingFilePath)
150                 .schemasPath(SCHEMAS_PATH)
151                 .build();
152     }
153 }