added test cases to TestMergeNode.java
[appc.git] / appc-config / appc-config-generator / provider / src / test / java / org / onap / sdnc / config / generator / merge / TestMergeNode.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  * Modification 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.merge;
27
28 import static org.junit.Assert.assertEquals;
29
30 import java.io.IOException;
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.junit.runner.RunWith;
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.powermock.modules.junit4.PowerMockRunner;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44
45 @RunWith(PowerMockRunner.class)
46 public class TestMergeNode {
47
48     @Test(expected = Exception.class)
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/vdbe_data.json"));
55         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
56         String templateData = IOUtils.toString(TestMergeNode.class.getClassLoader()
57                 .getResourceAsStream("merge/vdbe_template.xml"));
58         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, templateData);
59         SvcLogicContext ctx = new SvcLogicContext();
60         mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
61         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
62                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
63
64     }
65     
66     @Test(expected = Exception.class)
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         String templateData = IOUtils.toString(TestMergeNode.class.getClassLoader()
72                 .getResourceAsStream("merge/vdbe_template.xml"));
73         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, templateData);
74         SvcLogicContext ctx = new SvcLogicContext();
75         expectedEx.expect(SvcLogicException.class);
76         expectedEx.expectMessage("Param Data is missing..");
77         mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
78        
79     }
80
81     @Rule
82     public ExpectedException expectedEx = ExpectedException.none();
83
84     @Test(expected = Exception.class)
85     public void testMergeJsonDataOnTemplateForEmptyTemplateData() throws Exception {
86         MergeNode mergeNode = new MergeNode();
87         Map<String, String> inParams = new HashMap<String, String>();
88         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
89         String jsonData = IOUtils.toString(
90                 TestMergeNode.class.getClassLoader().getResourceAsStream("merge/vdbe_data.json"));
91         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
92         SvcLogicContext ctx = new SvcLogicContext();
93         expectedEx.expect(SvcLogicException.class);
94         expectedEx.expectMessage("Template data or Template file is missing");
95         mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
96     }
97
98     
99     @Test(expected = Exception.class)
100     public void testMergeComplexJsonDataOnTemplate() throws Exception {
101         MergeNode mergeNode = new MergeNode();
102         Map<String, String> inParams = new HashMap<String, String>();
103         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
104
105         String jsonData = IOUtils.toString(TestMergeNode.class.getClassLoader()
106                 .getResourceAsStream("merge/complex/vdbe_data.json"));
107         System.out.println("TestMergeNode.testMergeJsonComplexDataOnTemplate()" + jsonData);
108         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
109
110         String templateData = IOUtils.toString(TestMergeNode.class.getClassLoader()
111                 .getResourceAsStream("merge/complex/vdbe_template.xml"));
112         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, templateData);
113
114         SvcLogicContext ctx = new SvcLogicContext();
115         mergeNode.mergeComplexJsonDataOnTemplate(inParams, ctx);
116         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
117                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
118
119     }
120
121     @Test(expected = Exception.class)
122     public void testMergeJsonDataOnTemplateFile() 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
127         String jsonData = IOUtils.toString(
128                 TestMergeNode.class.getClassLoader().getResourceAsStream("merge/vdbe_data.json"));
129         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
130         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE, "merge/vdbe_template.xml");
131
132         SvcLogicContext ctx = new SvcLogicContext();
133         mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
134         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
135                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
136     }
137
138     @Test
139     public void testMmergeDataOnTemplate() throws SvcLogicException {
140         MergeNode mergeNode = new MergeNode();
141         SvcLogicContext ctx = new SvcLogicContext();
142         Map<String, String> inParams = new HashMap<String, String>();
143         mergeNode.mergeDataOnTemplate(inParams, ctx);
144     }
145     
146     @Test
147     public void testMmergeDataOnTemplateWithTemplateData() throws SvcLogicException, IOException {
148         MergeNode mergeNode = new MergeNode();
149         Map<String, String> inParams = new HashMap<String, String>();
150         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
151         String jsonData = "{name1:value1,name2:value2}";
152         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA, jsonData);
153         String templateData = "testTemplateData";
154         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, templateData);
155         SvcLogicContext ctx = new SvcLogicContext();
156         mergeNode.mergeJsonDataOnTemplate(inParams, ctx);
157         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
158
159     }
160
161     @Test
162     public void mergeYamlDataOnTemplate() throws SvcLogicException {
163         MergeNode mergeNode = new MergeNode();
164         SvcLogicContext ctx = new SvcLogicContext();
165         Map<String, String> inParams = new HashMap<String, String>();
166         mergeNode.mergeYamlDataOnTemplate(inParams, ctx);
167     }
168 }