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 void testProcessMissingParamKeys() throws Exception {
58 Map<String, String> inParams = new HashMap<String, String>();
59 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
71 SvcLogicContext ctx = new SvcLogicContext();
72 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
73 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
74 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
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);
87 SvcLogicContext ctx = new SvcLogicContext();
88 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
92 public void testProcessExternalSystemParamKeys() throws Exception {
93 Map<String, String> inParams = new HashMap<String, String>();
94 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
106 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
108 SvcLogicContext ctx = new SvcLogicContext();
109 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
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);
118 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
119 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
123 public void testProcessExternalSystemParamKeysForEmptyParamData() throws Exception {
124 Map<String, String> inParams = new HashMap<String, String>();
125 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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");
133 SvcLogicContext ctx = new SvcLogicContext();
134 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
135 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
136 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
142 public ExpectedException expectedEx = ExpectedException.none();
145 public void testProcessExternalSystemParamKeysForEmptySystemName() throws Exception {
146 Map<String, String> inParams = new HashMap<String, String>();
147 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
159 SvcLogicContext ctx = new SvcLogicContext();
160 expectedEx.expect(SvcLogicException.class);
161 expectedEx.expectMessage("Request Param (systemName) is Missing ..");
162 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
166 @Test(expected = SvcLogicException.class)
167 public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
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);
177 public void mergeJsonData() throws Exception {
178 Map<String, String> inParams = new HashMap<String, String>();
179 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
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);
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);
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);
200 public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
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);
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);
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());
230 PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
231 String yaml = transformer.convertPDToYaml(propertyDefinition);
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);
250 @Test(expected=SvcLogicException.class)
251 public void testValidateParamsForEmptyParams() throws Exception
253 Map<String, String> inParams = new HashMap<String, String>();
254 SvcLogicContext ctx = new SvcLogicContext();
255 propertyDefinitionNode.validateParams(inParams, ctx);
259 public void testValidateParamsForEmptyConfigParams() throws Exception
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);