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.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
33 import org.powermock.modules.junit4.PowerMockRunner;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
39 import static org.hamcrest.CoreMatchers.notNullValue;
40 import static org.mockito.Matchers.anyInt;
41 import static org.mockito.Matchers.argThat;
42 import static org.mockito.Matchers.eq;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.verify;
45 import static org.powermock.api.mockito.PowerMockito.when;
47 @RunWith(PowerMockRunner.class)
48 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Subscription"})
49 public class LogServletTest extends DrServletTestBase {
51 private static LogServlet logServlet;
54 private HttpServletRequest request;
56 private HttpServletResponse response;
59 public void setUp() throws Exception {
61 logServlet = new LogServlet(true);
62 setUpValidParameterValuesForMap();
66 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
68 logServlet.doDelete(request, response);
69 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
73 public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
75 when(request.getPathInfo()).thenReturn(null);
76 logServlet.doGet(request, response);
77 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
81 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type()
83 when(request.getParameter("type")).thenReturn("bad_type");
84 logServlet.doGet(request, response);
85 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
89 public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID()
91 when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
92 logServlet.doGet(request, response);
93 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
97 public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode()
99 when(request.getParameter("statusCode")).thenReturn("1'");
100 logServlet.doGet(request, response);
101 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
105 public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
107 when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
108 logServlet.doGet(request, response);
109 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
113 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
115 when(request.getParameter("start")).thenReturn("bad_startTime");
116 logServlet.doGet(request, response);
117 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
121 public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
123 when(request.getParameter("end")).thenReturn("bad_endTime");
124 logServlet.doGet(request, response);
125 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
129 public void Given_Request_Is_HTTP_GET_And_Is_FeedLog()
131 logServlet.doGet(request, response);
132 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
136 public void Given_Request_Is_HTTP_GET_And_Is_Not_FeedLog()
138 LogServlet logServletNotFeedlog = new LogServlet(false);
139 PowerMockito.mockStatic(Subscription.class);
140 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(mock(Subscription.class));
141 logServletNotFeedlog.doGet(request, response);
142 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
146 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
148 logServlet.doPut(request, response);
149 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
153 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
155 logServlet.doPost(request, response);
156 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
160 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish()
162 when(request.getParameter("type")).thenReturn("pub");
163 when(request.getParameter("expiryReason")).thenReturn(null);
164 logServlet.doGet(request, response);
165 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
169 public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery()
171 when(request.getParameter("type")).thenReturn("del");
172 when(request.getParameter("expiryReason")).thenReturn(null);
173 logServlet.doGet(request, response);
174 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
178 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire()
180 when(request.getParameter("type")).thenReturn("exp");
181 when(request.getParameter("statusCode")).thenReturn(null);
182 when(request.getParameter("expiryReason")).thenReturn(null);
183 ServletOutputStream s = mock(ServletOutputStream.class);
184 when(response.getOutputStream()).thenReturn(s);
185 logServlet.doGet(request, response);
186 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
190 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery()
192 LogServlet logServletNotFeedlog = new LogServlet(false);
193 when(request.getParameter("type")).thenReturn("del");
194 when(request.getParameter("statusCode")).thenReturn(null);
195 when(request.getParameter("expiryReason")).thenReturn(null);
196 PowerMockito.mockStatic(Subscription.class);
197 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(mock(Subscription.class));
198 logServletNotFeedlog.doGet(request, response);
199 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
203 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry()
205 LogServlet logServletNotFeedlog = new LogServlet(false);
206 when(request.getParameter("type")).thenReturn("exp");
207 when(request.getParameter("statusCode")).thenReturn(null);
208 when(request.getParameter("expiryReason")).thenReturn(null);
209 PowerMockito.mockStatic(Subscription.class);
210 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(mock(Subscription.class));
211 logServletNotFeedlog.doGet(request, response);
212 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
215 private void setUpValidParameterValuesForMap() throws Exception {
216 when(request.getPathInfo()).thenReturn("123");
217 when(request.getParameter("type")).thenReturn("exp");
218 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
219 when(request.getParameter("statusCode")).thenReturn("-1");
220 when(request.getParameter("expiryReason")).thenReturn("other");
221 when(request.getParameter("start")).thenReturn(null);
222 when(request.getParameter("end")).thenReturn(null);
223 ServletOutputStream s = mock(ServletOutputStream.class);
224 when(response.getOutputStream()).thenReturn(s);