From 56f2ad282e7a8d316b92878ae151767fea847265 Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Wed, 19 Sep 2018 14:35:00 +0200 Subject: HealthCheckController/test refactor Issue-ID: VID-312 Change-Id: Iabdb91c9b4940ff7a173656819ad5fa807a734e9 Signed-off-by: Joanna Jeremicz --- .../java/org/onap/vid/dao/FnAppDoaImplTest.java | 124 ++++++++------------- 1 file changed, 48 insertions(+), 76 deletions(-) (limited to 'vid-app-common/src/test/java/org/onap/vid/dao') diff --git a/vid-app-common/src/test/java/org/onap/vid/dao/FnAppDoaImplTest.java b/vid-app-common/src/test/java/org/onap/vid/dao/FnAppDoaImplTest.java index e7a7e3a3..2c2aa89a 100644 --- a/vid-app-common/src/test/java/org/onap/vid/dao/FnAppDoaImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/dao/FnAppDoaImplTest.java @@ -3,95 +3,67 @@ package org.onap.vid.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; -import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.assertj.core.api.Java6Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyString; + +@RunWith(MockitoJUnitRunner.class) public class FnAppDoaImplTest { - private FnAppDoaImpl createTestSubject() { - return new FnAppDoaImpl(); - } + private FnAppDoaImpl fnAppDoa; - @Test - public void testGetConnection() throws Exception { - String driver2 = ""; - String url = ""; - String username = ""; - String password = ""; - Connection result; - - // test 1 - url = null; - username = null; - password = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); - - // test 2 - url = ""; - username = null; - password = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); - - // test 3 - username = null; - url = null; - password = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); - - // test 4 - username = ""; - url = null; - password = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); - - // test 5 - password = null; - url = null; - username = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); - - // test 6 - password = ""; - url = null; - username = null; - result = FnAppDoaImpl.getConnection(driver2, url, username); - Assert.assertEquals(null, result); + @Mock + private ConnectionFactory connectionFactory; + + @Mock + private Connection connection; + + @Mock + private PreparedStatement preparedStatement; + + @Mock + private ResultSet resultSet; + + private static final String ERROR_MESSAGE = "error message"; + private static final String QUERY = "select count(*) from fn_app"; + + @Before + public void setUp() throws SQLException { + given(resultSet.next()).willReturn(true); + given(resultSet.getInt(1)).willReturn(5); + given(preparedStatement.executeQuery()).willReturn(resultSet); + given(connectionFactory.getConnection(anyString(), anyString(), anyString())).willReturn(connection); + fnAppDoa = new FnAppDoaImpl(connectionFactory); } - @Test - public void testCleanup() throws Exception { - ResultSet rs = null; - PreparedStatement st = null; + private void okCaseSetUp() throws SQLException { - // test 1 - rs = null; - FnAppDoaImpl.cleanup(rs, st, null); + given(connection.prepareStatement(QUERY)).willReturn(preparedStatement); + } - // test 2 - st = null; - FnAppDoaImpl.cleanup(rs, st, null); + private void nokCaseSetup() throws SQLException { + given(connection.prepareStatement(QUERY)).willThrow(new SQLException(ERROR_MESSAGE)); + } - // test 3 - FnAppDoaImpl.cleanup(rs, st, null); + @Test + public void getProfileCount_shouldReturnNumber_whenNoExceptionIsThrown() throws SQLException { + okCaseSetUp(); + assertThat(fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword")).isEqualTo(5); } @Test - public void testGetProfileCount() throws Exception { - FnAppDoaImpl testSubject; - String driver = ""; - String URL = ""; - String username = ""; - String password = ""; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getProfileCount(driver, URL, username, password); + public void getProfileCount_shouldRethrowSQLException() throws SQLException { + nokCaseSetup(); + assertThatThrownBy(() -> fnAppDoa.getProfileCount("anyUrl", "anyUsername", "anyPassword")) + .isInstanceOf(SQLException.class).hasMessage(ERROR_MESSAGE); } } \ No newline at end of file -- cgit 1.2.3-korg