summaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src
diff options
context:
space:
mode:
authorRam Koya <rk541m@att.com>2018-09-07 01:14:57 +0000
committerGerrit Code Review <gerrit@onap.org>2018-09-07 01:14:57 +0000
commit26311ae5220efa4460c03284d0a47b2def420ecb (patch)
tree8c033fcf195944f23451b827ae02cfe2584bac16 /datarouter-prov/src
parent5cbbe749bfb23c1ab24aec01fdd3f3f4b2e36ce9 (diff)
parent1d9710279b4d9bffacc0f8e360922119bb12e347 (diff)
Merge "unit test for StastisticsServlet with db"
Diffstat (limited to 'datarouter-prov/src')
-rwxr-xr-xdatarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java79
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java67
-rwxr-xr-xdatarouter-prov/src/test/resources/create.sql11
3 files changed, 89 insertions, 68 deletions
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 33bf3a35..4917402c 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 @@ public class StatisticsServlet extends BaseServlet {
outputType = req.getParameter("output_type");
}
- try {
-
- String filterQuery = this.queryGeneretor(map);
- eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery);
-
- ResultSet rs = this.getRecordsForSQL(filterQuery);
+ this.getRecordsForSQL(map, outputType, out, resp);
- 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 @@ public class StatisticsServlet extends BaseServlet {
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();
- }
- intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms");
- return rs;
+ e.printStackTrace();
}
+ 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 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();
+ }
}
diff --git a/datarouter-prov/src/test/resources/create.sql b/datarouter-prov/src/test/resources/create.sql
index 9ae11cb6..2f9d3d9c 100755
--- a/datarouter-prov/src/test/resources/create.sql
+++ b/datarouter-prov/src/test/resources/create.sql
@@ -164,4 +164,13 @@ insert into INGRESS_ROUTES(SEQUENCE, FEEDID , USERID, SUBNET, NODESET)
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