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