Add creation of metadata file on subscriber
[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.modules.junit4.PowerMockRunner;
32
33 import javax.servlet.ServletInputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.io.File;
37 import java.io.IOException;
38
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.*;
41
42 @RunWith(PowerMockRunner.class)
43 public class SampleSubscriberServletTest {
44
45   private SampleSubscriberServlet sampleSubServlet;
46   private SubscriberProps props = SubscriberProps.getInstance();
47
48   @Mock private HttpServletRequest request;
49   @Mock private HttpServletResponse response;
50
51   @Before
52   public void setUp() {
53     props =
54         SubscriberProps.getInstance(
55             System.getProperty(
56                 "org.onap.dmaap.datarouter.subscriber.properties", "testsubscriber.properties"));
57     sampleSubServlet = new SampleSubscriberServlet();
58     sampleSubServlet.init();
59   }
60
61   @After
62   public void tearDown() throws Exception {
63     FileUtils.deleteDirectory(
64         new File(props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir")));
65   }
66
67   @Test
68   public void
69       Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Null_Then_Unathorized_Response_Is_Generated()
70           throws Exception {
71     when(request.getHeader("Authorization")).thenReturn(null);
72     sampleSubServlet.doPut(request, response);
73     verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
74   }
75
76   @Test
77   public void
78       Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
79           throws Exception {
80     when(request.getHeader("Authorization")).thenReturn("Invalid Header");
81     sampleSubServlet.doPut(request, response);
82     verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN));
83   }
84
85   @Test
86   public void Given_Request_Is_HTTP_PUT_Then_Request_Succeeds() throws Exception {
87     setUpSuccessfulFlow();
88     sampleSubServlet.doPut(request, response);
89     verify(response, times(2)).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
90   }
91
92   @Test
93   public void Given_Request_Is_HTTP_DELETE_Then_Request_Succeeds() throws Exception {
94     setUpSuccessfulFlow();
95     sampleSubServlet.doDelete(request, response);
96     verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
97   }
98
99   private void setUpSuccessfulFlow() throws IOException {
100     when(request.getHeader("Authorization")).thenReturn("Basic TE9HSU46UEFTU1dPUkQ=");
101     when(request.getPathInfo()).thenReturn("/publish/1/testfile");
102     when(request.getHeader("X-ATT-DR-PUBLISH-ID")).thenReturn("1");
103     when(request.getHeader("X-ATT-DR-META")).thenReturn("{\"Key\":\"Value\"}");
104     when(request.getQueryString()).thenReturn(null);
105     ServletInputStream inStream = mock(ServletInputStream.class);
106     when(request.getInputStream()).thenReturn(inStream);
107   }
108 }