[DMaaP DR] JKD 11 migration
[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.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.Persistence;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.modules.junit4.PowerMockRunner;
45
46 @RunWith(PowerMockRunner.class)
47 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
48 public class StatisticsServletTest {
49
50   private StatisticsServlet statisticsServlet;
51
52   @Mock
53   private HttpServletRequest request;
54
55   @Mock
56   private HttpServletResponse response;
57
58   private static EntityManagerFactory emf;
59   private static EntityManager em;
60
61   @BeforeClass
62   public static void init() {
63     emf = Persistence.createEntityManagerFactory("dr-unit-tests");
64     em = emf.createEntityManager();
65     System.setProperty(
66             "org.onap.dmaap.datarouter.provserver.properties",
67             "src/test/resources/h2Database.properties");
68   }
69
70   @AfterClass
71   public static void tearDownClass() {
72     em.clear();
73     em.close();
74     emf.close();
75   }
76
77   @Before
78   public void setUp() throws Exception {
79     statisticsServlet = new StatisticsServlet();
80     buildRequestParameters();
81   }
82
83   @Test
84   public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
85       throws Exception {
86     statisticsServlet.doDelete(request, response);
87     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
88         anyString());
89   }
90
91   @Test
92   public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
93       throws Exception {
94     statisticsServlet.doPut(request, response);
95     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
96         anyString());
97   }
98
99   @Test
100   public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated()
101       throws Exception {
102     statisticsServlet.doPost(request, response);
103     verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED),
104         anyString());
105   }
106
107   @Test
108   public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated()
109       throws Exception {
110     when(request.getParameter("type")).thenReturn("get");
111     statisticsServlet.doGet(request, response);
112     verify(response)
113         .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
114   }
115
116   @Test
117   public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds()
118       throws Exception {
119     ServletOutputStream outStream = mock(ServletOutputStream.class);
120     when(response.getOutputStream()).thenReturn(outStream);
121     statisticsServlet.doGet(request, response);
122     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
123   }
124
125   @Test
126   public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds()
127       throws Exception {
128     when(request.getParameter("feedid")).thenReturn("1");
129     when(request.getParameter("statusCode")).thenReturn("500");
130     ServletOutputStream outStream = mock(ServletOutputStream.class);
131     when(response.getOutputStream()).thenReturn(outStream);
132     statisticsServlet.doGet(request, response);
133     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
134   }
135
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");
149   }
150 }