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 org.apache.commons.lang3.reflect.FieldUtils;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.onap.dmaap.datarouter.provisioning.utils.DB;
34 import org.powermock.modules.junit4.PowerMockRunner;
36 import javax.persistence.EntityManager;
37 import javax.persistence.EntityManagerFactory;
38 import javax.persistence.Persistence;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
43 import static org.hamcrest.Matchers.notNullValue;
44 import static org.mockito.Matchers.*;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
49 * Created by ezcoxem on 21/08/2018.
52 @RunWith(PowerMockRunner.class)
53 public class PublishServletTest {
54 private PublishServlet publishServlet;
57 private HttpServletRequest request;
60 private HttpServletResponse response;
62 private static EntityManagerFactory emf;
63 private static EntityManager em;
67 public static void init() {
68 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
69 em = emf.createEntityManager();
71 "org.onap.dmaap.datarouter.provserver.properties",
72 "src/test/resources/h2Database.properties");
76 public static void tearDownClass() {
84 public void setUp() throws Exception {
85 publishServlet = new PublishServlet();
90 public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
92 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[0], true);
93 publishServlet.doDelete(request, response);
94 verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE), argThat(notNullValue(String.class)));
95 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
99 public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
101 publishServlet.doDelete(request, response);
102 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
106 public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
109 when(request.getPathInfo()).thenReturn("/1/");
110 publishServlet.doDelete(request, response);
111 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
115 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
117 when(request.getPathInfo()).thenReturn("/122/fileName.txt");
118 publishServlet.doDelete(request, response);
119 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
123 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
125 when(request.getPathInfo()).thenReturn("/abc/fileName.txt");
126 publishServlet.doDelete(request, response);
127 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
132 public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
134 when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
135 setConditionsForPositiveSuccessFlow();
136 publishServlet.doDelete(request, response);
137 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
141 public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
143 setConditionsForPositiveSuccessFlow();
145 publishServlet.doPut(request, response);
146 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
150 public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
152 setConditionsForPositiveSuccessFlow();
154 publishServlet.doPost(request, response);
155 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
159 public void Given_Request_Is_HTTP_GET_And_Request_succeeds()
161 setConditionsForPositiveSuccessFlow();
163 publishServlet.doGet(request, response);
164 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
167 private void setConditionsForPositiveSuccessFlow() throws Exception {
168 FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
169 when(request.getPathInfo()).thenReturn("/1/fileName.txt");