3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.utils;
24 import static org.junit.jupiter.api.Assertions.*;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.List;
31 import java.util.Optional;
32 import org.junit.jupiter.api.Test;
33 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
34 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
35 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
36 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
37 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
38 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
39 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
40 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
42 class PropertyFilterConstraintDataDefinitionHelperTest {
44 private static final Path RESOURCE_PATH = Path.of("src", "test", "resources", "nodeFilter", "constraints");
47 void convertLegacyConstraintGetInputTest() throws IOException {
48 final var propertyFilterConstraint =
49 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input.yaml"));
50 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.GREATER_OR_EQUAL, FilterValueType.GET_INPUT);
51 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
52 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
53 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_INPUT, ToscaGetFunctionType.GET_INPUT, PropertySource.SELF,
54 List.of("inputName"), "inputName", null);
58 void convertLegacyConstraintGetInputSubPathTest() throws IOException {
59 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
60 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input-subProperty.yaml"));
61 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_INPUT);
62 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
63 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
64 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_INPUT, ToscaGetFunctionType.GET_INPUT, PropertySource.SELF,
65 List.of("inputName", "inputSubProperty", "inputSubSubProperty"), "inputSubSubProperty", null);
69 void convertLegacyConstraintGetPropertyFromInstanceTest() throws IOException {
70 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
71 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_property-from-instance.yaml"));
72 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_PROPERTY);
73 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
74 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
75 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_PROPERTY, ToscaGetFunctionType.GET_PROPERTY, PropertySource.INSTANCE,
76 List.of("property", "subProperty"), "subProperty", "Instance Name");
80 void convertLegacyConstraintGetInputFromSelfTest() throws IOException {
81 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
82 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input-from-self.yaml"));
83 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_INPUT);
84 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
85 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
86 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_INPUT, ToscaGetFunctionType.GET_INPUT, PropertySource.SELF,
87 List.of("SELF", "input", "subInput"), "subInput", null);
91 void convertLegacyConstraintGetAttributeFromInstanceTest() throws IOException {
92 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
93 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-instance.yaml"));
94 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE);
95 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
96 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
97 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.INSTANCE,
98 List.of("property", "subProperty"), "subProperty", "Instance Name");
103 void convertLegacyConstraintGetPropertyFromSelfTest() throws IOException {
104 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
105 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_property-from-self.yaml"));
106 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_PROPERTY);
107 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
108 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
109 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_PROPERTY, ToscaGetFunctionType.GET_PROPERTY, PropertySource.SELF,
110 List.of("property", "subProperty"), "subProperty", null);
114 void convertLegacyConstraintGetAttributeFromSelfTest() throws IOException {
115 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
116 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-self.yaml"));
117 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE);
118 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
119 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
120 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.SELF,
121 List.of("property", "subProperty"), "subProperty", null);
125 void convertLegacyConstraintStaticTest() throws IOException {
126 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
127 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-static.yaml"));
128 assertPropertyFilterConstraint(propertyFilterConstraint, "vnf_profile", null, ConstraintType.EQUAL, FilterValueType.STATIC);
129 assertTrue(propertyFilterConstraint.getValue() instanceof Map);
130 final Map<String, Object> value = (Map<String, Object>) propertyFilterConstraint.getValue();
131 assertEquals("1", value.get("instantiation_level"));
132 assertEquals(1, value.get("max_number_of_instances"));
133 assertEquals(1, value.get("min_number_of_instances"));
137 void convertLegacyConstraintConcatTest() throws IOException {
138 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
139 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("concat.yaml"));
140 assertPropertyFilterConstraint(propertyFilterConstraint, "descriptor_id", null, ConstraintType.EQUAL, FilterValueType.CONCAT);
141 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaConcatFunction);
142 final ToscaConcatFunction toscaConcatFunction = (ToscaConcatFunction) propertyFilterConstraint.getValue();
143 assertEquals(3, toscaConcatFunction.getParameters().size());
144 assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(0).getType());
145 assertEquals("aString", toscaConcatFunction.getParameters().get(0).getValue());
146 assertEquals(ToscaFunctionType.GET_INPUT, toscaConcatFunction.getParameters().get(1).getType());
147 assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(2).getType());
148 assertEquals("anotherString", toscaConcatFunction.getParameters().get(2).getValue());
151 private static void assertPropertyFilterConstraint(final PropertyFilterConstraintDataDefinition propertyFilterConstraint,
152 final String propertyName, final String capabilityName, final ConstraintType constraintType,
153 final FilterValueType filterValueType) {
154 assertEquals(propertyName, propertyFilterConstraint.getPropertyName());
155 assertEquals(capabilityName, propertyFilterConstraint.getCapabilityName());
156 assertEquals(constraintType, propertyFilterConstraint.getOperator());
157 assertEquals(filterValueType, propertyFilterConstraint.getValueType());
160 private void assertToscaGetFunction(final ToscaGetFunctionDataDefinition actualToscaGetFunction,
161 final ToscaFunctionType expectedToscaFunctionType, final ToscaGetFunctionType expectedToscaFunctionGetType,
162 final PropertySource expectedPropertySource, final List<String> expectedPropertyPathFromSource,
163 final String expectedPropertyName, final String expectedSourceName) {
164 assertEquals(expectedToscaFunctionType, actualToscaGetFunction.getType());
165 assertEquals(expectedToscaFunctionGetType, actualToscaGetFunction.getFunctionType());
166 assertEquals(expectedPropertySource, actualToscaGetFunction.getPropertySource());
167 assertEquals(expectedPropertyPathFromSource, actualToscaGetFunction.getPropertyPathFromSource());
168 assertEquals(expectedPropertyName, actualToscaGetFunction.getPropertyName());
169 assertEquals(expectedSourceName, actualToscaGetFunction.getSourceName());
170 assertNull(actualToscaGetFunction.getPropertyUniqueId());
171 assertNull(actualToscaGetFunction.getSourceUniqueId());
175 void convertFromToscaFunctionTypeTest() {
176 Optional<FilterValueType> filterValueType =
177 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_PROPERTY);
178 assertTrue(filterValueType.isPresent());
179 assertEquals(FilterValueType.GET_PROPERTY, filterValueType.get());
182 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_INPUT);
183 assertTrue(filterValueType.isPresent());
184 assertEquals(FilterValueType.GET_INPUT, filterValueType.get());
187 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_ATTRIBUTE);
188 assertTrue(filterValueType.isPresent());
189 assertEquals(FilterValueType.GET_ATTRIBUTE, filterValueType.get());
192 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.YAML);
193 assertTrue(filterValueType.isPresent());
194 assertEquals(FilterValueType.YAML, filterValueType.get());
197 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.CONCAT);
198 assertTrue(filterValueType.isPresent());
199 assertEquals(FilterValueType.CONCAT, filterValueType.get());
201 assertTrue(PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.STRING).isEmpty());
204 private String readConstraintFile(final String fileName) throws IOException {
205 return Files.readString(RESOURCE_PATH.resolve(fileName));