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.datatypes.elements;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
31 import java.util.List;
32 import org.junit.jupiter.api.Test;
33 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
36 class ToscaFunctionJsonDeserializerTest {
39 void testGetInputToscaFunction() throws JsonProcessingException {
40 final String toscaGetInputFunction = "{\n"
41 + " \"propertyUniqueId\": \"e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming\",\n"
42 + " \"type\": \"GET_INPUT\",\n"
43 + " \"propertySource\": \"SELF\",\n"
44 + " \"propertyName\": \"instance_name\",\n"
45 + " \"sourceName\": \"ciResVFc26a0b30ec20\",\n"
46 + " \"sourceUniqueId\": \"aee643c9-6c8e-4a24-af7a-a9aff5c072c0\",\n"
47 + " \"propertyPathFromSource\": [\n"
49 + " \"instance_name\"\n"
52 ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
53 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
54 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
55 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
56 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
57 assertEquals("e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming", toscaGetFunction.getPropertyUniqueId());
58 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
59 assertEquals("instance_name", toscaGetFunction.getPropertyName());
60 assertEquals("ciResVFc26a0b30ec20", toscaGetFunction.getSourceName());
61 assertEquals("aee643c9-6c8e-4a24-af7a-a9aff5c072c0", toscaGetFunction.getSourceUniqueId());
62 assertEquals(List.of("nf_naming", "instance_name"), toscaGetFunction.getPropertyPathFromSource());
66 void testGetInputToscaFunctionLegacyConversion() throws JsonProcessingException {
67 final String toscaGetInputFunction = "{\n"
68 + " \"propertyUniqueId\": \"e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming\",\n"
69 + " \"functionType\": \"GET_INPUT\",\n"
70 + " \"propertySource\": \"SELF\",\n"
71 + " \"propertyName\": \"instance_name\",\n"
72 + " \"sourceName\": \"ciResVFc26a0b30ec20\",\n"
73 + " \"sourceUniqueId\": \"aee643c9-6c8e-4a24-af7a-a9aff5c072c0\",\n"
74 + " \"propertyPathFromSource\": [\n"
76 + " \"instance_name\"\n"
79 ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
80 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
81 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
82 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
83 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
87 void testNoFunctionTypeProvided() {
88 final String toscaGetInputFunction = "{\n"
89 + " \"propertyUniqueId\": \"e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming\",\n"
90 + " \"propertySource\": \"SELF\",\n"
91 + " \"propertyName\": \"instance_name\",\n"
92 + " \"sourceName\": \"ciResVFc26a0b30ec20\",\n"
93 + " \"sourceUniqueId\": \"aee643c9-6c8e-4a24-af7a-a9aff5c072c0\",\n"
94 + " \"propertyPathFromSource\": [\n"
96 + " \"instance_name\"\n"
99 final ValueInstantiationException actualException =
100 assertThrows(ValueInstantiationException.class, () -> parseToscaFunction(toscaGetInputFunction));
101 assertTrue(actualException.getMessage().contains("Attribute type not provided"));
105 void testConcatToscaFunction() throws JsonProcessingException {
106 final String toscaGetInputFunction = "{\n"
107 + " \"type\": \"CONCAT\",\n"
108 + " \"parameters\": [\n"
110 + " \"propertyUniqueId\": \"e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming\",\n"
111 + " \"type\": \"GET_INPUT\",\n"
112 + " \"propertySource\": \"SELF\",\n"
113 + " \"propertyName\": \"instance_name\",\n"
114 + " \"sourceName\": \"ciResVFc26a0b30ec20\",\n"
115 + " \"sourceUniqueId\": \"aee643c9-6c8e-4a24-af7a-a9aff5c072c0\",\n"
116 + " \"propertyPathFromSource\": [\n"
117 + " \"nf_naming\",\n"
118 + " \"instance_name\"\n"
121 + " \"type\": \"STRING\",\n"
122 + " \"value\": \"my string\"\n"
125 + " \"type\": \"CONCAT\",\n"
126 + " \"parameters\": [\n"
128 + " \"type\": \"STRING\",\n"
129 + " \"value\": \"string1\"\n"
132 + " \"type\": \"STRING\",\n"
133 + " \"value\": \"string2\"\n"
139 ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
140 assertTrue(toscaFunction instanceof ToscaConcatFunction);
144 void testYamlFunction() throws JsonProcessingException {
145 String yamlFunction = "{\n"
146 + " \"type\": \"YAML\",\n"
147 + " \"value\": \"tosca_definitions_version: tosca_simple_yaml_1_0_0\\nnode_types: \\n tosca.nodes.Compute:\\n derived_from: tosca.nodes.Root\\n attributes:\\n private_address:\\n type: string\\n public_address:\\n type: string\\n networks:\\n type: map\\n entry_schema:\\n type: tosca.datatypes.network.NetworkInfo\\n ports:\\n type: map\\n entry_schema:\\n type: tosca.datatypes.network.PortInfo\\n requirements:\\n - local_storage: \\n capability: tosca.capabilities.Attachment\\n node: tosca.nodes.BlockStorage\\n relationship: tosca.relationships.AttachesTo\\n occurrences: [0, UNBOUNDED] \\n capabilities:\\n host: \\n type: tosca.capabilities.Container\\n valid_source_types: [tosca.nodes.SoftwareComponent] \\n endpoint :\\n type: tosca.capabilities.Endpoint.Admin \\n os: \\n type: tosca.capabilities.OperatingSystem\\n scalable:\\n type: tosca.capabilities.Scalable\\n binding:\\n type: tosca.capabilities.network.Bindable\\n\"\n"
149 ToscaFunction toscaFunction = parseToscaFunction(yamlFunction);
150 assertTrue(toscaFunction instanceof CustomYamlFunction);
153 private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException {
154 return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class);