[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / PublishServletTest.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
24 package org.onap.dmaap.datarouter.provisioning;
25
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28 import org.apache.commons.lang3.reflect.FieldUtils;
29 import org.junit.*;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
33 import org.powermock.core.classloader.annotations.PowerMockIgnore;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.Persistence;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43
44 import java.io.FileNotFoundException;
45 import java.net.InetAddress;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.mockito.ArgumentMatchers.anyString;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.when;
52 import static org.powermock.api.mockito.PowerMockito.mockStatic;
53
54 /**
55  * Created by ezcoxem on 21/08/2018.
56  */
57
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({InetAddress.class })
60 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
61 public class PublishServletTest extends DrServletTestBase {
62     private PublishServlet publishServlet;
63
64     @Mock
65     private HttpServletRequest request;
66
67     @Mock
68     private HttpServletResponse response;
69
70     private static EntityManagerFactory emf;
71     private static EntityManager em;
72
73     private ListAppender<ILoggingEvent> listAppender;
74
75     @BeforeClass
76     public static void init() {
77         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
78         em = emf.createEntityManager();
79         System.setProperty(
80                 "org.onap.dmaap.datarouter.provserver.properties",
81                 "src/test/resources/h2Database.properties");
82     }
83
84     @AfterClass
85     public static void tearDownClass() throws FileNotFoundException {
86         em.clear();
87         em.close();
88         emf.close();
89     }
90
91
92     @Before
93     public void setUp() throws Exception {
94         listAppender = setTestLogger(PublishServlet.class);
95         publishServlet = new PublishServlet();
96         ProvDbUtils.getInstance().initProvDB();
97     }
98
99     @Test
100     public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
101             throws Exception {
102         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[0], true);
103         publishServlet.doDelete(request, response);
104         verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE), anyString());
105         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
106         verifyEnteringExitCalled(listAppender);
107     }
108
109     @Test
110     public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
111             throws Exception {
112         publishServlet.doDelete(request, response);
113         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
114     }
115
116     @Test
117     public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
118             throws Exception {
119
120         when(request.getPathInfo()).thenReturn("/1/");
121         publishServlet.doDelete(request, response);
122         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
123     }
124
125     @Test
126     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
127             throws Exception {
128         when(request.getPathInfo()).thenReturn("/122/fileName.txt");
129         publishServlet.doDelete(request, response);
130         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
131     }
132
133     @Test
134     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
135             throws Exception {
136         when(request.getPathInfo()).thenReturn("/abc/fileName.txt");
137         publishServlet.doDelete(request, response);
138         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
139     }
140
141
142     @Test
143     public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
144             throws Exception {
145         when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
146         setConditionsForPositiveSuccessFlow();
147         publishServlet.doDelete(request, response);
148         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
149         verifyEnteringExitCalled(listAppender);
150     }
151
152     @Test
153     public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
154             throws Exception {
155         setConditionsForPositiveSuccessFlow();
156
157         publishServlet.doPut(request, response);
158         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
159         verifyEnteringExitCalled(listAppender);
160     }
161
162     @Test
163     public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
164             throws Exception {
165         setConditionsForPositiveSuccessFlow();
166
167         publishServlet.doPost(request, response);
168         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
169         verifyEnteringExitCalled(listAppender);
170     }
171
172     @Test
173     public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_is_empty()
174             throws Exception {
175         setConditionsForPositiveSuccessFlow();
176         mockStatic(InetAddress.class);
177         publishServlet.doGet(request, response);
178         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
179         verifyEnteringExitCalled(listAppender);
180         assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("RequestId"));
181         assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("InvocationId"));
182     }
183
184     @Test
185     public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_Is_Not_Empty()
186             throws Exception {
187         setConditionsForPositiveSuccessFlow();
188         when(request.getHeader("X-ONAP-RequestID")).thenReturn("123");
189         when(request.getHeader("X-InvocationID")).thenReturn("456");
190         publishServlet.doGet(request, response);
191         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
192         verifyEnteringExitCalled(listAppender);
193         assertEquals("123", listAppender.list.get(0).getMDCPropertyMap().get("RequestId"));
194         assertEquals("456", listAppender.list.get(0).getMDCPropertyMap().get("InvocationId"));
195     }
196
197     private void setConditionsForPositiveSuccessFlow() throws Exception {
198         FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
199         when(request.getPathInfo()).thenReturn("/1/fileName.txt");
200     }
201
202
203
204 }