25ab29ed9728ff6be8166d836dd135da8dc2acda
[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 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.params.parser;
26
27
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import java.nio.charset.Charset;
32 import java.util.HashMap;
33 import java.util.Map;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.sdnc.config.params.ParamsHandlerConstant;
40 import org.onap.sdnc.config.params.data.PropertyDefinition;
41 import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
42
43 public class TestPropertyDefinitionNode {
44
45     @Ignore
46     public void testProcessMissingParamKeys() throws Exception {
47         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
48         Map<String, String> inParams = new HashMap<String, String>();
49         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
50
51         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
52                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
53         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
54
55         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
56                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
57         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
58
59         SvcLogicContext ctx = new SvcLogicContext();
60         propertyDefinitionNode.processMissingParamKeys(inParams, ctx);
61         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
62                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
63
64     }
65
66     @Test
67     public void testProcessExternalSystemParamKeys() throws Exception {
68         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
69         Map<String, String> inParams = new HashMap<String, String>();
70         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
71
72         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
73                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
74         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, yamlData);
75
76         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
77                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
78         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
79
80         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "SOURCE");
81
82         SvcLogicContext ctx = new SvcLogicContext();
83         propertyDefinitionNode.processExternalSystemParamKeys(inParams, ctx);
84
85         for (Object key : ctx.getAttributeKeySet()) {
86             String parmName = (String) key;
87             String parmValue = ctx.getAttribute(parmName);
88             if (StringUtils.contains(parmName, "keys")) {
89                 System.out.println("Key: " + parmName + ",    Value: " + parmValue);
90             }
91         }
92         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
93                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
94     }
95
96     @Test
97     public void mergeJsonData() throws Exception {
98         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
99         Map<String, String> inParams = new HashMap<String, String>();
100         inParams.put(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
101
102         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
103                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
104         inParams.put(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA, jsonData);
105
106         String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
107                 .getResourceAsStream("parser/merge-param.json"), Charset.defaultCharset());
108         inParams.put(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA, mergeJsonData);
109
110         SvcLogicContext ctx = new SvcLogicContext();
111         propertyDefinitionNode.mergeJsonData(inParams, ctx);
112         String mergedParams = ctx
113                 .getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
114         assertNotNull(mergedParams);
115         assertEquals(ctx.getAttribute("test." + ParamsHandlerConstant.OUTPUT_PARAM_STATUS),
116                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
117     }
118
119     @Test
120     public void testArtificatTransformer() throws Exception {
121         ArtificatTransformer transformer = new ArtificatTransformer();
122         String yamlData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
123                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
124
125         PropertyDefinition propertyDefinition = transformer.convertYAMLToPD(yamlData);
126         String yaml = transformer.convertPDToYaml(propertyDefinition);
127     }
128
129
130     @Test
131     public void testValidationPd() throws Exception {
132         PropertyDefinitionNode propertyDefinitionNode = new PropertyDefinitionNode();
133         Map<String, String> inParams = new HashMap<String, String>();
134         SvcLogicContext ctx = new SvcLogicContext();
135         String jsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
136                 .getResourceAsStream("parser/pd.yaml"), Charset.defaultCharset());
137         String mergeJsonData = IOUtils.toString(TestPropertyDefinitionNode.class.getClassLoader()
138                 .getResourceAsStream("parser/request-param.json"), Charset.defaultCharset());
139         inParams.put(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT, jsonData);
140         inParams.put(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, mergeJsonData);
141         inParams.put(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME, "INSTAR");
142         propertyDefinitionNode.validateParams(inParams, ctx);
143     }
144
145 }