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.PowerMockIgnore;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
50 import com.att.nsa.configs.ConfigDbException;
51 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore("jdk.internal.reflect.*")
55 @PrepareForTest({ ServiceUtil.class })
56 public class AdminRestServiceTest {
59 AdminRestService adminRestService;
62 AdminService adminService;
65 DMaaPContext dmaapContext;
68 HttpServletRequest httpServReq;
70 private HttpServletResponse response;
73 Enumeration headerNames;
75 private DMaaPContext dmaaPContext;
77 private ConfigurationReader configReader;
80 public void setUp() throws Exception {
81 MockitoAnnotations.initMocks(this);
86 public void tearDown() throws Exception {
90 public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
91 adminRestService.getConsumerCache();
96 public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException {
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);
102 adminRestService.getConsumerCache();
103 } catch (CambriaApiException e) {
110 public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
111 adminRestService.dropConsumerCache();
116 public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{
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);
122 adminRestService.dropConsumerCache();
124 catch (CambriaApiException e) {
131 public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException {
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);
137 adminRestService.dropConsumerCache();
139 catch (CambriaApiException e) {
147 public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {
148 Vector headers = new Vector();
149 headers.add("Content-type");
150 Enumeration headerNms = headers.elements();
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");
157 adminRestService.getBlacklist();
162 public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
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");
171 adminRestService.getBlacklist();
173 catch (CambriaApiException e) {
179 public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
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");
188 adminRestService.getBlacklist();
190 catch (CambriaApiException e) {
197 public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
199 when(dmaapContext.getRequest()).thenReturn(httpServReq);
201 adminRestService.addToBlacklist("120.120.120.120");
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");
211 when(dmaapContext.getRequest()).thenReturn(httpServReq);
213 adminRestService.addToBlacklist("120.120.120.120");
214 }catch (CambriaApiException e) {
221 public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
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");
227 when(dmaapContext.getRequest()).thenReturn(httpServReq);
229 adminRestService.addToBlacklist("120.120.120.120");
230 }catch (CambriaApiException e) {
237 public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
239 when(dmaapContext.getRequest()).thenReturn(httpServReq);
241 adminRestService.removeFromBlacklist("120.120.120.120");
246 public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
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");
253 when(dmaapContext.getRequest()).thenReturn(httpServReq);
256 adminRestService.removeFromBlacklist("120.120.120.120");
257 }catch (CambriaApiException e) {
260 catch (AccessDeniedException e) {
263 catch (ConfigDbException e) {
270 public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
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");
277 when(dmaapContext.getRequest()).thenReturn(httpServReq);
280 adminRestService.removeFromBlacklist("120.120.120.120");
281 }catch (CambriaApiException e) {
284 catch (AccessDeniedException e) {
287 catch (ConfigDbException e) {