[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 org.openecomp.core.validation.types.MessageContainer;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.validation.util.ValidationTestUtil;
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29
30 import java.util.Map;
31
32 public class YamlValidatorTest {
33
34
35   public Map<String, MessageContainer> runValidation(String path) {
36     YamlValidator validator = new YamlValidator();
37     return ValidationTestUtil.testValidator(validator, path);
38
39   }
40
41   @Test
42   public void testValidYaml() {
43
44     Map<String, MessageContainer> messages = runValidation(
45         "/org/openecomp/validation/validators/yaml_validator/valid_yaml/input/validHeat.yaml");
46     Assert.assertNotNull(messages);
47     Assert.assertEquals(messages.size(), 0);
48   }
49
50   @Test
51   public void testInvalidTabYaml() {
52
53     Map<String, MessageContainer> messages = runValidation(
54         "/org/openecomp/validation/validators/yaml_validator/invalid_valid_yaml_structure/input/invalidYamlTab.yaml");
55     Assert.assertNotNull(messages);
56     Assert.assertEquals(messages.size(), 1);
57     ValidationTestUtil.validateErrorMessage(
58         messages.get("invalidYamlTab.yaml").getErrorMessageList().get(0).getMessage(),
59         "ERROR: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
60         "while scanning for the next tokenfound character '\\t(TAB)' that cannot start any token. (Do not use \\t(TAB) for indentation) in 'reader', line 14, column 5:        \tadmin_state_up: true        ^");
61
62   }
63
64
65   @Test
66   public void testDuplicateKeyInYaml() {
67
68     Map<String, MessageContainer> messages =
69         runValidation("/org/openecomp/validation/validators/yaml_validator/duplicateKey.yaml");
70     Assert.assertNotNull(messages);
71     Assert.assertEquals(messages.size(), 1);
72     Assert.assertTrue(messages.containsKey("duplicateKey.yaml"));
73     ValidationTestUtil.validateErrorMessage(
74         messages.get("duplicateKey.yaml").getErrorMessageList().get(0).getMessage(),
75         "ERROR: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
76         "while parsing MappingNode in 'reader', line 6, column 3:      Key_1_unique:      ^duplicate key: Key_2_not_unique in 'reader', line 31, column 1:        ^");
77   }
78
79
80   @Test
81   public void testInvalidYamlStructure() {
82
83     Map<String, MessageContainer> messages = runValidation(
84         "/org/openecomp/validation/validators/yaml_validator/invalidYamlStructure.yaml");
85     Assert.assertNotNull(messages);
86     Assert.assertEquals(messages.size(), 1);
87     Assert.assertTrue(messages.containsKey("invalidYamlStructure.yaml"));
88     ValidationTestUtil.validateErrorMessage(
89         messages.get("invalidYamlStructure.yaml").getErrorMessageList().get(0).getMessage(),
90         "ERROR: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
91         "while parsing a block mapping in 'reader', line 8, column 7:          admin_state_up: true          ^expected <block end>, but found BlockEntry in 'reader', line 10, column 7:          - shared: true          ^");
92   }
93
94   @Test
95   public void testEmptyYaml() {
96
97     Map<String, MessageContainer> messages =
98         runValidation("/org/openecomp/validation/validators/yaml_validator/emptyYaml.yaml");
99     Assert.assertNotNull(messages);
100     Assert.assertEquals(messages.size(), 1);
101     Assert.assertTrue(messages.containsKey("emptyYaml.yaml"));
102     ValidationTestUtil.validateErrorMessage(messages.get("emptyYaml.yaml").getErrorMessageList().get(0).getMessage(),
103         "ERROR: " + Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
104         Messages.EMPTY_YAML_FILE.getErrorMessage());
105   }
106
107 }