979a001b1211fd4882f29237bff80032556214d9
[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  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.generator.convert;
25
26 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
28 import java.util.HashMap;
29 import java.util.Map;
30 import org.apache.commons.io.IOUtils;
31 import org.junit.Test;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
35 import org.onap.sdnc.config.generator.merge.TestMergeNode;
36 import org.powermock.reflect.Whitebox;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40 public class TestConvertNode {
41     private static final EELFLogger log =
42             EELFManager.getInstance().getLogger(TestConvertNode.class);
43
44     @Test(expected = Exception.class)
45     public void testPayloadParametersConfig() throws Exception {
46         SvcLogicContext ctx = new SvcLogicContext();
47         Map<String, String> inParams = new HashMap<String, String>();
48         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
49         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
50                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
51         convertJson2Context("convert/payload_parameters_config.json", inParams, ctx);
52         Whitebox.invokeMethod("convertJson2Context", "convert/payload_parameters_config.json",
53                 inParams, ctx);
54         log.info("testPayloadParametersConfig Result: "
55                 + ctx.getAttribute("block_configuration-parameters"));
56     }
57
58     @Test
59     public void testPayloadCliConfig() throws Exception {
60         SvcLogicContext ctx = new SvcLogicContext();
61         Map<String, String> inParams = new HashMap<String, String>();
62         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
63         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
64                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
65         convertJson2Context("convert/payload_cli_config.json", inParams, ctx);
66         log.info("testPayloadCliConfig Result: "
67                 + ctx.getAttribute("block_configuration-parameters"));
68         log.info("testPayloadCliConfig Result: "
69                 + ctx.getAttribute("block_configuration.configuration-string"));
70     }
71
72     @Test(expected = Exception.class)
73     public void testPayloadXMLConfig() throws Exception {
74         SvcLogicContext ctx = new SvcLogicContext();
75         Map<String, String> inParams = new HashMap<String, String>();
76         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
77         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
78                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
79         convertJson2Context("convert/payload_xml_config.json", inParams, ctx);
80
81         log.info("testPayloadXMLConfig Result: "
82                 + ctx.getAttribute("block_configuration-parameters"));
83         log.info("testPayloadXMLConfig Result: "
84                 + ctx.getAttribute("block_configuration.configuration-string"));
85     }
86
87     @Test(expected = Exception.class)
88     public void testPayloadJsonConfig() throws Exception {
89         SvcLogicContext ctx = new SvcLogicContext();
90         Map<String, String> inParams = new HashMap<String, String>();
91         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
92         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
93                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
94         convertJson2Context("convert/payload_json_config.json", inParams, ctx);
95         log.info("testPayloadJsonConfig Result: "
96                 + ctx.getAttribute("block_configuration-parameters"));
97         log.info("testPayloadJsonConfig Result: "
98                 + ctx.getAttribute("block_configuration.configuration-json"));
99     }
100
101     private void convertJson2Context(String jsonFile, Map<String, String> inParams,
102             SvcLogicContext ctx) throws IOException, SvcLogicException {
103         ConvertNode convertNode = new ConvertNode();
104         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
105         String jsonData = IOUtils
106                 .toString(TestMergeNode.class.getClassLoader().getResourceAsStream(jsonFile));
107         log.info("TestConvertNode.testConvertJson2DGContext()" + jsonData);
108         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
109         convertNode.convertJson2DGContext(inParams, ctx);
110         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
111                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
112     }
113
114     @Test(expected = Exception.class)
115     public void testEscapeData() throws Exception {
116         SvcLogicContext ctx = new SvcLogicContext();
117         Map<String, String> inParams = new HashMap<String, String>();
118         String unescapeData = IOUtils.toString(TestMergeNode.class.getClassLoader()
119                 .getResourceAsStream("convert/escape/config_ssc.txt"));
120         log.info("TestConvertNode.testEscapeData() unescapeData :" + unescapeData);
121         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
122         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, unescapeData);
123         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE,
124                 ConfigGeneratorConstant.DATA_TYPE_SQL);
125         ConvertNode convertNode = new ConvertNode();
126         convertNode.escapeData(inParams, ctx);
127         log.info("testEscapeData Result: "
128                 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA));
129     }
130
131     @Test
132     public void testConvertContextToJson() throws Exception {
133         SvcLogicContext ctx = new SvcLogicContext();
134         ctx.setAttribute("tmp.uploadConfigInfo.UPLOAD-CONFIG-ID", "200");
135         ctx.setAttribute("tmp.uploadConfigInfo.VNF-ID", "00000");
136         ctx.setAttribute("tmp.uploadConfigInfo.test[0]", "test0");
137         ctx.setAttribute("tmp.uploadConfigInfo.test[1]", "test1");
138         ctx.setAttribute("tmp.uploadConfigInfo.test[2]", "test2");
139         ConvertNode convertNode = new ConvertNode();
140         Map<String, String> inParams = new HashMap<String, String>();
141         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
142         inParams.put("contextKey", "tmp.uploadConfigInfo");
143         convertNode.convertContextToJson(inParams, ctx);
144         log.info("JSON CONTENT " + ctx.getAttribute("test.jsonContent"));
145         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
146                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
147
148     }
149
150     @Test(expected = Exception.class)
151     public void testunEscapeData() throws Exception {
152         ConvertNode convertNode = new ConvertNode();
153         Map<String, String> inParams = new HashMap<String, String>();
154         SvcLogicContext ctx = new SvcLogicContext();
155         log.trace("Received unEscapeData call with params : " + inParams);
156         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
157         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA, "//");
158         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "String");
159         convertNode.unEscapeData(inParams, ctx);
160
161     }
162 }