aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java
diff options
context:
space:
mode:
authorJoanna Jeremicz <joanna.jeremicz@nokia.com>2018-09-19 14:35:00 +0200
committerJoanna Jeremicz <joanna.jeremicz@nokia.com>2018-09-28 07:39:18 +0200
commit56f2ad282e7a8d316b92878ae151767fea847265 (patch)
tree6fff9ecd3b4c197ff37bd0f96d430edd75d93adf /vid-app-common/src/test/java
parent52cb6586c32cb99b1aab99bebfa51b01d98beba4 (diff)
HealthCheckController/test refactor
Issue-ID: VID-312 Change-Id: Iabdb91c9b4940ff7a173656819ad5fa807a734e9 Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java120
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/dao/FnAppDoaImplTest.java124
2 files changed, 125 insertions, 119 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java
index 6055bc390..da9cdaa6e 100644
--- a/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java
@@ -3,65 +3,99 @@ package org.onap.vid.controllers;
import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
-import org.onap.vid.controllers.HealthCheckController.HealthStatus;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.vid.dao.FnAppDoaImpl;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import java.sql.SQLException;
+
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Matchers.anyString;
+import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
+import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
+@RunWith(MockitoJUnitRunner.class)
public class HealthCheckControllerTest {
- private HealthCheckController testSubject;
- private MockMvc mockMvc;
+ private static final String ERROR_MESSAGE = "error message";
+ private HealthCheckController testSubject;
+ private MockMvc mockMvc;
+
+ @Mock
+ private FnAppDoaImpl fnAppDoa;
- @Before
- public void setUp() {
- testSubject = new HealthCheckController();
- BasicConfigurator.configure();
- mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
- }
+ @Before
+ public void setUp() {
+ testSubject = new HealthCheckController(fnAppDoa);
+ BasicConfigurator.configure();
+ mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
+ }
- @Test
- public void testGetProfileCount() throws Exception {
- String driver = "";
- String URL = "";
- String username = "";
- String password = "";
- int result;
+ @Test
+ public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
+ databaseConnectionEstablished();
+ mockMvc.perform(get("/healthCheck")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.statusCode").value(OK.value()))
+ .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"));
+ }
- // default test
- result = testSubject.getProfileCount(driver, URL, username, password);
- }
+ @Test
+ public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
+ databaseNotAccessible();
+ mockMvc.perform(get("/healthCheck")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
+ .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
+ }
- @Test
- public void testGethealthCheckStatusforIDNS() throws Exception {
- HealthStatus result;
+ @Test
+ public void getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
+ databaseConnectionEstablished();
+ mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.statusCode").value(OK.value()))
+ .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"))
+ .andExpect(jsonPath("$.date").isString());
+ }
- // default test
- result = testSubject.gethealthCheckStatusforIDNS();
- }
+ @Test
+ public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
+ databaseNotAccessible();
+ mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
+ .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
+ }
- @Test
- public void testGetHealthCheck() throws Exception {
- String UserAgent = "";
- String ECOMPRequestID = "";
- HealthStatus result;
+ @Test
+ public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception {
+ mockMvc.perform(get("/commitInfo")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.commitId").value("123"))
+ .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
+ .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
+ }
- // default test
- result = testSubject.getHealthCheck(UserAgent, ECOMPRequestID);
- }
+ private void databaseConnectionEstablished() throws SQLException {
+ given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
+ .willReturn(0);
+ }
- @Test
- public void testCommitInfoEndpoint() throws Exception {
- mockMvc.perform(get("/commitInfo")
- .accept(MediaType.APPLICATION_JSON))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.commitId").value("123987"))
- .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
- .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
- }
+ private void databaseNotAccessible() throws SQLException {
+ given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
+ .willThrow(new SQLException(ERROR_MESSAGE));
+ }
} \ No newline at end of file
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 e7a7e3a37..2c2aa89a0 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