Remove redundant unit test
[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 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;
31
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
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.onap.dmaap.datarouter.provisioning.utils.DB;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import java.sql.Connection;
49 import java.sql.PreparedStatement;
50 import java.sql.SQLException;
51
52
53 @RunWith(PowerMockRunner.class)
54 public class StatisticsServletTest {
55
56   private StatisticsServlet statisticsServlet;
57
58   @Mock
59   private HttpServletRequest request;
60
61   @Mock
62   private HttpServletResponse response;
63
64   private DB db;
65
66   private static EntityManagerFactory emf;
67   private static EntityManager em;
68
69   @BeforeClass
70   public static void init() {
71     emf = Persistence.createEntityManagerFactory("dr-unit-tests");
72     em = emf.createEntityManager();
73     System.setProperty(
74             "org.onap.dmaap.datarouter.provserver.properties",
75             "src/test/resources/h2Database.properties");
76   }
77
78   @AfterClass
79   public static void tearDownClass() {
80     em.clear();
81     em.close();
82     emf.close();
83   }
84
85   @Before
86   public void setUp() throws Exception {
87     statisticsServlet = new StatisticsServlet();
88     db = new DB();
89     buildRequestParameters();
90   }
91
92   @Test
93   public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
94       throws Exception {
95     statisticsServlet.doDelete(request, response);
96     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
97         argThat(notNullValue(String.class)));
98   }
99
100   @Test
101   public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
102       throws Exception {
103     statisticsServlet.doPut(request, response);
104     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
105         argThat(notNullValue(String.class)));
106   }
107
108   @Test
109   public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
110       throws Exception {
111     statisticsServlet.doPost(request, response);
112     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
113         argThat(notNullValue(String.class)));
114   }
115
116   @Test
117   public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
118       throws Exception {
119     when(request.getParameter("type")).thenReturn("get");
120     statisticsServlet.doGet(request, response);
121     verify(response)
122         .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
123   }
124
125   @Test
126   public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
127       throws Exception {
128     ServletOutputStream outStream = mock(ServletOutputStream.class);
129     when(response.getOutputStream()).thenReturn(outStream);
130     statisticsServlet.doGet(request, response);
131     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
132   }
133
134   @Test
135   public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
136       throws Exception {
137     when(request.getParameter("feedid")).thenReturn("1");
138     when(request.getParameter("statusCode")).thenReturn("500");
139     ServletOutputStream outStream = mock(ServletOutputStream.class);
140     when(response.getOutputStream()).thenReturn(outStream);
141     statisticsServlet.doGet(request, response);
142     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
143   }
144
145   private void buildRequestParameters() {
146     when(request.getParameter("type")).thenReturn("exp");
147     when(request.getParameter("publishId")).thenReturn("ID");
148     when(request.getParameter("statusCode")).thenReturn("success");
149     when(request.getParameter("expiryReason")).thenReturn("other");
150     when(request.getParameter("start")).thenReturn("0");
151     when(request.getParameter("end")).thenReturn("0");
152     when(request.getParameter("output_type")).thenReturn("csv");
153     when(request.getParameter("start_time")).thenReturn("13");
154     when(request.getParameter("end_time")).thenReturn("15");
155     when(request.getParameter("time")).thenReturn("10");
156     when(request.getParameter("groupid")).thenReturn("1");
157     when(request.getParameter("subid")).thenReturn("1");
158   }
159 }