added test case to TestPropertyDefinitionNode.java
[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 import org.apache.commons.io.IOUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.junit.Test;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.sdnc.config.params.ParamsHandlerConstant;
41 import org.onap.sdnc.config.params.data.PropertyDefinition;
42 import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
43
44 public class TestPropertyDefinitionNode {
45
46     @Test
47     public void testProcessMissingParamKeys() throws Exception {
48         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
49         Map<String, String> inParams = new HashMap<String, String>();
50         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
51
52         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
53                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
54         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
55
56         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
57                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
58         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
59
60         SvcLogicContext ctx = new SvcLogicContext();
61         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
62         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
63                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
64
65     }
66     
67     @Test(expected= SvcLogicException.class)
68     public void testInProcessMissingParamKeysForEmptyPdContent() throws Exception {
69         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
70         Map<String, String> inParams = new HashMap<String, String>();
71         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
72         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
73                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
74         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
75
76         SvcLogicContext ctx = new SvcLogicContext();
77         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
78      }
79
80     @Test
81     public void testProcessExternalSystemParamKeys() throws Exception {
82         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
83         Map<String, String> inParams = new HashMap<String, String>();
84         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
85
86         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
87                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
88         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
89
90         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
91                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
92         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
93
94         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
95
96         SvcLogicContext ctx = new SvcLogicContext();
97         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
98
99         for (Object key : ctx.getAttributeKeySet()) {
100             String parmName = (String) key;
101             String parmValue = ctx.getAttribute(parmName);
102             if (StringUtils.contains(parmName, "keys")) {
103                 System.out.println("Key: " + parmName + ",    Value: " + parmValue);
104             }
105         }
106         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
107                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
108     }
109
110     @Test
111     public void mergeJsonData() throws Exception {
112         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
113         Map<String, String> inParams = new HashMap<String, String>();
114         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
115
116         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
117                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
118         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
119
120         String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
121                 .getResourceAsStream("parser/merge-param.json"), Charset.defaultCharset());
122         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
123
124         SvcLogicContext ctx = new SvcLogicContext();
125         propertyDefinitionNode.mergeJsonData(inParams, ctx);
126         String mergedParams = ctx
127                 .getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
128         assertNotNull(mergedParams);
129         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
130                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
131     }
132     
133     @Test
134     public void mergeJsonDataForEmptyParams() throws SvcLogicException, IOException {
135         
136         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
137         Map<String, String> inParams = new HashMap<String, String>();
138         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
139         String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
140                 .getResourceAsStream("parser/merge-param.json"), Charset.defaultCharset());
141         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
142         SvcLogicContext ctx = new SvcLogicContext();
143         propertyDefinitionNode.mergeJsonData(inParams, ctx);
144         String status= ctx.getAttribute("test.status");
145         assertEquals(ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS,status);
146                 
147     }
148
149     @Test
150     public void testArtificatTransformer() throws Exception {
151         ArtificatTransformer transformer = new ArtificatTransformer();
152         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
153                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
154
155         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
156         String yaml = transformer.convertPDToYaml(propertyDefinition);
157     }
158
159
160     @Test
161     public void testValidationPd() throws Exception {
162         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
163         Map<String, String> inParams = new HashMap<String, String>();
164         SvcLogicContext ctx = new SvcLogicContext();
165         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
166                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
167         String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
168                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
169         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
170         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
171         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
172         propertyDefinitionNode.validateParams(inParams, ctx);
173     }
174
175 }