unit test for StastisticsServlet with db
Change-Id: I32ed948f2485d13605d58fadef72903b05ca57f5
Signed-off-by: Emmett Cox <emmett.cox@ericsson.com>
Issue-ID: DMAAP-101
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java
index 33bf3a3..4917402 100755
--- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java
+++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java
@@ -180,39 +180,8 @@
outputType = req.getParameter("output_type");
}
- try {
+ this.getRecordsForSQL(map, outputType, out, resp);
- String filterQuery = this.queryGeneretor(map);
- eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery);
-
- ResultSet rs = this.getRecordsForSQL(filterQuery);
-
- if (outputType.equals("csv")) {
- resp.setContentType("application/octet-stream");
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
- resp.setHeader("Content-Disposition",
- "attachment; filename=\"result:" + dateFormat.format(date) + ".csv\"");
- eventlogger.info("Generating CSV file from Statistics resultset");
-
- rsToCSV(rs, out);
- } else {
- eventlogger.info("Generating JSON for Statistics resultset");
- this.rsToJson(rs, out);
- }
- } catch (IOException e) {
- eventlogger.error("IOException - Generating JSON/CSV:" + e);
- e.printStackTrace();
- } catch (JSONException e) {
- eventlogger.error("JSONException - executing SQL query:" + e);
- e.printStackTrace();
- } catch (SQLException e) {
- eventlogger.error("SQLException - executing SQL query:" + e);
- e.printStackTrace();
- } catch (ParseException e) {
- eventlogger.error("ParseException - executing SQL query:" + e);
- e.printStackTrace();
- }
}
@@ -565,21 +534,47 @@
intlogger.info("Error parsing time=" + s);
return -1;
}
- private ResultSet getRecordsForSQL(String sql) {
- intlogger.debug(sql);
+
+ private void getRecordsForSQL(Map<String, String> map, String outputType, ServletOutputStream out, HttpServletResponse resp) {
+ try {
+
+ String filterQuery = this.queryGeneretor(map);
+ eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery);
+ intlogger.debug(filterQuery);
long start = System.currentTimeMillis();
DB db = new DB();
ResultSet rs = null;
- try (
- Connection conn = db.getConnection()){
- try(PreparedStatement pst = conn.prepareStatement(sql)){
- rs = pst.executeQuery();
+ try (Connection conn = db.getConnection()) {
+ try (PreparedStatement pst = conn.prepareStatement(filterQuery)) {
+ rs = pst.executeQuery();
+ if (outputType.equals("csv")) {
+ resp.setContentType("application/octet-stream");
+ Date date = new Date();
+ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
+ resp.setHeader("Content-Disposition",
+ "attachment; filename=\"result:" + dateFormat.format(date) + ".csv\"");
+ eventlogger.info("Generating CSV file from Statistics resultset");
+
+ rsToCSV(rs, out);
+ } else {
+ eventlogger.info("Generating JSON for Statistics resultset");
+ this.rsToJson(rs, out);
}
+ }
} catch (SQLException e) {
- e.printStackTrace();
+ e.printStackTrace();
}
- intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms");
- return rs;
- }
+ intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms");
+ } catch (IOException e) {
+ eventlogger.error("IOException - Generating JSON/CSV:" + e);
+ e.printStackTrace();
+ } catch (JSONException e) {
+ eventlogger.error("JSONException - executing SQL query:" + e);
+ e.printStackTrace();
+ } catch (ParseException e) {
+ eventlogger.error("ParseException - executing SQL query:" + e);
+ e.printStackTrace();
+ }
}
+}
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 0babdc4..64d13e9 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 @@
@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 @@
@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 @@
@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 @@
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();
+ }
}
diff --git a/datarouter-prov/src/test/resources/create.sql b/datarouter-prov/src/test/resources/create.sql
index c72c42a..fe0a0f8 100755
--- a/datarouter-prov/src/test/resources/create.sql
+++ b/datarouter-prov/src/test/resources/create.sql
@@ -161,4 +161,13 @@
VALUES (2,1,'user',null,2);
insert into NODESETS(SETID, NODEID)
-VALUES (2,0);
\ No newline at end of file
+VALUES (2,0);
+
+insert into LOG_RECORDS(RECORD_ID,TYPE,EVENT_TIME,PUBLISH_ID,FEEDID,REQURI,METHOD,CONTENT_TYPE,CONTENT_LENGTH,FEED_FILEID,REMOTE_ADDR,USER,STATUS,DELIVERY_SUBID,DELIVERY_FILEID,RESULT,ATTEMPTS,REASON)
+VALUES(1,'pub',2536159564422,'ID',1,'URL','GET','application/vnd.att-dr.log-list; version=1.0',100,1,'172.0.0.8','user',204,1,1,204,0,'other');
+
+CREATE ALIAS IF NOT EXISTS `SUBSTRING_INDEX` AS $$
+ String Function(String one, String two, String three){
+ return "url";
+ }
+$$;
\ No newline at end of file