2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.validation.impl.validators;
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;
32 public class YamlValidatorTest {
35 private static final String RESOURCE_PATH = "/org/openecomp/validation/validators/yaml_validator";
37 public Map<String, MessageContainer> runValidation(String path) {
38 YamlValidator validator = new YamlValidator();
39 return ValidationTestUtil.testValidator(validator, path);
44 public void testValidYaml() {
46 Map<String, MessageContainer> messages = runValidation(
47 RESOURCE_PATH + "/valid_yaml/input/validHeat.yaml");
48 Assert.assertNotNull(messages);
49 Assert.assertEquals(messages.size(), 0);
53 public void testInvalidTabYaml() {
55 Map<String, MessageContainer> messages = runValidation(
56 RESOURCE_PATH + "/invalid_valid_yaml_structure/input/invalidYamlTab.yaml");
57 Assert.assertNotNull(messages);
58 Assert.assertEquals(messages.size(), 1);
59 ValidationTestUtil.validateErrorMessage(
60 messages.get("invalidYamlTab.yaml").getErrorMessageList().get(0).getMessage(),
61 "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
62 "while scanning for the next tokenfound character '\\t(TAB)' that cannot start " +
63 "any token. (Do not use \\t(TAB) for indentation) in 'reader', line 14, " +
64 "column 5: \tadmin_state_up: true ^");
68 public void testDuplicateKeyInYaml() {
70 Map<String, MessageContainer> messages =
71 runValidation(RESOURCE_PATH + "/duplicateKey.yaml");
72 Assert.assertNotNull(messages);
73 Assert.assertEquals(messages.size(), 1);
74 Assert.assertTrue(messages.containsKey("duplicateKey.yaml"));
75 ValidationTestUtil.validateErrorMessage(
76 messages.get("duplicateKey.yaml").getErrorMessageList().get(0).getMessage(),
77 "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
78 "while parsing MappingNode in 'reader', line 6, column 3: " +
79 "Key_1_unique: ^duplicate key: Key_2_not_unique in 'reader', line 31, " +
84 public void testInvalidYamlStructure() {
86 Map<String, MessageContainer> messages = runValidation(
87 RESOURCE_PATH + "/invalidYamlStructure.yaml");
88 Assert.assertNotNull(messages);
89 Assert.assertEquals(messages.size(), 1);
90 Assert.assertTrue(messages.containsKey("invalidYamlStructure.yaml"));
91 ValidationTestUtil.validateErrorMessage(
92 messages.get("invalidYamlStructure.yaml").getErrorMessageList().get(0).getMessage(),
93 "ERROR: " +"[YML2]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
94 "while parsing a block mapping in 'reader', line 8, column 7: " +
95 "admin_state_up: true ^expected <block end>, but found BlockEntry in 'reader', " +
96 "line 10, column 7: - shared: true ^");
100 public void testEmptyYaml() {
102 Map<String, MessageContainer> messages =
103 runValidation(RESOURCE_PATH + "/emptyYaml.yaml");
104 Assert.assertNotNull(messages);
105 Assert.assertEquals(messages.size(), 1);
106 Assert.assertTrue(messages.containsKey("emptyYaml.yaml"));
107 ValidationTestUtil.validateErrorMessage(messages.get("emptyYaml.yaml").getErrorMessageList()
108 .get(0).getMessage(),
109 "ERROR: " +"[YML1]: "+ Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
110 Messages.EMPTY_YAML_FILE.getErrorMessage());