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 convertLegacyConstraintGetAttributeFromInstanceTest() throws IOException {
81 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
82 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-instance.yaml"));
83 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE);
84 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
85 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
86 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.INSTANCE,
87 List.of("property", "subProperty"), "subProperty", "Instance Name");
92 void convertLegacyConstraintGetPropertyFromSelfTest() throws IOException {
93 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
94 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_property-from-self.yaml"));
95 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_PROPERTY);
96 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
97 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
98 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_PROPERTY, ToscaGetFunctionType.GET_PROPERTY, PropertySource.SELF,
99 List.of("property", "subProperty"), "subProperty", null);
103 void convertLegacyConstraintGetAttributeFromSelfTest() throws IOException {
104 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
105 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-self.yaml"));
106 assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE);
107 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
108 final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue();
109 assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.SELF,
110 List.of("property", "subProperty"), "subProperty", null);
114 void convertLegacyConstraintStaticTest() throws IOException {
115 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
116 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-static.yaml"));
117 assertPropertyFilterConstraint(propertyFilterConstraint, "vnf_profile", null, ConstraintType.EQUAL, FilterValueType.STATIC);
118 assertTrue(propertyFilterConstraint.getValue() instanceof Map);
119 final Map<String, Object> value = (Map<String, Object>) propertyFilterConstraint.getValue();
120 assertEquals("1", value.get("instantiation_level"));
121 assertEquals(1, value.get("max_number_of_instances"));
122 assertEquals(1, value.get("min_number_of_instances"));
126 void convertLegacyConstraintConcatTest() throws IOException {
127 final PropertyFilterConstraintDataDefinition propertyFilterConstraint =
128 PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("concat.yaml"));
129 assertPropertyFilterConstraint(propertyFilterConstraint, "descriptor_id", null, ConstraintType.EQUAL, FilterValueType.CONCAT);
130 assertTrue(propertyFilterConstraint.getValue() instanceof ToscaConcatFunction);
131 final ToscaConcatFunction toscaConcatFunction = (ToscaConcatFunction) propertyFilterConstraint.getValue();
132 assertEquals(3, toscaConcatFunction.getParameters().size());
133 assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(0).getType());
134 assertEquals("aString", toscaConcatFunction.getParameters().get(0).getValue());
135 assertEquals(ToscaFunctionType.GET_INPUT, toscaConcatFunction.getParameters().get(1).getType());
136 assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(2).getType());
137 assertEquals("anotherString", toscaConcatFunction.getParameters().get(2).getValue());
140 private static void assertPropertyFilterConstraint(final PropertyFilterConstraintDataDefinition propertyFilterConstraint,
141 final String propertyName, final String capabilityName, final ConstraintType constraintType,
142 final FilterValueType filterValueType) {
143 assertEquals(propertyName, propertyFilterConstraint.getPropertyName());
144 assertEquals(capabilityName, propertyFilterConstraint.getCapabilityName());
145 assertEquals(constraintType, propertyFilterConstraint.getOperator());
146 assertEquals(filterValueType, propertyFilterConstraint.getValueType());
149 private void assertToscaGetFunction(final ToscaGetFunctionDataDefinition actualToscaGetFunction,
150 final ToscaFunctionType expectedToscaFunctionType, final ToscaGetFunctionType expectedToscaFunctionGetType,
151 final PropertySource expectedPropertySource, final List<String> expectedPropertyPathFromSource,
152 final String expectedPropertyName, final String expectedSourceName) {
153 assertEquals(expectedToscaFunctionType, actualToscaGetFunction.getType());
154 assertEquals(expectedToscaFunctionGetType, actualToscaGetFunction.getFunctionType());
155 assertEquals(expectedPropertySource, actualToscaGetFunction.getPropertySource());
156 assertEquals(expectedPropertyPathFromSource, actualToscaGetFunction.getPropertyPathFromSource());
157 assertEquals(expectedPropertyName, actualToscaGetFunction.getPropertyName());
158 assertEquals(expectedSourceName, actualToscaGetFunction.getSourceName());
159 assertNull(actualToscaGetFunction.getPropertyUniqueId());
160 assertNull(actualToscaGetFunction.getSourceUniqueId());
164 void convertFromToscaFunctionTypeTest() {
165 Optional<FilterValueType> filterValueType =
166 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_PROPERTY);
167 assertTrue(filterValueType.isPresent());
168 assertEquals(FilterValueType.GET_PROPERTY, filterValueType.get());
171 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_INPUT);
172 assertTrue(filterValueType.isPresent());
173 assertEquals(FilterValueType.GET_INPUT, filterValueType.get());
176 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_ATTRIBUTE);
177 assertTrue(filterValueType.isPresent());
178 assertEquals(FilterValueType.GET_ATTRIBUTE, filterValueType.get());
181 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.YAML);
182 assertTrue(filterValueType.isPresent());
183 assertEquals(FilterValueType.YAML, filterValueType.get());
186 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.CONCAT);
187 assertTrue(filterValueType.isPresent());
188 assertEquals(FilterValueType.CONCAT, filterValueType.get());
190 assertTrue(PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.STRING).isEmpty());
193 private String readConstraintFile(final String fileName) throws IOException {
194 return Files.readString(RESOURCE_PATH.resolve(fileName));