[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.*;
34
35 public class FunctionTranslationGetParamImplTest extends BaseResourceTranslationTest {
36
37   @Override
38   @Before
39   public void setUp() throws IOException {
40     // do not delete this function. it prevents the superclass setup from running
41   }
42
43   @Test
44   public void testStringGetParamFuncValue() throws Exception {
45     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
46     String functionValue = "parameter1";
47
48     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
49         functionValue, "dummy", null, null, null);
50
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"));
55     }
56
57   }
58
59   @Test
60   public void testListGetParamFuncValue() throws Exception {
61     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
62     List functionValue = new ArrayList();
63     functionValue.add("parameter1");
64     functionValue.add(0);
65     functionValue.add("key1");
66
67     Object translatedFunc = translationImpl.translateFunction(null, null, null,"get_param",
68         functionValue, "dummy", null, null, null);
69
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) {
76
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));
80       }
81     }
82
83   }
84
85   @Test
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]}
91
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");
99
100     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
101         functionValue, "dummy", null, null, null);
102
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))
114               .get("get_input"));
115         }
116       }
117
118     }
119   }
120
121   @Test
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]}
127
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");
140
141     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
142         functionValue, "dummy", null, null, null);
143
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")
155               instanceof List);
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"));
162         }
163       }
164
165     }
166   }
167
168   @Test
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]}
174
175     FunctionTranslationGetParamImpl translationImpl = new FunctionTranslationGetParamImpl();
176     List functionValue = new ArrayList();
177     functionValue.add("parameter1");
178
179     Map templateMap = new HashMap();
180     templateMap.put("template", "$AAkgiru");
181
182     Map strReplaceFuncMap = new HashMap();
183     Map getParamMapInner = new HashMap();
184     getParamMapInner.put("get_param", "parameter2");
185     templateMap.put("AA", getParamMapInner);
186
187     Map innerParamValue = new HashMap();
188     innerParamValue.putAll(templateMap);
189     innerParamValue.putAll(strReplaceFuncMap);
190
191     strReplaceFuncMap.put("str_replace", innerParamValue);
192     functionValue.add(strReplaceFuncMap);
193
194     functionValue.add("key1");
195
196     Object translatedFunc = translationImpl.translateFunction(null, null, null, "get_param",
197         functionValue, "dummy", null, null, null);
198
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))
211                   .get("get_input"));
212         }
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"));
222           }
223         }
224       }
225     }
226   }
227
228   @Test
229   public void testTranslateHeatPseudoParamUsedFromMainHeat() throws Exception {
230     inputFilesPath =
231         "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/inputfiles";
232     outputFilesPath =
233         "/mock/services/heattotosca/heatPseudoParameters/usedFromMainHeat/expectedoutputfiles";
234     initTranslatorAndTranslate();
235     testTranslation();
236   }
237
238   @Test
239   public void testTranslateHeatPseudoParamUsedFromNestedHeat() throws Exception {
240     inputFilesPath =
241         "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/inputfiles";
242     outputFilesPath =
243         "/mock/services/heattotosca/heatPseudoParameters/usedFromNestedHeat/expectedoutputfiles";
244     initTranslatorAndTranslate();
245     testTranslation();
246   }
247
248
249 }