566315437de858f0158493ea10f09ebd624aa7f5
[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 org.junit.jupiter.api.Test;
24
25 import java.util.Map;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28
29 public class StndDefinedValidatorBuilderTest {
30
31     private static final String TEST_RESOURCES = "src/main/test/resources/externalRepo/";
32
33     @Test
34     void shouldGenerateValidatorWithAllSchemaMappings() {
35         //when
36         StndDefinedValidator validator = getValidator("schema-map.json");
37
38         Map<String, String> mappingsCache = getMappingsCache(validator);
39
40         //then
41         assertThat(mappingsCache.size()).isEqualTo(6);
42     }
43
44     @Test
45     void shouldGenerateValidatorWithoutSchemaMappingsWithReferenceToNotExistingLocalResource() {
46         //when
47         StndDefinedValidator validator = getValidator("schema-map-no-local-resource.json");
48         Map<String, String> mappingsCache = getMappingsCache(validator);
49
50         //then
51         assertThat(mappingsCache.size()).isEqualTo(5);
52     }
53
54     @Test
55     void shouldGenerateValidatorWithoutSchemaMappingsWithEmptyLocalFileContent() {
56         //when
57         StndDefinedValidator validator = getValidator("schema-map-empty-content.json");
58         Map<String, String> mappingsCache = getMappingsCache(validator);
59
60         //then
61         assertThat(mappingsCache.size()).isEqualTo(4);
62     }
63
64     @Test
65     void shouldGenerateValidatorWithoutSchemaMappingsWithIncorrectYamlFormat() {
66         //when
67         StndDefinedValidator validator = getValidator("schema-map-incorrect-yaml-format.json");
68         Map<String, String> mappingsCache = getMappingsCache(validator);
69
70         //then
71         assertThat(mappingsCache.size()).isEqualTo(3);
72     }
73
74     @Test
75     void shouldGenerateValidatorWithoutSchemaMappingsWhenSchemaMappingFileHasNotJsonFormat() {
76         //when
77         StndDefinedValidator validator = getValidator("schema-map-no-json-format.json");
78         Map<String, String> mappingsCache = getMappingsCache(validator);
79
80         //then
81         assertThat(mappingsCache.size()).isEqualTo(0);
82     }
83
84     @Test
85     void shouldGenerateValidatorWithoutSchemaMappingsWhenSchemaMappingFileHasWrongFieldName() {
86         //when
87         StndDefinedValidator validator = getValidator("schema-map-wrong-field-name.json");
88         Map<String, String> mappingsCache = getMappingsCache(validator);
89
90         //then
91         assertThat(mappingsCache.size()).isEqualTo(0);
92     }
93
94     private StndDefinedValidator getValidator(String mappingFilePath) {
95         return new StndDefinedValidator.ValidatorBuilder()
96                 .mappingFilePath(TEST_RESOURCES + mappingFilePath)
97                 .schemasPath(TEST_RESOURCES)
98                 .schemaRefPath("/event/stndDefinedFields/schemaReference")
99                 .stndDefinedDataPath("/event/stndDefinedFields/data")
100                 .build();
101     }
102
103     private Map<String, String> getMappingsCache(StndDefinedValidator validator) {
104         return validator.getValidatorCache()
105                 .getSchemaReferenceMapper()
106                 .getUrlMapper()
107                 .getMappingsCache();
108     }
109 }