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