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 org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.dmaap.dmf.mr.CambriaApiException;
33 import static org.junit.Assert.assertTrue;
34 import static org.mockito.Mockito.when;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
43 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
45 import java.io.IOException;
46 import java.util.Enumeration;
47 import org.onap.dmaap.dmf.mr.service.AdminService;
48 import com.att.nsa.configs.ConfigDbException;
49 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
50 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
51 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
54 @RunWith(PowerMockRunner.class)
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 {
149 when(dmaapContext.getRequest()).thenReturn(httpServReq);
150 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
151 when(headerNames.nextElement()).thenReturn("key");
152 when(httpServReq.getHeader("key")).thenReturn("value");
154 adminRestService.getBlacklist();
159 public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
161 PowerMockito.mockStatic(ServiceUtil.class);
162 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
163 PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(dmaaPContext);
164 when(dmaapContext.getRequest()).thenReturn(httpServReq);
165 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
166 when(headerNames.nextElement()).thenReturn("key");
167 when(httpServReq.getHeader("key")).thenReturn("value");
168 when(headerNames.hasMoreElements()).thenReturn(false);
170 adminRestService.getBlacklist();
172 catch (CambriaApiException e) {
179 public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
181 PowerMockito.mockStatic(ServiceUtil.class);
182 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
183 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(dmaaPContext);
184 when(dmaapContext.getRequest()).thenReturn(httpServReq);
185 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
186 when(headerNames.nextElement()).thenReturn("key");
187 when(httpServReq.getHeader("key")).thenReturn("value");
188 when(headerNames.hasMoreElements()).thenReturn(false);
190 adminRestService.getBlacklist();
192 catch (CambriaApiException e) {
199 public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
201 when(dmaapContext.getRequest()).thenReturn(httpServReq);
203 adminRestService.addToBlacklist("120.120.120.120");
208 public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
209 PowerMockito.mockStatic(ServiceUtil.class);
210 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
211 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
213 when(dmaapContext.getRequest()).thenReturn(httpServReq);
215 adminRestService.addToBlacklist("120.120.120.120");
216 }catch (CambriaApiException e) {
223 public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
225 PowerMockito.mockStatic(ServiceUtil.class);
226 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
227 PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
229 when(dmaapContext.getRequest()).thenReturn(httpServReq);
231 adminRestService.addToBlacklist("120.120.120.120");
232 }catch (CambriaApiException e) {
239 public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
241 when(dmaapContext.getRequest()).thenReturn(httpServReq);
243 adminRestService.removeFromBlacklist("120.120.120.120");
248 public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
250 PowerMockito.mockStatic(ServiceUtil.class);
251 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
252 PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
255 when(dmaapContext.getRequest()).thenReturn(httpServReq);
258 adminRestService.removeFromBlacklist("120.120.120.120");
259 }catch (CambriaApiException e) {
262 catch (AccessDeniedException e) {
265 catch (ConfigDbException e) {
272 public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
274 PowerMockito.mockStatic(ServiceUtil.class);
275 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
276 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
279 when(dmaapContext.getRequest()).thenReturn(httpServReq);
282 adminRestService.removeFromBlacklist("120.120.120.120");
283 }catch (CambriaApiException e) {
286 catch (AccessDeniedException e) {
289 catch (ConfigDbException e) {