2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package com.att.nsa.dmaap.service;
\r
23 import org.junit.After;
\r
24 import org.junit.Before;
\r
25 import org.junit.Test;
\r
26 import org.junit.runner.RunWith;
\r
28 import org.mockito.InjectMocks;
\r
29 import org.mockito.Mock;
\r
30 import org.mockito.MockitoAnnotations;
\r
31 import org.onap.dmaap.dmf.mr.CambriaApiException;
\r
33 import static org.junit.Assert.assertTrue;
\r
34 import static org.mockito.Mockito.when;
\r
36 import org.powermock.api.mockito.PowerMockito;
\r
37 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
38 import org.powermock.modules.junit4.PowerMockRunner;
\r
40 import javax.servlet.http.HttpServletRequest;
\r
41 import javax.servlet.http.HttpServletResponse;
\r
43 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
\r
45 import java.io.IOException;
\r
46 import java.util.Enumeration;
\r
47 import org.onap.dmaap.dmf.mr.service.AdminService;
\r
48 import com.att.nsa.configs.ConfigDbException;
\r
49 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
\r
50 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
\r
51 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
\r
52 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
54 @RunWith(PowerMockRunner.class)
\r
55 @PrepareForTest({ ServiceUtil.class })
\r
56 public class AdminRestServiceTest {
\r
59 AdminRestService adminRestService;
\r
62 AdminService adminService;
\r
65 DMaaPContext dmaapContext;
\r
68 HttpServletRequest httpServReq;
\r
70 private HttpServletResponse response;
\r
73 Enumeration headerNames;
\r
75 private DMaaPContext dmaaPContext;
\r
77 private ConfigurationReader configReader;
\r
80 public void setUp() throws Exception {
\r
81 MockitoAnnotations.initMocks(this);
\r
86 public void tearDown() throws Exception {
\r
90 public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
\r
91 adminRestService.getConsumerCache();
\r
96 public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException {
\r
98 PowerMockito.mockStatic(ServiceUtil.class);
\r
99 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
100 PowerMockito.doThrow(new IOException("error")).when(adminService).showConsumerCache(dmaaPContext);
\r
102 adminRestService.getConsumerCache();
\r
103 } catch (CambriaApiException e) {
\r
110 public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
\r
111 adminRestService.dropConsumerCache();
\r
116 public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{
\r
118 PowerMockito.mockStatic(ServiceUtil.class);
\r
119 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
120 PowerMockito.doThrow(new IOException("error")).when(adminService).dropConsumerCache(dmaaPContext);
\r
122 adminRestService.dropConsumerCache();
\r
124 catch (CambriaApiException e) {
\r
131 public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException {
\r
133 PowerMockito.mockStatic(ServiceUtil.class);
\r
134 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
135 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(dmaaPContext);
\r
137 adminRestService.dropConsumerCache();
\r
139 catch (CambriaApiException e) {
\r
147 public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {
\r
149 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
150 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
\r
151 when(headerNames.nextElement()).thenReturn("key");
\r
152 when(httpServReq.getHeader("key")).thenReturn("value");
\r
154 adminRestService.getBlacklist();
\r
159 public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
\r
161 PowerMockito.mockStatic(ServiceUtil.class);
\r
162 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
163 PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(dmaaPContext);
\r
164 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
165 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
\r
166 when(headerNames.nextElement()).thenReturn("key");
\r
167 when(httpServReq.getHeader("key")).thenReturn("value");
\r
168 when(headerNames.hasMoreElements()).thenReturn(false);
\r
170 adminRestService.getBlacklist();
\r
172 catch (CambriaApiException e) {
\r
179 public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
\r
181 PowerMockito.mockStatic(ServiceUtil.class);
\r
182 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
183 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(dmaaPContext);
\r
184 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
185 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
\r
186 when(headerNames.nextElement()).thenReturn("key");
\r
187 when(httpServReq.getHeader("key")).thenReturn("value");
\r
188 when(headerNames.hasMoreElements()).thenReturn(false);
\r
190 adminRestService.getBlacklist();
\r
192 catch (CambriaApiException e) {
\r
199 public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
\r
201 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
203 adminRestService.addToBlacklist("120.120.120.120");
\r
208 public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
\r
209 PowerMockito.mockStatic(ServiceUtil.class);
\r
210 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
211 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
\r
213 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
215 adminRestService.addToBlacklist("120.120.120.120");
\r
216 }catch (CambriaApiException e) {
\r
223 public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
\r
225 PowerMockito.mockStatic(ServiceUtil.class);
\r
226 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
227 PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
\r
229 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
231 adminRestService.addToBlacklist("120.120.120.120");
\r
232 }catch (CambriaApiException e) {
\r
239 public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
\r
241 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
243 adminRestService.removeFromBlacklist("120.120.120.120");
\r
248 public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
\r
250 PowerMockito.mockStatic(ServiceUtil.class);
\r
251 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
252 PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
\r
255 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
258 adminRestService.removeFromBlacklist("120.120.120.120");
\r
259 }catch (CambriaApiException e) {
\r
262 catch (AccessDeniedException e) {
\r
265 catch (ConfigDbException e) {
\r
272 public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
\r
274 PowerMockito.mockStatic(ServiceUtil.class);
\r
275 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
\r
276 PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
\r
279 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
282 adminRestService.removeFromBlacklist("120.120.120.120");
\r
283 }catch (CambriaApiException e) {
\r
286 catch (AccessDeniedException e) {
\r
289 catch (ConfigDbException e) {
\r