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;
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;
31 public class YamlValidatorTest {
34 private static final String RESOURCE_PATH = "/org/openecomp/validation/validators/yaml_validator";
36 public Map<String, MessageContainer> runValidation(String path) {
37 YamlValidator validator = new YamlValidator();
38 return ValidationTestUtil.testValidator(validator, path);
43 public void testValidYaml() {
45 Map<String, MessageContainer> messages = runValidation(
46 RESOURCE_PATH + "/valid_yaml/input/validHeat.yaml");
47 Assert.assertNotNull(messages);
48 Assert.assertEquals(messages.size(), 0);
52 public void testInvalidTabYaml() {
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 ^");
67 public void testDuplicateKeyInYaml() {
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, " +
83 public void testInvalidYamlStructure() {
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 ^");
99 public void testEmptyYaml() {
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());