2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
23 package org.onap.policy.daoImp;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Properties;
34 import javax.script.SimpleBindings;
35 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
36 import org.h2.tools.Server;
37 import org.hibernate.SessionFactory;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.conf.HibernateSession;
44 import org.onap.policy.controller.PolicyController;
45 import org.onap.policy.rest.jpa.OnapName;
46 import org.onap.policy.rest.jpa.PolicyEntity;
47 import org.onap.policy.rest.jpa.PolicyRoles;
48 import org.onap.policy.rest.jpa.PolicyVersion;
49 import org.onap.policy.rest.jpa.SystemLogDB;
50 import org.onap.policy.rest.jpa.UserInfo;
51 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
52 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
53 import org.springframework.test.annotation.Rollback;
54 import org.springframework.transaction.annotation.Transactional;
56 public class CommonClassDaoImplTest {
58 private static Logger logger = FlexLogger.getLogger(CommonClassDaoImplTest.class);
60 SessionFactory sessionFactory;
62 CommonClassDaoImpl commonClassDao;
65 public void setUp() throws Exception {
67 BasicDataSource dataSource = new BasicDataSource();
68 dataSource.setDriverClassName("org.h2.Driver");
69 // In-memory DB for testing
70 dataSource.setUrl("jdbc:h2:mem:test");
71 dataSource.setUsername("sa");
72 dataSource.setPassword("");
73 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
74 sessionBuilder.scanPackages("org.onap.*", "com.*");
76 Properties properties = new Properties();
77 properties.put("hibernate.show_sql", "false");
78 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
79 properties.put("hibernate.hbm2ddl.auto", "drop");
80 properties.put("hibernate.hbm2ddl.auto", "create");
82 sessionBuilder.addProperties(properties);
83 sessionFactory = sessionBuilder.buildSessionFactory();
85 // Set up dao with SessionFactory
86 commonClassDao = new CommonClassDaoImpl();
87 CommonClassDaoImpl.setSessionfactory(sessionFactory);
88 PolicyController.setLogTableLimit("1");
89 HibernateSession.setSession(sessionFactory);
90 SystemLogDB data1 = new SystemLogDB();
91 data1.setDate(new Date());
92 data1.setLogtype("INFO");
93 data1.setRemote("Test");
94 data1.setSystem("Test");
95 data1.setType("Test");
96 SystemLogDB data2 = new SystemLogDB();
97 data2.setDate(new Date());
98 data2.setLogtype("error");
99 data2.setRemote("Test");
100 data2.setSystem("Test");
101 data2.setType("Test");
102 HibernateSession.getSession().save(data1);
103 HibernateSession.getSession().save(data2);
104 // Create TCP server for troubleshooting
105 server = Server.createTcpServer("-tcpAllowOthers").start();
106 System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test");
108 } catch (Exception e) {
109 System.err.println(e);
117 public void testDB() {
120 UserInfo userinfo = new UserInfo();
121 userinfo.setUserLoginId("Test");
122 userinfo.setUserName("Test");
123 commonClassDao.save(userinfo);
124 OnapName onapName = new OnapName();
125 onapName.setOnapName("Test");
126 onapName.setUserCreatedBy(userinfo);
127 onapName.setUserModifiedBy(userinfo);
128 onapName.setModifiedDate(new Date());
129 commonClassDao.save(onapName);
131 List<Object> list = commonClassDao.getData(OnapName.class);
132 assertTrue(list.size() == 1);
133 logger.debug(list.size());
134 logger.debug(list.get(0));
135 } catch (Exception e) {
136 logger.debug("Exception Occured" + e);
144 public void testUser() {
147 UserInfo userinfo = new UserInfo();
148 String loginId_userName = "Test";
149 userinfo.setUserLoginId(loginId_userName);
150 userinfo.setUserName(loginId_userName);
151 commonClassDao.save(userinfo);
153 List<Object> dataCur =
154 commonClassDao.getDataByQuery("from UserInfo", new SimpleBindings());
156 assertEquals(1, dataCur.size());
157 UserInfo cur = (UserInfo) dataCur.get(0);
158 assertEquals(loginId_userName, cur.getUserLoginId());
159 assertEquals(loginId_userName, cur.getUserName());
161 assertFalse(dataCur.isEmpty());
163 } catch (Exception e) {
164 logger.debug("Exception Occured" + e);
172 public void getDataByQuery_DashboardController() {
175 PolicyEntity pe = new PolicyEntity();
176 String name = "TestPolicy";
177 pe.setPolicyName(name);
178 pe.setPolicyData("dummyData");
180 pe.setScope("dummyScope");
181 pe.setDescription("descr");
182 pe.setDeleted(false);
183 pe.setCreatedBy("Test");
184 commonClassDao.save(pe);
186 List<Object> dataCur =
187 commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings());
189 assertTrue(1 == dataCur.size());
190 assertTrue(dataCur.get(0) instanceof PolicyEntity);
191 assertEquals(name, ((PolicyEntity) dataCur.get(0)).getPolicyName());
192 assertEquals(pe, ((PolicyEntity) dataCur.get(0)));
194 } catch (Exception e) {
195 logger.debug("Exception Occured" + e);
203 public void getDataByQuery_AutoPushController() {
206 PolicyVersion pv = new PolicyVersion();
207 pv.setActiveVersion(2);
208 pv.setPolicyName("myPname");
210 pv.setCreatedBy("Test");
211 pv.setModifiedBy("Test");
213 PolicyVersion pv2 = new PolicyVersion();
214 pv2.setActiveVersion(1);
215 pv2.setPolicyName("test");
217 pv2.setCreatedBy("Test");
218 pv2.setModifiedBy("Test");
220 commonClassDao.save(pv);
221 commonClassDao.save(pv2);
225 String query = "From PolicyVersion where policy_name like :scope and id > 0";
226 SimpleBindings params = new SimpleBindings();
227 params.put("scope", scope);
228 List<Object> dataCur = commonClassDao.getDataByQuery(query, params);
230 assertTrue(1 == dataCur.size());
231 assertEquals(pv, (PolicyVersion) dataCur.get(0));
233 } catch (Exception e) {
234 logger.debug("Exception Occured" + e);
242 public void getDataByQuery_PolicyNotificationMail() {
245 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
246 String policyFileName = "banana";
247 watch.setLoginIds("Test");
248 watch.setPolicyName("bananaWatch");
249 commonClassDao.save(watch);
251 if (policyFileName.contains("/")) {
252 policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
253 policyFileName = policyFileName.replace("/", File.separator);
255 if (policyFileName.contains("\\")) {
256 policyFileName = policyFileName.substring(0, policyFileName.indexOf("\\"));
257 policyFileName = policyFileName.replace("\\", "\\\\");
260 // Current Implementation
261 policyFileName += "%";
262 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
263 SimpleBindings params = new SimpleBindings();
264 params.put("policyFileName", policyFileName);
265 List<Object> dataCur = commonClassDao.getDataByQuery(query, params);
268 assertTrue(dataCur.size() == 1);
269 assertTrue(dataCur.get(0) instanceof WatchPolicyNotificationTable);
270 assertEquals(watch, (WatchPolicyNotificationTable) dataCur.get(0));
272 } catch (Exception e) {
273 logger.debug("Exception Occured" + e);
281 public void getDataByQuery_PolicyController() {
284 PolicyEntity pe = new PolicyEntity();
285 String name = "actionDummy";
286 pe.setPolicyName(name);
287 pe.setPolicyData("dummyData");
289 pe.setScope("dummyScope");
290 pe.setDescription("descr");
291 pe.setDeleted(false);
292 pe.setCreatedBy("Test");
293 commonClassDao.save(pe);
295 String dbCheckName = "dummyScope:action";
296 String[] splitDBCheckName = dbCheckName.split(":");
298 // Current Implementation
300 "FROM PolicyEntity where policyName like :splitDBCheckName1 and scope = :splitDBCheckName0";
301 SimpleBindings params = new SimpleBindings();
302 params.put("splitDBCheckName1", splitDBCheckName[1] + "%");
303 params.put("splitDBCheckName0", splitDBCheckName[0]);
304 List<Object> dataCur = commonClassDao.getDataByQuery(query, params);
307 assertTrue(dataCur.size() == 1);
308 assertTrue(dataCur.get(0) instanceof PolicyEntity);
309 assertEquals(pe, (PolicyEntity) dataCur.get(0));
311 } catch (Exception e) {
312 logger.debug("Exception Occured" + e);
320 public void getDataByQuery_PolicyNotificationController() {
323 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
324 String finalName = "banana"; // Policy File Name
325 String userId = "Test";
326 watch.setLoginIds(userId);
327 watch.setPolicyName(finalName);
328 commonClassDao.save(watch);
330 // Current Implementation
332 "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
333 SimpleBindings params = new SimpleBindings();
334 params.put("finalName", finalName);
335 params.put("userId", userId);
336 List<Object> dataCur = commonClassDao.getDataByQuery(query, params);
339 assertTrue(dataCur.size() == 1);
340 assertTrue(dataCur.get(0) instanceof WatchPolicyNotificationTable);
341 assertEquals(watch, (WatchPolicyNotificationTable) dataCur.get(0));
343 } catch (Exception e) {
344 logger.debug("Exception Occured" + e);
350 * Test for SQL Injection Protection
356 public void getDataByQuery_PolicyNotificationController_Injection() {
359 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
360 String userId = "Test";
361 watch.setLoginIds(userId);
362 watch.setPolicyName("banana");
363 commonClassDao.save(watch);
365 WatchPolicyNotificationTable watch2 = new WatchPolicyNotificationTable();
366 watch2.setLoginIds(userId);
367 watch2.setPolicyName("banana2");
368 commonClassDao.save(watch2);
370 // SQL Injection attempt
371 String finalName = "banana' OR '1'='1";
373 // Current Implementation
375 "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
376 SimpleBindings params = new SimpleBindings();
377 params.put("finalName", finalName);
378 params.put("userId", userId);
379 List<Object> dataCur = commonClassDao.getDataByQuery(query, params);
382 assertTrue(dataCur.size() <= 1);
384 if (dataCur.size() >= 1) {
385 assertTrue(dataCur.get(0) instanceof WatchPolicyNotificationTable);
386 assertFalse(watch.equals((WatchPolicyNotificationTable) dataCur.get(0)));
387 assertFalse(watch.equals((WatchPolicyNotificationTable) dataCur.get(0)));
389 } catch (Exception e) {
390 logger.debug("Exception Occured" + e);
396 public void testCommonClassDaoImplMethods() {
398 UserInfo userInfo = new UserInfo();
399 userInfo.setUserLoginId("TestID");
400 userInfo.setUserName("Test");
401 commonClassDao.save(userInfo);
402 List<Object> data = commonClassDao.getDataById(UserInfo.class, "userLoginId:userName",
404 assertTrue(data.size() == 1);
405 UserInfo userInfoUpdate = (UserInfo) data.get(0);
406 userInfoUpdate.setUserName("Test1");
407 commonClassDao.update(userInfoUpdate);
408 List<String> data1 = commonClassDao.getDataByColumn(UserInfo.class, "userLoginId");
409 assertTrue(data1.size() == 1);
410 UserInfo data2 = (UserInfo) commonClassDao.getEntityItem(UserInfo.class,
411 "userLoginId:userName", "TestID:Test1");
412 assertTrue("TestID".equals(data2.getUserLoginId()));
413 List<Object> data3 = commonClassDao.checkDuplicateEntry("TestID:Test1",
414 "userLoginId:userName", UserInfo.class);
415 assertTrue(data3.size() == 1);
416 PolicyRoles roles = new PolicyRoles();
417 roles.setRole("admin");
418 roles.setLoginId(userInfo);
419 roles.setScope("test");
420 commonClassDao.save(roles);
421 List<PolicyRoles> roles1 = commonClassDao.getUserRoles();
422 assertTrue(roles1.size() == 1);
423 List<String> multipleData = new ArrayList<>();
424 multipleData.add("TestID:Test1");
425 List<Object> data4 = commonClassDao.getMultipleDataOnAddingConjunction(UserInfo.class,
426 "userLoginId:userName", multipleData);
427 assertTrue(data4.size() == 1);
428 commonClassDao.delete(data2);
429 } catch (Exception e) {
430 logger.debug("Exception Occured" + e);
436 public final void testGetLoggingData() {
437 SystemLogDbDaoImpl system = new SystemLogDbDaoImpl();
438 PolicyController.setjUnit(true);
440 assertTrue(system.getLoggingData() != null);
441 } catch (Exception e) {
447 public final void testGetSystemAlertData() {
448 SystemLogDbDaoImpl system = new SystemLogDbDaoImpl();
449 PolicyController.setjUnit(true);
451 assertTrue(system.getSystemAlertData() != null);
452 } catch (Exception e) {
458 public void deleteDB() {
459 sessionFactory.close();