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