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.Test;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42 import org.onap.sdnc.config.params.ParamsHandlerConstant;
43 import org.onap.sdnc.config.params.data.PropertyDefinition;
44 import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
46 public class TestPropertyDefinitionNode {
47 private PropertyDefinitionNode propertyDefinitionNode;
51 propertyDefinitionNode = new PropertyDefinitionNode();
55 public void testProcessMissingParamKeys() throws Exception {
56 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
57 Map<String, String> inParams = new HashMap<String, String>();
58 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
60 String yamlData = IOUtils.toString(
61 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
62 Charset.defaultCharset());
63 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
65 String jsonData = IOUtils.toString(
66 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
67 Charset.defaultCharset());
68 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
70 SvcLogicContext ctx = new SvcLogicContext();
71 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
72 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
73 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
77 @Test(expected = SvcLogicException.class)
78 public void testInProcessMissingParamKeysForEmptyPdContent() throws Exception {
79 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
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 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
94 Map<String, String> inParams = new HashMap<String, String>();
95 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
97 String yamlData = IOUtils.toString(
98 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
99 Charset.defaultCharset());
100 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
102 String jsonData = IOUtils.toString(
103 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
104 Charset.defaultCharset());
105 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
107 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
109 SvcLogicContext ctx = new SvcLogicContext();
110 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
112 for (Object key : ctx.getAttributeKeySet()) {
113 String parmName = (String) key;
114 String parmValue = ctx.getAttribute(parmName);
115 if (StringUtils.contains(parmName, "keys")) {
116 System.out.println("Key: " + parmName + ", Value: " + parmValue);
119 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
120 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
123 @Test(expected = SvcLogicException.class)
124 public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
126 Map<String, String> inParams = new HashMap<String, String>();
127 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
128 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
129 SvcLogicContext ctx = new SvcLogicContext();
130 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
134 public void mergeJsonData() throws Exception {
135 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
136 Map<String, String> inParams = new HashMap<String, String>();
137 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
139 String jsonData = IOUtils.toString(
140 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
141 Charset.defaultCharset());
142 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
144 String mergeJsonData = IOUtils.toString(
145 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
146 Charset.defaultCharset());
147 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
149 SvcLogicContext ctx = new SvcLogicContext();
150 propertyDefinitionNode.mergeJsonData(inParams, ctx);
151 String mergedParams = ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
152 assertNotNull(mergedParams);
153 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
154 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
158 public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
160 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
161 Map<String, String> inParams = new HashMap<String, String>();
162 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
163 String mergeJsonData = IOUtils.toString(
164 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
165 Charset.defaultCharset());
166 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
167 SvcLogicContext ctx = new SvcLogicContext();
168 propertyDefinitionNode.mergeJsonData(inParams, ctx);
169 String status = ctx.getAttribute("test.status");
170 assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS, status);
174 @Test(expected = SvcLogicException.class)
175 public void testMergeJsonDataCatchPortion() throws Exception {
176 Map<String, String> inParams = new HashMap<String, String>();
177 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
178 SvcLogicContext ctx = new SvcLogicContext();
179 propertyDefinitionNode.mergeJsonData(inParams, ctx);
183 public void testArtificatTransformer() throws Exception {
184 ArtificatTransformer transformer = new ArtificatTransformer();
185 String yamlData = IOUtils.toString(
186 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
187 Charset.defaultCharset());
189 PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
190 String yaml = transformer.convertPDToYaml(propertyDefinition);
194 public void testValidationPd() throws Exception {
195 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
196 Map<String, String> inParams = new HashMap<String, String>();
197 SvcLogicContext ctx = new SvcLogicContext();
198 String jsonData = IOUtils.toString(
199 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
200 Charset.defaultCharset());
201 String mergeJsonData = IOUtils.toString(
202 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
203 Charset.defaultCharset());
204 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
205 inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
206 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
207 propertyDefinitionNode.validateParams(inParams, ctx);
210 @Test(expected=SvcLogicException.class)
211 public void testValidateParamsForEmptyParams() throws Exception
213 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
214 Map<String, String> inParams = new HashMap<String, String>();
215 SvcLogicContext ctx = new SvcLogicContext();
216 propertyDefinitionNode.validateParams(inParams, ctx);
220 public void testValidateParamsForEmptyConfigParams() throws Exception
222 Map<String, String> inParams = new HashMap<String, String>();
223 String jsonData = IOUtils.toString(
224 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
225 Charset.defaultCharset());
226 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
227 SvcLogicContext ctx = new SvcLogicContext();
228 propertyDefinitionNode.validateParams(inParams, ctx);
229 String status = ctx.getAttribute("status");
230 assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);