Support import service with yaml tosca function
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / NodeFilterValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.be.components.validation;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import fj.data.Either;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.Assert;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
44 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.PropertyDefinition;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.common.impl.ExternalConfiguration;
51 import org.openecomp.sdc.common.impl.FSConfigurationSource;
52 import org.openecomp.sdc.exception.ResponseFormat;
53
54 public class NodeFilterValidationTest {
55
56     private static final String EMPTY_STR = "";
57         private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
58     private static final String INNER_SERVICE = "innerService";
59     private static final String PROPERTY_NAME = "Prop1";
60     private static final String VALUE = "value";
61     private static final String FLOAT_TYPE = "float";
62     private static final String STRING_TYPE = "string";
63     private static final String LIST_TYPE = "list";
64     private static final String COMPONENT1_ID = "component1";
65     private static final String INTEGER_TYPE = "integer";
66     private static final String PARENTSERVICE_ID = "parentservice";
67     private static final String COMPONENT2_ID = "component2";
68     private ComponentsUtils componentsUtils;
69
70     @InjectMocks
71     private NodeFilterValidator nodeFilterValidator;
72
73     @BeforeEach
74     public void setup() {
75         componentsUtils = Mockito.mock(ComponentsUtils.class);
76         MockitoAnnotations.openMocks(this);
77         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
78     }
79
80     @Test
81     public void testValidateComponentInstanceExist() {
82         Either<Boolean, ResponseFormat> either =
83                 nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE);
84         assertTrue(either.isRight());
85         assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
86         assertEquals(500, either.right().value().getStatus());
87
88         Service service = createService("booleanIncorrect");
89         either = nodeFilterValidator.validateComponentInstanceExist(service, INNER_SERVICE);
90         assertTrue(either.isRight());
91         assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
92         assertEquals(500, either.right().value().getStatus());
93
94         List<ComponentInstance> list = new LinkedList<>();
95         ComponentInstance instance = new ComponentInstance();
96         instance.setUniqueId("uniqueId");
97         list.add(instance);
98         service.setComponentInstances(list);
99         either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
100         assertTrue(either.isLeft());
101     }
102
103     @Test
104     public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
105         Service service = createService("booleanIncorrect");
106         Either<Boolean, ResponseFormat> either =
107                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
108                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
109                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
110         assertTrue(either.isRight());
111
112         either =
113                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
114                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
115                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.CAPABILITIES, EMPTY_STR);
116         assertTrue(either.isRight());
117     }
118
119     @Test
120     public void testValidateComponentFilter() {
121         Service service = createService("booleanIncorrect");
122         String property = "Prop1: {equal: {get_property: ['test','test2']}}";
123         Either<Boolean, ResponseFormat> either =
124                 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
125                         NodeFilterConstraintAction.ADD);
126         assertTrue(either.isRight());
127
128         property = "Prop1: {equal: {get_property: ['parentservice','Prop1']}}";
129         either =
130                 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
131                         NodeFilterConstraintAction.ADD);
132         assertTrue(either.isLeft());
133
134         String staticStr = "Prop1: {equal: 1}";
135         either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
136                         NodeFilterConstraintAction.ADD);
137         assertTrue(either.isLeft());
138         assertTrue(either.left().value());
139
140         staticStr = "Prop1: {equal: 'true'}";
141         either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
142                         NodeFilterConstraintAction.ADD);
143         assertTrue(either.isRight());
144
145         staticStr = "Prop1: {greater_than: '3'}";
146         either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
147                 NodeFilterConstraintAction.ADD);
148         assertTrue(either.isRight());
149
150         staticStr = "test: {greater_than: '3'}";
151         either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
152                 NodeFilterConstraintAction.ADD);
153         assertTrue(either.isRight());
154     }
155
156     @Test
157     public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
158         Service service = createService("boolean");
159         Either<Boolean, ResponseFormat> either =
160                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
161                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
162                                 .replace("equal", "greater_than")),
163                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
164
165         Assert.assertFalse(either.isLeft());
166     }
167
168     @Test
169     public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
170         Service service = createService("boolean");
171         Either<Boolean, ResponseFormat> either =
172                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
173                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
174                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
175
176         Assert.assertFalse(either.isLeft());
177     }
178
179     @Test
180     public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
181         Service service = createService(STRING_TYPE);
182         Either<Boolean, ResponseFormat> either =
183                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
184                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
185                                 .replace("equal", "greater_than")),
186                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
187
188         Assert.assertTrue(either.isLeft());
189     }
190
191     @Test
192     public void testValidateNodeFilterIntegerValueSuccess() {
193         Service service = createService(INTEGER_TYPE);
194         Either<Boolean, ResponseFormat> either =
195                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
196                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
197                                 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
198
199         Assert.assertTrue(either.isLeft());
200     }
201
202     @Test
203     public void testValidateNodeFilterIntegerValueFail() {
204         Service service = createService(INTEGER_TYPE);
205
206         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
207                 .thenReturn(new ResponseFormat());
208
209         Either<Boolean, ResponseFormat> either =
210                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
211                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
212                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
213
214         Assert.assertTrue(either.isRight());
215     }
216
217     @Test
218     public void testValidateNodeFilterFloatValueSuccess() {
219         Service service = createService(FLOAT_TYPE);
220         Either<Boolean, ResponseFormat> either =
221                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
222                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
223                         NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
224
225         Assert.assertTrue(either.isLeft());
226     }
227
228     @Test
229     public void testValidateNodeFilterFloatValueFail() {
230         Service service = createService(FLOAT_TYPE);
231
232         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
233                 .thenReturn(new ResponseFormat());
234
235         Either<Boolean, ResponseFormat> either =
236                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
237                         Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
238                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
239
240         Assert.assertTrue(either.isRight());
241     }
242
243     @Test
244     public void testValidateNodeFilterStringValueSuccess() {
245         Service service = createService(STRING_TYPE);
246         Either<Boolean, ResponseFormat> either =
247                 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
248                     Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
249                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
250
251         Assert.assertTrue(either.isLeft());
252     }
253
254     @Test
255     public void testValidatePropertyConstraintBrotherSuccess() {
256         Service service = createService(STRING_TYPE);
257         Either<Boolean, ResponseFormat> either =
258                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
259                         + "  equal:  { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
260                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
261
262         Assert.assertTrue(either.isLeft());
263     }
264
265     @Test
266     public void testValidatePropertyConstraintParentSuccess() {
267         Service service = createService(STRING_TYPE);
268         Either<Boolean, ResponseFormat> either =
269                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
270                         + "  equal:  { get_property : [SELF, Prop1]}\n"), NodeFilterConstraintAction.ADD,
271                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
272
273         Assert.assertTrue(either.isLeft());
274     }
275
276     @Test
277     public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
278         Service service = createService(STRING_TYPE);
279         service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
280
281         Either<Boolean, ResponseFormat> either =
282                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
283                         + "  equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
284                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
285
286         Assert.assertFalse(either.isLeft());
287     }
288
289     @Test
290     public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
291         Service service = createService(STRING_TYPE);
292         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
293
294         Either<Boolean, ResponseFormat> either =
295                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
296                         + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
297                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
298
299         Assert.assertFalse(either.isLeft());
300     }
301
302     @Test
303     public void testValidatePropertyConstraintParentPropertyNotFound() {
304         Service service = createService(STRING_TYPE);
305         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
306
307         Either<Boolean, ResponseFormat> either =
308                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
309                         + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
310                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
311
312         Assert.assertFalse(either.isLeft());
313     }
314
315     @Test
316     public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
317         Service service = createService(STRING_TYPE);
318         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
319
320         Either<Boolean, ResponseFormat> either =
321                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
322                         + "  equal:  { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
323                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
324
325         Assert.assertFalse(either.isLeft());
326     }
327
328     @Test
329     public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
330         Service service = createService(LIST_TYPE,STRING_TYPE);
331         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
332
333         Either<Boolean, ResponseFormat> either =
334                 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
335                     + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
336                     NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
337
338         Assert.assertFalse(either.isLeft());
339     }
340
341     private Service createService(String type) {
342         return createService(type, null);
343     }
344
345     private Service createService(String type, String schemaType) {
346         Service service = new Service();
347         service.setName(PARENTSERVICE_ID);
348
349         PropertyDefinition propertyDefinition = new PropertyDefinition();
350         propertyDefinition.setName(PROPERTY_NAME);
351         propertyDefinition.setType(type);
352         if (schemaType != null){
353             SchemaDefinition schemaDefinition = new SchemaDefinition();
354             PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
355             schemaProperty.setType(schemaType);
356             schemaDefinition.setProperty(schemaProperty);
357             propertyDefinition.setSchema(schemaDefinition);
358         }
359         service.setProperties(Collections.singletonList(propertyDefinition));
360
361         ComponentInstance componentInstance = new ComponentInstance();
362         componentInstance.setUniqueId(COMPONENT1_ID);
363         componentInstance.setName(COMPONENT1_ID);
364
365         ComponentInstance componentInstance2 = new ComponentInstance();
366         componentInstance2.setUniqueId(COMPONENT2_ID);
367         componentInstance2.setName(COMPONENT2_ID);
368
369         service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
370
371         ComponentInstanceProperty componentInstanceProperty  = new ComponentInstanceProperty();
372         componentInstanceProperty.setName(PROPERTY_NAME);
373         componentInstanceProperty.setType(type);
374
375         ComponentInstanceProperty componentInstanceProperty2  = new ComponentInstanceProperty();
376         componentInstanceProperty2.setName(PROPERTY_NAME);
377         componentInstanceProperty2.setType(type);
378
379         Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
380         componentInstancePropertyMap.put(componentInstance.getUniqueId(),
381                 Collections.singletonList(componentInstanceProperty));
382         componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
383                 Collections.singletonList(componentInstanceProperty2));
384         componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
385
386         service.setComponentInstancesProperties(componentInstancePropertyMap);
387
388         return service;
389     }
390
391 }