1 package org.onap.vid.dao;
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
8 import org.junit.Before;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12 import org.mockito.runners.MockitoJUnitRunner;
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;
19 @RunWith(MockitoJUnitRunner.class)
20 public class FnAppDoaImplTest {
22 private FnAppDoaImpl fnAppDoa;
25 private ConnectionFactory connectionFactory;
28 private Connection connection;
31 private PreparedStatement preparedStatement;
34 private ResultSet resultSet;
36 private static final String ERROR_MESSAGE = "error message";
37 private static final String QUERY = "select count(*) from fn_app";
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);
48 private void okCaseSetUp() throws SQLException {
50 given(connection.prepareStatement(QUERY)).willReturn(preparedStatement);
53 private void nokCaseSetup() throws SQLException {
54 given(connection.prepareStatement(QUERY)).willThrow(new SQLException(ERROR_MESSAGE));
58 public void getProfileCount_shouldReturnNumber_whenNoExceptionIsThrown() throws SQLException {
60 assertThat(fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword")).isEqualTo(5);
64 public void getProfileCount_shouldRethrowSQLException() throws SQLException {
66 assertThatThrownBy(() -> fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword"))
67 .isInstanceOf(SQLException.class).hasMessage(ERROR_MESSAGE);