added test case and formatted code
[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  * 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
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         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
57         Map<String, String> inParams = new HashMap<String, String>();
58         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
59
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);
64
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);
69
70         SvcLogicContext ctx = new SvcLogicContext();
71         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
72         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
73                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
74
75     }
76
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);
86
87         SvcLogicContext ctx = new SvcLogicContext();
88         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
89     }
90
91     @Test
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");
96
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);
101
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);
106
107         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
108
109         SvcLogicContext ctx = new SvcLogicContext();
110         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
111
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);
117             }
118         }
119         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
120                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
121     }
122
123     @Test(expected = SvcLogicException.class)
124     public void testProcessExternalSystemParamKeysForEmptyPdContent() throws Exception {
125
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);
131     }
132
133     @Test
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");
138
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);
143
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);
148
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);
155     }
156
157     @Test
158     public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
159
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);
171
172     }
173
174     @Test
175     public void testArtificatTransformer() throws Exception {
176         ArtificatTransformer transformer = new ArtificatTransformer();
177         String yamlData = IOUtils.toString(
178                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
179                 Charset.defaultCharset());
180
181         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
182         String yaml = transformer.convertPDToYaml(propertyDefinition);
183     }
184
185     @Test
186     public void testValidationPd() throws Exception {
187         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
188         Map<String, String> inParams = new HashMap<String, String>();
189         SvcLogicContext ctx = new SvcLogicContext();
190         String jsonData = IOUtils.toString(
191                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/pd.yaml"),
192                 Charset.defaultCharset());
193         String mergeJsonData = IOUtils.toString(
194                 TestPropertyDefinitionNode.class.getClassLoader().getResourceAsStream("parser/request-param.json"),
195                 Charset.defaultCharset());
196         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
197         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
198         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
199         propertyDefinitionNode.validateParams(inParams, ctx);
200     }
201
202 }