push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / test / java / org / openecomp / sdc / validation / impl / validators / validators / HeatValidatorTest.java
1 package org.openecomp.sdc.validation.impl.validators.validators;
2
3 import org.openecomp.sdc.validation.impl.validators.HeatValidator;
4 import org.openecomp.sdc.validation.impl.validators.ValidatorBaseTest;
5 import org.openecomp.core.validation.types.MessageContainer;
6
7 import org.testng.Assert;
8 import org.testng.annotations.Test;
9
10 import java.io.IOException;
11 import java.util.Map;
12
13 public class HeatValidatorTest extends ValidatorBaseTest {
14
15
16     @Test
17     public void testInvalidHeatFormat(){
18         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/invalid_heat_format/negative_test/input");
19
20         Assert.assertNotNull(messages);
21         Assert.assertEquals(messages.size(), 1);
22
23         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
24         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Invalid HEAT format problem - [Cannot create property=kuku for JavaBean=Resource{type='null', properties=null, metadata=null, depends_on=null, update_policy='null', deletion_policy='null'}\n" +
25                 " in 'reader', line 25, column 5:\n" +
26                 "        kuku: kuku\n" +
27                 "        ^\n" +
28                 "Unable to find property 'kuku' on class: org.openecomp.sdc.heat.datatypes.model.Resource\n" +
29                 " in 'reader', line 25, column 11:\n" +
30                 "        kuku: kuku\n" +
31                 "              ^\n" +
32                 "]");
33     }
34
35
36     @Test
37     public void testResourcesReferencesExistInHeat() throws IOException {
38         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/resource_references_exist_in_heat/negative_test/input");
39
40         Assert.assertNotNull(messages);
41         Assert.assertEquals(messages.size(), 1);
42
43         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 2);
44         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Referenced resource - not_existing_resource not found");
45         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "invalid get_resource syntax is in use - null , get_resource function should get the resource id of the referenced resource");
46     }
47
48
49     @Test
50     public void testGetResourceValueIsValid(){
51         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/get_resource_value_valid/negative_test/input");
52
53         Assert.assertNotNull(messages);
54         Assert.assertEquals(messages.size(), 1);
55
56         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 3);
57         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "invalid get_resource syntax is in use - [param_1, param_2] , get_resource function should get the resource id of the referenced resource");
58         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "invalid get_resource syntax is in use - {get_param=param_1} , get_resource function should get the resource id of the referenced resource");
59         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(2).getMessage(), "invalid get_resource syntax is in use - null , get_resource function should get the resource id of the referenced resource");
60     }
61
62     @Test
63     public void testTwoResourcesDoesNotHoldSameId() throws IOException {
64         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/two_resources_does_not_hold_same_id/positive_test/input");
65         Assert.assertNotNull(messages);
66         Assert.assertEquals(messages.size(), 0);
67     }
68
69     @Test
70     public void negativeTestGetParamPointToExistingParameter() throws IOException {
71         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/get_param_points_to_existing_parameter/negative_test/input");
72
73         Assert.assertNotNull(messages);
74         Assert.assertEquals(messages.size(), 1);
75
76         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
77         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Referenced parameter - not_existing_param_1 - not found, used in resource - server_pcrf_psm_001");
78     }
79
80     @Test
81     public void testGetAttrFromNested() throws IOException {
82         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/get_attr_from_nested/negative_test/input");
83
84         Assert.assertNotNull(messages);
85         Assert.assertEquals(messages.size(), 1);
86
87         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
88         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "get_attr attribute not found - nested_output in resource server_pcrf_psm_001");
89     }
90
91     @Test
92     public void testPropertiesMatchNestedParameters() throws IOException {
93         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/properties_match_nested_parameters/negative_test/input");
94
95         Assert.assertNotNull(messages);
96         Assert.assertEquals(messages.size(), 1);
97
98         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
99         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Referenced parameter not found in nested file - nested-pps_v1.0.yaml, resource name - server_pcrf_pps_001, parameter name - parameter_not_existing_in_nested");
100     }
101
102     @Test
103     public void testNovaPropertiesHasAssignedValue() throws IOException {
104         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/nova_properties_has_assigned_value/negative_test/input");
105
106         Assert.assertNotNull(messages);
107         Assert.assertEquals(messages.size(), 1);
108
109         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
110         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Missing both Image and Flavor in NOVA Server - nova_server_resource_missing_both");
111     }
112
113     @Test
114     public void testNoLoopsNesting() throws IOException {
115         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/no_loops_nesting/negative_test/input");
116
117         Assert.assertNotNull(messages);
118         Assert.assertEquals(messages.size(), 4);
119
120         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 2);
121         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "A resource has an invalid or unsupported type - null, Resource ID [server_pcrf_psm_002]");
122         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "Nested files loop - [hot-nimbus-psm_v1.0.yaml -- nested-psm_v1.0.yaml -- nested-points-to-hot-nimbus-psm.yaml -- hot-nimbus-psm_v1.0.yaml]");
123
124         Assert.assertEquals(messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().size(), 2);
125         Assert.assertEquals(messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().get(0).getMessage(), "Nested files loop - [nested-points-to-hot-nimbus-psm.yaml -- hot-nimbus-psm_v1.0.yaml -- nested-psm_v1.0.yaml -- nested-points-to-hot-nimbus-psm.yaml]");
126         Assert.assertEquals(messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().get(1).getMessage(), "Nested files loop - [nested-points-to-hot-nimbus-psm.yaml -- nested-psm_v1.0.yaml -- nested-points-to-hot-nimbus-psm.yaml]");
127
128         Assert.assertEquals(messages.get("yaml-point-to-itself.yaml").getErrorMessageList().size(), 1);
129         Assert.assertEquals(messages.get("yaml-point-to-itself.yaml").getErrorMessageList().get(0).getMessage(), "Nested files loop - [yaml-point-to-itself.yaml -- yaml-point-to-itself.yaml]");
130
131         Assert.assertEquals(messages.get("nested-psm_v1.0.yaml").getErrorMessageList().size(), 1);
132         Assert.assertEquals(messages.get("nested-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Nested files loop - [nested-psm_v1.0.yaml -- nested-points-to-hot-nimbus-psm.yaml -- hot-nimbus-psm_v1.0.yaml -- nested-psm_v1.0.yaml]");
133     }
134
135     @Test
136     public void testOnlyOneNovaPointsToOnePort() throws IOException {
137         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/one_nova_points_to_one_port/negative_test/input");
138
139         Assert.assertNotNull(messages);
140         Assert.assertEquals(messages.size(), 1);
141
142         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
143         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Resource Port oam1_int_port exceed allowed relations from NovaServer");
144     }
145
146     @Test
147     public void testServerGroupsPointedByServersDefinedCorrectly() throws IOException {
148         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/server_groups_defined_correctly/negative_test/input");
149
150         Assert.assertNotNull(messages);
151         Assert.assertEquals(messages.size(), 1);
152
153         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 3);
154         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Missing server group definition - BE_Affinity_2, nova_server_1");
155         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "Missing server group definition - BE_Affinity_2, nova_server_2");
156         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(2).getMessage(), "Referenced resource - BE_Affinity_1 not found");
157     }
158
159
160     @Test
161     public void testPolicyIsAffinityOrAntiAffinity() throws IOException {
162         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/policy_is_affinity_or_anti_affinity/negative_test/input");
163
164         Assert.assertNotNull(messages);
165         Assert.assertEquals(messages.size(), 1);
166
167         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
168         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Wrong policy in server group - pcrf_server_policies_1");
169     }
170
171
172     @Test
173     public void testEnvContentIsSubSetOfHeatParameters() throws IOException {
174         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/env_content_is_subset_of_heat/negative_test/input");
175
176         Assert.assertNotNull(messages);
177         Assert.assertEquals(messages.size(), 1);
178
179         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.env").getErrorMessageList().size(), 1);
180         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.env").getErrorMessageList().get(0).getMessage(), "Env file hot-nimbus-pps_v1.0.env includes a parameter not in HEAT - mock_param");
181     }
182
183     @Test
184     public void testDefaultValueAlignWithType() throws IOException {
185         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/default_value_align_with_type/negative_test/input");
186
187         Assert.assertNotNull(messages);
188         Assert.assertEquals(messages.size(), 1);
189
190         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
191         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Parameter - pcrf_pps_image_name_1 default value not align with type number");
192     }
193
194
195     @Test
196     public void testEnvParametersMatchDefinedHeatParameterTypes() throws IOException {
197         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/env_parameters_match_defined_types/negative_test/input");
198
199         Assert.assertNotNull(messages);
200         Assert.assertEquals(messages.size(), 1);
201
202         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.env").getErrorMessageList().size(), 1);
203         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.env").getErrorMessageList().get(0).getMessage(), "Parameter env value pcrf_pps_flavor_name not align with type");
204
205     }
206
207     @Test
208     public void testReferencedArtifactsExist() throws IOException {
209         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/referenced_artifacts_exist/negative_test/input");
210
211         Assert.assertNotNull(messages);
212         Assert.assertEquals(messages.size(), 1);
213
214         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
215         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Missing artifact - nimbus-ethernet");
216
217     }
218
219
220     @Test
221     public void testResourcesGroupWithNested() throws IOException {
222         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/resources_group_with_nested/negative_test/input");
223
224         Assert.assertNotNull(messages);
225         Assert.assertEquals(messages.size(), 3);
226
227         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 2);
228         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Referenced parameter not found in nested file - nested-from-resources-group.yaml, resource name - nested-from-resources-group.yaml, parameter name - property_not_in_nested");
229         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "OS::Heat::ResourceGroup resource with resource_def which is not pointing to nested heat file is not supported, Resource ID [resource_without_resources_group], resource_def type [OS::Nova::Server]");
230
231         Assert.assertEquals(messages.get("nested-pps_v1.0.yaml").getErrorMessageList().size(), 1);
232         Assert.assertEquals(messages.get("nested-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "Nested files loop - [nested-pps_v1.0.yaml -- nested-from-resources-group.yaml -- hot-nimbus-pps_v1.0.yaml -- nested-pps_v1.0.yaml]");
233
234         Assert.assertEquals(messages.get("nested-not-exist.yaml").getErrorMessageList().size(), 1);
235         Assert.assertEquals(messages.get("nested-not-exist.yaml").getErrorMessageList().get(0).getMessage(), "Missing nested file - nested-not-exist.yaml");
236     }
237
238
239     @Test
240     public void testResourceGroupWithInvalidType(){
241         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/resource_group_invalid_type/negative_test/input");
242
243         Assert.assertNotNull(messages);
244         Assert.assertEquals(messages.size(), 1);
245
246         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 3);
247         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "OS::Heat::ResourceGroup resource with resource_def which is not pointing to nested heat file is not supported, Resource ID [resource_with_resources_group_1], resource_def type [{get_param=pcrf_vnf_id}]");
248         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "OS::Heat::ResourceGroup resource with resource_def which is not pointing to nested heat file is not supported, Resource ID [resource_with_resources_group_2], resource_def type [OS::Nova::Server]");
249         Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(2).getMessage(), "A resource has an invalid or unsupported type - null, Resource ID [resource_with_resources_group_3]");
250     }
251
252
253     @Test
254     public void testNetworkPolicyAssociatedWithAttachPolicy() throws IOException {
255         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/network_policy_associated_with_attach_policy/input");
256
257         Assert.assertNotNull(messages);
258         Assert.assertEquals(messages.size(), 1);
259
260         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
261         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "NetworkPolicy not in use, Resource Id [not_used_server_pcrf_policy]");
262     }
263
264
265     @Test
266     public void testSecurityGroupsCalledByPort() throws IOException {
267         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/security_group_called_by_port/input");
268
269         Assert.assertNotNull(messages);
270         Assert.assertEquals(messages.size(), 1);
271
272         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 2);
273         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "SecurityGroup not in use, Resource Id [not_used_security_group]");
274         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "Port not bind to any NOVA Server, Resource Id [attach_policy_resource]");
275     }
276
277
278     @Test
279     public void testServerGroupCalledByServer() throws IOException {
280         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/server_group_called_by_nova_server/input");
281
282         Assert.assertNotNull(messages);
283         Assert.assertEquals(messages.size(), 1);
284
285         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
286         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "ServerGroup not in use, Resource Id [not_used_server_group]");
287
288     }
289
290
291     @Test
292     public void testSecurityGroupBaseFileNoPorts() throws IOException {
293         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/security_group_base_file_no_ports/input");
294
295         Assert.assertNotNull(messages);
296         Assert.assertEquals(messages.size(), 1);
297
298         Assert.assertEquals(messages.get("baseFile.yaml").getErrorMessageList().size(), 1);
299         Assert.assertEquals(messages.get("baseFile.yaml").getErrorMessageList().get(0).getMessage(), "SecurityGroup not in use, Resource Id [shared_security_group_id3]");
300     }
301
302
303     @Test
304     public void testDependsOn(){
305         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/depends_on_points_to_existing_resource/input");
306
307         Assert.assertNotNull(messages);
308         Assert.assertEquals(messages.size(), 1);
309
310         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 2);
311         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "a Missing resource in depend On Missing Resource ID [resource_not_exist]");
312         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "a Missing resource in depend On Missing Resource ID [resource_3]");
313     }
314
315
316     @Test
317     public void testSharedResourcesValidation(){
318         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/shared_resources/input");
319
320         Assert.assertNotNull(messages);
321         Assert.assertEquals(messages.size(), 1);
322
323         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 2);
324         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(), "SecurityGroup not in use, Resource Id [not_used_security_group]");
325         Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(1).getMessage(), "Port not bind to any NOVA Server, Resource Id [attach_policy_resource]");
326     }
327
328
329     @Test
330     public void testNoErrorWhenEmptyValueForParameterInEnv(){
331         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/env_empty_value/input");
332
333         Assert.assertNotNull(messages);
334         Assert.assertEquals(messages.size(), 1);
335
336         MessageContainer envMessages = messages.get("env_empty_value.env");
337         Assert.assertNull(envMessages);
338     }
339
340
341     @Test
342     public void testGetParamPseudoParameters(){
343         Map<String, MessageContainer> messages = runValidation("/openecomp/org/validation/validators/heat_validator/pseudo_parameters/input");
344
345         Assert.assertNotNull(messages);
346         Assert.assertEquals(messages.size(), 0);
347
348     }
349
350
351     @Override
352     public Map<String, MessageContainer> runValidation(String path) {
353         HeatValidator heatValidator = new HeatValidator();
354         return testValidator(heatValidator, path);
355     }
356 }