96db0fd19df6dcb4010bdb18c432fe75f3420244
[sdc.git] /
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 import org.openecomp.core.validation.types.MessageContainer;
24 import org.openecomp.sdc.datatypes.error.ErrorMessage;
25 import org.openecomp.sdc.validation.Validator;
26 import org.openecomp.sdc.validation.util.ValidationTestUtil;
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29
30 import java.util.List;
31 import java.util.Map;
32
33 /**
34  * @author Avrahamg
35  * @since October 06, 2016
36  */
37 public class ContrailValidatorTest {
38
39   private static final String RESOURCE_PATH = "/org/openecomp/validation/validators/contrailvalidatorresources";
40   private Validator validator = new ContrailValidator();
41
42   @Test
43   public void testWarningMessageExistWhenConrailV1AndV2ResourcesCollidesInSameHeatFile() {
44     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
45         RESOURCE_PATH + "/collidesinsameheatfile/");
46     validateMessage(messages,
47         "WARNING: [CTL2]: HEAT Package includes both Contrail 2 and Contrail 3 " +
48             "resources. Contrail 2 resources can be found in  file 'first.yaml' , resources :" +
49             "'jsa_net1' . Contrail 3 resources can be found in  file 'first.yaml' , resources :" +
50             "'jsa_net2' ",
51         "first.yaml", 2);
52   }
53
54   @Test
55   public void testParseException(){
56     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
57         RESOURCE_PATH + "/parseException/");
58     validateMessage(messages,
59         "ERROR: [CTL4]: Invalid HEAT format problem - [while scanning for the next " +
60             "token\n" + "found character '\\t(TAB)' that cannot start any token. " +
61             "(Do not use \\t(TAB) for indentation)\n" +
62             " in 'reader', line 10, column 1:\n" +
63             "    \t\t\tresources:\n" +
64             "    ^\n" +
65             "]",
66         "first.yaml", 1);
67
68   }
69
70   @Test
71   public void testWarningMessageExistWhenConrailV1AndV2ResourcesCollidesInDifferentHeatFiles() {
72     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
73         RESOURCE_PATH + "/collidesindifferentheatfiles/");
74     validateMessage(messages,
75         "WARNING: [CTL2]: HEAT Package includes both Contrail 2 and Contrail 3 " +
76             "resources. Contrail 2 resources can be found in  file 'first.yaml' , resources :" +
77             "'jsa_net1', 'jsa_net3' . Contrail 3 resources can be found in  file 'second.yaml' , " +
78             "resources :'jsa_net2', 'jsa_net4',  file 'first.yaml' , resources :'jsa_net5' ",
79         "first.yaml", 3);
80   }
81
82   @Test
83   public void testWarningMessageNotExistWhenConrailV1AndV2ResourcesCollidesInNonHeatFile() {
84     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
85         RESOURCE_PATH + "/collidesinnontheatfiles/");
86     validateMessage(messages,
87         "WARNING: [CTL2]: HEAT Package includes both Contrail 2 and Contrail 3 " +
88             "resources. Contrail 2 resources can be found in  file 'first.yaml' , resources :" +
89             "'jsa_net1' . Contrail 3 resources can be found in  file 'second.yaml' , " +
90             "resources :'jsa_net2' ",
91         "first.yaml", 2);
92     ;
93   }
94
95   @Test
96   public void testWarningMessageNotExistWhenOnlyConrailV1Resources() {
97     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
98         RESOURCE_PATH + "/notcollides/");
99     validateMessage(messages,
100         "WARNING: [CTL3]: Contrail 2.x deprecated resource is in use, " +
101             "Resource ID [jsa_net1]", "first.yaml",
102         2);
103   }
104
105
106   @Test
107   public void testWarningMessageOnResourceWithContrailType() {
108     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
109         RESOURCE_PATH + "/validatenocontrailresource/");
110     validateMessage(messages,
111         "WARNING: [CTL3]: Contrail 2.x deprecated resource is in use, " +
112             "Resource ID [template_NetworkPolicy]",
113         "first.yaml", 1);
114   }
115
116   @Test
117   public void testInvalidHeatStructure(){
118     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
119         RESOURCE_PATH + "/invalidHeatStructure/");
120     validateMessage(messages,
121         "ERROR: [CTL1]: Invalid HEAT format problem - [The file 'first.yaml' " +
122             "has no content]",
123         "first.yaml", 1);
124   }
125
126   @Test
127   public void testInvalidHeatStructuredueToParsingError(){
128     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(validator,
129         RESOURCE_PATH + "/invalidHeatStructure/");
130     validateMessage(messages,
131         "ERROR: [CTL1]: Invalid HEAT format problem - [The file 'first.yaml' " +
132             "has no content]",
133         "first.yaml", 1);
134   }
135
136
137   private void validateMessage(Map<String, MessageContainer> messages, String expectedMessage,
138                                String fileNameWithErrorToCheck, int sizeOfFileMessageList) {
139     Assert.assertEquals(messages.size(), 1);
140     List<ErrorMessage> errorMessageList =
141         messages.get(fileNameWithErrorToCheck).getErrorMessageList();
142     Assert.assertEquals(errorMessageList.size(), sizeOfFileMessageList);
143     Assert.assertEquals(errorMessageList.get(0).getMessage(), expectedMessage);
144   }
145
146
147 }