2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.service;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.Matchers.any;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Vector;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
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;
49 import com.att.nsa.configs.ConfigDbException;
50 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({ ServiceUtil.class })
54 public class AdminRestServiceTest {
57 AdminRestService adminRestService;
60 AdminService adminService;
63 DMaaPContext dmaapContext;
66 HttpServletRequest httpServReq;
68 private HttpServletResponse response;
71 Enumeration headerNames;
73 private DMaaPContext dmaaPContext;
75 private ConfigurationReader configReader;
78 public void setUp() throws Exception {
79 MockitoAnnotations.initMocks(this);
84 public void tearDown() throws Exception {
88 public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
89 adminRestService.getConsumerCache();
94 public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException {
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);
100 adminRestService.getConsumerCache();
101 } catch (CambriaApiException e) {
108 public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
109 adminRestService.dropConsumerCache();
114 public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{
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);
120 adminRestService.dropConsumerCache();
122 catch (CambriaApiException e) {
129 public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException {
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);
135 adminRestService.dropConsumerCache();
137 catch (CambriaApiException e) {
145 public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {
146 Vector headers = new Vector();
147 headers.add("Content-type");
148 Enumeration headerNms = headers.elements();
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");
155 adminRestService.getBlacklist();
160 public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
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");
169 adminRestService.getBlacklist();
171 catch (CambriaApiException e) {
177 public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
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");
186 adminRestService.getBlacklist();
188 catch (CambriaApiException e) {
195 public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
197 when(dmaapContext.getRequest()).thenReturn(httpServReq);
199 adminRestService.addToBlacklist("120.120.120.120");
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");
209 when(dmaapContext.getRequest()).thenReturn(httpServReq);
211 adminRestService.addToBlacklist("120.120.120.120");
212 }catch (CambriaApiException e) {
219 public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
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");
225 when(dmaapContext.getRequest()).thenReturn(httpServReq);
227 adminRestService.addToBlacklist("120.120.120.120");
228 }catch (CambriaApiException e) {
235 public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
237 when(dmaapContext.getRequest()).thenReturn(httpServReq);
239 adminRestService.removeFromBlacklist("120.120.120.120");
244 public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
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");
251 when(dmaapContext.getRequest()).thenReturn(httpServReq);
254 adminRestService.removeFromBlacklist("120.120.120.120");
255 }catch (CambriaApiException e) {
258 catch (AccessDeniedException e) {
261 catch (ConfigDbException e) {
268 public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
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");
275 when(dmaapContext.getRequest()).thenReturn(httpServReq);
278 adminRestService.removeFromBlacklist("120.120.120.120");
279 }catch (CambriaApiException e) {
282 catch (AccessDeniedException e) {
285 catch (ConfigDbException e) {