2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.sdnc.config.params.parser;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
31 import java.io.IOException;
32 import java.nio.charset.Charset;
33 import java.util.HashMap;
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;
48 public class TestPropertyDefinitionNode {
49 private PropertyDefinitionNode propertyDefinitionNode;
53 propertyDefinitionNode = new PropertyDefinitionNode();
57 public ExpectedException expectedEx = ExpectedException.none();
60 public void testProcessMissingParamKeys() throws Exception {
61 Map<String, String> inParams = new HashMap<String, String>();
62 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
74 SvcLogicContext ctx = new SvcLogicContext();
75 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
76 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
77 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
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);
96 public void testProcessExternalSystemParamKeys() throws Exception {
97 Map<String, String> inParams = new HashMap<String, String>();
98 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
110 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
112 SvcLogicContext ctx = new SvcLogicContext();
113 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
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);
122 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
123 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
127 public void testProcessExternalSystemParamKeysForEmptyParamData() throws Exception {
128 Map<String, String> inParams = new HashMap<String, String>();
129 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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");
137 SvcLogicContext ctx = new SvcLogicContext();
138 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
139 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
140 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
144 public void testProcessExternalSystemParamKeysForEmptySystemName() throws Exception {
145 Map<String, String> inParams = new HashMap<String, String>();
146 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
158 SvcLogicContext ctx = new SvcLogicContext();
159 expectedEx.expect(SvcLogicException.class);
160 expectedEx.expectMessage("Request Param (systemName) is Missing ..");
161 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
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);
176 public void mergeJsonData() throws Exception {
177 Map<String, String> inParams = new HashMap<String, String>();
178 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
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);
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);
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);
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());
227 PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
228 String yaml = transformer.convertPDToYaml(propertyDefinition);
229 assertNotNull(transformer);
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);
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));
267 @Test(expected=SvcLogicException.class)
268 public void testValidateParamsForEmptyParams() throws Exception
270 Map<String, String> inParams = new HashMap<String, String>();
271 SvcLogicContext ctx = new SvcLogicContext();
272 propertyDefinitionNode.validateParams(inParams, ctx);
276 public void testValidateParamsForEmptyConfigParams() throws Exception
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);