2  * ============LICENSE_START=======================================================
 
   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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  21  * ============LICENSE_END=========================================================
 
  24 package org.onap.sdnc.config.generator.convert;
 
  26 import static org.junit.Assert.assertEquals;
 
  27 import java.io.IOException;
 
  28 import java.util.HashMap;
 
  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;
 
  40 public class TestConvertNode {
 
  41     private static final EELFLogger log =
 
  42             EELFManager.getInstance().getLogger(TestConvertNode.class);
 
  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",
 
  54         log.info("testPayloadParametersConfig Result: "
 
  55                 + ctx.getAttribute("block_configuration-parameters"));
 
  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"));
 
  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);
 
  81         log.info("testPayloadXMLConfig Result: "
 
  82                 + ctx.getAttribute("block_configuration-parameters"));
 
  83         log.info("testPayloadXMLConfig Result: "
 
  84                 + ctx.getAttribute("block_configuration.configuration-string"));
 
  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"));
 
 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);
 
 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));
 
 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);
 
 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);