Changed to unmaintained
[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.nio.charset.StandardCharsets;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.mockito.Mockito;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
41 import org.onap.sdnc.config.generator.merge.TestMergeNode;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44
45 public class TestConvertNode {
46     private static final EELFLogger log =
47             EELFManager.getInstance().getLogger(TestConvertNode.class);
48
49     private ConvertNode convertNode = new ConvertNode();
50
51     @Rule
52     public ExpectedException expectedEx = ExpectedException.none();
53
54     @Test
55     public void testConvertJson2DGContextFailure() throws SvcLogicException, IOException {
56         SvcLogicContext ctx = new SvcLogicContext();
57         Map<String, String> inParams = new HashMap<String, String>();
58         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "Y");
59         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
60         expectedEx.expect(SvcLogicException.class);
61         expectedEx.expectMessage("A JSONObject text must begin with '{'");
62         convertJson2Context("convert/payload_parameters_config.json", inParams, ctx);
63     }
64
65     @Test
66     public void testConvertJson2DGContextSuccess() throws SvcLogicException, IOException {
67         SvcLogicContext ctx = new SvcLogicContext();
68         Map<String, String> inParams = new HashMap<String, String>();
69         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
70         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
71         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
72                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
73         convertJson2Context("convert/payload_cli_config.json", inParams, ctx);
74         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
75                 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
76         log.info("testPayloadCliConfig Result: "
77                 + ctx.getAttribute("block_configuration-parameters"));
78         log.info("testPayloadCliConfig Result: "
79                 + ctx.getAttribute("block_configuration.configuration-string"));
80     }
81
82     @Test
83     public void testEscapeDataForJsonDataType() throws Exception {
84         SvcLogicContext ctx = new SvcLogicContext();
85         Map<String, String> inParams = new HashMap<String, String>();
86         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA,"{}");
87         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_JSON);
88         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
89         convertNode.escapeData(inParams, ctx);
90         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
91                 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
92     }
93
94     @Test
95     public void testEscapeDataForXMLDataType() throws Exception {
96         SvcLogicContext ctx = new SvcLogicContext();
97         Map<String, String> inParams = new HashMap<String, String>();
98         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA,"<>");
99         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_XML);
100         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
101         convertNode.escapeData(inParams, ctx);
102         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
103                 ctx.getAttribute(ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
104     }
105
106     @Test
107     public void testEscapeDataForSQLDataType() throws Exception {
108         SvcLogicContext ctx = new SvcLogicContext();
109         Map<String, String> inParams = new HashMap<String, String>();
110         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA,"SHOW TABLES;");
111         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_SQL);
112         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
113         convertNode.escapeData(inParams, ctx);
114         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
115             ctx.getAttribute(ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
116     }
117
118     @Test
119     public void testEscapeDataForUnsupportedDataType() throws Exception {
120         SvcLogicContext ctx = new SvcLogicContext();
121         Map<String, String> inParams = new HashMap<String, String>();
122         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA,"lorum ipsem");
123         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_TEXT);
124         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
125         expectedEx.expect(SvcLogicException.class);
126         expectedEx.expectMessage("Datatype (dataType) param  value (TEXT)is not supported  for escapeData conversion.");
127         convertNode.escapeData(inParams, ctx);
128     }
129
130     @Test
131     public void testUnescapeDataForJsonDataType() throws Exception {
132         SvcLogicContext ctx = new SvcLogicContext();
133         Map<String, String> inParams = new HashMap<String, String>();
134         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA,"{}");
135         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_JSON);
136         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
137         convertNode.unEscapeData(inParams, ctx);
138         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
139                 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
140     }
141
142     @Test
143     public void testUnescapeDataForXMLDataType() throws Exception {
144         SvcLogicContext ctx = new SvcLogicContext();
145         Map<String, String> inParams = new HashMap<String, String>();
146         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA,"<>");
147         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_XML);
148         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
149         convertNode.unEscapeData(inParams, ctx);
150         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
151                 ctx.getAttribute(ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
152     }
153
154     @Test
155     public void testUnescapeDataForUnsupportedDataType() throws Exception {
156         SvcLogicContext ctx = new SvcLogicContext();
157         Map<String, String> inParams = new HashMap<String, String>();
158         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA,"lorum ipsem");
159         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_TEXT);
160         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
161         expectedEx.expect(SvcLogicException.class);
162         expectedEx.expectMessage("Datatype (dataType) param  value (TEXT)is not supported  for unEscapeData conversion.");
163         convertNode.unEscapeData(inParams, ctx);
164     }
165
166     @Test
167     public void testUnescapeDataForNoDataSent() throws Exception {
168         SvcLogicContext ctx = new SvcLogicContext();
169         Map<String, String> inParams = new HashMap<String, String>();
170         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_TEXT);
171         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
172         expectedEx.expect(SvcLogicException.class);
173         expectedEx.expectMessage("Escape (escapeData) param is missing for escapeData conversion.");
174         convertNode.unEscapeData(inParams, ctx);
175     }
176
177     @Test
178     public void testUnescapeDataForNoDataTypeSent() throws Exception {
179         SvcLogicContext ctx = new SvcLogicContext();
180         Map<String, String> inParams = new HashMap<String, String>();
181         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA,"lorum ipsem");
182         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
183         expectedEx.expect(SvcLogicException.class);
184         expectedEx.expectMessage("Datatype (dataType)param is missing for escapeData conversion.");
185         convertNode.unEscapeData(inParams, ctx);
186     }
187
188     @Test
189     public void testEscapeDataForNoDataSent() throws Exception {
190         SvcLogicContext ctx = new SvcLogicContext();
191         Map<String, String> inParams = new HashMap<String, String>();
192         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, ConfigGeneratorConstant.DATA_TYPE_TEXT);
193         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
194         expectedEx.expect(SvcLogicException.class);
195         expectedEx.expectMessage("Unescape (unEscapeData) param is missing for escapeData conversion.");
196         convertNode.escapeData(inParams, ctx);
197     }
198
199     @Test
200     public void testEscapeDataForNoDataTypeSent() throws Exception {
201         SvcLogicContext ctx = new SvcLogicContext();
202         Map<String, String> inParams = new HashMap<String, String>();
203         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA,"lorum ipsem");
204         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
205         expectedEx.expect(SvcLogicException.class);
206         expectedEx.expectMessage("Datatype (dataType)param is missing for escapeData conversion.");
207         convertNode.escapeData(inParams, ctx);
208     }
209
210     @Test
211     public void testConvertContextToJson() throws Exception {
212         SvcLogicContext ctx = new SvcLogicContext();
213         ctx.setAttribute("tmp.uploadConfigInfo.UPLOAD-CONFIG-ID", "200");
214         ctx.setAttribute("tmp.uploadConfigInfo.VNF-ID", "00000");
215         ctx.setAttribute("tmp.uploadConfigInfo.test[0]", "test0");
216         ctx.setAttribute("tmp.uploadConfigInfo.test[1]", "test1");
217         ctx.setAttribute("tmp.uploadConfigInfo.test[2]", "test2");
218         ConvertNode convertNode = new ConvertNode();
219         Map<String, String> inParams = new HashMap<String, String>();
220         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
221         inParams.put("contextKey", "tmp.uploadConfigInfo");
222         convertNode.convertContextToJson(inParams, ctx);
223         log.info("JSON CONTENT " + ctx.getAttribute("test.jsonContent"));
224         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
225     }
226
227     @Test
228     public void testConvertContextToJsonFailure() throws Exception {
229         SvcLogicContext ctx = Mockito.spy(new SvcLogicContext());
230         Mockito.doThrow(new NullPointerException("Mock Exception")).when(ctx).getAttributeKeySet();
231         ctx.setAttribute("tmp.uploadConfigInfo.UPLOAD-CONFIG-ID", "200");
232         ctx.setAttribute("tmp.uploadConfigInfo.VNF-ID", "00000");
233         ctx.setAttribute("tmp.uploadConfigInfo.test[0]", "test0");
234         ctx.setAttribute("tmp.uploadConfigInfo.test[1]", "test1");
235         ctx.setAttribute("tmp.uploadConfigInfo.test[2]", null);
236         ConvertNode convertNode = new ConvertNode();
237         Map<String, String> inParams = new HashMap<String, String>();
238         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
239         inParams.put("contextKey", "tmp.uploadConfigInfo");
240         expectedEx.expect(SvcLogicException.class);
241         expectedEx.expectMessage("Mock Exception");
242         convertNode.convertContextToJson(inParams, ctx);
243     }
244
245     private void convertJson2Context(String jsonFile, Map<String, String> inParams,
246             SvcLogicContext ctx) throws IOException, SvcLogicException {
247         ConvertNode convertNode = new ConvertNode();
248         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
249         String jsonData = IOUtils
250                 .toString(TestMergeNode.class.getClassLoader().getResourceAsStream(jsonFile),
251                         StandardCharsets.UTF_8);
252         log.info("TestConvertNode.testConvertJson2DGContext()" + jsonData);
253         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
254         convertNode.convertJson2DGContext(inParams, ctx);
255         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
256     }
257 }