Fix for APPC-1264
[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  * Modifications Copyright (C) 2018 Ericsson
12  * =============================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  * 
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  * 
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.sdnc.config.generator.tool;
29
30 import static org.junit.Assert.assertEquals;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import org.apache.commons.io.IOUtils;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
43 import org.onap.sdnc.config.generator.pattern.TestPatternNode;
44
45
46 public class TestDataTool {
47
48     @Rule
49     public ExpectedException expectedEx = ExpectedException.none();
50
51     @Mock
52     private LogParserTool logParserTool = new LogParserTool();
53
54     @Test
55     public void testCheckData() throws IOException {
56         String data = IOUtils.toString(
57             TestPatternNode.class.getClassLoader()
58                 .getResourceAsStream("convert/payload_cli_config.json"),
59             ConfigGeneratorConstant.STRING_ENCODING);
60         assertEquals(ConfigGeneratorConstant.DATA_TYPE_JSON,
61                 CheckDataTool.checkData(data));
62     }
63
64     @Test
65     public void testIsJSON() throws IOException {
66         String data = IOUtils.toString(
67             TestPatternNode.class.getClassLoader()
68                 .getResourceAsStream("convert/payload_cli_config.json"),
69             ConfigGeneratorConstant.STRING_ENCODING);
70         assertEquals(true, CheckDataTool.isJSON(data));
71     }
72
73     @Test
74     public void testIsXML() throws IOException {
75         String data = IOUtils.toString(
76             TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/xml_data.xml"),
77             ConfigGeneratorConstant.STRING_ENCODING);
78         assertEquals(true, CheckDataTool.isXML(data));
79     }
80
81     @Test
82     public void testNode() {
83         CustomJsonNodeFactory c = new CustomJsonNodeFactory();
84         String text = "test";
85         assertEquals(CustomTextNode.class, c.textNode(text).getClass());;
86     }
87
88     @Test
89     public void testCustomText() {
90         CustomTextNode c = new CustomTextNode("test");
91         assertEquals("test",c.toString());
92     }
93
94     @Test
95     public void testEscapeUtils() {
96         String s = "test\\";
97         String st = "test\"test";
98         String str = "test\'" + "test";
99         String strng = "test\0";
100         assertEquals("test\\\\", EscapeUtils.escapeString(s));
101         assertEquals("test\"test", EscapeUtils.escapeSql(st));
102         assertEquals("test\\'test", EscapeUtils.escapeString(str));
103         assertEquals("test\\0", EscapeUtils.escapeString(strng));
104         assertEquals(null, EscapeUtils.escapeString(null));
105     }
106
107     @Test
108     public void testgetData() throws Exception {
109         List<String> argList = null;
110         String schema = "sdnctl";
111         String tableName = "dual";
112         String getselectData = "123";
113         String getDataClasue = "123='123'";
114         expectedEx.expect(NullPointerException.class);
115         DbServiceUtil.getData(tableName, argList, schema, getselectData, getDataClasue);
116     }
117
118     @Test
119     public void testupdateDB() throws Exception {
120         String setClause = null;
121         String tableName = "dual";
122         List<String> inputArgs = null;
123         String whereClause = "123='123'";
124         expectedEx.expect(NullPointerException.class);
125         DbServiceUtil.updateDB(tableName, inputArgs, whereClause, setClause);
126     }
127
128     // @Test(expected = Exception.class)
129     public void testinitDbLibService() throws Exception {
130         DbServiceUtil.initDbLibService();
131     }
132
133     @Test
134     public void testJSONTool() throws Exception {
135         String data = IOUtils.toString(
136             TestPatternNode.class.getClassLoader()
137                 .getResourceAsStream("convert/payload_cli_config.json"),
138             ConfigGeneratorConstant.STRING_ENCODING);
139         JSONTool.convertToProperties(data);
140         List<String> blockKeys = new ArrayList<String>();
141         blockKeys.add("vnf-type");
142         blockKeys.add("request-parameters");
143         assertEquals(HashMap.class, JSONTool.convertToProperties(data, blockKeys).getClass());
144     }
145
146     @Test
147     public void testLogParserTool() throws Exception {
148         String data = IOUtils.toString(
149             TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/errorlog.txt"),
150             ConfigGeneratorConstant.STRING_ENCODING);
151         LogParserTool lpt = new LogParserTool();
152         assertEquals("Did not find the string 'Starting orchestration of file backed up to /var/opt"+
153                 "/MetaSwitch/orch/orch_conf.json' in the log file with timestamp within the last 5 minutes", 
154                 lpt.parseErrorLog(data));
155     }
156
157     @Test
158     public void testMergeTool() throws Exception {
159         String template = "test";
160         Map<String, String> dataMap = new HashMap<String, String>();
161         assertEquals("test", MergeTool.mergeMap2TemplateData(template, dataMap));
162     }
163
164     @Test
165     public void testcheckDateTime() throws Exception {
166         String line = "2017-08-20T17:40:23.100361+00:00 Error parsing orchestration file:";
167         assertEquals("Did not find the string 'Starting orchestration of file backed up to /var/opt"+
168                 "/MetaSwitch/orch/orch_conf.json' in the log file with timestamp within the last 5 minutes",
169                 logParserTool.parseErrorLog(line));
170     }
171
172     @Test
173     public void testCheckDataForInvalidXml()
174     {
175         String data="<xml><configuration</configuration>";
176         assertEquals(ConfigGeneratorConstant.DATA_TYPE_TEXT,CheckDataTool.checkData(data));
177     }
178
179     @Test
180     public void testCheckDataForValidXml()
181     {
182         String data="<xml><configuration></configuration></xml>";
183         assertEquals(ConfigGeneratorConstant.DATA_TYPE_XML,CheckDataTool.checkData(data));
184     }
185 }