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