2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.BaseResourceTranslationTest;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertNotNull;
36 public class FunctionTranslationGetParamImplTest extends BaseResourceTranslationTest {
40 public void setUp() throws IOException {
41 // do not delete this function. it prevents the superclass setup from running
45 public void testStringGetParamFuncValue() throws Exception {
46 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
47 String functionValue = "parameter1";
49 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
50 functionValue, "dummy", null, null, null);
52 assertEquals(true, translatedFunc instanceof Map);
53 if (translatedFunc instanceof Map) {
54 assertNotNull(((Map) translatedFunc).get("get_input"));
55 assertEquals(functionValue, ((Map) translatedFunc).get("get_input"));
61 public void testListGetParamFuncValue() throws Exception {
62 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
63 List functionValue = new ArrayList();
64 functionValue.add("parameter1");
66 functionValue.add("key1");
68 Object translatedFunc = translationImpl.translateFunction(null, null, null,"get_param",
69 functionValue, "dummy", null, null, null);
71 assertEquals(true, translatedFunc instanceof Map);
72 if (translatedFunc instanceof Map) {
73 assertNotNull(((Map) translatedFunc).get("get_input"));
74 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
75 assertEquals(true, translatedFunValue instanceof List);
76 if (translatedFunValue instanceof List) {
78 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
79 assertEquals(functionValue.get(1), ((List) translatedFunValue).get(1));
80 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
87 public void testMapGetParamFuncValue() throws Exception {
88 // input heat function expression
89 //{get_param: [parameter1, {get_param:indexParam}, key1]}
90 // output translated function expression
91 //{get_input: [parameter1, {get_input:indexParam}, key1]}
93 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
94 List functionValue = new ArrayList();
95 functionValue.add("parameter1");
96 Map innerParamMap = new HashMap();
97 innerParamMap.put("get_param", "indexParam");
98 functionValue.add(innerParamMap);
99 functionValue.add("key1");
101 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
102 functionValue, "dummy", null, null, null);
104 assertEquals(true, translatedFunc instanceof Map);
105 if (translatedFunc instanceof Map) {
106 assertNotNull(((Map) translatedFunc).get("get_input"));
107 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
108 assertEquals(true, translatedFunValue instanceof List);
109 if (translatedFunValue instanceof List) {
110 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
111 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
112 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
113 if (((List) translatedFunValue).get(1) instanceof Map) {
114 assertEquals(innerParamMap.get("get_param"), ((Map) ((List) translatedFunValue).get(1))
123 public void testMapWithMapGetParamFuncValue() throws Exception {
124 // input heat function expression
125 //{get_param: [parameter1, {get_param:[parameter2, {get_param:indexParam}]}, key1]}
126 // output translated function expression
127 //{get_input: [parameter1, {get_input:[parameter2, {get_input:indexParam}]}, key1]}
129 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
130 List functionValue = new ArrayList();
131 functionValue.add("parameter1");
132 Map firstInnerParamMap = new HashMap();
133 Map secondInnerParamMap = new HashMap();
134 secondInnerParamMap.put("get_param", "indexParam");
135 List innerfunction = new ArrayList();
136 innerfunction.add("parameter2");
137 innerfunction.add(secondInnerParamMap);
138 firstInnerParamMap.put("get_param", innerfunction);
139 functionValue.add(firstInnerParamMap);
140 functionValue.add("key1");
142 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
143 functionValue, "dummy", null, null, null);
145 assertEquals(true, translatedFunc instanceof Map);
146 if (translatedFunc instanceof Map) {
147 assertNotNull(((Map) translatedFunc).get("get_input"));
148 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
149 assertEquals(true, translatedFunValue instanceof List);
150 if (translatedFunValue instanceof List) {
151 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
152 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
153 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
154 if (((List) translatedFunValue).get(1) instanceof Map) {
155 assertEquals(true, ((Map) ((List) translatedFunValue).get(1)).get("get_input")
157 List innerTranslatedFunction =
158 (List) ((Map) ((List) translatedFunValue).get(1)).get("get_input");
159 assertEquals(innerfunction.get(0), innerTranslatedFunction.get(0));
160 assertEquals(true, innerTranslatedFunction.get(1) instanceof Map);
161 assertEquals(secondInnerParamMap.get("get_param"),
162 ((Map) innerTranslatedFunction.get(1)).get("get_input"));
170 public void testInnerNotSupportedFuncGetParamFuncValue() throws Exception {
171 // input heat function expression
172 //{get_param: [parameter1, {str_replace: {template:$AAkgiru, AA:{get_param:prameter2}}}, key1]}
173 // output translated function expression
174 //{get_input: [parameter1, {str_replace: {template:$AAkgiru, AA:{get_input:parameter2}}}, key1]}
176 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
177 List functionValue = new ArrayList();
178 functionValue.add("parameter1");
180 Map templateMap = new HashMap();
181 templateMap.put("template", "$AAkgiru");
183 Map strReplaceFuncMap = new HashMap();
184 Map getParamMapInner = new HashMap();
185 getParamMapInner.put("get_param", "parameter2");
186 templateMap.put("AA", getParamMapInner);
188 Map innerParamValue = new HashMap();
189 innerParamValue.putAll(templateMap);
190 innerParamValue.putAll(strReplaceFuncMap);
192 strReplaceFuncMap.put("str_replace", innerParamValue);
193 functionValue.add(strReplaceFuncMap);
195 functionValue.add("key1");
197 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
198 functionValue, "dummy", null, null, null);
200 assertEquals(true, translatedFunc instanceof Map);
201 if (translatedFunc instanceof Map) {
202 assertNotNull(((Map) translatedFunc).get("get_input"));
203 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
204 assertEquals(true, translatedFunValue instanceof List);
205 if (translatedFunValue instanceof List) {
206 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
207 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
208 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
209 if (((List) translatedFunValue).get(1) instanceof Map) {
210 assertEquals(strReplaceFuncMap.get("get_param"),
211 ((Map) ((List) translatedFunValue).get(1))
214 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
215 if (((List) translatedFunValue).get(1) instanceof Map) {
216 assertEquals(true, ((Map) ((List) translatedFunValue).get(1)).get("str_replace") instanceof Map);
217 Map strReplacefunctionValue = (Map) ((Map) ((List) translatedFunValue).get(1)).get("str_replace");
218 assertEquals(templateMap.get("template"), strReplacefunctionValue.get("template"));
219 assertEquals(true, strReplacefunctionValue.get("AA") instanceof Map);
220 if (strReplacefunctionValue.get("AA") instanceof Map) {
221 assertEquals(getParamMapInner.get("get_param"), ((Map) strReplacefunctionValue.get
222 ("AA")).get("get_input"));
230 public void testTranslateHeatPseudoParamUsedFromMainHeat() throws Exception {
232 "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/inputfiles";
234 "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/expectedoutputfiles";
235 initTranslatorAndTranslate();
240 public void testTranslateHeatPseudoParamUsedFromNestedHeat() throws Exception {
242 "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/inputfiles";
244 "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/expectedoutputfiles";
245 initTranslatorAndTranslate();