Add and Modify JUnits for Code Coverage (daoImpl)
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / daoImp / SystemLogDbDaoImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.daoImp;
22
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.when;
26
27 import java.util.List;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.policy.conf.HibernateSession;
36 import org.onap.policy.controller.PolicyController;
37 import org.onap.policy.rest.jpa.SystemLogDb;
38
39 public class SystemLogDbDaoImplTest {
40     private static Session mockSession = Mockito.mock(Session.class);
41     private static SessionFactory sfMock = Mockito.mock(SessionFactory.class);
42     private static Transaction mockTransaction = Mockito.mock(Transaction.class);
43     private SystemLogDbDaoImpl sysLogImpl = new SystemLogDbDaoImpl();
44
45     @SuppressWarnings("unchecked")
46     @BeforeClass
47     public static void setup() {
48         HibernateSession.setSession(sfMock);
49
50         when(sfMock.openSession()).thenReturn(mockSession);
51         when(mockSession.beginTransaction()).thenReturn(mockTransaction);
52
53         doThrow(Exception.class).when(mockTransaction).commit();
54         when(mockSession.close()).thenThrow(Exception.class);
55
56         PolicyController.setjUnit(true);
57     }
58
59     @Test
60     public void testGettingLoggingData() {
61         List<SystemLogDb> loggingData = sysLogImpl.getLoggingData();
62         assertNull(loggingData);
63     }
64
65     @Test
66     public void testGetSystemAlertData() {
67         List<SystemLogDb> systemAlertData = sysLogImpl.getSystemAlertData();
68         assertNull(systemAlertData);
69     }
70
71     @AfterClass
72     public static void resetjUnit() {
73         PolicyController.setjUnit(false);
74     }
75 }