2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification Copyright (C) 2018 IBM.
10 * =============================================================================
11 * Modification Copyright (C) 2018 Ericsson
12 * =============================================================================
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.sdnc.config.generator.merge;
30 import static org.junit.Assert.assertEquals;
32 import java.nio.charset.StandardCharsets;
33 import java.util.HashMap;
35 import org.apache.commons.io.IOUtils;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
43 public class TestMergeNode {
46 public ExpectedException expectedEx = ExpectedException.none();
49 public void testMergeJsonDataOnTemplate() throws Exception {
50 MergeNode mergeNode = new MergeNode();
51 Map<String, String> inParams = new HashMap<String, String>();
52 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
53 String jsonData = IOUtils.toString(
54 TestMergeNode.class.getClassLoader().getResourceAsStream("merge/valid.json"),
55 StandardCharsets.UTF_8);
56 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
57 String templateData = IOUtils.toString(TestMergeNode.class.getClassLoader()
58 .getResourceAsStream("merge/valid.xml"), StandardCharsets.UTF_8);
59 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, templateData);
60 SvcLogicContext ctx = new SvcLogicContext();
61 mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
62 assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
63 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
67 public void testMergeJsonDataOnTemplateForEmptyParamData() throws Exception {
68 MergeNode mergeNode = new MergeNode();
69 Map<String, String> inParams = new HashMap<String, String>();
70 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
71 SvcLogicContext ctx = new SvcLogicContext();
72 expectedEx.expect(SvcLogicException.class);
73 expectedEx.expectMessage("JSON Data is missing");
74 mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
78 public void testMergeJsonDataOnTemplateForEmptyTemplateData() throws Exception {
79 MergeNode mergeNode = new MergeNode();
80 Map<String, String> inParams = new HashMap<String, String>();
81 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
82 String jsonData = IOUtils.toString(
83 TestMergeNode.class.getClassLoader().getResourceAsStream("merge/valid.json"),
84 StandardCharsets.UTF_8);
85 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
86 SvcLogicContext ctx = new SvcLogicContext();
87 expectedEx.expect(SvcLogicException.class);
88 expectedEx.expectMessage("Template data or Template file is missing");
89 mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
93 public void testMergeComplexJsonDataOnTemplate() throws Exception {
94 MergeNode mergeNode = new MergeNode();
95 Map<String, String> inParams = new HashMap<String, String>();
96 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
97 String jsonData = IOUtils.toString(TestMergeNode.class.getClassLoader()
98 .getResourceAsStream("merge/valid.json"), StandardCharsets.UTF_8);
99 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
100 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE, "merge/valid.xml");
101 SvcLogicContext ctx = new SvcLogicContext();
102 mergeNode.mergeComplexJsonDataOnTemplate(inParams, ctx);
103 assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
104 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
108 public void testMergeComplexJsonDataWithMissingJson() throws Exception {
109 MergeNode mergeNode = new MergeNode();
110 Map<String, String> inParams = new HashMap<String, String>();
111 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
112 String jsonData = IOUtils.toString(TestMergeNode.class.getClassLoader()
113 .getResourceAsStream("merge/blank.json"), StandardCharsets.UTF_8);
114 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
115 SvcLogicContext ctx = new SvcLogicContext();
116 expectedEx.expect(SvcLogicException.class);
117 expectedEx.expectMessage("JSON Data is missing");
118 mergeNode.mergeComplexJsonDataOnTemplate(inParams, ctx);
122 public void testMergeComplexJsonDataWithMissingTemplateDataAndFile() throws Exception {
123 MergeNode mergeNode = new MergeNode();
124 Map<String, String> inParams = new HashMap<String, String>();
125 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
126 String jsonData = IOUtils.toString(TestMergeNode.class.getClassLoader()
127 .getResourceAsStream("merge/valid.json"), StandardCharsets.UTF_8);
128 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
129 SvcLogicContext ctx = new SvcLogicContext();
130 expectedEx.expect(SvcLogicException.class);
131 expectedEx.expectMessage("Template data or Template file is missing");
132 mergeNode.mergeComplexJsonDataOnTemplate(inParams, ctx);