diff options
author | Emmett Cox <emmett.cox@ericsson.com> | 2018-09-06 13:58:27 +0100 |
---|---|---|
committer | Emmett Cox <emmett.cox@ericsson.com> | 2018-09-06 16:40:13 +0100 |
commit | 1d9710279b4d9bffacc0f8e360922119bb12e347 (patch) | |
tree | 1c72d75019a04ba9b7856bf8e088a63455b4db5d /datarouter-prov/src/test/java | |
parent | 079a007f29994785912ca891c58e09f755189e58 (diff) |
unit test for StastisticsServlet with db
Change-Id: I32ed948f2485d13605d58fadef72903b05ca57f5
Signed-off-by: Emmett Cox <emmett.cox@ericsson.com>
Issue-ID: DMAAP-101
Diffstat (limited to 'datarouter-prov/src/test/java')
-rwxr-xr-x | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java | 67 |
1 files changed, 42 insertions, 25 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java index 0babdc47..64d13e94 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java @@ -23,32 +23,35 @@ package org.onap.dmaap.datarouter.provisioning; import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doCallRealMethod; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; +import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.powermock.modules.junit4.PowerMockRunner; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + @RunWith(PowerMockRunner.class) -@PrepareForTest(StatisticsServlet.class) -public class StatisticsServletTest extends DrServletTestBase { +public class StatisticsServletTest { private StatisticsServlet statisticsServlet; @@ -58,10 +61,31 @@ public class StatisticsServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + private DB db; + + private static EntityManagerFactory emf; + private static EntityManager em; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before public void setUp() throws Exception { - super.setUp(); statisticsServlet = new StatisticsServlet(); + db = new DB(); buildRequestParameters(); } @@ -101,15 +125,9 @@ public class StatisticsServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds() throws Exception { + addAliasForSubstringIndex(); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); - statisticsServlet = PowerMockito.mock(StatisticsServlet.class); - PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); - PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject()); - PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString()); - doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); - doCallRealMethod().when(statisticsServlet).doGet(request, response); - when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); statisticsServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -117,18 +135,11 @@ public class StatisticsServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds() throws Exception { + addAliasForSubstringIndex(); when(request.getParameter("feedid")).thenReturn("1"); when(request.getParameter("statusCode")).thenReturn("500"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); - statisticsServlet = PowerMockito.mock(StatisticsServlet.class); - PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); - PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject()); - PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString()); - doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); - doCallRealMethod().when(statisticsServlet).doGet(request, response); - doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject()); - when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); statisticsServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -147,4 +158,10 @@ public class StatisticsServletTest extends DrServletTestBase { when(request.getParameter("groupid")).thenReturn("1"); when(request.getParameter("subid")).thenReturn("1"); } + private void addAliasForSubstringIndex() throws SQLException { + String sql = "CREATE ALIAS IF NOT EXISTS `SUBSTRING_INDEX`AS $$ String Function(String one, String two, String three){ return \"url\"; }$$;"; + Connection conn = db.getConnection(); + PreparedStatement pst = conn.prepareStatement(sql); + pst.execute(); + } } |