No properties found when trying to add a node filter to a VF
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / NodeFilterValidatorTest.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 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.when;
28
29 import fj.data.Either;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
45 import org.openecomp.sdc.be.config.ConfigurationManager;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
51 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
52 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
53 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
54 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
55 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
56 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
57 import org.openecomp.sdc.be.impl.ComponentsUtils;
58 import org.openecomp.sdc.be.model.ComponentInstance;
59 import org.openecomp.sdc.be.model.ComponentInstanceInput;
60 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
61 import org.openecomp.sdc.be.model.DataTypeDefinition;
62 import org.openecomp.sdc.be.model.PropertyDefinition;
63 import org.openecomp.sdc.be.model.Service;
64 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
65 import org.openecomp.sdc.be.model.dto.FilterConstraintDto;
66 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
67 import org.openecomp.sdc.be.model.validation.FilterConstraintValidator;
68 import org.openecomp.sdc.common.impl.ExternalConfiguration;
69 import org.openecomp.sdc.common.impl.FSConfigurationSource;
70 import org.openecomp.sdc.exception.ResponseFormat;
71
72 class NodeFilterValidatorTest {
73
74     private static final String INNER_SERVICE = "innerService";
75     private static final String PROPERTY_NAME = "Prop1";
76     private static final String COMPONENT1_ID = "component1";
77     private static final String PARENT_SERVICE_ID = "parentservice";
78     private static final String COMPONENT2_ID = "component2";
79     private ComponentsUtils componentsUtils;
80
81     @Mock
82     private ApplicationDataTypeCache applicationDataTypeCache;
83     @Mock
84     private FilterConstraintValidator filterConstraintValidator;
85     @InjectMocks
86     private NodeFilterValidator nodeFilterValidator;
87     private FilterConstraintDto baseFilterConstraintDto;
88
89     protected static ToscaGetFunctionDataDefinition createToscaGetFunction(final String sourceName,
90                                                                            final PropertySource propertySource,
91                                                                            final ToscaGetFunctionType toscaGetFunctionType,
92                                                                            final List<String> propertyPathFromSource,
93                                                                            final List<Object> toscaIndexList) {
94         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
95         toscaGetFunction.setFunctionType(toscaGetFunctionType);
96         toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource);
97         toscaGetFunction.setSourceName(sourceName);
98         toscaGetFunction.setPropertySource(propertySource);
99         toscaGetFunction.setPropertyName(propertyPathFromSource.get(0));
100         toscaGetFunction.setToscaIndexList(toscaIndexList);
101         return toscaGetFunction;
102     }
103
104     private static FilterConstraintDto buildFilterConstraintDto(final String propertyName, final FilterValueType valueType,
105                                                                 final ConstraintType constraintType,
106                                                                 final PropertyFilterTargetType targetType, Object value) {
107         final var filterConstraintDto = new FilterConstraintDto();
108         filterConstraintDto.setPropertyName(propertyName);
109         filterConstraintDto.setValueType(valueType);
110         filterConstraintDto.setOperator(constraintType);
111         filterConstraintDto.setTargetType(targetType);
112         filterConstraintDto.setValue(value);
113         return filterConstraintDto;
114     }
115
116     @BeforeEach
117     void setup() {
118         componentsUtils = Mockito.mock(ComponentsUtils.class);
119         MockitoAnnotations.openMocks(this);
120         baseFilterConstraintDto = new FilterConstraintDto();
121         baseFilterConstraintDto.setPropertyName(PROPERTY_NAME);
122         baseFilterConstraintDto.setValueType(FilterValueType.STATIC);
123         baseFilterConstraintDto.setOperator(ConstraintType.EQUAL);
124         baseFilterConstraintDto.setTargetType(PropertyFilterTargetType.PROPERTY);
125         baseFilterConstraintDto.setValue("value");
126         when(applicationDataTypeCache.getAll(any())).thenReturn(Either.left(Map.of()));
127         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
128     }
129
130     @Test
131     void testValidateComponentInstanceExist() {
132         final ResponseFormat expectedResponse = new ResponseFormat();
133         when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, "?", INNER_SERVICE)).thenReturn(expectedResponse);
134         Either<Boolean, ResponseFormat> either =
135             nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE);
136         assertTrue(either.isRight());
137         assertEquals(expectedResponse, either.right().value());
138
139         Service service = createService("booleanIncorrect");
140         when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, service.getName(), "uniqueId"))
141             .thenReturn(expectedResponse);
142         either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
143         assertTrue(either.isRight());
144         assertEquals(expectedResponse, either.right().value());
145
146         List<ComponentInstance> list = new LinkedList<>();
147         ComponentInstance instance = new ComponentInstance();
148         instance.setUniqueId("uniqueId");
149         list.add(instance);
150         service.setComponentInstances(list);
151         either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
152         assertTrue(either.isLeft());
153     }
154
155     @Test
156     void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
157         final Service service = createService("booleanIncorrect");
158         final FilterConstraintDto filterConstraintDto = buildFilterConstraintDto(PROPERTY_NAME, FilterValueType.STATIC, ConstraintType.EQUAL,
159             PropertyFilterTargetType.PROPERTY, "true");
160         Either<Boolean, ResponseFormat> either =
161             nodeFilterValidator.validateFilter(service, INNER_SERVICE, filterConstraintDto);
162         assertTrue(either.isRight());
163         filterConstraintDto.setTargetType(PropertyFilterTargetType.CAPABILITY);
164         either = nodeFilterValidator.validateFilter(service, INNER_SERVICE, filterConstraintDto);
165         assertTrue(either.isRight());
166     }
167
168     @Test
169     void testValidateComponentFilter() {
170         Service service = createService("integer");
171         final var filterConstraint1 = buildFilterConstraintDto(
172             PROPERTY_NAME,
173             FilterValueType.GET_PROPERTY,
174             ConstraintType.EQUAL,
175             PropertyFilterTargetType.PROPERTY,
176             createToscaGetFunction("test", PropertySource.INSTANCE, ToscaGetFunctionType.GET_PROPERTY, List.of("test2"), null)
177         );
178         Either<Boolean, ResponseFormat> actualValidationResult =
179             nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1));
180         assertTrue(actualValidationResult.isRight());
181
182         final var filterConstraint2 = buildFilterConstraintDto(
183             PROPERTY_NAME,
184             FilterValueType.GET_PROPERTY,
185             ConstraintType.EQUAL,
186             PropertyFilterTargetType.PROPERTY,
187             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of("Prop1"), null)
188         );
189         actualValidationResult =
190             nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint2));
191         assertTrue(actualValidationResult.isLeft());
192
193         final var staticFilter1 = buildFilterConstraintDto(
194             PROPERTY_NAME,
195             FilterValueType.STATIC,
196             ConstraintType.EQUAL,
197             PropertyFilterTargetType.PROPERTY,
198             1
199         );
200         actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter1));
201         assertTrue(actualValidationResult.isLeft());
202         assertTrue(actualValidationResult.left().value());
203
204         final var staticFilter2 = buildFilterConstraintDto(
205             PROPERTY_NAME,
206             FilterValueType.STATIC,
207             ConstraintType.EQUAL,
208             PropertyFilterTargetType.PROPERTY,
209             "true"
210         );
211         actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter2));
212         assertTrue(actualValidationResult.isRight());
213
214         service = createService(ToscaPropertyType.BOOLEAN.getType());
215         final var staticFilter3 = buildFilterConstraintDto(
216             PROPERTY_NAME,
217             FilterValueType.STATIC,
218             ConstraintType.GREATER_THAN,
219             PropertyFilterTargetType.PROPERTY,
220             "3"
221         );
222         actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter3));
223         assertTrue(actualValidationResult.isRight());
224
225         final var staticFilter4 = buildFilterConstraintDto(
226             "test",
227             FilterValueType.STATIC,
228             ConstraintType.GREATER_THAN,
229             PropertyFilterTargetType.PROPERTY,
230             "3"
231         );
232         actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(staticFilter4));
233         assertTrue(actualValidationResult.isRight());
234     }
235
236     @Test
237     void testValidateComponentFilterWithIndex() {
238         Service service = createService("string", "schema");
239         final var filterConstraint1 = buildFilterConstraintDto(
240             PROPERTY_NAME,
241             FilterValueType.GET_PROPERTY,
242             ConstraintType.EQUAL,
243             PropertyFilterTargetType.PROPERTY,
244             createToscaGetFunction("test", PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME, "alist"), List.of("1"))
245         );
246         Map<String, DataTypeDefinition> data = new HashMap<>();
247         DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
248         List<PropertyDefinition> properties = new ArrayList<>();
249         PropertyDefinition propertyDefinition = new PropertyDefinition();
250         propertyDefinition.setName("alist");
251         propertyDefinition.setType("list");
252
253         SchemaDefinition schemaDefinition = new SchemaDefinition();
254         PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
255         schemaProperty.setType("string");
256         schemaDefinition.setProperty(schemaProperty);
257         propertyDefinition.setSchema(schemaDefinition);
258
259         properties.add(propertyDefinition);
260         dataTypeDefinition.setProperties(properties);
261
262         data.put("string", dataTypeDefinition);
263         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = Either.left(data);
264         when(applicationDataTypeCache.getAll(null)).thenReturn(allDataTypes);
265
266         Either<Boolean, ResponseFormat> actualValidationResult =
267             nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1));
268         assertTrue(actualValidationResult.isLeft());
269     }
270
271     @Test
272     void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
273         Service service = createService(ToscaPropertyType.BOOLEAN.getType());
274         final FilterConstraintDto filterConstraintDto = buildFilterConstraintDto(
275             PROPERTY_NAME,
276             FilterValueType.STATIC,
277             ConstraintType.GREATER_THAN,
278             PropertyFilterTargetType.PROPERTY,
279             "true"
280         );
281         final ResponseFormat expectedResponse = new ResponseFormat();
282         when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, ToscaPropertyType.BOOLEAN.getType(),
283             filterConstraintDto.getPropertyName(), "\"true\"")
284         ).thenReturn(expectedResponse);
285         final Either<Boolean, ResponseFormat> validationResult =
286             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(filterConstraintDto));
287         assertTrue(validationResult.isRight());
288         assertEquals(expectedResponse, validationResult.right().value());
289     }
290
291     @Test
292     void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
293         final Service service = createService(ToscaPropertyType.BOOLEAN.getType());
294         baseFilterConstraintDto.setValue("trues");
295
296         final ResponseFormat responseFormat = new ResponseFormat();
297         when(componentsUtils.getResponseFormat
298             (ActionStatus.UNSUPPORTED_VALUE_PROVIDED, ToscaPropertyType.BOOLEAN.getType(), PROPERTY_NAME, "\"trues\"")
299         ).thenReturn(responseFormat);
300         final Either<Boolean, ResponseFormat> validationResult =
301             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
302
303         assertTrue(validationResult.isRight());
304         assertEquals(responseFormat, validationResult.right().value());
305     }
306
307     @Test
308     void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
309         Service service = createService(ToscaPropertyType.STRING.getType());
310         baseFilterConstraintDto.setValue("true");
311         baseFilterConstraintDto.setOperator(ConstraintType.GREATER_THAN);
312         Either<Boolean, ResponseFormat> either =
313             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
314
315         assertTrue(either.isLeft());
316     }
317
318     @Test
319     void testValidateNodeFilterIntegerValueSuccess() {
320         Service service = createService(ToscaPropertyType.INTEGER.getType());
321         baseFilterConstraintDto.setValue(1);
322         Either<Boolean, ResponseFormat> validationResult =
323             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
324
325         assertTrue(validationResult.isLeft());
326     }
327
328     @Test
329     void testValidateNodeFilterIntegerValueFail() {
330         Service service = createService(ToscaPropertyType.INTEGER.getType());
331
332         baseFilterConstraintDto.setValue(1.0);
333
334         final ResponseFormat expectedResponse = new ResponseFormat();
335         when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, ToscaPropertyType.INTEGER.getType(),
336             baseFilterConstraintDto.getPropertyName(), "1.0")
337         ).thenReturn(expectedResponse);
338         Either<Boolean, ResponseFormat> validationResult =
339             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
340
341         assertTrue(validationResult.isRight());
342         assertEquals(expectedResponse, validationResult.right().value());
343     }
344
345     @Test
346     void testValidateNodeFilterFloatValueSuccess() {
347         final Service service = createService(ToscaPropertyType.FLOAT.getType());
348         baseFilterConstraintDto.setValue(1.0);
349         final Either<Boolean, ResponseFormat> validationResult =
350             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
351
352         assertTrue(validationResult.isLeft());
353         assertTrue(validationResult.left().value());
354     }
355
356     @Test
357     void testValidateNodeFilterFloatValueFail() {
358         Service service = createService(ToscaPropertyType.FLOAT.getType());
359
360         when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1")).thenReturn(new ResponseFormat());
361
362         Either<Boolean, ResponseFormat> either =
363             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
364
365         assertTrue(either.isRight());
366     }
367
368     @Test
369     void testValidateNodeFilterStringValueSuccess() {
370         Service service = createService(ToscaPropertyType.STRING.getType());
371         Either<Boolean, ResponseFormat> either =
372             nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
373
374         assertTrue(either.isLeft());
375     }
376
377     @Test
378     void testValidatePropertyConstraintBrotherSuccess() {
379         Service service = createService(ToscaPropertyType.STRING.getType());
380         final ToscaGetFunctionDataDefinition toscaGetFunction =
381             createToscaGetFunction(COMPONENT2_ID, PropertySource.INSTANCE, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
382         final var filterConstraintDto = buildFilterConstraintDto(
383             PROPERTY_NAME,
384             FilterValueType.GET_PROPERTY,
385             ConstraintType.EQUAL,
386             PropertyFilterTargetType.PROPERTY,
387             toscaGetFunction
388         );
389         Either<Boolean, ResponseFormat> either =
390             nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
391
392         assertTrue(either.isLeft());
393         assertTrue(either.left().value());
394     }
395
396     @Test
397     void testValidatePropertyConstraintParentSuccess() {
398         final var service = createService(ToscaPropertyType.STRING.getType());
399         final ToscaGetFunctionDataDefinition toscaGetFunction =
400             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
401         final var filterConstraintDto = buildFilterConstraintDto(
402             PROPERTY_NAME,
403             FilterValueType.GET_PROPERTY,
404             ConstraintType.EQUAL,
405             PropertyFilterTargetType.PROPERTY,
406             toscaGetFunction
407         );
408         final Either<Boolean, ResponseFormat> validationResult =
409             nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
410
411         assertTrue(validationResult.isLeft());
412         assertTrue(validationResult.left().value());
413     }
414
415     @Test
416     void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
417         final Service service = createService(ToscaPropertyType.STRING.getType());
418         service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(ToscaPropertyType.INTEGER.getType());
419         final ToscaGetFunctionDataDefinition toscaGetFunction =
420             createToscaGetFunction(COMPONENT2_ID, PropertySource.INSTANCE, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
421         final var filterConstraintDto = buildFilterConstraintDto(
422             PROPERTY_NAME,
423             FilterValueType.GET_PROPERTY,
424             ConstraintType.EQUAL,
425             PropertyFilterTargetType.PROPERTY,
426             toscaGetFunction
427         );
428
429         final ResponseFormat expectedResponse = new ResponseFormat();
430         when(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_PROPERTY_TYPE_MISMATCH,
431             PROPERTY_NAME, ToscaPropertyType.INTEGER.getType(), PROPERTY_NAME, ToscaPropertyType.STRING.getType())
432         ).thenReturn(expectedResponse);
433
434         final Either<Boolean, ResponseFormat> either =
435             nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
436
437         assertTrue(either.isRight());
438         assertEquals(expectedResponse, either.right().value());
439     }
440
441     @Test
442     void testValidatePropertyConstraintParentPropertyTypeMismatch() {
443         final Service service = createService(ToscaPropertyType.STRING.getType());
444         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(ToscaPropertyType.INTEGER.getType());
445         final ToscaGetFunctionDataDefinition toscaGetFunction =
446             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
447         final var filterConstraintDto = buildFilterConstraintDto(
448             PROPERTY_NAME,
449             FilterValueType.GET_PROPERTY,
450             ConstraintType.EQUAL,
451             PropertyFilterTargetType.PROPERTY,
452             toscaGetFunction
453         );
454
455         final ResponseFormat expectedResponse = new ResponseFormat();
456         when(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_PROPERTY_TYPE_MISMATCH,
457             PROPERTY_NAME, ToscaPropertyType.STRING.getType(), PROPERTY_NAME, ToscaPropertyType.INTEGER.getType())
458         ).thenReturn(expectedResponse);
459
460         Either<Boolean, ResponseFormat> either =
461             nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
462
463         assertTrue(either.isRight());
464         assertEquals(expectedResponse, either.right().value());
465     }
466
467     @Test
468     void testValidatePropertyConstraintParentPropertyNotFound() {
469         final Service service = createService(ToscaPropertyType.STRING.getType());
470         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
471
472         final ResponseFormat expectedResponse = new ResponseFormat();
473         when(componentsUtils.getResponseFormat(eq(ActionStatus.FILTER_PROPERTY_NOT_FOUND), any(), any()))
474             .thenReturn(expectedResponse);
475
476         final ToscaGetFunctionDataDefinition toscaGetFunction =
477             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
478         final var filterConstraintDto = buildFilterConstraintDto(
479             PROPERTY_NAME,
480             FilterValueType.GET_PROPERTY,
481             ConstraintType.EQUAL,
482             PropertyFilterTargetType.PROPERTY,
483             toscaGetFunction
484         );
485         Assertions.assertThrows(ComponentException.class,
486             () -> nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto)));
487     }
488
489     @Test
490     void testValidatePropertyConstraintBrotherPropertyNotFound() {
491         Service service = createService(ToscaPropertyType.STRING.getType());
492         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
493         final ToscaGetFunctionDataDefinition toscaGetFunction =
494             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
495         final var filterConstraintDto = buildFilterConstraintDto(
496             PROPERTY_NAME,
497             FilterValueType.GET_PROPERTY,
498             ConstraintType.EQUAL,
499             PropertyFilterTargetType.PROPERTY,
500             toscaGetFunction
501         );
502         final ResponseFormat expectedResponse = new ResponseFormat();
503         when(componentsUtils.getResponseFormat(ActionStatus.FILTER_PROPERTY_NOT_FOUND, "Target", PROPERTY_NAME))
504             .thenReturn(expectedResponse);
505         Assertions.assertThrows(ComponentException.class,
506             () -> nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto)));
507     }
508
509     @Test
510     void testValidatePropertyConstraintParentPropertySchemaMismatch() {
511         final Service service = createService(ToscaPropertyType.LIST.getType(), ToscaPropertyType.STRING.getType());
512         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(ToscaPropertyType.LIST.getType());
513         final var schemaProperty = new PropertyDataDefinition();
514         schemaProperty.setType(ToscaPropertyType.INTEGER.getType());
515         final var schemaDefinition = new SchemaDefinition();
516         schemaDefinition.setProperty(schemaProperty);
517         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setSchema(schemaDefinition);
518
519         final ToscaGetFunctionDataDefinition toscaGetFunction =
520             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
521         final var filterConstraintDto = buildFilterConstraintDto(
522             PROPERTY_NAME,
523             FilterValueType.GET_PROPERTY,
524             ConstraintType.EQUAL,
525             PropertyFilterTargetType.PROPERTY,
526             toscaGetFunction
527         );
528
529         final ResponseFormat expectedResponse = new ResponseFormat();
530         when(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_SCHEMA_MISMATCH, PROPERTY_NAME, ToscaPropertyType.INTEGER.getType(),
531             PROPERTY_NAME, ToscaPropertyType.STRING.getType())
532         ).thenReturn(expectedResponse);
533
534         Either<Boolean, ResponseFormat> either =
535             nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
536
537         assertTrue(either.isRight());
538         assertEquals(expectedResponse, either.right().value());
539     }
540
541     @Test
542     void testValidateNodeFilterForVfStaticValue() {
543         Service service = createService(ToscaPropertyType.INTEGER.getType());
544         addComponentInstanceToService(service, OriginTypeEnum.VF, "vfInstance", ToscaPropertyType.INTEGER.getType());
545         baseFilterConstraintDto.setValue(1);
546         Either<Boolean, ResponseFormat> validationResult =
547             nodeFilterValidator.validateFilter(service, "vfInstance", List.of(baseFilterConstraintDto));
548
549         assertTrue(validationResult.isLeft());
550     }
551
552     @Test
553     void testValidateNodeFilterForVfToscaGetProperty() {
554         Service service = createService(ToscaPropertyType.INTEGER.getType());
555         addComponentInstanceToService(service, OriginTypeEnum.VF, "vfInstance", ToscaPropertyType.INTEGER.getType());
556         final ToscaGetFunctionDataDefinition toscaGetFunction =
557             createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
558         final var filterConstraintDto = buildFilterConstraintDto(
559             PROPERTY_NAME,
560             FilterValueType.GET_PROPERTY,
561             ConstraintType.EQUAL,
562             PropertyFilterTargetType.PROPERTY,
563             toscaGetFunction
564         );
565         Either<Boolean, ResponseFormat> validationResult =
566             nodeFilterValidator.validateFilter(service, "vfInstance", List.of(filterConstraintDto));
567
568         assertTrue(validationResult.isLeft());
569     }
570
571     private void addComponentInstanceToService(Service service, OriginTypeEnum originTypeEnum, String instanceName, String type) {
572         ComponentInstance componentInstance = new ComponentInstance();
573         componentInstance.setUniqueId(instanceName);
574         componentInstance.setName(instanceName);
575         componentInstance.setOriginType(originTypeEnum);
576
577         List<ComponentInstance> compInstances = new ArrayList<>();
578         service.getComponentInstances().forEach(compInstance -> compInstances.add(compInstance));
579         compInstances.add(componentInstance);
580         service.setComponentInstances(compInstances);
581
582         if (isInput(originTypeEnum)) {
583             ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
584             componentInstanceInput.setName(PROPERTY_NAME);
585             componentInstanceInput.setType(type);
586             if (service.getComponentInstancesInputs() == null) {
587                 service.setComponentInstancesInputs(new HashMap<>());
588             }
589             service.getComponentInstancesInputs().put(instanceName, Collections.singletonList(componentInstanceInput));
590         } else {
591             ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
592             componentInstanceProperty.setName(PROPERTY_NAME);
593             componentInstanceProperty.setType(type);
594             if (service.getComponentInstancesProperties() == null) {
595                 service.setComponentInstancesProperties(new HashMap<>());
596             }
597             service.getComponentInstancesProperties().put(instanceName, Collections.singletonList(componentInstanceProperty));
598         }
599     }
600
601     private boolean isInput(OriginTypeEnum instanceType) {
602         return OriginTypeEnum.VF.equals(instanceType) || OriginTypeEnum.PNF.equals(instanceType) || OriginTypeEnum.CVFC.equals(instanceType) ||
603             OriginTypeEnum.CR.equals(instanceType);
604     }
605
606     private Service createService(String type) {
607         return createService(type, null);
608     }
609
610     private Service createService(String type, String schemaType) {
611         Service service = new Service();
612         service.setName(PARENT_SERVICE_ID);
613
614         PropertyDefinition propertyDefinition = new PropertyDefinition();
615         propertyDefinition.setName(PROPERTY_NAME);
616         propertyDefinition.setType(type);
617         if (schemaType != null) {
618             SchemaDefinition schemaDefinition = new SchemaDefinition();
619             PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
620             schemaProperty.setType(schemaType);
621             schemaDefinition.setProperty(schemaProperty);
622             propertyDefinition.setSchema(schemaDefinition);
623         }
624         service.setProperties(Collections.singletonList(propertyDefinition));
625
626         ComponentInstance componentInstance = new ComponentInstance();
627         componentInstance.setUniqueId(COMPONENT1_ID);
628         componentInstance.setName(COMPONENT1_ID);
629         componentInstance.setOriginType(OriginTypeEnum.VFC);
630
631         ComponentInstance componentInstance2 = new ComponentInstance();
632         componentInstance2.setUniqueId(COMPONENT2_ID);
633         componentInstance2.setName(COMPONENT2_ID);
634         componentInstance2.setOriginType(OriginTypeEnum.VFC);
635
636         ComponentInstance componentInstance3 = new ComponentInstance();
637         componentInstance3.setUniqueId(INNER_SERVICE);
638         componentInstance3.setName(INNER_SERVICE);
639         componentInstance3.setOriginType(OriginTypeEnum.ServiceProxy);
640
641         service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2, componentInstance3));
642
643         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
644         componentInstanceProperty.setName(PROPERTY_NAME);
645         componentInstanceProperty.setType(type);
646
647         ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
648         componentInstanceProperty2.setName(PROPERTY_NAME);
649         componentInstanceProperty2.setType(type);
650
651         Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
652         componentInstancePropertyMap.put(componentInstance.getUniqueId(),
653             Collections.singletonList(componentInstanceProperty));
654         componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
655             Collections.singletonList(componentInstanceProperty2));
656         componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
657
658         service.setComponentInstancesProperties(componentInstancePropertyMap);
659
660         return service;
661     }
662
663 }