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 org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.powermock.modules.junit4.PowerMockRunner;
31 import javax.servlet.ServletOutputStream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import static org.hamcrest.Matchers.notNullValue;
36 import static org.mockito.Matchers.*;
37 import static org.mockito.Mockito.*;
39 @RunWith(PowerMockRunner.class)
40 public class StatisticsServletTest extends DrServletTestBase{
41 private StatisticsServlet statisticsServlet;
44 private HttpServletRequest request;
47 private HttpServletResponse response;
50 public void setUp() throws Exception{
52 statisticsServlet = new StatisticsServlet();
53 buildRequestParameters();
57 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
58 statisticsServlet.doDelete(request, response);
59 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
63 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
64 statisticsServlet.doPut(request, response);
65 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
69 public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
70 statisticsServlet.doPost(request, response);
71 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
75 public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated() throws Exception {
76 when(request.getParameter("type")).thenReturn("get");
77 statisticsServlet.doGet(request, response);
78 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
82 public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds() throws Exception {
83 ServletOutputStream outStream = mock(ServletOutputStream.class);
84 when(response.getOutputStream()).thenReturn(outStream);
85 statisticsServlet = mock(StatisticsServlet.class);
86 doCallRealMethod().when(statisticsServlet).doGet(request, response);
87 doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject());
88 when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1"));
89 statisticsServlet.doGet(request, response);
90 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
94 public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds() throws Exception {
95 when(request.getParameter("feedid")).thenReturn("1");
96 ServletOutputStream outStream = mock(ServletOutputStream.class);
97 when(response.getOutputStream()).thenReturn(outStream);
98 statisticsServlet = mock(StatisticsServlet.class);
99 doCallRealMethod().when(statisticsServlet).doGet(request, response);
100 doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject());
101 doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject());
102 when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1"));
103 statisticsServlet.doGet(request, response);
104 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
107 private void buildRequestParameters() {
108 when(request.getParameter("type")).thenReturn("exp");
109 when(request.getParameter("publishId")).thenReturn("ID");
110 when(request.getParameter("statusCode")).thenReturn("success");
111 when(request.getParameter("expiryReason")).thenReturn("other");
112 when(request.getParameter("start")).thenReturn("0");
113 when(request.getParameter("end")).thenReturn("0");
114 when(request.getParameter("output_type")).thenReturn("csv");
115 when(request.getParameter("start_time")).thenReturn("13");
116 when(request.getParameter("end_time")).thenReturn("15");
117 when(request.getParameter("time")).thenReturn("10");
118 when(request.getParameter("groupid")).thenReturn("1");
119 when(request.getParameter("subid")).thenReturn("1");