cee09e5ce90fe676d80079383d05143eda0ced86
[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 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;
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 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;
47
48 /**
49  * Created by ezcoxem on 21/08/2018.
50  */
51
52 @RunWith(PowerMockRunner.class)
53 public class PublishServletTest {
54     private PublishServlet publishServlet;
55
56     @Mock
57     private HttpServletRequest request;
58
59     @Mock
60     private HttpServletResponse response;
61
62     private static EntityManagerFactory emf;
63     private static EntityManager em;
64     private DB db;
65
66     @BeforeClass
67     public static void init() {
68         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
69         em = emf.createEntityManager();
70         System.setProperty(
71                 "org.onap.dmaap.datarouter.provserver.properties",
72                 "src/test/resources/h2Database.properties");
73     }
74
75     @AfterClass
76     public static void tearDownClass() {
77         em.clear();
78         em.close();
79         emf.close();
80     }
81
82
83     @Before
84     public void setUp() throws Exception {
85         publishServlet = new PublishServlet();
86         db = new DB();
87     }
88
89     @Test
90     public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
91             throws Exception {
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);
96     }
97
98     @Test
99     public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
100             throws Exception {
101         publishServlet.doDelete(request, response);
102         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
103     }
104
105     @Test
106     public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
107             throws Exception {
108
109         when(request.getPathInfo()).thenReturn("/1/");
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_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
116             throws Exception {
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)));
120     }
121
122     @Test
123     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
124             throws Exception {
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)));
128     }
129
130
131     @Test
132     public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
133             throws Exception {
134         when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
135         setConditionsForPositiveSuccessFlow();
136         publishServlet.doDelete(request, response);
137         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
138     }
139
140     @Test
141     public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
142             throws Exception {
143         setConditionsForPositiveSuccessFlow();
144
145         publishServlet.doPut(request, response);
146         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
147     }
148
149     @Test
150     public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
151             throws Exception {
152         setConditionsForPositiveSuccessFlow();
153
154         publishServlet.doPost(request, response);
155         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
156     }
157
158     @Test
159     public void Given_Request_Is_HTTP_GET_And_Request_succeeds()
160             throws Exception {
161         setConditionsForPositiveSuccessFlow();
162
163         publishServlet.doGet(request, response);
164         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
165     }
166
167     private void setConditionsForPositiveSuccessFlow() throws Exception {
168         FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
169         when(request.getPathInfo()).thenReturn("/1/fileName.txt");
170     }
171
172
173
174 }