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