diff options
-rw-r--r-- | POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java | 99 | ||||
-rw-r--r-- | POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/SystemLogDbDaoImplTest.java | 75 |
2 files changed, 159 insertions, 15 deletions
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java index 3453f6acc..dbe49c7c3 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/CommonClassDaoImplTest.java @@ -24,23 +24,29 @@ package org.onap.policy.daoImp; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; - import javax.script.SimpleBindings; - import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; import org.h2.tools.Server; +import org.hibernate.Criteria; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.conf.HibernateSession; @@ -136,7 +142,7 @@ public class CommonClassDaoImplTest { onapName.setModifiedDate(new Date()); commonClassDao.save(onapName); - List<Object> list = commonClassDao.getData(OnapName.class); + List<?> list = commonClassDao.getData(OnapName.class); assertTrue(list.size() == 1); logger.debug(list.size()); logger.debug(list.get(0)); @@ -158,7 +164,7 @@ public class CommonClassDaoImplTest { userinfo.setUserName(loginIdUserName); commonClassDao.save(userinfo); - List<Object> dataCur = commonClassDao.getDataByQuery("from UserInfo", new SimpleBindings()); + List<?> dataCur = commonClassDao.getDataByQuery("from UserInfo", new SimpleBindings()); assertEquals(1, dataCur.size()); UserInfo cur = (UserInfo) dataCur.get(0); @@ -190,7 +196,7 @@ public class CommonClassDaoImplTest { pe.setCreatedBy("Test"); commonClassDao.save(pe); - List<Object> dataCur = commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings()); + List<?> dataCur = commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings()); assertTrue(1 == dataCur.size()); assertTrue(dataCur.get(0) instanceof PolicyEntity); @@ -231,7 +237,7 @@ public class CommonClassDaoImplTest { String query = "From PolicyVersion where policy_name like :scope and id > 0"; SimpleBindings params = new SimpleBindings(); params.put("scope", scope); - List<Object> dataCur = commonClassDao.getDataByQuery(query, params); + List<?> dataCur = commonClassDao.getDataByQuery(query, params); assertTrue(1 == dataCur.size()); assertEquals(pv, dataCur.get(0)); @@ -268,7 +274,7 @@ public class CommonClassDaoImplTest { String query = "from WatchPolicyNotificationTable where policyName like:policyFileName"; SimpleBindings params = new SimpleBindings(); params.put("policyFileName", policyFileName); - List<Object> dataCur = commonClassDao.getDataByQuery(query, params); + List<?> dataCur = commonClassDao.getDataByQuery(query, params); // Assertions assertTrue(dataCur.size() == 1); @@ -306,7 +312,7 @@ public class CommonClassDaoImplTest { String[] splitDbCheckName = dbCheckName.split(":"); params.put("splitDBCheckName1", splitDbCheckName[1] + "%"); params.put("splitDBCheckName0", splitDbCheckName[0]); - List<Object> dataCur = commonClassDao.getDataByQuery(query, params); + List<?> dataCur = commonClassDao.getDataByQuery(query, params); // Assertions assertTrue(dataCur.size() == 1); @@ -337,7 +343,7 @@ public class CommonClassDaoImplTest { SimpleBindings params = new SimpleBindings(); params.put("finalName", finalName); params.put("userId", userId); - List<Object> dataCur = commonClassDao.getDataByQuery(query, params); + List<?> dataCur = commonClassDao.getDataByQuery(query, params); // Assertions assertTrue(dataCur.size() == 1); @@ -379,7 +385,7 @@ public class CommonClassDaoImplTest { SimpleBindings params = new SimpleBindings(); params.put("finalName", finalName); params.put("userId", userId); - List<Object> dataCur = commonClassDao.getDataByQuery(query, params); + List<?> dataCur = commonClassDao.getDataByQuery(query, params); // Assertions assertTrue(dataCur.size() <= 1); @@ -402,7 +408,7 @@ public class CommonClassDaoImplTest { userInfo.setUserLoginId("TestID"); userInfo.setUserName("Test"); commonClassDao.save(userInfo); - List<Object> data = commonClassDao.getDataById(UserInfo.class, "userLoginId:userName", "TestID:Test"); + List<?> data = commonClassDao.getDataById(UserInfo.class, "userLoginId:userName", "TestID:Test"); assertTrue(data.size() == 1); UserInfo userInfoUpdate = (UserInfo) data.get(0); userInfoUpdate.setUserName("Test1"); @@ -412,8 +418,7 @@ public class CommonClassDaoImplTest { UserInfo data2 = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId:userName", "TestID:Test1"); assertTrue("TestID".equals(data2.getUserLoginId())); - List<Object> data3 = - commonClassDao.checkDuplicateEntry("TestID:Test1", "userLoginId:userName", UserInfo.class); + List<?> data3 = commonClassDao.checkDuplicateEntry("TestID:Test1", "userLoginId:userName", UserInfo.class); assertTrue(data3.size() == 1); PolicyRoles roles = new PolicyRoles(); roles.setRole("admin"); @@ -424,8 +429,8 @@ public class CommonClassDaoImplTest { assertTrue(roles1.size() == 1); List<String> multipleData = new ArrayList<>(); multipleData.add("TestID:Test1"); - List<Object> data4 = commonClassDao.getMultipleDataOnAddingConjunction(UserInfo.class, - "userLoginId:userName", multipleData); + List<?> data4 = commonClassDao.getMultipleDataOnAddingConjunction(UserInfo.class, "userLoginId:userName", + multipleData); assertTrue(data4.size() == 1); commonClassDao.delete(data2); } catch (Exception e) { @@ -456,6 +461,70 @@ public class CommonClassDaoImplTest { } } + @SuppressWarnings("unchecked") + @Test + public void testExceptions() { + SessionFactory sfMock = Mockito.mock(SessionFactory.class); + Session mockSession = Mockito.mock(Session.class); + Criteria crMock = Mockito.mock(Criteria.class); + Transaction mockTransaction = Mockito.mock(Transaction.class); + + CommonClassDaoImpl.setSessionfactory(sfMock); + + when(sfMock.openSession()).thenReturn(mockSession); + when(mockSession.createCriteria(OnapName.class)).thenReturn(crMock); + + when(crMock.list()).thenThrow(Exception.class); + when(mockSession.close()).thenThrow(Exception.class); + + when(mockSession.beginTransaction()).thenReturn(mockTransaction); + doThrow(Exception.class).when(mockTransaction).commit(); + + List<?> dataList = commonClassDao.getData(OnapName.class); + assertNull(dataList); + + List<?> dataByIdList = commonClassDao.getDataById(UserInfo.class, "userLoginId:userName", "TestID:Test"); + assertNull(dataByIdList); + + commonClassDao.save(null); + commonClassDao.delete(null); + commonClassDao.update(null); + + List<?> dupEntryList = + commonClassDao.checkDuplicateEntry("TestID:Test", "userLoginId:userName", UserInfo.class); + assertNull(dupEntryList); + + List<PolicyRoles> userRoles = commonClassDao.getUserRoles(); + assertNull(userRoles); + + Object entityItem = commonClassDao.getEntityItem(UserInfo.class, "testColName", "testKey"); + assertNull(entityItem); + + commonClassDao.updateQuery("testQueryString"); + + List<String> dataByColumn = commonClassDao.getDataByColumn(UserInfo.class, "testColName"); + assertNull(dataByColumn); + + List<?> entityData = commonClassDao.getMultipleDataOnAddingConjunction(UserInfo.class, "", null); + assertNull(entityData); + } + + @Test + public void testCheckExistingGroupListforUpdate() { + Object retObj = commonClassDao.checkExistingGroupListforUpdate("testString1", "testString2"); + assertNotNull(retObj); + assertTrue(retObj instanceof List); + List<?> retList = (List<?>) retObj; + assertTrue(retList.isEmpty()); + } + + @Test + public void testEmptyMethods() { + commonClassDao.deleteAll(); + commonClassDao.updateClAlarms("TestString1", "TestString2"); + commonClassDao.updateClYaml("TestString1", "TestString2"); + } + @After public void deleteDB() { sessionFactory.close(); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/SystemLogDbDaoImplTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/SystemLogDbDaoImplTest.java new file mode 100644 index 000000000..5675d3fc9 --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/daoImp/SystemLogDbDaoImplTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.daoImp; + +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.conf.HibernateSession; +import org.onap.policy.controller.PolicyController; +import org.onap.policy.rest.jpa.SystemLogDb; + +public class SystemLogDbDaoImplTest { + private static Session mockSession = Mockito.mock(Session.class); + private static SessionFactory sfMock = Mockito.mock(SessionFactory.class); + private static Transaction mockTransaction = Mockito.mock(Transaction.class); + private SystemLogDbDaoImpl sysLogImpl = new SystemLogDbDaoImpl(); + + @SuppressWarnings("unchecked") + @BeforeClass + public static void setup() { + HibernateSession.setSession(sfMock); + + when(sfMock.openSession()).thenReturn(mockSession); + when(mockSession.beginTransaction()).thenReturn(mockTransaction); + + doThrow(Exception.class).when(mockTransaction).commit(); + when(mockSession.close()).thenThrow(Exception.class); + + PolicyController.setjUnit(true); + } + + @Test + public void testGettingLoggingData() { + List<SystemLogDb> loggingData = sysLogImpl.getLoggingData(); + assertNull(loggingData); + } + + @Test + public void testGetSystemAlertData() { + List<SystemLogDb> systemAlertData = sysLogImpl.getSystemAlertData(); + assertNull(systemAlertData); + } + + @AfterClass + public static void resetjUnit() { + PolicyController.setjUnit(false); + } +} |