14a23d66d9d95a3cd0c53b52dd406c28c391567a
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
19 import org.openecomp.core.validation.types.GlobalValidationContext;
20 import org.openecomp.core.validation.types.MessageContainer;
21 import org.openecomp.sdc.heat.datatypes.model.Resource;
22 import org.openecomp.sdc.validation.impl.validators.HeatResourceValidator;
23 import org.openecomp.sdc.validation.util.ValidationTestUtil;
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
26
27 import java.util.Map;
28 import java.util.Optional;
29
30 /**
31  * Created by TALIO on 2/28/2017.
32  */
33 public class NestedResourceValidatorTest {
34
35   private final HeatResourceValidator baseValidator = new HeatResourceValidator();
36   private final NestedResourceValidator resourceValidator = new NestedResourceValidator();
37   private static final String  PATH = "/org/openecomp/validation/validators/heat_validator/";
38
39   @Test
40   public void testNoLoopsNesting() {
41
42     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(baseValidator,
43             resourceValidator, null,
44             PATH + "no_loops_nesting/negative_test/input");
45
46     Assert.assertNotNull(messages);
47     Assert.assertEquals(messages.size(), 4);
48
49     Assert.assertEquals(messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().size(), 1);
50     Assert.assertEquals(
51             messages.get("hot-nimbus-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(),
52             "ERROR: [HNR2]: 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]");
53
54     Assert.assertEquals(
55             messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().size(), 2);
56     Assert.assertEquals(
57             messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().get(0)
58                     .getMessage(),
59             "ERROR: [HNR2]: 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]");
60     Assert.assertEquals(
61             messages.get("nested-points-to-hot-nimbus-psm.yaml").getErrorMessageList().get(1)
62                     .getMessage(),
63             "ERROR: [HNR2]: Nested files loop - [nested-points-to-hot-nimbus-psm.yaml -- nested-psm_v1.0.yaml -- nested-points-to-hot-nimbus-psm.yaml]");
64
65     Assert.assertEquals(messages.get("yaml-point-to-itself.yaml").getErrorMessageList().size(), 1);
66     Assert.assertEquals(
67             messages.get("yaml-point-to-itself.yaml").getErrorMessageList().get(0).getMessage(),
68             "ERROR: [HNR2]: Nested files loop - [yaml-point-to-itself.yaml -- yaml-point-to-itself.yaml]");
69
70     Assert.assertEquals(messages.get("nested-psm_v1.0.yaml").getErrorMessageList().size(), 1);
71     Assert.assertEquals(
72             messages.get("nested-psm_v1.0.yaml").getErrorMessageList().get(0).getMessage(),
73             "ERROR: [HNR2]: 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]");
74   }
75
76   @Test
77   public void testPropertiesMatchNestedParameters() {
78     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(baseValidator,
79             resourceValidator, null,
80             PATH + "properties_match_nested_parameters/negative_test/input");
81
82     Assert.assertNotNull(messages);
83     Assert.assertEquals(messages.size(), 1);
84
85     Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
86     Assert.assertEquals(
87             messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(),
88             "ERROR: [HNR3]: Referenced parameter not found in nested file - nested-pps_v1.0.yaml, parameter name [server_pcrf_pps_001], Resource ID [parameter_not_existing_in_nested]");
89   }
90
91   @Test
92   public void testWrongValueTypeAssigned() {
93     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(baseValidator,
94             resourceValidator, null,
95             PATH + "properties_match_nested_parameters/wrong_value_type_assigned/input");
96
97     Assert.assertNotNull(messages);
98     Assert.assertEquals(messages.size(), 1);
99
100     Assert.assertEquals(messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().size(), 1);
101     Assert.assertEquals(
102             messages.get("hot-nimbus-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(),
103             "WARNING: [HNR4]: Wrong value type assigned to a nested input parameter, nested resource [server_pcrf_pps_001], property name [index_integer], nested file [nested-pps_v1.0.yaml]");
104   }
105
106   @Test
107   public void testMissingNestedFile() {
108     final Resource resource = new Resource();
109     resource.setType("nested-pps_v1.0.yaml");
110
111     final GlobalValidationContext globalValidationContext = ValidationTestUtil.createGlobalContextFromPath(PATH + "missing_nested_file/input");
112
113     NestedResourceValidator.validateAllPropertiesMatchNestedParameters(null, null, resource, Optional.empty(), globalValidationContext);
114
115     Map<String, MessageContainer> messages = globalValidationContext.getContextMessageContainers();
116     Assert.assertNotNull(messages);
117     Assert.assertEquals(messages.size(), 1);
118
119     Assert.assertEquals(messages.get("nested-pps_v1.0.yaml").getErrorMessageList().size(), 1);
120     Assert.assertEquals(
121             messages.get("nested-pps_v1.0.yaml").getErrorMessageList().get(0).getMessage(),
122             "ERROR: [HNR1]: Missing nested file - nested-pps_v1.0.yaml");
123   }
124 }