Upgrade Vulnerable Direct Dependencies [snakeyaml]
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / test / java / org / openecomp / sdc / validation / impl / validators / YamlValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.openecomp.sdc.validation.impl.validators;
22
23
24 import java.util.Map;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.core.validation.types.MessageContainer;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.validation.util.ValidationTestUtil;
30
31 public class YamlValidatorTest {
32
33
34     private static final String RESOURCE_PATH = "/org/openecomp/validation/validators/yaml_validator";
35
36     public Map<String, MessageContainer> runValidation(String path) {
37         return new ValidationTestUtil().testValidator(new YamlValidator(), path);
38     }
39
40     @Test
41     public void testValidYaml() {
42         Map<String, MessageContainer> messages = runValidation(RESOURCE_PATH + "/valid_yaml/input/validHeat.yaml");
43         Assert.assertNotNull(messages);
44         Assert.assertEquals(0, messages.size());
45     }
46
47     @Test
48     public void testInvalidTabYaml() {
49
50         Map<String, MessageContainer> messages = runValidation(
51                 RESOURCE_PATH + "/invalid_valid_yaml_structure/input/invalidYamlTab.yaml");
52         Assert.assertNotNull(messages);
53         Assert.assertEquals(1, messages.size());
54         new ValidationTestUtil().validateErrorMessage(
55                 messages.get("invalidYamlTab.yaml").getErrorMessageList().get(0).getMessage(),
56                 "ERROR: " + "[YML2]: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
57                 "while scanning for the next tokenfound character '\\t(TAB)' that cannot start " +
58                         "any token. (Do not use \\t(TAB) for indentation) in 'reader', line 14, " +
59                         "column 5:        \tadmin_state_up: true        ^");
60     }
61
62     @Test
63     public void testDuplicateKeyInYaml() {
64         Map<String, MessageContainer> messages = new ValidationTestUtil().testValidator(new YamlValidator(), RESOURCE_PATH + "/duplicateKey.yaml");
65         Assert.assertNotNull(messages);
66         Assert.assertEquals(1, messages.size());
67         Assert.assertTrue(messages.containsKey("duplicateKey.yaml"));
68         Assert.assertTrue(messages.get("duplicateKey.yaml").getErrorMessageList().get(0).getMessage().contains("Key_2_not_unique"));
69     }
70
71     @Test
72     public void testInvalidYamlStructure() {
73         Map<String, MessageContainer> messages = runValidation(RESOURCE_PATH + "/invalidYamlStructure.yaml");
74         Assert.assertNotNull(messages);
75         Assert.assertEquals(1, messages.size());
76         Assert.assertTrue(messages.containsKey("invalidYamlStructure.yaml"));
77         new ValidationTestUtil().validateErrorMessage(
78                 messages.get("invalidYamlStructure.yaml").getErrorMessageList().get(0).getMessage(),
79                 "ERROR: " + "[YML2]: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
80                 "while parsing a block mapping in 'reader', line 8, column 7:          " +
81                         "admin_state_up: true          ^expected <block end>, but found '-' in 'reader', " +
82                         "line 10, column 7:          - shared: true          ^");
83     }
84
85     @Test
86     public void testEmptyYaml() {
87         Map<String, MessageContainer> messages = runValidation(RESOURCE_PATH + "/emptyYaml.yaml");
88         Assert.assertNotNull(messages);
89         Assert.assertEquals(1, messages.size());
90         Assert.assertTrue(messages.containsKey("emptyYaml.yaml"));
91         new ValidationTestUtil().validateErrorMessage(messages.get("emptyYaml.yaml").getErrorMessageList()
92                         .get(0).getMessage(),
93                 "ERROR: " + "[YML1]: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
94                 Messages.EMPTY_YAML_FILE.getErrorMessage());
95     }
96
97 }