1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
25 import static org.hamcrest.Matchers.notNullValue;
26 import static org.mockito.Matchers.argThat;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.Persistence;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.onap.dmaap.datarouter.provisioning.utils.DB;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 import java.sql.Connection;
49 import java.sql.PreparedStatement;
50 import java.sql.SQLException;
53 @RunWith(PowerMockRunner.class)
54 public class StatisticsServletTest {
56 private StatisticsServlet statisticsServlet;
59 private HttpServletRequest request;
62 private HttpServletResponse response;
66 private static EntityManagerFactory emf;
67 private static EntityManager em;
70 public static void init() {
71 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
72 em = emf.createEntityManager();
74 "org.onap.dmaap.datarouter.provserver.properties",
75 "src/test/resources/h2Database.properties");
79 public static void tearDownClass() {
86 public void setUp() throws Exception {
87 statisticsServlet = new StatisticsServlet();
89 buildRequestParameters();
93 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
95 statisticsServlet.doDelete(request, response);
96 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
97 argThat(notNullValue(String.class)));
101 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
103 statisticsServlet.doPut(request, response);
104 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
105 argThat(notNullValue(String.class)));
109 public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
111 statisticsServlet.doPost(request, response);
112 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
113 argThat(notNullValue(String.class)));
117 public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
119 when(request.getParameter("type")).thenReturn("get");
120 statisticsServlet.doGet(request, response);
122 .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
126 public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
128 addAliasForSubstringIndex();
129 ServletOutputStream outStream = mock(ServletOutputStream.class);
130 when(response.getOutputStream()).thenReturn(outStream);
131 statisticsServlet.doGet(request, response);
132 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
136 public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
138 addAliasForSubstringIndex();
139 when(request.getParameter("feedid")).thenReturn("1");
140 when(request.getParameter("statusCode")).thenReturn("500");
141 ServletOutputStream outStream = mock(ServletOutputStream.class);
142 when(response.getOutputStream()).thenReturn(outStream);
143 statisticsServlet.doGet(request, response);
144 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
147 private void buildRequestParameters() {
148 when(request.getParameter("type")).thenReturn("exp");
149 when(request.getParameter("publishId")).thenReturn("ID");
150 when(request.getParameter("statusCode")).thenReturn("success");
151 when(request.getParameter("expiryReason")).thenReturn("other");
152 when(request.getParameter("start")).thenReturn("0");
153 when(request.getParameter("end")).thenReturn("0");
154 when(request.getParameter("output_type")).thenReturn("csv");
155 when(request.getParameter("start_time")).thenReturn("13");
156 when(request.getParameter("end_time")).thenReturn("15");
157 when(request.getParameter("time")).thenReturn("10");
158 when(request.getParameter("groupid")).thenReturn("1");
159 when(request.getParameter("subid")).thenReturn("1");
161 private void addAliasForSubstringIndex() throws SQLException {
162 String sql = "CREATE ALIAS IF NOT EXISTS `SUBSTRING_INDEX`AS $$ String Function(String one, String two, String three){ return \"url\"; }$$;";
163 Connection conn = db.getConnection();
164 PreparedStatement pst = conn.prepareStatement(sql);