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 Map<String, String> inParams = new HashMap<String, String>();
57 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
59 String yamlData = IOUtils.toString(
60 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
61 Charset.defaultCharset());
62 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
64 String jsonData = IOUtils.toString(
65 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
66 Charset.defaultCharset());
67 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
69 SvcLogicContext ctx = new SvcLogicContext();
70 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
71 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
72 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
76 @Test(expected = SvcLogicException.class)
77 public void testInProcessMissingParamKeysForEmptyPdContent() throws Exception {
78 Map<String, String> inParams = new HashMap<String, String>();
79 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
80 String jsonData = IOUtils.toString(
81 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
82 Charset.defaultCharset());
83 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
85 SvcLogicContext ctx = new SvcLogicContext();
86 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
90 public void testProcessExternalSystemParamKeys() throws Exception {
91 Map<String, String> inParams = new HashMap<String, String>();
92 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
94 String yamlData = IOUtils.toString(
95 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
96 Charset.defaultCharset());
97 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
99 String jsonData = IOUtils.toString(
100 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
101 Charset.defaultCharset());
102 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
104 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
106 SvcLogicContext ctx = new SvcLogicContext();
107 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
109 for (Object key : ctx.getAttributeKeySet()) {
110 String parmName = (String) key;
111 String parmValue = ctx.getAttribute(parmName);
112 if (StringUtils.contains(parmName, "keys")) {
113 System.out.println("Key: " + parmName + ", Value: " + parmValue);
116 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
117 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
120 @Test(expected = SvcLogicException.class)
121 public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
123 Map<String, String> inParams = new HashMap<String, String>();
124 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
125 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
126 SvcLogicContext ctx = new SvcLogicContext();
127 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
131 public void mergeJsonData() throws Exception {
132 Map<String, String> inParams = new HashMap<String, String>();
133 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
135 String jsonData = IOUtils.toString(
136 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
137 Charset.defaultCharset());
138 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
140 String mergeJsonData = IOUtils.toString(
141 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
142 Charset.defaultCharset());
143 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
145 SvcLogicContext ctx = new SvcLogicContext();
146 propertyDefinitionNode.mergeJsonData(inParams, ctx);
147 String mergedParams = ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
148 assertNotNull(mergedParams);
149 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
150 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
154 public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
156 Map<String, String> inParams = new HashMap<String, String>();
157 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
158 String mergeJsonData = IOUtils.toString(
159 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/merge-param.json"),
160 Charset.defaultCharset());
161 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
162 SvcLogicContext ctx = new SvcLogicContext();
163 propertyDefinitionNode.mergeJsonData(inParams, ctx);
164 String status = ctx.getAttribute("test.status");
165 assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS, status);
169 @Test(expected = SvcLogicException.class)
170 public void testMergeJsonDataCatchPortion() throws Exception {
171 Map<String, String> inParams = new HashMap<String, String>();
172 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
173 SvcLogicContext ctx = new SvcLogicContext();
174 propertyDefinitionNode.mergeJsonData(inParams, ctx);
178 public void testArtificatTransformer() throws Exception {
179 ArtificatTransformer transformer = new ArtificatTransformer();
180 String yamlData = IOUtils.toString(
181 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
182 Charset.defaultCharset());
184 PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
185 String yaml = transformer.convertPDToYaml(propertyDefinition);
189 public void testValidationPd() throws Exception {
190 Map<String, String> inParams = new HashMap<String, String>();
191 SvcLogicContext ctx = new SvcLogicContext();
192 String jsonData = IOUtils.toString(
193 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
194 Charset.defaultCharset());
195 String mergeJsonData = IOUtils.toString(
196 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
197 Charset.defaultCharset());
198 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
199 inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
200 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
201 propertyDefinitionNode.validateParams(inParams, ctx);
204 @Test(expected=SvcLogicException.class)
205 public void testValidateParamsForEmptyParams() throws Exception
207 Map<String, String> inParams = new HashMap<String, String>();
208 SvcLogicContext ctx = new SvcLogicContext();
209 propertyDefinitionNode.validateParams(inParams, ctx);
213 public void testValidateParamsForEmptyConfigParams() throws Exception
215 Map<String, String> inParams = new HashMap<String, String>();
216 String jsonData = IOUtils.toString(
217 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
218 Charset.defaultCharset());
219 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
220 SvcLogicContext ctx = new SvcLogicContext();
221 propertyDefinitionNode.validateParams(inParams, ctx);
222 String status = ctx.getAttribute("status");
223 assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);