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.*;
35 public class FunctionTranslationGetParamImplTest extends BaseResourceTranslationTest {
39 public void setUp() throws IOException {
40 // do not delete this function. it prevents the superclass setup from running
44 public void testStringGetParamFuncValue() throws Exception {
45 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
46 String functionValue = "parameter1";
48 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
49 functionValue, "dummy", null, null, null);
51 assertEquals(true, translatedFunc instanceof Map);
52 if (translatedFunc instanceof Map) {
53 assertNotNull(((Map) translatedFunc).get("get_input"));
54 assertEquals(functionValue, ((Map) translatedFunc).get("get_input"));
60 public void testListGetParamFuncValue() throws Exception {
61 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
62 List functionValue = new ArrayList();
63 functionValue.add("parameter1");
65 functionValue.add("key1");
67 Object translatedFunc = translationImpl.translateFunction(null, null, null,"get_param",
68 functionValue, "dummy", null, null, null);
70 assertEquals(true, translatedFunc instanceof Map);
71 if (translatedFunc instanceof Map) {
72 assertNotNull(((Map) translatedFunc).get("get_input"));
73 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
74 assertEquals(true, translatedFunValue instanceof List);
75 if (translatedFunValue instanceof List) {
77 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
78 assertEquals(functionValue.get(1), ((List) translatedFunValue).get(1));
79 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
86 public void testMapGetParamFuncValue() throws Exception {
87 // input heat function expression
88 //{get_param: [parameter1, {get_param:indexParam}, key1]}
89 // output translated function expression
90 //{get_input: [parameter1, {get_input:indexParam}, key1]}
92 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
93 List functionValue = new ArrayList();
94 functionValue.add("parameter1");
95 Map innerParamMap = new HashMap();
96 innerParamMap.put("get_param", "indexParam");
97 functionValue.add(innerParamMap);
98 functionValue.add("key1");
100 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
101 functionValue, "dummy", null, null, null);
103 assertEquals(true, translatedFunc instanceof Map);
104 if (translatedFunc instanceof Map) {
105 assertNotNull(((Map) translatedFunc).get("get_input"));
106 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
107 assertEquals(true, translatedFunValue instanceof List);
108 if (translatedFunValue instanceof List) {
109 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
110 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
111 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
112 if (((List) translatedFunValue).get(1) instanceof Map) {
113 assertEquals(innerParamMap.get("get_param"), ((Map) ((List) translatedFunValue).get(1))
122 public void testMapWithMapGetParamFuncValue() throws Exception {
123 // input heat function expression
124 //{get_param: [parameter1, {get_param:[parameter2, {get_param:indexParam}]}, key1]}
125 // output translated function expression
126 //{get_input: [parameter1, {get_input:[parameter2, {get_input:indexParam}]}, key1]}
128 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
129 List functionValue = new ArrayList();
130 functionValue.add("parameter1");
131 Map firstInnerParamMap = new HashMap();
132 Map secondInnerParamMap = new HashMap();
133 secondInnerParamMap.put("get_param", "indexParam");
134 List innerfunction = new ArrayList();
135 innerfunction.add("parameter2");
136 innerfunction.add(secondInnerParamMap);
137 firstInnerParamMap.put("get_param", innerfunction);
138 functionValue.add(firstInnerParamMap);
139 functionValue.add("key1");
141 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
142 functionValue, "dummy", null, null, null);
144 assertEquals(true, translatedFunc instanceof Map);
145 if (translatedFunc instanceof Map) {
146 assertNotNull(((Map) translatedFunc).get("get_input"));
147 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
148 assertEquals(true, translatedFunValue instanceof List);
149 if (translatedFunValue instanceof List) {
150 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
151 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
152 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
153 if (((List) translatedFunValue).get(1) instanceof Map) {
154 assertEquals(true, ((Map) ((List) translatedFunValue).get(1)).get("get_input")
156 List innerTranslatedFunction =
157 (List) ((Map) ((List) translatedFunValue).get(1)).get("get_input");
158 assertEquals(innerfunction.get(0), innerTranslatedFunction.get(0));
159 assertEquals(true, innerTranslatedFunction.get(1) instanceof Map);
160 assertEquals(secondInnerParamMap.get("get_param"),
161 ((Map) innerTranslatedFunction.get(1)).get("get_input"));
169 public void testInnerNotSupportedFuncGetParamFuncValue() throws Exception {
170 // input heat function expression
171 //{get_param: [parameter1, {str_replace: {template:$AAkgiru, AA:{get_param:prameter2}}}, key1]}
172 // output translated function expression
173 //{get_input: [parameter1, {str_replace: {template:$AAkgiru, AA:{get_input:parameter2}}}, key1]}
175 FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
176 List functionValue = new ArrayList();
177 functionValue.add("parameter1");
179 Map templateMap = new HashMap();
180 templateMap.put("template", "$AAkgiru");
182 Map strReplaceFuncMap = new HashMap();
183 Map getParamMapInner = new HashMap();
184 getParamMapInner.put("get_param", "parameter2");
185 templateMap.put("AA", getParamMapInner);
187 Map innerParamValue = new HashMap();
188 innerParamValue.putAll(templateMap);
189 innerParamValue.putAll(strReplaceFuncMap);
191 strReplaceFuncMap.put("str_replace", innerParamValue);
192 functionValue.add(strReplaceFuncMap);
194 functionValue.add("key1");
196 Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
197 functionValue, "dummy", null, null, null);
199 assertEquals(true, translatedFunc instanceof Map);
200 if (translatedFunc instanceof Map) {
201 assertNotNull(((Map) translatedFunc).get("get_input"));
202 Object translatedFunValue = ((Map) translatedFunc).get("get_input");
203 assertEquals(true, translatedFunValue instanceof List);
204 if (translatedFunValue instanceof List) {
205 assertEquals(functionValue.get(0), ((List) translatedFunValue).get(0));
206 assertEquals(functionValue.get(2), ((List) translatedFunValue).get(2));
207 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
208 if (((List) translatedFunValue).get(1) instanceof Map) {
209 assertEquals(strReplaceFuncMap.get("get_param"),
210 ((Map) ((List) translatedFunValue).get(1))
213 assertEquals(true, ((List) translatedFunValue).get(1) instanceof Map);
214 if (((List) translatedFunValue).get(1) instanceof Map) {
215 assertEquals(true, ((Map) ((List) translatedFunValue).get(1)).get("str_replace") instanceof Map);
216 Map strReplacefunctionValue = (Map) ((Map) ((List) translatedFunValue).get(1)).get("str_replace");
217 assertEquals(templateMap.get("template"), strReplacefunctionValue.get("template"));
218 assertEquals(true, strReplacefunctionValue.get("AA") instanceof Map);
219 if (strReplacefunctionValue.get("AA") instanceof Map) {
220 assertEquals(getParamMapInner.get("get_param"), ((Map) strReplacefunctionValue.get
221 ("AA")).get("get_input"));
229 public void testTranslateHeatPseudoParamUsedFromMainHeat() throws Exception {
231 "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/inputfiles";
233 "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/expectedoutputfiles";
234 initTranslatorAndTranslate();
239 public void testTranslateHeatPseudoParamUsedFromNestedHeat() throws Exception {
241 "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/inputfiles";
243 "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/expectedoutputfiles";
244 initTranslatorAndTranslate();