[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-subscriber / src / test / java / org / onap / dmaap / datarouter / subscriber / SampleSubscriberServletTest.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.subscriber;
24
25 import org.apache.commons.io.FileUtils;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.powermock.core.classloader.annotations.PowerMockIgnore;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 import javax.servlet.ServletInputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.File;
38 import java.io.IOException;
39
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.*;
42
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 {
46
47   private SampleSubscriberServlet sampleSubServlet;
48   private SubscriberProps props = SubscriberProps.getInstance();
49
50   @Mock private HttpServletRequest request;
51   @Mock private HttpServletResponse response;
52
53   @Before
54   public void setUp() {
55     props =
56         SubscriberProps.getInstance(
57             System.getProperty(
58                 "org.onap.dmaap.datarouter.subscriber.properties", "testsubscriber.properties"));
59     sampleSubServlet = new SampleSubscriberServlet();
60     sampleSubServlet.init();
61   }
62
63   @After
64   public void tearDown() throws Exception {
65     FileUtils.deleteDirectory(
66         new File(props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir")));
67   }
68
69   @Test
70   public void
71       Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Null_Then_Unathorized_Response_Is_Generated()
72           throws Exception {
73     when(request.getHeader("Authorization")).thenReturn(null);
74     sampleSubServlet.doPut(request, response);
75     verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
76   }
77
78   @Test
79   public void
80       Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
81           throws Exception {
82     when(request.getHeader("Authorization")).thenReturn("Invalid Header");
83     sampleSubServlet.doPut(request, response);
84     verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN));
85   }
86
87   @Test
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));
92   }
93
94   @Test
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));
99   }
100
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);
109   }
110 }