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.subscriber;
25 import jakarta.servlet.ServletInputStream;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.modules.junit4.PowerMockRunner;
35 import jakarta.servlet.http.HttpServletRequest;
36 import jakarta.servlet.http.HttpServletResponse;
38 import java.io.IOException;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.*;
43 @RunWith(PowerMockRunner.class)
44 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*", "com.sun.org.apache.xalan.*"})
45 public class SampleSubscriberServletTest {
47 private SampleSubscriberServlet sampleSubServlet;
48 private SubscriberProps props = SubscriberProps.getInstance();
50 @Mock private HttpServletRequest request;
51 @Mock private HttpServletResponse response;
56 SubscriberProps.getInstance(
58 "org.onap.dmaap.datarouter.subscriber.properties", "testsubscriber.properties"));
59 sampleSubServlet = new SampleSubscriberServlet();
60 sampleSubServlet.init();
64 public void tearDown() throws Exception {
65 FileUtils.deleteDirectory(
66 new File(props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir")));
71 Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Null_Then_Unathorized_Response_Is_Generated()
73 when(request.getHeader("Authorization")).thenReturn(null);
74 sampleSubServlet.doPut(request, response);
75 verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
80 Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
82 when(request.getHeader("Authorization")).thenReturn("Invalid Header");
83 sampleSubServlet.doPut(request, response);
84 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN));
88 public void Given_Request_Is_HTTP_PUT_Then_Request_Succeeds() throws Exception {
89 setUpSuccessfulFlow();
90 sampleSubServlet.doPut(request, response);
91 verify(response, times(2)).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
95 public void Given_Request_Is_HTTP_DELETE_Then_Request_Succeeds() throws Exception {
96 setUpSuccessfulFlow();
97 sampleSubServlet.doDelete(request, response);
98 verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
101 private void setUpSuccessfulFlow() throws IOException {
102 when(request.getHeader("Authorization")).thenReturn("Basic TE9HSU46UEFTU1dPUkQ=");
103 when(request.getPathInfo()).thenReturn("/publish/1/testfile");
104 when(request.getHeader("X-DMAAP-DR-PUBLISH-ID")).thenReturn("1");
105 when(request.getHeader("X-DMAAP-DR-META")).thenReturn("{\"Key\":\"Value\"}");
106 when(request.getQueryString()).thenReturn(null);
107 ServletInputStream inStream = mock(ServletInputStream.class);
108 when(request.getInputStream()).thenReturn(inStream);