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