Changed to unmaintained
[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         assertNotNull(transformer);
230     }
231
232     @Test
233     public void testValidationPd() throws Exception {
234         Map<String, String> inParams = new HashMap<String, String>();
235         SvcLogicContext ctx = new SvcLogicContext();
236         String jsonData = IOUtils.toString(
237                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
238                 Charset.defaultCharset());
239         String mergeJsonData = IOUtils.toString(
240                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
241                 Charset.defaultCharset());
242         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
243         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
244         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
245         propertyDefinitionNode.validateParams(inParams, ctx);
246     }
247     
248     @Test
249     public void testValidationPdWithMissingKeys() throws Exception {
250         Map<String, String> inParams = new HashMap<String, String>();
251         SvcLogicContext ctx = new SvcLogicContext();
252         String jsonData = IOUtils.toString(
253                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd_with_required_keys.yaml"),
254                 Charset.defaultCharset());
255         String mergeJsonData = IOUtils.toString(
256                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param_missing_keys.json"),
257                 Charset.defaultCharset());
258         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
259         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
260         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
261         expectedEx.expect(SvcLogicException.class);
262         expectedEx.expectMessage("Missing  Mandatory Keys and source are{\"LOCAL_ACCESS_IP_ADDR\":\"INSTAR\"}");
263         propertyDefinitionNode.validateParams(inParams, ctx);
264         //assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS));
265     }
266     
267     @Test(expected=SvcLogicException.class)
268     public void testValidateParamsForEmptyParams() throws Exception
269     {
270         Map<String, String> inParams = new HashMap<String, String>();
271         SvcLogicContext ctx = new SvcLogicContext();
272         propertyDefinitionNode.validateParams(inParams, ctx);
273     }
274     
275     @Test
276     public void testValidateParamsForEmptyConfigParams() throws Exception
277     {
278         Map<String, String> inParams = new HashMap<String, String>();
279         String jsonData = IOUtils.toString(
280                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
281                 Charset.defaultCharset());
282         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
283         SvcLogicContext ctx = new SvcLogicContext();
284         propertyDefinitionNode.validateParams(inParams, ctx);
285         String status = ctx.getAttribute("status");
286         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);
287     }
288
289 }