2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.validation;
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;
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;
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;
72 class NodeFilterValidatorTest {
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;
82 private ApplicationDataTypeCache applicationDataTypeCache;
84 private FilterConstraintValidator filterConstraintValidator;
86 private NodeFilterValidator nodeFilterValidator;
87 private FilterConstraintDto baseFilterConstraintDto;
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;
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;
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"));
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());
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());
146 List<ComponentInstance> list = new LinkedList<>();
147 ComponentInstance instance = new ComponentInstance();
148 instance.setUniqueId("uniqueId");
150 service.setComponentInstances(list);
151 either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
152 assertTrue(either.isLeft());
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());
169 void testValidateComponentFilter() {
170 Service service = createService("integer");
171 final var filterConstraint1 = buildFilterConstraintDto(
173 FilterValueType.GET_PROPERTY,
174 ConstraintType.EQUAL,
175 PropertyFilterTargetType.PROPERTY,
176 createToscaGetFunction("test", PropertySource.INSTANCE, ToscaGetFunctionType.GET_PROPERTY, List.of("test2"), null)
178 Either<Boolean, ResponseFormat> actualValidationResult =
179 nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1));
180 assertTrue(actualValidationResult.isRight());
182 final var filterConstraint2 = buildFilterConstraintDto(
184 FilterValueType.GET_PROPERTY,
185 ConstraintType.EQUAL,
186 PropertyFilterTargetType.PROPERTY,
187 createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of("Prop1"), null)
189 actualValidationResult =
190 nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint2));
191 assertTrue(actualValidationResult.isLeft());
193 final var staticFilter1 = buildFilterConstraintDto(
195 FilterValueType.STATIC,
196 ConstraintType.EQUAL,
197 PropertyFilterTargetType.PROPERTY,
200 actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter1));
201 assertTrue(actualValidationResult.isLeft());
202 assertTrue(actualValidationResult.left().value());
204 final var staticFilter2 = buildFilterConstraintDto(
206 FilterValueType.STATIC,
207 ConstraintType.EQUAL,
208 PropertyFilterTargetType.PROPERTY,
211 actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter2));
212 assertTrue(actualValidationResult.isRight());
214 service = createService(ToscaPropertyType.BOOLEAN.getType());
215 final var staticFilter3 = buildFilterConstraintDto(
217 FilterValueType.STATIC,
218 ConstraintType.GREATER_THAN,
219 PropertyFilterTargetType.PROPERTY,
222 actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, List.of(staticFilter3));
223 assertTrue(actualValidationResult.isRight());
225 final var staticFilter4 = buildFilterConstraintDto(
227 FilterValueType.STATIC,
228 ConstraintType.GREATER_THAN,
229 PropertyFilterTargetType.PROPERTY,
232 actualValidationResult = nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(staticFilter4));
233 assertTrue(actualValidationResult.isRight());
237 void testValidateComponentFilterWithIndex() {
238 Service service = createService("string", "schema");
239 final var filterConstraint1 = buildFilterConstraintDto(
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"))
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");
253 SchemaDefinition schemaDefinition = new SchemaDefinition();
254 PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
255 schemaProperty.setType("string");
256 schemaDefinition.setProperty(schemaProperty);
257 propertyDefinition.setSchema(schemaDefinition);
259 properties.add(propertyDefinition);
260 dataTypeDefinition.setProperties(properties);
262 data.put("string", dataTypeDefinition);
263 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = Either.left(data);
264 when(applicationDataTypeCache.getAll(null)).thenReturn(allDataTypes);
266 Either<Boolean, ResponseFormat> actualValidationResult =
267 nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1));
268 assertTrue(actualValidationResult.isLeft());
272 void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
273 Service service = createService(ToscaPropertyType.BOOLEAN.getType());
274 final FilterConstraintDto filterConstraintDto = buildFilterConstraintDto(
276 FilterValueType.STATIC,
277 ConstraintType.GREATER_THAN,
278 PropertyFilterTargetType.PROPERTY,
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());
292 void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
293 final Service service = createService(ToscaPropertyType.BOOLEAN.getType());
294 baseFilterConstraintDto.setValue("trues");
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));
303 assertTrue(validationResult.isRight());
304 assertEquals(responseFormat, validationResult.right().value());
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));
315 assertTrue(either.isLeft());
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));
325 assertTrue(validationResult.isLeft());
329 void testValidateNodeFilterIntegerValueFail() {
330 Service service = createService(ToscaPropertyType.INTEGER.getType());
332 baseFilterConstraintDto.setValue(1.0);
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));
341 assertTrue(validationResult.isRight());
342 assertEquals(expectedResponse, validationResult.right().value());
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));
352 assertTrue(validationResult.isLeft());
353 assertTrue(validationResult.left().value());
357 void testValidateNodeFilterFloatValueFail() {
358 Service service = createService(ToscaPropertyType.FLOAT.getType());
360 when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1")).thenReturn(new ResponseFormat());
362 Either<Boolean, ResponseFormat> either =
363 nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
365 assertTrue(either.isRight());
369 void testValidateNodeFilterStringValueSuccess() {
370 Service service = createService(ToscaPropertyType.STRING.getType());
371 Either<Boolean, ResponseFormat> either =
372 nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto));
374 assertTrue(either.isLeft());
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(
384 FilterValueType.GET_PROPERTY,
385 ConstraintType.EQUAL,
386 PropertyFilterTargetType.PROPERTY,
389 Either<Boolean, ResponseFormat> either =
390 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
392 assertTrue(either.isLeft());
393 assertTrue(either.left().value());
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(
403 FilterValueType.GET_PROPERTY,
404 ConstraintType.EQUAL,
405 PropertyFilterTargetType.PROPERTY,
408 final Either<Boolean, ResponseFormat> validationResult =
409 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
411 assertTrue(validationResult.isLeft());
412 assertTrue(validationResult.left().value());
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(
423 FilterValueType.GET_PROPERTY,
424 ConstraintType.EQUAL,
425 PropertyFilterTargetType.PROPERTY,
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);
434 final Either<Boolean, ResponseFormat> either =
435 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
437 assertTrue(either.isRight());
438 assertEquals(expectedResponse, either.right().value());
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(
449 FilterValueType.GET_PROPERTY,
450 ConstraintType.EQUAL,
451 PropertyFilterTargetType.PROPERTY,
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);
460 Either<Boolean, ResponseFormat> either =
461 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
463 assertTrue(either.isRight());
464 assertEquals(expectedResponse, either.right().value());
468 void testValidatePropertyConstraintParentPropertyNotFound() {
469 final Service service = createService(ToscaPropertyType.STRING.getType());
470 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
472 final ResponseFormat expectedResponse = new ResponseFormat();
473 when(componentsUtils.getResponseFormat(eq(ActionStatus.FILTER_PROPERTY_NOT_FOUND), any(), any()))
474 .thenReturn(expectedResponse);
476 final ToscaGetFunctionDataDefinition toscaGetFunction =
477 createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
478 final var filterConstraintDto = buildFilterConstraintDto(
480 FilterValueType.GET_PROPERTY,
481 ConstraintType.EQUAL,
482 PropertyFilterTargetType.PROPERTY,
485 Assertions.assertThrows(ComponentException.class,
486 () -> nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto)));
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(
497 FilterValueType.GET_PROPERTY,
498 ConstraintType.EQUAL,
499 PropertyFilterTargetType.PROPERTY,
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)));
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);
519 final ToscaGetFunctionDataDefinition toscaGetFunction =
520 createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null);
521 final var filterConstraintDto = buildFilterConstraintDto(
523 FilterValueType.GET_PROPERTY,
524 ConstraintType.EQUAL,
525 PropertyFilterTargetType.PROPERTY,
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);
534 Either<Boolean, ResponseFormat> either =
535 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto));
537 assertTrue(either.isRight());
538 assertEquals(expectedResponse, either.right().value());
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));
549 assertTrue(validationResult.isLeft());
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(
560 FilterValueType.GET_PROPERTY,
561 ConstraintType.EQUAL,
562 PropertyFilterTargetType.PROPERTY,
565 Either<Boolean, ResponseFormat> validationResult =
566 nodeFilterValidator.validateFilter(service, "vfInstance", List.of(filterConstraintDto));
568 assertTrue(validationResult.isLeft());
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);
577 List<ComponentInstance> compInstances = new ArrayList<>();
578 service.getComponentInstances().forEach(compInstance -> compInstances.add(compInstance));
579 compInstances.add(componentInstance);
580 service.setComponentInstances(compInstances);
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<>());
589 service.getComponentInstancesInputs().put(instanceName, Collections.singletonList(componentInstanceInput));
591 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
592 componentInstanceProperty.setName(PROPERTY_NAME);
593 componentInstanceProperty.setType(type);
594 if (service.getComponentInstancesProperties() == null) {
595 service.setComponentInstancesProperties(new HashMap<>());
597 service.getComponentInstancesProperties().put(instanceName, Collections.singletonList(componentInstanceProperty));
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);
606 private Service createService(String type) {
607 return createService(type, null);
610 private Service createService(String type, String schemaType) {
611 Service service = new Service();
612 service.setName(PARENT_SERVICE_ID);
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);
624 service.setProperties(Collections.singletonList(propertyDefinition));
626 ComponentInstance componentInstance = new ComponentInstance();
627 componentInstance.setUniqueId(COMPONENT1_ID);
628 componentInstance.setName(COMPONENT1_ID);
629 componentInstance.setOriginType(OriginTypeEnum.VFC);
631 ComponentInstance componentInstance2 = new ComponentInstance();
632 componentInstance2.setUniqueId(COMPONENT2_ID);
633 componentInstance2.setName(COMPONENT2_ID);
634 componentInstance2.setOriginType(OriginTypeEnum.VFC);
636 ComponentInstance componentInstance3 = new ComponentInstance();
637 componentInstance3.setUniqueId(INNER_SERVICE);
638 componentInstance3.setName(INNER_SERVICE);
639 componentInstance3.setOriginType(OriginTypeEnum.ServiceProxy);
641 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2, componentInstance3));
643 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
644 componentInstanceProperty.setName(PROPERTY_NAME);
645 componentInstanceProperty.setType(type);
647 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
648 componentInstanceProperty2.setName(PROPERTY_NAME);
649 componentInstanceProperty2.setType(type);
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));
658 service.setComponentInstancesProperties(componentInstancePropertyMap);