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 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28 import org.apache.commons.lang3.reflect.FieldUtils;
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;
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;
44 import java.io.FileNotFoundException;
45 import java.net.InetAddress;
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;
55 * Created by ezcoxem on 21/08/2018.
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;
65 private HttpServletRequest request;
68 private HttpServletResponse response;
70 private static EntityManagerFactory emf;
71 private static EntityManager em;
73 private ListAppender<ILoggingEvent> listAppender;
76 public static void init() {
77 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
78 em = emf.createEntityManager();
80 "org.onap.dmaap.datarouter.provserver.properties",
81 "src/test/resources/h2Database.properties");
85 public static void tearDownClass() throws FileNotFoundException {
93 public void setUp() throws Exception {
94 listAppender = setTestLogger(PublishServlet.class);
95 publishServlet = new PublishServlet();
96 ProvDbUtils.getInstance().initProvDB();
100 public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
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);
110 public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
112 publishServlet.doDelete(request, response);
113 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
117 public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
120 when(request.getPathInfo()).thenReturn("/1/");
121 publishServlet.doDelete(request, response);
122 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
126 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
128 when(request.getPathInfo()).thenReturn("/122/fileName.txt");
129 publishServlet.doDelete(request, response);
130 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
134 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
136 when(request.getPathInfo()).thenReturn("/abc/fileName.txt");
137 publishServlet.doDelete(request, response);
138 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
143 public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
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);
153 public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
155 setConditionsForPositiveSuccessFlow();
157 publishServlet.doPut(request, response);
158 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
159 verifyEnteringExitCalled(listAppender);
163 public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
165 setConditionsForPositiveSuccessFlow();
167 publishServlet.doPost(request, response);
168 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
169 verifyEnteringExitCalled(listAppender);
173 public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_is_empty()
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"));
185 public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_Is_Not_Empty()
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"));
197 private void setConditionsForPositiveSuccessFlow() throws Exception {
198 FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
199 when(request.getPathInfo()).thenReturn("/1/fileName.txt");