Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / java / org / openecomp / sdc / translator / services / heattotosca / impl / functiontranslation / FunctionTranslationGetParamImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.BaseResourceTranslationTest;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertNotNull;
35
36 public class FunctionTranslationGetParamImplTest extends BaseResourceTranslationTest {
37
38   @Override
39   @Before
40   public void setUp() throws IOException {
41     // do not delete this function. it prevents the superclass setup from running
42   }
43
44   @Test
45   public void testStringGetParamFuncValue() throws Exception {
46     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
47     String functionValue = "parameter1";
48
49     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
50         functionValue, "dummy", null, null, null);
51
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"));
56     }
57
58   }
59
60   @Test
61   public void testListGetParamFuncValue() throws Exception {
62     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
63     List functionValue = new ArrayList();
64     functionValue.add("parameter1");
65     functionValue.add(0);
66     functionValue.add("key1");
67
68     Object translatedFunc = translationImpl.translateFunction(null, null, null,"get_param",
69         functionValue, "dummy", null, null, null);
70
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) {
77
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));
81       }
82     }
83
84   }
85
86   @Test
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]}
92
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");
100
101     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
102         functionValue, "dummy", null, null, null);
103
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))
115               .get("get_input"));
116         }
117       }
118
119     }
120   }
121
122   @Test
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]}
128
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");
141
142     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
143         functionValue, "dummy", null, null, null);
144
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")
156               instanceof List);
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"));
163         }
164       }
165
166     }
167   }
168
169   @Test
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]}
175
176     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
177     List functionValue = new ArrayList();
178     functionValue.add("parameter1");
179
180     Map templateMap = new HashMap();
181     templateMap.put("template", "$AAkgiru");
182
183     Map strReplaceFuncMap = new HashMap();
184     Map getParamMapInner = new HashMap();
185     getParamMapInner.put("get_param", "parameter2");
186     templateMap.put("AA", getParamMapInner);
187
188     Map innerParamValue = new HashMap();
189     innerParamValue.putAll(templateMap);
190     innerParamValue.putAll(strReplaceFuncMap);
191
192     strReplaceFuncMap.put("str_replace", innerParamValue);
193     functionValue.add(strReplaceFuncMap);
194
195     functionValue.add("key1");
196
197     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
198         functionValue, "dummy", null, null, null);
199
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))
212                   .get("get_input"));
213         }
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"));
223           }
224         }
225       }
226     }
227   }
228
229   @Test
230   public void testTranslateHeatPseudoParamUsedFromMainHeat() throws Exception {
231     inputFilesPath =
232         "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/inputfiles";
233     outputFilesPath =
234         "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/expectedoutputfiles";
235     initTranslatorAndTranslate();
236     testTranslation();
237   }
238
239   @Test
240   public void testTranslateHeatPseudoParamUsedFromNestedHeat() throws Exception {
241     inputFilesPath =
242         "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/inputfiles";
243     outputFilesPath =
244         "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/expectedoutputfiles";
245     initTranslatorAndTranslate();
246     testTranslation();
247   }
248
249
250 }