Support for concat TOSCA function
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / datatypes / elements / ToscaFunctionJsonDeserializerTest.java
1 /*
2  * -
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.datatypes.elements;
23
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;
27
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;
35
36 class ToscaFunctionJsonDeserializerTest {
37
38     @Test
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"
48             + "                \"nf_naming\",\n"
49             + "                \"instance_name\"\n"
50             + "            ]\n"
51             + "        }";
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());
63     }
64
65     @Test
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"
75             + "                \"nf_naming\",\n"
76             + "                \"instance_name\"\n"
77             + "            ]\n"
78             + "        }";
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());
84     }
85
86     @Test
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"
95             + "                \"nf_naming\",\n"
96             + "                \"instance_name\"\n"
97             + "            ]\n"
98             + "        }";
99         final ValueInstantiationException actualException =
100             assertThrows(ValueInstantiationException.class, () -> parseToscaFunction(toscaGetInputFunction));
101         assertTrue(actualException.getMessage().contains("Attribute type not provided"));
102     }
103
104     @Test
105     void testConcatToscaFunction() throws JsonProcessingException {
106         final String toscaGetInputFunction = "{\n"
107             + "  \"type\": \"CONCAT\",\n"
108             + "  \"parameters\": [\n"
109             + "    {\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"
119             + "      ]\n"
120             + "    }, {\n"
121             + "      \"type\": \"STRING\",\n"
122             + "      \"value\": \"my string\"\n"
123             + "    },\n"
124             + "    {\n"
125             + "      \"type\": \"CONCAT\",\n"
126             + "      \"parameters\": [\n"
127             + "        {\n"
128             + "          \"type\": \"STRING\",\n"
129             + "          \"value\": \"string1\"\n"
130             + "        },\n"
131             + "        {\n"
132             + "          \"type\": \"STRING\",\n"
133             + "          \"value\": \"string2\"\n"
134             + "        }\n"
135             + "      ]\n"
136             + "    }\n"
137             + "  ]\n"
138             + "}";
139         ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
140         assertTrue(toscaFunction instanceof ToscaConcatFunction);
141     }
142
143     @Test
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"
148             + "}";
149         ToscaFunction toscaFunction = parseToscaFunction(yamlFunction);
150         assertTrue(toscaFunction instanceof CustomYamlFunction);
151     }
152
153     private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException {
154         return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class);
155     }
156 }