2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * ============LICENSE_END=========================================================
28 package org.onap.sdnc.config.generator.tool;
30 import static org.junit.Assert.assertEquals;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
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;
46 public class TestDataTool {
49 public ExpectedException expectedEx = ExpectedException.none();
52 private LogParserTool logParserTool = new LogParserTool();
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));
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));
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));
82 public void testNode() {
83 CustomJsonNodeFactory c = new CustomJsonNodeFactory();
85 assertEquals(CustomTextNode.class, c.textNode(text).getClass());;
89 public void testCustomText() {
90 CustomTextNode c = new CustomTextNode("test");
91 assertEquals("test",c.toString());
95 public void testEscapeUtils() {
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));
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);
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);
128 // @Test(expected = Exception.class)
129 public void testinitDbLibService() throws Exception {
130 DbServiceUtil.initDbLibService();
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());
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));
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));
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));
173 public void testCheckDataForInvalidXml()
175 String data="<xml><configuration</configuration>";
176 assertEquals(ConfigGeneratorConstant.DATA_TYPE_TEXT,CheckDataTool.checkData(data));
180 public void testCheckDataForValidXml()
182 String data="<xml><configuration></configuration></xml>";
183 assertEquals(ConfigGeneratorConstant.DATA_TYPE_XML,CheckDataTool.checkData(data));