Test case improvements for APPC-1262
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / onap / sdnc / config / params / parser / TestPropertyDefinitionNode.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  * Modifications 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.params.parser;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30
31 import java.io.IOException;
32 import java.nio.charset.Charset;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import org.apache.commons.io.IOUtils;
37 import org.apache.commons.lang.StringUtils;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.onap.sdnc.config.params.ParamsHandlerConstant;
45 import org.onap.sdnc.config.params.data.PropertyDefinition;
46 import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
47
48 public class TestPropertyDefinitionNode {
49     private PropertyDefinitionNode propertyDefinitionNode;
50
51     @Before
52     public void setup() {
53         propertyDefinitionNode = new PropertyDefinitionNode();
54     }
55     
56     @Rule
57     public ExpectedException expectedEx = ExpectedException.none();
58
59     @Test
60     public void testProcessMissingParamKeys() throws Exception {
61         Map<String, String> inParams = new HashMap<String, String>();
62         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
63
64         String yamlData = IOUtils.toString(
65                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
66                 Charset.defaultCharset());
67         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
68
69         String jsonData = IOUtils.toString(
70                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
71                 Charset.defaultCharset());
72         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
73
74         SvcLogicContext ctx = new SvcLogicContext();
75         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
76         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
77                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
78
79     }
80
81     @Test
82     public void testInProcessMissingParamKeysForEmptyPdContent() throws Exception {
83         Map<String, String> inParams = new HashMap<String, String>();
84         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
85         String jsonData = IOUtils.toString(
86                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
87                 Charset.defaultCharset());
88         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
89         expectedEx.expect(SvcLogicException.class);
90         expectedEx.expectMessage("Request Param (pdContent) is Missing ..");
91         SvcLogicContext ctx = new SvcLogicContext();
92         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
93     }
94
95     @Test
96     public void testProcessExternalSystemParamKeys() throws Exception {
97         Map<String, String> inParams = new HashMap<String, String>();
98         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
99
100         String yamlData = IOUtils.toString(
101                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
102                 Charset.defaultCharset());
103         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
104
105         String jsonData = IOUtils.toString(
106                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
107                 Charset.defaultCharset());
108         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
109
110         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
111
112         SvcLogicContext ctx = new SvcLogicContext();
113         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
114
115         for (Object key : ctx.getAttributeKeySet()) {
116             String parmName = (String) key;
117             String parmValue = ctx.getAttribute(parmName);
118             if (StringUtils.contains(parmName, "keys")) {
119                 System.out.println("Key: " + parmName + ",    Value: " + parmValue);
120             }
121         }
122         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
123                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
124     }
125
126     @Test
127     public void testProcessExternalSystemParamKeysForEmptyParamData() throws Exception {
128         Map<String, String> inParams = new HashMap<String, String>();
129         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
130
131         String yamlData = IOUtils.toString(
132                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
133                 Charset.defaultCharset());
134         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
135         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
136
137         SvcLogicContext ctx = new SvcLogicContext();
138         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
139         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
140                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
141     }
142
143     @Test
144     public void testProcessExternalSystemParamKeysForEmptySystemName() throws Exception {
145         Map<String, String> inParams = new HashMap<String, String>();
146         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
147
148         String yamlData = IOUtils.toString(
149                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
150                 Charset.defaultCharset());
151         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
152
153         String jsonData = IOUtils.toString(
154                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
155                 Charset.defaultCharset());
156         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
157
158         SvcLogicContext ctx = new SvcLogicContext();
159         expectedEx.expect(SvcLogicException.class);
160         expectedEx.expectMessage("Request Param (systemName) is Missing ..");
161         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
162     }
163
164     @Test
165     public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
166         Map<String, String> inParams = new HashMap<String, String>();
167         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
168         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
169         SvcLogicContext ctx = new SvcLogicContext();
170         expectedEx.expect(SvcLogicException.class);
171         expectedEx.expectMessage("Request Param (pdContent) is Missing ..");
172         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
173     }
174
175     @Test
176     public void mergeJsonData() throws Exception {
177         Map<String, String> inParams = new HashMap<String, String>();
178         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
179
180         String jsonData = IOUtils.toString(
181                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
182                 Charset.defaultCharset());
183         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
184
185         String mergeJsonData = IOUtils.toString(
186                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
187                 Charset.defaultCharset());
188         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
189
190         SvcLogicContext ctx = new SvcLogicContext();
191         propertyDefinitionNode.mergeJsonData(inParams, ctx);
192         String mergedParams = ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
193         assertNotNull(mergedParams);
194         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
195                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
196     }
197
198     @Test
199     public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
200         Map<String, String> inParams = new HashMap<String, String>();
201         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
202         String mergeJsonData = IOUtils.toString(
203                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
204                 Charset.defaultCharset());
205         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
206         SvcLogicContext ctx = new SvcLogicContext();
207         propertyDefinitionNode.mergeJsonData(inParams, ctx);
208         String status = ctx.getAttribute("test.status");
209         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS, status);
210     }
211
212     @Test(expected = SvcLogicException.class)
213     public void testMergeJsonDataCatchPortion() throws Exception {
214         Map<String, String> inParams = new HashMap<String, String>();
215         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
216         SvcLogicContext ctx = new SvcLogicContext();
217         propertyDefinitionNode.mergeJsonData(inParams, ctx);
218     }
219
220     @Test
221     public void testArtificatTransformer() throws Exception {
222         ArtificatTransformer transformer = new ArtificatTransformer();
223         String yamlData = IOUtils.toString(
224                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
225                 Charset.defaultCharset());
226
227         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
228         String yaml = transformer.convertPDToYaml(propertyDefinition);
229     }
230
231     @Test
232     public void testValidationPd() throws Exception {
233         Map<String, String> inParams = new HashMap<String, String>();
234         SvcLogicContext ctx = new SvcLogicContext();
235         String jsonData = IOUtils.toString(
236                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
237                 Charset.defaultCharset());
238         String mergeJsonData = IOUtils.toString(
239                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
240                 Charset.defaultCharset());
241         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
242         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
243         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
244         propertyDefinitionNode.validateParams(inParams, ctx);
245     }
246     
247     @Test
248     public void testValidationPdWithMissingKeys() throws Exception {
249         Map<String, String> inParams = new HashMap<String, String>();
250         SvcLogicContext ctx = new SvcLogicContext();
251         String jsonData = IOUtils.toString(
252                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd_with_required_keys.yaml"),
253                 Charset.defaultCharset());
254         String mergeJsonData = IOUtils.toString(
255                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param_missing_keys.json"),
256                 Charset.defaultCharset());
257         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
258         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
259         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
260         expectedEx.expect(SvcLogicException.class);
261         expectedEx.expectMessage("Missing  Mandatory Keys and source are{\"LOCAL_ACCESS_IP_ADDR\":\"INSTAR\"}");
262         propertyDefinitionNode.validateParams(inParams, ctx);
263         //assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS));
264     }
265     
266     @Test(expected=SvcLogicException.class)
267     public void testValidateParamsForEmptyParams() throws Exception
268     {
269         Map<String, String> inParams = new HashMap<String, String>();
270         SvcLogicContext ctx = new SvcLogicContext();
271         propertyDefinitionNode.validateParams(inParams, ctx);
272     }
273     
274     @Test
275     public void testValidateParamsForEmptyConfigParams() throws Exception
276     {
277         Map<String, String> inParams = new HashMap<String, String>();
278         String jsonData = IOUtils.toString(
279                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
280                 Charset.defaultCharset());
281         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
282         SvcLogicContext ctx = new SvcLogicContext();
283         propertyDefinitionNode.validateParams(inParams, ctx);
284         String status = ctx.getAttribute("status");
285         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);
286     }
287
288 }