removed redundant object assignment
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / onap / sdnc / config / params / parser / TestPropertyDefinitionNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.params.parser;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30
31 import java.io.IOException;
32 import java.nio.charset.Charset;
33 import java.util.HashMap;
34 import java.util.Map;
35
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;
45
46 public class TestPropertyDefinitionNode {
47     private PropertyDefinitionNode propertyDefinitionNode;
48
49     @Before
50     public void setup() {
51         propertyDefinitionNode = new PropertyDefinitionNode();
52     }
53
54     @Test
55     public void testProcessMissingParamKeys() throws Exception {
56         Map<String, String> inParams = new HashMap<String, String>();
57         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
58
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);
63
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);
68
69         SvcLogicContext ctx = new SvcLogicContext();
70         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
71         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
72                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
73
74     }
75
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);
84
85         SvcLogicContext ctx = new SvcLogicContext();
86         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
87     }
88
89     @Test
90     public void testProcessExternalSystemParamKeys() throws Exception {
91         Map<String, String> inParams = new HashMap<String, String>();
92         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
93
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);
98
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);
103
104         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
105
106         SvcLogicContext ctx = new SvcLogicContext();
107         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
108
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);
114             }
115         }
116         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
117                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
118     }
119
120     @Test(expected = SvcLogicException.class)
121     public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
122
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);
128     }
129
130     @Test
131     public void mergeJsonData() throws Exception {
132         Map<String, String> inParams = new HashMap<String, String>();
133         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
134
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);
139
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);
144
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);
151     }
152
153     @Test
154     public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
155
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);
166
167     }
168
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);
175     }
176
177     @Test
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());
183
184         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
185         String yaml = transformer.convertPDToYaml(propertyDefinition);
186     }
187
188     @Test
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);
202     }
203     
204     @Test(expected=SvcLogicException.class)
205     public void testValidateParamsForEmptyParams() throws Exception
206     {
207         Map<String, String> inParams = new HashMap<String, String>();
208         SvcLogicContext ctx = new SvcLogicContext();
209         propertyDefinitionNode.validateParams(inParams, ctx);
210     }
211     
212     @Test
213     public void testValidateParamsForEmptyConfigParams() throws Exception
214     {
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);
224     }
225
226 }