Refactor Prov DB handling
[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.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
71     private ListAppender<ILoggingEvent> listAppender;
72
73     @BeforeClass
74     public static void init() {
75         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
76         em = emf.createEntityManager();
77         System.setProperty(
78                 "org.onap.dmaap.datarouter.provserver.properties",
79                 "src/test/resources/h2Database.properties");
80     }
81
82     @AfterClass
83     public static void tearDownClass() throws FileNotFoundException {
84         em.clear();
85         em.close();
86         emf.close();
87     }
88
89
90     @Before
91     public void setUp() throws Exception {
92         listAppender = setTestLogger(PublishServlet.class);
93         publishServlet = new PublishServlet();
94         ProvDbUtils.getInstance().initProvDB();
95     }
96
97     @Test
98     public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
99             throws Exception {
100         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[0], true);
101         publishServlet.doDelete(request, response);
102         verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE), argThat(notNullValue(String.class)));
103         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
104         verifyEnteringExitCalled(listAppender);
105     }
106
107     @Test
108     public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
109             throws Exception {
110         publishServlet.doDelete(request, response);
111         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
112     }
113
114     @Test
115     public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
116             throws Exception {
117
118         when(request.getPathInfo()).thenReturn("/1/");
119         publishServlet.doDelete(request, response);
120         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
121     }
122
123     @Test
124     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
125             throws Exception {
126         when(request.getPathInfo()).thenReturn("/122/fileName.txt");
127         publishServlet.doDelete(request, response);
128         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
129     }
130
131     @Test
132     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
133             throws Exception {
134         when(request.getPathInfo()).thenReturn("/abc/fileName.txt");
135         publishServlet.doDelete(request, response);
136         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
137     }
138
139
140     @Test
141     public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
142             throws Exception {
143         when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
144         setConditionsForPositiveSuccessFlow();
145         publishServlet.doDelete(request, response);
146         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
147         verifyEnteringExitCalled(listAppender);
148     }
149
150     @Test
151     public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
152             throws Exception {
153         setConditionsForPositiveSuccessFlow();
154
155         publishServlet.doPut(request, response);
156         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
157         verifyEnteringExitCalled(listAppender);
158     }
159
160     @Test
161     public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
162             throws Exception {
163         setConditionsForPositiveSuccessFlow();
164
165         publishServlet.doPost(request, response);
166         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
167         verifyEnteringExitCalled(listAppender);
168     }
169
170     @Test
171     public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_is_empty()
172             throws Exception {
173         setConditionsForPositiveSuccessFlow();
174         mockStatic(InetAddress.class);
175         publishServlet.doGet(request, response);
176         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
177         verifyEnteringExitCalled(listAppender);
178         assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("RequestId"));
179         assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("InvocationId"));
180     }
181
182     @Test
183     public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_Is_Not_Empty()
184             throws Exception {
185         setConditionsForPositiveSuccessFlow();
186         when(request.getHeader("X-ONAP-RequestID")).thenReturn("123");
187         when(request.getHeader("X-InvocationID")).thenReturn("456");
188         publishServlet.doGet(request, response);
189         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
190         verifyEnteringExitCalled(listAppender);
191         assertEquals("123", listAppender.list.get(0).getMDCPropertyMap().get("RequestId"));
192         assertEquals("456", listAppender.list.get(0).getMDCPropertyMap().get("InvocationId"));
193     }
194
195     private void setConditionsForPositiveSuccessFlow() throws Exception {
196         FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
197         when(request.getPathInfo()).thenReturn("/1/fileName.txt");
198     }
199
200
201
202 }