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.components.csar;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Optional;
33 import org.junit.jupiter.api.Test;
34 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
35 import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction;
36 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
37 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
38 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.ToscaStringParameter;
40 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
41 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
43 class ToscaFunctionYamlParsingHandlerTest {
45 final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler = new ToscaFunctionYamlParsingHandler();
47 private static void assertGetInput(final ToscaFunction actualGetInputFunction, final List<String> expectedGetInputParameters) {
48 assertEquals(ToscaFunctionType.GET_INPUT, actualGetInputFunction.getType());
49 assertTrue(actualGetInputFunction instanceof ToscaGetFunctionDataDefinition);
50 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualGetInputFunction;
51 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
52 assertEquals(expectedGetInputParameters.get(expectedGetInputParameters.size() - 1), toscaGetFunction.getPropertyName());
53 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
54 assertEquals(expectedGetInputParameters, toscaGetFunction.getPropertyPathFromSource());
55 assertNull(toscaGetFunction.getPropertyUniqueId());
56 assertNull(toscaGetFunction.getSourceName());
60 void buildToscaFunctionBasedOnPropertyValue_NotAToscaFunctionTest() {
61 assertEquals(Optional.empty(), toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(null));
65 void buildToscaFunctionBasedOnPropertyValue_GetInputTest() {
66 final List<String> getInputParameters = List.of("input", "subProperty");
67 final Map<String, Object> getInput = Map.of(ToscaFunctionType.GET_INPUT.getName(), getInputParameters);
68 final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getInput);
69 assertTrue(actualToscaFunctionOpt.isPresent());
70 final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
71 assertGetInput(actualToscaFunction, getInputParameters);
75 void buildToscaFunctionBasedOnPropertyValue_GetPropertyTest() {
76 final List<String> getPropertyValue = List.of(PropertySource.SELF.getName(), "aProperty", "aSubProperty");
77 final Map<String, Object> getProperty = Map.of(ToscaFunctionType.GET_PROPERTY.getName(), getPropertyValue);
79 final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getProperty);
80 assertTrue(actualToscaFunctionOpt.isPresent());
81 final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
82 assertEquals(ToscaFunctionType.GET_PROPERTY, actualToscaFunction.getType());
83 assertTrue(actualToscaFunction instanceof ToscaGetFunctionDataDefinition);
84 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualToscaFunction;
85 assertEquals(ToscaGetFunctionType.GET_PROPERTY, toscaGetFunction.getFunctionType());
86 assertEquals("aSubProperty", toscaGetFunction.getPropertyName());
87 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
88 assertEquals(getPropertyValue.subList(1, getPropertyValue.size()), toscaGetFunction.getPropertyPathFromSource());
89 assertNull(toscaGetFunction.getPropertyUniqueId());
90 assertNull(toscaGetFunction.getSourceName());
94 void buildToscaFunctionBasedOnPropertyValue_GetAttributeTest() {
95 final List<String> getPropertyValue = List.of(PropertySource.INSTANCE.getName(), "anAttribute", "aSubAttribute");
96 final Map<String, Object> getProperty = Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), getPropertyValue);
98 final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getProperty);
99 assertTrue(actualToscaFunctionOpt.isPresent());
100 final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
101 assertEquals(ToscaFunctionType.GET_ATTRIBUTE, actualToscaFunction.getType());
102 assertTrue(actualToscaFunction instanceof ToscaGetFunctionDataDefinition);
103 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualToscaFunction;
104 assertEquals(ToscaGetFunctionType.GET_ATTRIBUTE, toscaGetFunction.getFunctionType());
105 assertEquals("aSubAttribute", toscaGetFunction.getPropertyName());
106 assertEquals(PropertySource.INSTANCE, toscaGetFunction.getPropertySource());
107 assertEquals(getPropertyValue.subList(1, getPropertyValue.size()), toscaGetFunction.getPropertyPathFromSource());
108 assertEquals(getPropertyValue.get(0), toscaGetFunction.getSourceName());
109 assertNull(toscaGetFunction.getPropertyUniqueId());
113 void buildToscaFunctionBasedOnPropertyValue_ConcatTest() {
114 final List<Object> concatValue = List.of("string1", "-", Map.of(ToscaFunctionType.GET_INPUT.getName(), "inputName"));
115 final Map<String, Object> concatValueMap = Map.of(ToscaFunctionType.CONCAT.getName(), concatValue);
117 final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(concatValueMap);
118 assertTrue(actualToscaFunctionOpt.isPresent());
119 final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
120 assertEquals(ToscaFunctionType.CONCAT, actualToscaFunction.getType());
121 assertTrue(actualToscaFunction instanceof ToscaConcatFunction);
122 final ToscaConcatFunction toscaConcatFunction = (ToscaConcatFunction) actualToscaFunction;
123 assertEquals(3, toscaConcatFunction.getParameters().size());
124 assertTrue(toscaConcatFunction.getParameters().get(0) instanceof ToscaStringParameter);
125 final ToscaStringParameter parameter1 = (ToscaStringParameter) toscaConcatFunction.getParameters().get(0);
126 assertEquals("string1", parameter1.getValue());
127 assertTrue(toscaConcatFunction.getParameters().get(1) instanceof ToscaStringParameter);
128 final ToscaStringParameter parameter2 = (ToscaStringParameter) toscaConcatFunction.getParameters().get(1);
129 assertEquals("-", parameter2.getValue());
130 assertTrue(toscaConcatFunction.getParameters().get(2) instanceof ToscaGetFunctionDataDefinition);
131 final ToscaGetFunctionDataDefinition getFunction = (ToscaGetFunctionDataDefinition) toscaConcatFunction.getParameters().get(2);
132 assertGetInput(getFunction, List.of("inputName"));
136 void buildToscaFunctionBasedOnPropertyValue_CustomTest() {
137 final List<Object> customValue = List.of("string1", "-", Map.of(ToscaFunctionType.GET_INPUT.getName(), "inputName"));
138 final Map<String, Object> customValueMap = Map.of("$customFuncName", customValue);
140 final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(customValueMap);
141 assertTrue(actualToscaFunctionOpt.isPresent());
142 final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
143 assertEquals(ToscaFunctionType.CUSTOM, actualToscaFunction.getType());
144 assertTrue(actualToscaFunction instanceof ToscaCustomFunction);
145 final ToscaCustomFunction toscaCustomFunction = (ToscaCustomFunction) actualToscaFunction;
146 final String functionName = toscaCustomFunction.getName();
147 assertEquals("customFuncName", functionName);
148 assertEquals(3, toscaCustomFunction.getParameters().size());
149 assertTrue(toscaCustomFunction.getParameters().get(0) instanceof ToscaStringParameter);
150 final ToscaStringParameter parameter1 = (ToscaStringParameter) toscaCustomFunction.getParameters().get(0);
151 assertEquals("string1", parameter1.getValue());
152 assertTrue(toscaCustomFunction.getParameters().get(1) instanceof ToscaStringParameter);
153 final ToscaStringParameter parameter2 = (ToscaStringParameter) toscaCustomFunction.getParameters().get(1);
154 assertEquals("-", parameter2.getValue());
155 assertTrue(toscaCustomFunction.getParameters().get(2) instanceof ToscaGetFunctionDataDefinition);
156 final ToscaGetFunctionDataDefinition getFunction = (ToscaGetFunctionDataDefinition) toscaCustomFunction.getParameters().get(2);
157 assertGetInput(getFunction, List.of("inputName"));
161 void isPropertyValueToscaFunctionTest() {
162 assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(ToscaFunctionType.GET_INPUT.getName()));
163 assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(new HashMap<>()));
164 assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(
165 Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), "", ToscaFunctionType.GET_INPUT.getName(), "")
168 assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), "")));
169 assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_INPUT.getName(), "")));
170 assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_PROPERTY.getName(), "")));
171 assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.CONCAT.getName(), "")));
172 assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.YAML.getName(), "")));
173 assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.STRING.getName(), "")));