added test cases to TestPropertyDefinitionNode
[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     @Test
57     public void testProcessMissingParamKeys() throws Exception {
58         Map<String, String> inParams = new HashMap<String, String>();
59         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
60
61         String yamlData = IOUtils.toString(
62                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
63                 Charset.defaultCharset());
64         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
65
66         String jsonData = IOUtils.toString(
67                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
68                 Charset.defaultCharset());
69         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
70
71         SvcLogicContext ctx = new SvcLogicContext();
72         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
73         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
74                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
75
76     }
77
78     @Test(expected = SvcLogicException.class)
79     public void testInProcessMissingParamKeysForEmptyPdContent() throws Exception {
80         Map<String, String> inParams = new HashMap<String, String>();
81         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
82         String jsonData = IOUtils.toString(
83                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
84                 Charset.defaultCharset());
85         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
86
87         SvcLogicContext ctx = new SvcLogicContext();
88         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
89     }
90
91     @Test
92     public void testProcessExternalSystemParamKeys() throws Exception {
93         Map<String, String> inParams = new HashMap<String, String>();
94         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
95
96         String yamlData = IOUtils.toString(
97                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
98                 Charset.defaultCharset());
99         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
100
101         String jsonData = IOUtils.toString(
102                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
103                 Charset.defaultCharset());
104         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
105
106         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
107
108         SvcLogicContext ctx = new SvcLogicContext();
109         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
110
111         for (Object key : ctx.getAttributeKeySet()) {
112             String parmName = (String) key;
113             String parmValue = ctx.getAttribute(parmName);
114             if (StringUtils.contains(parmName, "keys")) {
115                 System.out.println("Key: " + parmName + ",    Value: " + parmValue);
116             }
117         }
118         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
119                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
120     }
121     
122     @Test
123     public void testProcessExternalSystemParamKeysForEmptyParamData() throws Exception {
124         Map<String, String> inParams = new HashMap<String, String>();
125         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
126
127         String yamlData = IOUtils.toString(
128                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
129                 Charset.defaultCharset());
130         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
131         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
132
133         SvcLogicContext ctx = new SvcLogicContext();
134         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
135         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
136                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
137
138       
139     }
140
141     @Rule
142     public ExpectedException expectedEx = ExpectedException.none();
143
144     @Test
145     public void testProcessExternalSystemParamKeysForEmptySystemName() throws Exception {
146         Map<String, String> inParams = new HashMap<String, String>();
147         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
148
149         String yamlData = IOUtils.toString(
150                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
151                 Charset.defaultCharset());
152         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
153
154         String jsonData = IOUtils.toString(
155                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
156                 Charset.defaultCharset());
157         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
158
159         SvcLogicContext ctx = new SvcLogicContext();
160         expectedEx.expect(SvcLogicException.class);
161         expectedEx.expectMessage("Request Param (systemName) is Missing ..");
162         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
163
164     }
165
166     @Test(expected = SvcLogicException.class)
167     public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
168
169         Map<String, String> inParams = new HashMap<String, String>();
170         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
171         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
172         SvcLogicContext ctx = new SvcLogicContext();
173         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
174     }
175
176     @Test
177     public void mergeJsonData() throws Exception {
178         Map<String, String> inParams = new HashMap<String, String>();
179         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
180
181         String jsonData = IOUtils.toString(
182                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
183                 Charset.defaultCharset());
184         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
185
186         String mergeJsonData = IOUtils.toString(
187                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
188                 Charset.defaultCharset());
189         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
190
191         SvcLogicContext ctx = new SvcLogicContext();
192         propertyDefinitionNode.mergeJsonData(inParams, ctx);
193         String mergedParams = ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
194         assertNotNull(mergedParams);
195         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
196                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
197     }
198
199     @Test
200     public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
201
202         Map<String, String> inParams = new HashMap<String, String>();
203         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
204         String mergeJsonData = IOUtils.toString(
205                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
206                 Charset.defaultCharset());
207         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
208         SvcLogicContext ctx = new SvcLogicContext();
209         propertyDefinitionNode.mergeJsonData(inParams, ctx);
210         String status = ctx.getAttribute("test.status");
211         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS, status);
212
213     }
214
215     @Test(expected = SvcLogicException.class)
216     public void testMergeJsonDataCatchPortion() throws Exception {
217         Map<String, String> inParams = new HashMap<String, String>();
218         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
219         SvcLogicContext ctx = new SvcLogicContext();
220         propertyDefinitionNode.mergeJsonData(inParams, ctx);
221     }
222
223     @Test
224     public void testArtificatTransformer() throws Exception {
225         ArtificatTransformer transformer = new ArtificatTransformer();
226         String yamlData = IOUtils.toString(
227                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
228                 Charset.defaultCharset());
229
230         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
231         String yaml = transformer.convertPDToYaml(propertyDefinition);
232     }
233
234     @Test
235     public void testValidationPd() throws Exception {
236         Map<String, String> inParams = new HashMap<String, String>();
237         SvcLogicContext ctx = new SvcLogicContext();
238         String jsonData = IOUtils.toString(
239                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
240                 Charset.defaultCharset());
241         String mergeJsonData = IOUtils.toString(
242                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
243                 Charset.defaultCharset());
244         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
245         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
246         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
247         propertyDefinitionNode.validateParams(inParams, ctx);
248     }
249     
250     @Test(expected=SvcLogicException.class)
251     public void testValidateParamsForEmptyParams() throws Exception
252     {
253         Map<String, String> inParams = new HashMap<String, String>();
254         SvcLogicContext ctx = new SvcLogicContext();
255         propertyDefinitionNode.validateParams(inParams, ctx);
256     }
257     
258     @Test
259     public void testValidateParamsForEmptyConfigParams() throws Exception
260     {
261         Map<String, String> inParams = new HashMap<String, String>();
262         String jsonData = IOUtils.toString(
263                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
264                 Charset.defaultCharset());
265         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
266         SvcLogicContext ctx = new SvcLogicContext();
267         propertyDefinitionNode.validateParams(inParams, ctx);
268         String status = ctx.getAttribute("status");
269         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);
270     }
271
272 }