3d7b7fb8468ac082df0dbf6c39bfe42a641178c8
[appc.git] / appc-config / appc-config-generator / provider / src / test / java / org / onap / sdnc / config / generator / tool / TestDataTool.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.generator.tool;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
37 import org.onap.sdnc.config.generator.pattern.TestPatternNode;
38 import org.powermock.reflect.Whitebox;
39 import static org.junit.Assert.assertEquals;
40
41 public class TestDataTool {
42
43     @Mock
44     private LogParserTool logParserTool = new LogParserTool();
45
46     @Test
47     public void testCheckData() throws IOException {
48         String data = IOUtils.toString(
49             TestPatternNode.class.getClassLoader()
50                 .getResourceAsStream("convert/payload_cli_config.json"),
51             ConfigGeneratorConstant.STRING_ENCODING);
52         CheckDataTool.checkData(data);
53     }
54
55     @Test
56     public void testIsJSON() throws IOException {
57         String data = IOUtils.toString(
58             TestPatternNode.class.getClassLoader()
59                 .getResourceAsStream("convert/payload_cli_config.json"),
60             ConfigGeneratorConstant.STRING_ENCODING);
61         CheckDataTool.isJSON(data);
62     }
63
64     @Test
65     public void testIsXML() throws IOException {
66         String data = IOUtils.toString(
67             TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/xml_data.xml"),
68             ConfigGeneratorConstant.STRING_ENCODING);
69         CheckDataTool.isXML(data);
70     }
71
72     @Test
73     public void testNode() {
74         CustomJsonNodeFactory c = new CustomJsonNodeFactory();
75         String text = "test";
76         c.textNode(text);
77     }
78
79     @Test
80     public void testCustomText() {
81         CustomTextNode c = new CustomTextNode("test");
82         c.toString();
83     }
84
85     @Test
86     public void testEscapeUtils() {
87         String s = "test\\";
88         String st = "test\"test";
89         String str = "test\'" + "test";
90         String strng = "test\0";
91         EscapeUtils.escapeString(s);
92         EscapeUtils.escapeSql(s);
93         EscapeUtils.escapeString(st);
94         EscapeUtils.escapeString(str);
95         EscapeUtils.escapeString(strng);
96         EscapeUtils.escapeString(null);
97     }
98
99     @Test(expected = Exception.class)
100     public void testgetData() throws Exception {
101         List<String> argList = null;
102         String schema = "sdnctl";
103         String tableName = "dual";
104         String getselectData = "123";
105         String getDataClasue = "123='123'";
106         DbServiceUtil.getData(tableName, argList, schema, getselectData, getDataClasue);
107     }
108
109     @Test(expected = Exception.class)
110     public void testupdateDB() throws Exception {
111         String setClause = null;
112         String tableName = "dual";
113         List<String> inputArgs = null;
114         String whereClause = "123='123'";
115         DbServiceUtil.updateDB(tableName, inputArgs, whereClause, setClause);
116     }
117
118     // @Test(expected = Exception.class)
119     public void testinitDbLibService() throws Exception {
120         DbServiceUtil.initDbLibService();
121     }
122
123     @Test
124     public void testJSONTool() throws Exception {
125         String data = IOUtils.toString(
126             TestPatternNode.class.getClassLoader()
127                 .getResourceAsStream("convert/payload_cli_config.json"),
128             ConfigGeneratorConstant.STRING_ENCODING);
129         JSONTool.convertToProperties(data);
130         List<String> blockKeys = new ArrayList<String>();
131         blockKeys.add("vnf-type");
132         blockKeys.add("request-parameters");
133         JSONTool.convertToProperties(data, blockKeys);
134     }
135
136     @Test
137     public void testLogParserTool() throws Exception {
138         String data = IOUtils.toString(
139             TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/errorlog.txt"),
140             ConfigGeneratorConstant.STRING_ENCODING);
141         LogParserTool lpt = new LogParserTool();
142         lpt.parseErrorLog(data);
143     }
144
145     @Test
146     public void testMergeTool() throws Exception {
147         String template = "test";
148         Map<String, String> dataMap = new HashMap<String, String>();
149         MergeTool.mergeMap2TemplateData(template, dataMap);
150     }
151
152     @Test
153     public void testcheckDateTime() throws Exception {
154         String line = "2017-08-20T17:40:23.100361+00:00";
155         Whitebox.invokeMethod(logParserTool, "checkDateTime", line);
156     }
157     
158     @Test
159     public void testCheckDataForInvalidXml()
160     {
161         String data="<xml><configuration</configuration>";
162         assertEquals(ConfigGeneratorConstant.DATA_TYPE_TEXT,CheckDataTool.checkData(data));
163     }
164     
165     @Test
166     public void testCheckDataForValidXml()
167     {
168         String data="<xml><configuration></configuration></xml>";
169         assertEquals(ConfigGeneratorConstant.DATA_TYPE_XML,CheckDataTool.checkData(data));
170     }
171 }