Upgraded to Java 11
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / DbDaoUtilTest.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.utils;
18
19 import io.dropwizard.db.DataSourceFactory;
20 import io.dropwizard.jdbi.DBIFactory;
21 import io.dropwizard.setup.Environment;
22 import org.easymock.EasyMock;
23 import org.junit.Before;
24 import org.junit.Ignore;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.powermock.api.easymock.PowerMock;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32 import org.powermock.reflect.Whitebox;
33 import org.skife.jdbi.v2.DBI;
34 import org.skife.jdbi.v2.Handle;
35
36 import static org.easymock.EasyMock.anyObject;
37 import static org.easymock.EasyMock.expect;
38 import static org.hamcrest.core.IsEqual.equalTo;
39 import static org.junit.Assert.assertThat;
40
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest({DbDaoUtil.class, DBIFactory.class, DBI.class})
43 public class DbDaoUtilTest {
44
45     @Rule
46     public ExpectedException thrown = ExpectedException.none();
47
48     private DBI jdbi;
49
50     private Environment environmentProvider;
51
52     private DataSourceFactory dataSourceFactoryProvider;
53
54     private DbDaoUtil dbDaoUtil;
55
56     private DBIFactory factory;
57
58     @Before
59     public void before() throws Exception {
60         dbDaoUtil = new DbDaoUtil();
61
62         jdbi = PowerMock.createMock(DBI.class);
63         environmentProvider = PowerMock.createMock(Environment.class);
64         dataSourceFactoryProvider = PowerMock.createMock(DataSourceFactory.class);
65         factory = PowerMock.createMock(DBIFactory.class);
66         PowerMock.expectNew(DBIFactory.class).andReturn(factory);
67
68         Whitebox.setInternalState(dbDaoUtil, "environmentProvider", environmentProvider);
69         Whitebox.setInternalState(dbDaoUtil, "dataSourceFactoryProvider",
70                 dataSourceFactoryProvider);
71
72         PowerMock.resetAll();
73     }
74
75     @Test
76     @Ignore
77     public void init() {
78         PowerMock.createMock(DBI.class);
79
80         expect(factory.build(anyObject(Environment.class), anyObject(DataSourceFactory.class),
81                 anyObject(String.class))).andReturn(jdbi);
82
83         PowerMock.replayAll();
84
85         dbDaoUtil.init();
86
87         PowerMock.verifyAll();
88     }
89
90     @Test
91     public void getDao_normal() {
92         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
93         expect(jdbi.open(anyObject(Class.class))).andReturn(Class.class);
94
95         PowerMock.replayAll();
96
97         dbDaoUtil.getDao(String.class);
98
99         PowerMock.verifyAll();
100     }
101
102     @Test
103     public void getDao_exception() {
104         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
105
106         expect(jdbi.open(anyObject(Class.class))).andThrow(new RuntimeException(""));
107
108         PowerMock.replayAll();
109
110         Object o = dbDaoUtil.getDao(String.class);
111
112         PowerMock.verifyAll();
113
114         assertThat(o, equalTo(null));
115     }
116
117     @Test
118     public void getHandle_normal() {
119         Handle handle = PowerMock.createMock(Handle.class);
120
121         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
122         expect(jdbi.open()).andReturn(handle);
123
124         PowerMock.replayAll();
125
126         dbDaoUtil.getHandle();
127
128         PowerMock.verifyAll();
129     }
130
131     @Test
132     public void getHandle_exception() {
133         Handle handle = PowerMock.createMock(Handle.class);
134
135         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
136         expect(jdbi.open()).andThrow(new RuntimeException(""));
137
138         PowerMock.replayAll();
139
140         Handle handle1 = dbDaoUtil.getHandle();
141
142         PowerMock.verifyAll();
143
144         assertThat(handle1, equalTo(null));
145     }
146
147     @Test
148     public void close_normal() {
149         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
150         jdbi.close(anyObject());
151
152         PowerMock.replayAll();
153
154         dbDaoUtil.close(new Object());
155
156         PowerMock.verifyAll();
157     }
158
159     @Test
160     public void close_exception() {
161         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
162         jdbi.close(anyObject());
163         EasyMock.expectLastCall().andThrow(new RuntimeException(""));
164         PowerMock.replayAll();
165
166         dbDaoUtil.close(new Object());
167
168         PowerMock.verifyAll();
169     }
170
171     @Test
172     public void testGetJdbiDaoByOnDemand() {
173         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
174         expect(jdbi.onDemand(anyObject(Class.class))).andReturn(Class.class);
175
176         PowerMock.replayAll();
177
178         dbDaoUtil.getJdbiDaoByOnDemand(String.class);
179
180         PowerMock.verifyAll();
181     }
182
183     @Test
184     public void testGetJdbiDaoByOpen() {
185         Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi);
186         expect(jdbi.open(anyObject(Class.class))).andReturn(Class.class);
187
188         PowerMock.replayAll();
189
190         dbDaoUtil.getJdbiDaoByOpen(String.class);
191
192         PowerMock.verifyAll();
193     }
194 }