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.fail;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.when;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Vector;
31 import javax.servlet.http.HttpServletRequest;
33 import org.json.JSONException;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.dmaap.dmf.mr.CambriaApiException;
41 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
42 import org.onap.dmaap.dmf.mr.service.AdminService;
44 import com.att.nsa.configs.ConfigDbException;
45 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
47 @RunWith(MockitoJUnitRunner.class)
48 public class AdminRestServiceTest {
51 AdminRestService adminRestService;
54 AdminService adminService;
57 DMaaPContext dmaapContext;
60 HttpServletRequest httpServReq;
63 Enumeration headerNames;
66 public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
67 adminRestService.getConsumerCache();
71 @Test(expected=CambriaApiException.class)
72 public void testGetConsumerCache_IOException() throws CambriaApiException, AccessDeniedException, IOException {
73 doThrow(new IOException("error")).when(adminService).showConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
75 adminRestService.getConsumerCache();
76 fail("Was expecting an exception to be thrown");
80 public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
81 adminRestService.dropConsumerCache();
84 @Test(expected=CambriaApiException.class)
85 public void testDropConsumerCach_IOException() throws CambriaApiException, AccessDeniedException ,IOException{
86 doThrow(new IOException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
88 adminRestService.dropConsumerCache();
89 fail("Was expecting an exception to be thrown");
92 @Test(expected=CambriaApiException.class)
93 public void testDropConsumerCache_AccessDeniedException() throws CambriaApiException, AccessDeniedException,IOException {
94 doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
96 adminRestService.dropConsumerCache();
97 fail("Was expecting an exception to be thrown");
100 @Test(expected=CambriaApiException.class)
101 public void testDropConsumerCache_JSONException() throws CambriaApiException, AccessDeniedException,IOException {
102 doThrow(new JSONException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
104 adminRestService.dropConsumerCache();
105 fail("Was expecting an exception to be thrown");
109 public void testGetBlacklist() throws CambriaApiException {
110 Vector headers = new Vector();
111 headers.add("Content-type");
112 Enumeration headerNms = headers.elements();
113 when(httpServReq.getHeaderNames()).thenReturn(headerNms);
115 adminRestService.getBlacklist();
118 @Test(expected=CambriaApiException.class)
119 public void testGetBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException, IOException {
121 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
122 doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(ArgumentMatchers.any(DMaaPContext.class));
124 adminRestService.getBlacklist();
125 fail("Was expecting an exception to be thrown");
128 @Test(expected=CambriaApiException.class)
129 public void testGetBlacklist_IOException() throws CambriaApiException, AccessDeniedException,IOException {
130 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
131 doThrow(new IOException("error")).when(adminService).getBlacklist(ArgumentMatchers.any(DMaaPContext.class));
133 adminRestService.getBlacklist();
134 fail("Was expecting an exception to be thrown");
138 public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
140 adminRestService.addToBlacklist("120.120.120.120");
144 @Test(expected=CambriaApiException.class)
145 public void testAddToBlacklist_IOException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
146 doThrow(new IOException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
148 adminRestService.addToBlacklist("120.120.120.120");
149 fail("Was expecting an exception to be thrown");
152 @Test(expected=CambriaApiException.class)
153 public void testAddToBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
154 doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
156 adminRestService.addToBlacklist("120.120.120.120");
157 fail("Was expecting an exception to be thrown");
160 @Test(expected=CambriaApiException.class)
161 public void testAddToBlacklist_ConfigDbException() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
162 doThrow(new ConfigDbException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
164 adminRestService.addToBlacklist("120.120.120.120");
165 fail("Was expecting an exception to be thrown");
169 public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException {
171 adminRestService.removeFromBlacklist("120.120.120.120");
175 @Test(expected=CambriaApiException.class)
176 public void testRemoveFromBlacklist_ConfigDbException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
177 doThrow(new ConfigDbException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
179 adminRestService.removeFromBlacklist("120.120.120.120");
180 fail("Was expecting an exception to be thrown");
183 @Test(expected=CambriaApiException.class)
184 public void testRemoveFromBlacklist_IOException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
185 doThrow(new IOException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
187 adminRestService.removeFromBlacklist("120.120.120.120");
188 fail("Was expecting an exception to be thrown");
191 @Test(expected=CambriaApiException.class)
192 public void testRemoveFromBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
193 doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
195 adminRestService.removeFromBlacklist("120.120.120.120");
196 fail("Was expecting an exception to be thrown");