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