2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification 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;
35 import org.apache.commons.io.IOUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.sdnc.config.params.ParamsHandlerConstant;
42 import org.onap.sdnc.config.params.data.PropertyDefinition;
43 import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
45 public class TestPropertyDefinitionNode {
48 public void testProcessMissingParamKeys() throws Exception {
49 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
50 Map<String, String> inParams = new HashMap<String, String>();
51 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
53 String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
54 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
55 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
57 String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
58 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
59 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
61 SvcLogicContext ctx = new SvcLogicContext();
62 propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
63 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
64 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
69 public void testProcessExternalSystemParamKeys() throws Exception {
70 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
71 Map<String, String> inParams = new HashMap<String, String>();
72 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
74 String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
75 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
76 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
78 String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
79 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
80 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
82 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
84 SvcLogicContext ctx = new SvcLogicContext();
85 propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
87 for (Object key : ctx.getAttributeKeySet()) {
88 String parmName = (String) key;
89 String parmValue = ctx.getAttribute(parmName);
90 if (StringUtils.contains(parmName, "keys")) {
91 System.out.println("Key: " + parmName + ", Value: " + parmValue);
94 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
95 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
99 public void mergeJsonData() throws Exception {
100 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
101 Map<String, String> inParams = new HashMap<String, String>();
102 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
104 String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
105 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
106 inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
108 String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
109 .getResourceAsStream("parser/merge-param.json"), Charset.defaultCharset());
110 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
112 SvcLogicContext ctx = new SvcLogicContext();
113 propertyDefinitionNode.mergeJsonData(inParams, ctx);
114 String mergedParams = ctx
115 .getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
116 assertNotNull(mergedParams);
117 assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
118 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
122 public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
124 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
125 Map<String, String> inParams = new HashMap<String, String>();
126 inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
127 String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
128 .getResourceAsStream("parser/merge-param.json"), Charset.defaultCharset());
129 inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
130 SvcLogicContext ctx = new SvcLogicContext();
131 propertyDefinitionNode.mergeJsonData(inParams, ctx);
132 String status= ctx.getAttribute("test.status");
133 assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);
138 public void testArtificatTransformer() throws Exception {
139 ArtificatTransformer transformer = new ArtificatTransformer();
140 String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
141 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
143 PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
144 String yaml = transformer.convertPDToYaml(propertyDefinition);
149 public void testValidationPd() throws Exception {
150 PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
151 Map<String, String> inParams = new HashMap<String, String>();
152 SvcLogicContext ctx = new SvcLogicContext();
153 String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
154 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
155 String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
156 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
157 inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
158 inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
159 inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
160 propertyDefinitionNode.validateParams(inParams, ctx);