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.anyInt;
27 import static org.mockito.Matchers.anyObject;
28 import static org.mockito.Matchers.anyString;
29 import static org.mockito.Matchers.argThat;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.doCallRealMethod;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
37 import javax.servlet.ServletOutputStream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest(StatisticsServlet.class)
51 public class StatisticsServletTest extends DrServletTestBase {
53 private StatisticsServlet statisticsServlet;
56 private HttpServletRequest request;
59 private HttpServletResponse response;
62 public void setUp() throws Exception {
64 statisticsServlet = new StatisticsServlet();
65 buildRequestParameters();
69 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
71 statisticsServlet.doDelete(request, response);
72 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
73 argThat(notNullValue(String.class)));
77 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
79 statisticsServlet.doPut(request, response);
80 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
81 argThat(notNullValue(String.class)));
85 public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
87 statisticsServlet.doPost(request, response);
88 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
89 argThat(notNullValue(String.class)));
93 public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
95 when(request.getParameter("type")).thenReturn("get");
96 statisticsServlet.doGet(request, response);
98 .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
102 public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
104 ServletOutputStream outStream = mock(ServletOutputStream.class);
105 when(response.getOutputStream()).thenReturn(outStream);
106 statisticsServlet = PowerMockito.mock(StatisticsServlet.class);
107 PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString());
108 PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject());
109 PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString());
110 doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject());
111 doCallRealMethod().when(statisticsServlet).doGet(request, response);
112 when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1"));
113 statisticsServlet.doGet(request, response);
114 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
118 public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
120 when(request.getParameter("feedid")).thenReturn("1");
121 when(request.getParameter("statusCode")).thenReturn("500");
122 ServletOutputStream outStream = mock(ServletOutputStream.class);
123 when(response.getOutputStream()).thenReturn(outStream);
124 statisticsServlet = PowerMockito.mock(StatisticsServlet.class);
125 PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString());
126 PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject());
127 PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString());
128 doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject());
129 doCallRealMethod().when(statisticsServlet).doGet(request, response);
130 doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject());
131 when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1"));
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");