8ba69f3a89b71f2f68c204165ae116b9be1f6ee3
[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 static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.Matchers.any;
26
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Vector;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.dmaap.dmf.mr.CambriaApiException;
42 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
43 import org.onap.dmaap.dmf.mr.service.AdminService;
44 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PowerMockIgnore;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 import com.att.nsa.configs.ConfigDbException;
51 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
52
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore("jdk.internal.reflect.*")
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                 Vector headers = new Vector();
149                 headers.add("Content-type");
150                 Enumeration headerNms = headers.elements();
151
152                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
153                 when(httpServReq.getHeaderNames()).thenReturn(headerNms);
154                 when(headerNames.nextElement()).thenReturn("key");
155                 when(httpServReq.getHeader("key")).thenReturn("value");
156
157                 adminRestService.getBlacklist();
158
159         }
160         
161         @Test
162         public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
163                 
164                 PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
165                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
166                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
167                 when(headerNames.nextElement()).thenReturn("key");
168                 when(httpServReq.getHeader("key")).thenReturn("value");
169
170                 try {
171                 adminRestService.getBlacklist();
172                 }
173                 catch (CambriaApiException e) {
174                         assertTrue(true);
175                 }
176         }
177         
178         @Test
179         public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
180                 
181                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
182                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
183                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
184                 when(headerNames.nextElement()).thenReturn("key");
185                 when(httpServReq.getHeader("key")).thenReturn("value");
186
187                 try {
188                 adminRestService.getBlacklist();
189                 }
190                 catch (CambriaApiException e) {
191                         assertTrue(true);
192                 }
193
194         }
195
196         @Test
197         public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
198
199                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
200
201                 adminRestService.addToBlacklist("120.120.120.120");
202
203         }
204         
205         @Test
206         public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
207                 PowerMockito.mockStatic(ServiceUtil.class);
208                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
209                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
210
211                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
212                 try {
213                 adminRestService.addToBlacklist("120.120.120.120");
214                 }catch (CambriaApiException e) {
215                         assertTrue(true);
216                 }
217
218         }
219         
220         @Test
221         public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
222                 
223                 PowerMockito.mockStatic(ServiceUtil.class);
224                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
225                 PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
226
227                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
228                 try {
229                 adminRestService.addToBlacklist("120.120.120.120");
230                 }catch (CambriaApiException e) {
231                         assertTrue(true);
232                 }
233
234         }
235
236         @Test
237         public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
238
239                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
240
241                 adminRestService.removeFromBlacklist("120.120.120.120");
242
243         }
244         
245         @Test
246         public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
247                 
248                 PowerMockito.mockStatic(ServiceUtil.class);
249                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
250                 PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
251
252
253                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
254                 try {
255
256                 adminRestService.removeFromBlacklist("120.120.120.120");
257                 }catch (CambriaApiException e) {
258                         assertTrue(true);
259                 }
260                 catch (AccessDeniedException e) {
261                         assertTrue(true);
262                 }
263                 catch (ConfigDbException e) {
264                         assertTrue(true);
265                 }
266
267         }
268         
269         @Test
270         public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
271                 
272                 PowerMockito.mockStatic(ServiceUtil.class);
273                 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
274                 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
275
276
277                 when(dmaapContext.getRequest()).thenReturn(httpServReq);
278                 try {
279
280                 adminRestService.removeFromBlacklist("120.120.120.120");
281                 }catch (CambriaApiException e) {
282                         assertTrue(true);
283                 }
284                 catch (AccessDeniedException e) {
285                         assertTrue(true);
286                 }
287                 catch (ConfigDbException e) {
288                         assertTrue(true);
289                 }
290
291         }
292
293 }