HealthCheckController/test refactor
[vid.git] / vid-app-common / src / test / java / org / onap / vid / dao / FnAppDoaImplTest.java
1 package org.onap.vid.dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12 import org.mockito.runners.MockitoJUnitRunner;
13
14 import static org.assertj.core.api.Java6Assertions.assertThat;
15 import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
16 import static org.mockito.BDDMockito.given;
17 import static org.mockito.Matchers.anyString;
18
19 @RunWith(MockitoJUnitRunner.class)
20 public class FnAppDoaImplTest {
21
22     private FnAppDoaImpl fnAppDoa;
23
24     @Mock
25     private ConnectionFactory connectionFactory;
26
27     @Mock
28     private Connection connection;
29
30     @Mock
31     private PreparedStatement preparedStatement;
32
33     @Mock
34     private ResultSet resultSet;
35
36     private static final String ERROR_MESSAGE = "error message";
37     private static final String QUERY = "select count(*) from fn_app";
38
39     @Before
40     public void setUp() throws SQLException {
41         given(resultSet.next()).willReturn(true);
42         given(resultSet.getInt(1)).willReturn(5);
43         given(preparedStatement.executeQuery()).willReturn(resultSet);
44         given(connectionFactory.getConnection(anyString(), anyString(), anyString())).willReturn(connection);
45         fnAppDoa = new FnAppDoaImpl(connectionFactory);
46     }
47
48     private void okCaseSetUp() throws SQLException {
49
50         given(connection.prepareStatement(QUERY)).willReturn(preparedStatement);
51     }
52
53     private void nokCaseSetup() throws SQLException {
54         given(connection.prepareStatement(QUERY)).willThrow(new SQLException(ERROR_MESSAGE));
55     }
56
57     @Test
58     public void getProfileCount_shouldReturnNumber_whenNoExceptionIsThrown() throws SQLException {
59         okCaseSetUp();
60         assertThat(fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword")).isEqualTo(5);
61     }
62
63     @Test
64     public void getProfileCount_shouldRethrowSQLException() throws SQLException {
65         nokCaseSetup();
66         assertThatThrownBy(() -> fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword"))
67                 .isInstanceOf(SQLException.class).hasMessage(ERROR_MESSAGE);
68     }
69 }