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;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 @RunWith(PowerMockRunner.class)
47 public class StatisticsServletTest {
49 private StatisticsServlet statisticsServlet;
52 private HttpServletRequest request;
55 private HttpServletResponse response;
57 private static EntityManagerFactory emf;
58 private static EntityManager em;
61 public static void init() {
62 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
63 em = emf.createEntityManager();
65 "org.onap.dmaap.datarouter.provserver.properties",
66 "src/test/resources/h2Database.properties");
70 public static void tearDownClass() {
77 public void setUp() throws Exception {
78 statisticsServlet = new StatisticsServlet();
79 buildRequestParameters();
83 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
85 statisticsServlet.doDelete(request, response);
86 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
87 argThat(notNullValue(String.class)));
91 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
93 statisticsServlet.doPut(request, response);
94 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
95 argThat(notNullValue(String.class)));
99 public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
101 statisticsServlet.doPost(request, response);
102 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
103 argThat(notNullValue(String.class)));
107 public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
109 when(request.getParameter("type")).thenReturn("get");
110 statisticsServlet.doGet(request, response);
112 .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
116 public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
118 ServletOutputStream outStream = mock(ServletOutputStream.class);
119 when(response.getOutputStream()).thenReturn(outStream);
120 statisticsServlet.doGet(request, response);
121 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
125 public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
127 when(request.getParameter("feedid")).thenReturn("1");
128 when(request.getParameter("statusCode")).thenReturn("500");
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));
135 private void buildRequestParameters() {
136 when(request.getParameter("type")).thenReturn("exp");
137 when(request.getParameter("publishId")).thenReturn("ID");
138 when(request.getParameter("statusCode")).thenReturn("success");
139 when(request.getParameter("expiryReason")).thenReturn("other");
140 when(request.getParameter("start")).thenReturn("0");
141 when(request.getParameter("end")).thenReturn("0");
142 when(request.getParameter("output_type")).thenReturn("csv");
143 when(request.getParameter("start_time")).thenReturn("13");
144 when(request.getParameter("end_time")).thenReturn("15");
145 when(request.getParameter("time")).thenReturn("10");
146 when(request.getParameter("groupid")).thenReturn("1");
147 when(request.getParameter("subid")).thenReturn("1");