added test case to TestConvertNode.java
[appc.git] / appc-config / appc-config-generator / provider / src / test / java / org / onap / sdnc / config / generator / convert / TestConvertNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2018 IBM.
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.generator.convert;
27
28 import static org.junit.Assert.assertEquals; 
29 import java.io.IOException;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
37 import org.onap.sdnc.config.generator.merge.TestMergeNode;
38 import org.powermock.reflect.Whitebox;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42 public class TestConvertNode {
43     private static final EELFLogger log =
44             EELFManager.getInstance().getLogger(TestConvertNode.class);
45
46     @Test(expected = Exception.class)
47     public void testPayloadParametersConfig() throws Exception {
48         SvcLogicContext ctx = new SvcLogicContext();
49         Map<String, String> inParams = new HashMap<String, String>();
50         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
51         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
52                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
53         convertJson2Context("convert/payload_parameters_config.json", inParams, ctx);
54         Whitebox.invokeMethod("convertJson2Context", "convert/payload_parameters_config.json",
55                 inParams, ctx);
56         log.info("testPayloadParametersConfig Result: "
57                 + ctx.getAttribute("block_configuration-parameters"));
58     }
59
60     @Test
61     public void testPayloadCliConfig() throws Exception {
62         SvcLogicContext ctx = new SvcLogicContext();
63         Map<String, String> inParams = new HashMap<String, String>();
64         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
65         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
66                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
67         convertJson2Context("convert/payload_cli_config.json", inParams, ctx);
68         log.info("testPayloadCliConfig Result: "
69                 + ctx.getAttribute("block_configuration-parameters"));
70         log.info("testPayloadCliConfig Result: "
71                 + ctx.getAttribute("block_configuration.configuration-string"));
72     }
73
74     @Test(expected = Exception.class)
75     public void testPayloadXMLConfig() throws Exception {
76         SvcLogicContext ctx = new SvcLogicContext();
77         Map<String, String> inParams = new HashMap<String, String>();
78         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
79         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
80                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
81         convertJson2Context("convert/payload_xml_config.json", inParams, ctx);
82
83         log.info("testPayloadXMLConfig Result: "
84                 + ctx.getAttribute("block_configuration-parameters"));
85         log.info("testPayloadXMLConfig Result: "
86                 + ctx.getAttribute("block_configuration.configuration-string"));
87     }
88
89     @Test(expected = Exception.class)
90     public void testPayloadJsonConfig() throws Exception {
91         SvcLogicContext ctx = new SvcLogicContext();
92         Map<String, String> inParams = new HashMap<String, String>();
93         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
94         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
95                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
96         convertJson2Context("convert/payload_json_config.json", inParams, ctx);
97         log.info("testPayloadJsonConfig Result: "
98                 + ctx.getAttribute("block_configuration-parameters"));
99         log.info("testPayloadJsonConfig Result: "
100                 + ctx.getAttribute("block_configuration.configuration-json"));
101     }
102
103     private void convertJson2Context(String jsonFile, Map<String, String> inParams,
104             SvcLogicContext ctx) throws IOException, SvcLogicException {
105         ConvertNode convertNode = new ConvertNode();
106         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
107         String jsonData = IOUtils
108                 .toString(TestMergeNode.class.getClassLoader().getResourceAsStream(jsonFile));
109         log.info("TestConvertNode.testConvertJson2DGContext()" + jsonData);
110         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
111         convertNode.convertJson2DGContext(inParams, ctx);
112         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
113                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
114     }
115
116     @Test(expected = Exception.class)
117     public void testEscapeData() throws Exception {
118         SvcLogicContext ctx = new SvcLogicContext();
119         Map<String, String> inParams = new HashMap<String, String>();
120         String unescapeData = IOUtils.toString(TestMergeNode.class.getClassLoader()
121                 .getResourceAsStream("convert/escape/config_ssc.txt"));
122         log.info("TestConvertNode.testEscapeData() unescapeData :" + unescapeData);
123         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
124         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, unescapeData);
125         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE,
126                 ConfigGeneratorConstant.DATA_TYPE_SQL);
127         ConvertNode convertNode = new ConvertNode();
128         convertNode.escapeData(inParams, ctx);
129         log.info("testEscapeData Result: "
130                 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA));
131     }
132
133     @Test
134     public void testConvertContextToJson() throws Exception {
135         SvcLogicContext ctx = new SvcLogicContext();
136         ctx.setAttribute("tmp.uploadConfigInfo.UPLOAD-CONFIG-ID", "200");
137         ctx.setAttribute("tmp.uploadConfigInfo.VNF-ID", "00000");
138         ctx.setAttribute("tmp.uploadConfigInfo.test[0]", "test0");
139         ctx.setAttribute("tmp.uploadConfigInfo.test[1]", "test1");
140         ctx.setAttribute("tmp.uploadConfigInfo.test[2]", "test2");
141         ConvertNode convertNode = new ConvertNode();
142         Map<String, String> inParams = new HashMap<String, String>();
143         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
144         inParams.put("contextKey", "tmp.uploadConfigInfo");
145         convertNode.convertContextToJson(inParams, ctx);
146         log.info("JSON CONTENT " + ctx.getAttribute("test.jsonContent"));
147         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
148                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
149
150     }
151
152     @Test(expected = Exception.class)
153     public void testunEscapeData() throws Exception {
154         ConvertNode convertNode = new ConvertNode();
155         Map<String, String> inParams = new HashMap<String, String>();
156         SvcLogicContext ctx = new SvcLogicContext();
157         log.trace("Received unEscapeData call with params : " + inParams);
158         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
159         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA, "//");
160         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "String");
161         convertNode.unEscapeData(inParams, ctx);
162
163     }
164     
165     @Test(expected = SvcLogicException.class)
166     public void testunEscapeDataForInvalidDataType() throws Exception {
167         ConvertNode convertNode = new ConvertNode();
168         Map<String, String> inParams = new HashMap<String, String>();
169         SvcLogicContext ctx = new SvcLogicContext();
170         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
171         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, "//");
172         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "TXT");
173         convertNode.escapeData(inParams, ctx);
174
175     }
176     
177     @Test(expected = SvcLogicException.class)
178     public void testunEscapeDataForEmptyDataType() throws Exception {
179         ConvertNode convertNode = new ConvertNode();
180         Map<String, String> inParams = new HashMap<String, String>();
181         SvcLogicContext ctx = new SvcLogicContext();
182         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
183         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, "//");
184         convertNode.escapeData(inParams, ctx);
185
186     }
187     
188     @Test
189     public void testEscapeDataForValidUnescapeDataString() throws Exception {
190         SvcLogicContext ctx = new SvcLogicContext();
191         Map<String, String> inParams = new HashMap<String, String>();
192         String unescapeData = "testUnescapeData";
193         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
194         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, unescapeData);
195         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE,
196                 ConfigGeneratorConstant.DATA_TYPE_SQL);
197         ConvertNode convertNode = new ConvertNode();
198         convertNode.escapeData(inParams, ctx);
199         assertEquals(unescapeData, ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA));
200     }
201   
202      @Test
203     public void testunEscapeDataForJsonDataType() throws Exception {
204         ConvertNode convertNode = new ConvertNode();
205         Map<String, String> inParams = new HashMap<String, String>();
206         SvcLogicContext ctx = new SvcLogicContext();
207         log.trace("Received unEscapeData call with params : " + inParams);
208         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
209         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA, "//");
210         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "JSON");
211         convertNode.unEscapeData(inParams, ctx);
212         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute("tmp." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
213
214     }
215     
216     @Test
217     public void testunEscapeDataForXmlDataType() throws Exception {
218         ConvertNode convertNode = new ConvertNode();
219         Map<String, String> inParams = new HashMap<String, String>();
220         SvcLogicContext ctx = new SvcLogicContext();
221         log.trace("Received unEscapeData call with params : " + inParams);
222         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
223         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA, "//");
224         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "XML");
225         convertNode.unEscapeData(inParams, ctx);
226         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute("tmp." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
227     }
228     
229 }