Update appc-config-generator to use onap packaging
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.convert;
26
27 import static org.junit.Assert.assertEquals;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.apache.commons.io.IOUtils;
32 import org.junit.Test;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
36 import org.onap.sdnc.config.generator.merge.TestMergeNode;
37 import org.powermock.reflect.Whitebox;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40
41 public class TestConvertNode {
42     private static final EELFLogger log =
43             EELFManager.getInstance().getLogger(TestConvertNode.class);
44
45     @Test(expected = Exception.class)
46     public void testPayloadParametersConfig() throws Exception {
47         SvcLogicContext ctx = new SvcLogicContext();
48         Map<String, String> inParams = new HashMap<String, String>();
49         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
50         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
51                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
52         convertJson2Context("convert/payload_parameters_config.json", inParams, ctx);
53         Whitebox.invokeMethod("convertJson2Context", "convert/payload_parameters_config.json",
54                 inParams, ctx);
55         log.info("testPayloadParametersConfig Result: "
56                 + ctx.getAttribute("block_configuration-parameters"));
57     }
58
59     @Test
60     public void testPayloadCliConfig() throws Exception {
61         SvcLogicContext ctx = new SvcLogicContext();
62         Map<String, String> inParams = new HashMap<String, String>();
63         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
64         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
65                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
66         convertJson2Context("convert/payload_cli_config.json", inParams, ctx);
67         log.info("testPayloadCliConfig Result: "
68                 + ctx.getAttribute("block_configuration-parameters"));
69         log.info("testPayloadCliConfig Result: "
70                 + ctx.getAttribute("block_configuration.configuration-string"));
71     }
72
73     @Test(expected = Exception.class)
74     public void testPayloadXMLConfig() throws Exception {
75         SvcLogicContext ctx = new SvcLogicContext();
76         Map<String, String> inParams = new HashMap<String, String>();
77         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
78         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
79                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
80         convertJson2Context("convert/payload_xml_config.json", inParams, ctx);
81
82         log.info("testPayloadXMLConfig Result: "
83                 + ctx.getAttribute("block_configuration-parameters"));
84         log.info("testPayloadXMLConfig Result: "
85                 + ctx.getAttribute("block_configuration.configuration-string"));
86     }
87
88     @Test(expected = Exception.class)
89     public void testPayloadJsonConfig() throws Exception {
90         SvcLogicContext ctx = new SvcLogicContext();
91         Map<String, String> inParams = new HashMap<String, String>();
92         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED, "N");
93         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS,
94                 "configuration-parameters,configuration.configuration-json,configuration.configuration-string");
95         convertJson2Context("convert/payload_json_config.json", inParams, ctx);
96         log.info("testPayloadJsonConfig Result: "
97                 + ctx.getAttribute("block_configuration-parameters"));
98         log.info("testPayloadJsonConfig Result: "
99                 + ctx.getAttribute("block_configuration.configuration-json"));
100     }
101
102     private void convertJson2Context(String jsonFile, Map<String, String> inParams,
103             SvcLogicContext ctx) throws IOException, SvcLogicException {
104         ConvertNode convertNode = new ConvertNode();
105         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
106         String jsonData = IOUtils
107                 .toString(TestMergeNode.class.getClassLoader().getResourceAsStream(jsonFile));
108         log.info("TestConvertNode.testConvertJson2DGContext()" + jsonData);
109         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
110         convertNode.convertJson2DGContext(inParams, ctx);
111         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
112                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
113     }
114
115     @Test(expected = Exception.class)
116     public void testEscapeData() throws Exception {
117         SvcLogicContext ctx = new SvcLogicContext();
118         Map<String, String> inParams = new HashMap<String, String>();
119         String unescapeData = IOUtils.toString(TestMergeNode.class.getClassLoader()
120                 .getResourceAsStream("convert/escape/config_ssc.txt"));
121         log.info("TestConvertNode.testEscapeData() unescapeData :" + unescapeData);
122         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
123         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA, unescapeData);
124         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE,
125                 ConfigGeneratorConstant.DATA_TYPE_SQL);
126         ConvertNode convertNode = new ConvertNode();
127         convertNode.escapeData(inParams, ctx);
128         log.info("testEscapeData Result: "
129                 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA));
130     }
131
132     @Test
133     public void testConvertContextToJson() throws Exception {
134         SvcLogicContext ctx = new SvcLogicContext();
135         ctx.setAttribute("tmp.uploadConfigInfo.UPLOAD-CONFIG-ID", "200");
136         ctx.setAttribute("tmp.uploadConfigInfo.VNF-ID", "00000");
137         ctx.setAttribute("tmp.uploadConfigInfo.test[0]", "test0");
138         ctx.setAttribute("tmp.uploadConfigInfo.test[1]", "test1");
139         ctx.setAttribute("tmp.uploadConfigInfo.test[2]", "test2");
140         ConvertNode convertNode = new ConvertNode();
141         Map<String, String> inParams = new HashMap<String, String>();
142         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
143         inParams.put("contextKey", "tmp.uploadConfigInfo");
144         convertNode.convertContextToJson(inParams, ctx);
145         log.info("JSON CONTENT " + ctx.getAttribute("test.jsonContent"));
146         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
147                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
148
149     }
150
151     @Test(expected = Exception.class)
152     public void testunEscapeData() throws Exception {
153         ConvertNode convertNode = new ConvertNode();
154         Map<String, String> inParams = new HashMap<String, String>();
155         SvcLogicContext ctx = new SvcLogicContext();
156         log.trace("Received unEscapeData call with params : " + inParams);
157         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "tmp");
158         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA, "//");
159         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE, "String");
160         convertNode.unEscapeData(inParams, ctx);
161
162     }
163 }