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;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.AfterClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.powermock.modules.junit4.PowerMockRunner;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.Persistence;
41 import static org.hamcrest.CoreMatchers.notNullValue;
42 import static org.mockito.Matchers.argThat;
43 import static org.mockito.Matchers.eq;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.verify;
46 import static org.powermock.api.mockito.PowerMockito.when;
49 @RunWith(PowerMockRunner.class)
50 public class LogServletTest extends DrServletTestBase {
52 private static EntityManagerFactory emf;
53 private static EntityManager em;
54 private static LogServlet logServlet;
57 private HttpServletRequest request;
59 private HttpServletResponse response;
62 public static void init() {
63 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
64 em = emf.createEntityManager();
66 "org.onap.dmaap.datarouter.provserver.properties",
67 "src/test/resources/h2Database.properties");
71 public static void tearDownClass() {
78 public void setUp() throws Exception {
79 logServlet = new LogServlet(true);
80 setUpValidParameterValuesForMap();
84 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
86 logServlet.doDelete(request, response);
87 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
91 public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
93 when(request.getPathInfo()).thenReturn(null);
94 logServlet.doGet(request, response);
95 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
99 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
101 when(request.getParameter("type")).thenReturn("bad_type");
102 logServlet.doGet(request, response);
103 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
107 public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
109 when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
110 logServlet.doGet(request, response);
111 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
115 public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
117 when(request.getParameter("statusCode")).thenReturn("1'");
118 logServlet.doGet(request, response);
119 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
123 public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
125 when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
126 logServlet.doGet(request, response);
127 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
131 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
133 when(request.getParameter("start")).thenReturn("bad_startTime");
134 logServlet.doGet(request, response);
135 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
139 public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
141 when(request.getParameter("end")).thenReturn("bad_endTime");
142 logServlet.doGet(request, response);
143 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
147 public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated()
149 logServlet.doGet(request, response);
150 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
154 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
156 logServlet.doPut(request, response);
157 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
161 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
163 logServlet.doPost(request, response);
164 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
168 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated()
170 when(request.getParameter("type")).thenReturn("pub");
171 when(request.getParameter("expiryReason")).thenReturn(null);
172 logServlet.doGet(request, response);
173 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
177 public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
179 when(request.getParameter("type")).thenReturn("del");
180 when(request.getParameter("expiryReason")).thenReturn(null);
181 logServlet.doGet(request, response);
182 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
186 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
188 when(request.getParameter("type")).thenReturn("exp");
189 when(request.getParameter("statusCode")).thenReturn(null);
190 when(request.getParameter("expiryReason")).thenReturn(null);
191 ServletOutputStream s = mock(ServletOutputStream.class);
192 when(response.getOutputStream()).thenReturn(s);
193 logServlet.doGet(request, response);
194 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
198 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
200 LogServlet logServletNotFeedlog = new LogServlet(false);
201 when(request.getParameter("type")).thenReturn("del");
202 when(request.getParameter("statusCode")).thenReturn(null);
203 when(request.getParameter("expiryReason")).thenReturn(null);
204 logServletNotFeedlog.doGet(request, response);
205 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
209 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
211 LogServlet logServletNotFeedlog = new LogServlet(false);
212 when(request.getParameter("type")).thenReturn("exp");
213 when(request.getParameter("statusCode")).thenReturn(null);
214 when(request.getParameter("expiryReason")).thenReturn(null);
215 logServletNotFeedlog.doGet(request, response);
216 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
219 private void setUpValidParameterValuesForMap() throws Exception {
220 when(request.getPathInfo()).thenReturn("123");
221 when(request.getParameter("type")).thenReturn("exp");
222 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
223 when(request.getParameter("statusCode")).thenReturn("-1");
224 when(request.getParameter("expiryReason")).thenReturn("other");
225 when(request.getParameter("start")).thenReturn(null);
226 when(request.getParameter("end")).thenReturn(null);
227 ServletOutputStream s = mock(ServletOutputStream.class);
228 when(response.getOutputStream()).thenReturn(s);