update the package name
[dmaap/messagerouter/messageservice.git] / src / test / java / com / att / nsa / dmaap / service / AdminRestServiceTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy Engine\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package com.att.nsa.dmaap.service;\r
22 \r
23 import org.junit.After;\r
24 import org.junit.Before;\r
25 import org.junit.Test;\r
26 import org.junit.runner.RunWith;\r
27 \r
28 import org.mockito.InjectMocks;\r
29 import org.mockito.Mock;\r
30 import org.mockito.MockitoAnnotations;\r
31 import org.onap.dmaap.dmf.mr.CambriaApiException;\r
32 \r
33 import static org.junit.Assert.assertTrue;\r
34 import static org.mockito.Mockito.when;\r
35 \r
36 import org.powermock.api.mockito.PowerMockito;\r
37 import org.powermock.core.classloader.annotations.PrepareForTest;\r
38 import org.powermock.modules.junit4.PowerMockRunner;\r
39 \r
40 import javax.servlet.http.HttpServletRequest;\r
41 import javax.servlet.http.HttpServletResponse;\r
42 \r
43 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;\r
44 \r
45 import java.io.IOException;\r
46 import java.util.Enumeration;\r
47 import org.onap.dmaap.dmf.mr.service.AdminService;\r
48 import com.att.nsa.configs.ConfigDbException;\r
49 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
50 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;\r
51 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;\r
52 import org.powermock.core.classloader.annotations.PrepareForTest;\r
53 \r
54 @RunWith(PowerMockRunner.class)\r
55 @PrepareForTest({ ServiceUtil.class })\r
56 public class AdminRestServiceTest {\r
57 \r
58         @InjectMocks\r
59         AdminRestService adminRestService;\r
60 \r
61         @Mock\r
62         AdminService adminService;\r
63 \r
64         @Mock\r
65         DMaaPContext dmaapContext;\r
66 \r
67         @Mock\r
68         HttpServletRequest httpServReq;\r
69         @Mock\r
70         private HttpServletResponse response;\r
71 \r
72         @Mock\r
73         Enumeration headerNames;\r
74         @Mock\r
75         private DMaaPContext dmaaPContext;\r
76         @Mock\r
77         private ConfigurationReader configReader;\r
78 \r
79         @Before\r
80         public void setUp() throws Exception {\r
81                 MockitoAnnotations.initMocks(this);\r
82 \r
83         }\r
84 \r
85         @After\r
86         public void tearDown() throws Exception {\r
87         }\r
88 \r
89         @Test\r
90         public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {\r
91                 adminRestService.getConsumerCache();\r
92 \r
93         }\r
94 \r
95         @Test\r
96         public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException {\r
97 \r
98                 PowerMockito.mockStatic(ServiceUtil.class);\r
99                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
100                 PowerMockito.doThrow(new IOException("error")).when(adminService).showConsumerCache(dmaaPContext);\r
101                 try {\r
102                         adminRestService.getConsumerCache();\r
103                 } catch (CambriaApiException e) {\r
104                         assertTrue(true);\r
105                 }\r
106 \r
107         }\r
108 \r
109         @Test\r
110         public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {\r
111                 adminRestService.dropConsumerCache();\r
112 \r
113         }\r
114         \r
115         @Test\r
116         public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{\r
117                 \r
118                 PowerMockito.mockStatic(ServiceUtil.class);\r
119                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
120                 PowerMockito.doThrow(new IOException("error")).when(adminService).dropConsumerCache(dmaaPContext);\r
121                 try {\r
122                 adminRestService.dropConsumerCache();\r
123                 }\r
124                 catch (CambriaApiException e) {\r
125                         assertTrue(true);\r
126                 }\r
127                 \r
128 \r
129         }\r
130         @Test\r
131         public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException {\r
132                 \r
133                 PowerMockito.mockStatic(ServiceUtil.class);\r
134                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
135                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(dmaaPContext);\r
136                 try {\r
137                 adminRestService.dropConsumerCache();\r
138                 }\r
139                 catch (CambriaApiException e) {\r
140                         assertTrue(true);\r
141                 }\r
142                 \r
143 \r
144         }\r
145 \r
146         @Test\r
147         public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {\r
148 \r
149                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
150                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);\r
151                 when(headerNames.nextElement()).thenReturn("key");\r
152                 when(httpServReq.getHeader("key")).thenReturn("value");\r
153 \r
154                 adminRestService.getBlacklist();\r
155 \r
156         }\r
157         \r
158         //@Test\r
159         public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {\r
160                 \r
161                 PowerMockito.mockStatic(ServiceUtil.class);\r
162                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
163                 PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(dmaaPContext);\r
164                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
165                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);\r
166                 when(headerNames.nextElement()).thenReturn("key");\r
167                 when(httpServReq.getHeader("key")).thenReturn("value");\r
168                 when(headerNames.hasMoreElements()).thenReturn(false);\r
169                 try {\r
170                 adminRestService.getBlacklist();\r
171                 }\r
172                 catch (CambriaApiException e) {\r
173                         assertTrue(true);\r
174                 }\r
175 \r
176         }\r
177         \r
178         ////@Test\r
179         public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {\r
180                 \r
181                 PowerMockito.mockStatic(ServiceUtil.class);\r
182                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
183                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(dmaaPContext);\r
184                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
185                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);\r
186                 when(headerNames.nextElement()).thenReturn("key");\r
187                 when(httpServReq.getHeader("key")).thenReturn("value");\r
188                 when(headerNames.hasMoreElements()).thenReturn(false);\r
189                 try {\r
190                 adminRestService.getBlacklist();\r
191                 }\r
192                 catch (CambriaApiException e) {\r
193                         assertTrue(true);\r
194                 }\r
195 \r
196         }\r
197 \r
198         @Test\r
199         public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {\r
200 \r
201                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
202 \r
203                 adminRestService.addToBlacklist("120.120.120.120");\r
204 \r
205         }\r
206         \r
207         @Test\r
208         public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {\r
209                 PowerMockito.mockStatic(ServiceUtil.class);\r
210                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
211                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");\r
212 \r
213                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
214                 try {\r
215                 adminRestService.addToBlacklist("120.120.120.120");\r
216                 }catch (CambriaApiException e) {\r
217                         assertTrue(true);\r
218                 }\r
219 \r
220         }\r
221         \r
222         @Test\r
223         public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {\r
224                 \r
225                 PowerMockito.mockStatic(ServiceUtil.class);\r
226                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
227                 PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");\r
228 \r
229                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
230                 try {\r
231                 adminRestService.addToBlacklist("120.120.120.120");\r
232                 }catch (CambriaApiException e) {\r
233                         assertTrue(true);\r
234                 }\r
235 \r
236         }\r
237 \r
238         @Test\r
239         public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {\r
240 \r
241                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
242 \r
243                 adminRestService.removeFromBlacklist("120.120.120.120");\r
244 \r
245         }\r
246         \r
247         @Test\r
248         public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{\r
249                 \r
250                 PowerMockito.mockStatic(ServiceUtil.class);\r
251                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
252                 PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");\r
253 \r
254 \r
255                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
256                 try {\r
257 \r
258                 adminRestService.removeFromBlacklist("120.120.120.120");\r
259                 }catch (CambriaApiException e) {\r
260                         assertTrue(true);\r
261                 }\r
262                 catch (AccessDeniedException e) {\r
263                         assertTrue(true);\r
264                 }\r
265                 catch (ConfigDbException e) {\r
266                         assertTrue(true);\r
267                 }\r
268 \r
269         }\r
270         \r
271         @Test\r
272         public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {\r
273                 \r
274                 PowerMockito.mockStatic(ServiceUtil.class);\r
275                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);\r
276                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");\r
277 \r
278 \r
279                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
280                 try {\r
281 \r
282                 adminRestService.removeFromBlacklist("120.120.120.120");\r
283                 }catch (CambriaApiException e) {\r
284                         assertTrue(true);\r
285                 }\r
286                 catch (AccessDeniedException e) {\r
287                         assertTrue(true);\r
288                 }\r
289                 catch (ConfigDbException e) {\r
290                         assertTrue(true);\r
291                 }\r
292 \r
293         }\r
294 \r
295 }