Add Tests for StatisticsServlet and Group Class
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / StatisticsServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
24
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;
30
31 import javax.servlet.ServletOutputStream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import static org.hamcrest.Matchers.notNullValue;
36 import static org.mockito.Matchers.*;
37 import static org.mockito.Mockito.*;
38
39 @RunWith(PowerMockRunner.class)
40 public class StatisticsServletTest extends DrServletTestBase{
41     private StatisticsServlet statisticsServlet;
42
43     @Mock
44     private HttpServletRequest request;
45
46     @Mock
47     private HttpServletResponse response;
48
49     @Before
50     public void setUp() throws Exception{
51         super.setUp();
52         statisticsServlet = new StatisticsServlet();
53         buildRequestParameters();
54     }
55
56     @Test
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)));
60     }
61
62     @Test
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)));
66     }
67
68     @Test
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)));
72     }
73
74     @Test
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)));
79     }
80
81     @Test
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));
91     }
92
93     @Test
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));
105     }
106
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");
120     }
121 }