Sorted out unit-test libraries in onboarding
[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     YamlValidator validator = new YamlValidator();
38     return ValidationTestUtil.testValidator(validator, path);
39
40   }
41
42   @Test
43   public void testValidYaml() {
44
45     Map<String, MessageContainer> messages = runValidation(
46         RESOURCE_PATH + "/valid_yaml/input/validHeat.yaml");
47     Assert.assertNotNull(messages);
48     Assert.assertEquals(messages.size(), 0);
49   }
50
51   @Test
52   public void testInvalidTabYaml() {
53
54     Map<String, MessageContainer> messages = runValidation(
55         RESOURCE_PATH + "/invalid_valid_yaml_structure/input/invalidYamlTab.yaml");
56     Assert.assertNotNull(messages);
57     Assert.assertEquals(messages.size(), 1);
58     ValidationTestUtil.validateErrorMessage(
59         messages.get("invalidYamlTab.yaml").getErrorMessageList().get(0).getMessage(),
60         "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
61         "while scanning for the next tokenfound character '\\t(TAB)' that cannot start " +
62             "any token. (Do not use \\t(TAB) for indentation) in 'reader', line 14, " +
63             "column 5:        \tadmin_state_up: true        ^");
64   }
65
66   @Test
67   public void testDuplicateKeyInYaml() {
68
69     Map<String, MessageContainer> messages =
70         runValidation(RESOURCE_PATH + "/duplicateKey.yaml");
71     Assert.assertNotNull(messages);
72     Assert.assertEquals(messages.size(), 1);
73     Assert.assertTrue(messages.containsKey("duplicateKey.yaml"));
74     ValidationTestUtil.validateErrorMessage(
75         messages.get("duplicateKey.yaml").getErrorMessageList().get(0).getMessage(),
76         "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
77         "while parsing MappingNode in 'reader', line 6, column 3:      " +
78             "Key_1_unique:      ^duplicate key: Key_2_not_unique in 'reader', line 31, " +
79             "column 1:        ^");
80   }
81
82   @Test
83   public void testInvalidYamlStructure() {
84
85     Map<String, MessageContainer> messages = runValidation(
86         RESOURCE_PATH + "/invalidYamlStructure.yaml");
87     Assert.assertNotNull(messages);
88     Assert.assertEquals(messages.size(), 1);
89     Assert.assertTrue(messages.containsKey("invalidYamlStructure.yaml"));
90     ValidationTestUtil.validateErrorMessage(
91         messages.get("invalidYamlStructure.yaml").getErrorMessageList().get(0).getMessage(),
92         "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
93         "while parsing a block mapping in 'reader', line 8, column 7:          " +
94             "admin_state_up: true          ^expected <block end>, but found BlockEntry in 'reader', " +
95             "line 10, column 7:          - shared: true          ^");
96   }
97
98   @Test
99   public void testEmptyYaml() {
100
101     Map<String, MessageContainer> messages =
102         runValidation(RESOURCE_PATH + "/emptyYaml.yaml");
103     Assert.assertNotNull(messages);
104     Assert.assertEquals(messages.size(), 1);
105     Assert.assertTrue(messages.containsKey("emptyYaml.yaml"));
106     ValidationTestUtil.validateErrorMessage(messages.get("emptyYaml.yaml").getErrorMessageList()
107             .get(0).getMessage(),
108         "ERROR: " +"[YML1]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
109         Messages.EMPTY_YAML_FILE.getErrorMessage());
110   }
111
112 }