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.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.Persistence;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 @RunWith(PowerMockRunner.class)
47 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
48 public class StatisticsServletTest {
50 private StatisticsServlet statisticsServlet;
53 private HttpServletRequest request;
56 private HttpServletResponse response;
58 private static EntityManagerFactory emf;
59 private static EntityManager em;
62 public static void init() {
63 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
64 em = emf.createEntityManager();
66 "org.onap.dmaap.datarouter.provserver.properties",
67 "src/test/resources/h2Database.properties");
71 public static void tearDownClass() {
78 public void setUp() throws Exception {
79 statisticsServlet = new StatisticsServlet();
80 buildRequestParameters();
84 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
86 statisticsServlet.doDelete(request, response);
87 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
92 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
94 statisticsServlet.doPut(request, response);
95 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
100 public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
102 statisticsServlet.doPost(request, response);
103 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
108 public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
110 when(request.getParameter("type")).thenReturn("get");
111 statisticsServlet.doGet(request, response);
113 .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
117 public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
119 ServletOutputStream outStream = mock(ServletOutputStream.class);
120 when(response.getOutputStream()).thenReturn(outStream);
121 statisticsServlet.doGet(request, response);
122 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
126 public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
128 when(request.getParameter("feedid")).thenReturn("1");
129 when(request.getParameter("statusCode")).thenReturn("500");
130 ServletOutputStream outStream = mock(ServletOutputStream.class);
131 when(response.getOutputStream()).thenReturn(outStream);
132 statisticsServlet.doGet(request, response);
133 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
136 private void buildRequestParameters() {
137 when(request.getParameter("type")).thenReturn("exp");
138 when(request.getParameter("publishId")).thenReturn("ID");
139 when(request.getParameter("statusCode")).thenReturn("success");
140 when(request.getParameter("expiryReason")).thenReturn("other");
141 when(request.getParameter("start")).thenReturn("0");
142 when(request.getParameter("end")).thenReturn("0");
143 when(request.getParameter("output_type")).thenReturn("csv");
144 when(request.getParameter("start_time")).thenReturn("13");
145 when(request.getParameter("end_time")).thenReturn("15");
146 when(request.getParameter("time")).thenReturn("10");
147 when(request.getParameter("groupid")).thenReturn("1");
148 when(request.getParameter("subid")).thenReturn("1");