DMAAP-MR Unit test improvements
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / service / AdminRestServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21  package org.onap.dmaap.service;
22
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.when;
26
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Vector;
30
31 import javax.servlet.http.HttpServletRequest;
32
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;
43
44 import com.att.nsa.configs.ConfigDbException;
45 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class AdminRestServiceTest {
49
50         @InjectMocks
51         AdminRestService adminRestService;
52
53         @Mock
54         AdminService adminService;
55
56         @Mock
57         DMaaPContext dmaapContext;
58
59         @Mock
60         HttpServletRequest httpServReq;
61
62         @Mock
63         Enumeration headerNames;
64
65         @Test
66         public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
67                 adminRestService.getConsumerCache();
68
69         }
70
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));
74
75                 adminRestService.getConsumerCache();
76                 fail("Was expecting an exception to be thrown");
77         }
78
79         @Test
80         public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
81                 adminRestService.dropConsumerCache();
82         }
83
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));
87
88                 adminRestService.dropConsumerCache();
89                 fail("Was expecting an exception to be thrown");
90         }
91
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));
95
96                 adminRestService.dropConsumerCache();
97                 fail("Was expecting an exception to be thrown");
98         }
99
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));
103
104                 adminRestService.dropConsumerCache();
105                 fail("Was expecting an exception to be thrown");
106         }
107
108         @Test
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);
114
115                 adminRestService.getBlacklist();
116         }
117
118         @Test(expected=CambriaApiException.class)
119         public void testGetBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException, IOException {
120
121                 when(httpServReq.getHeaderNames()).thenReturn(headerNames);
122                 doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(ArgumentMatchers.any(DMaaPContext.class));
123
124                 adminRestService.getBlacklist();
125                 fail("Was expecting an exception to be thrown");
126         }
127
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));
132
133                 adminRestService.getBlacklist();
134                 fail("Was expecting an exception to be thrown");
135         }
136
137         @Test
138         public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
139
140                 adminRestService.addToBlacklist("120.120.120.120");
141
142         }
143
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));
147
148                 adminRestService.addToBlacklist("120.120.120.120");
149                 fail("Was expecting an exception to be thrown");
150         }
151
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));
155
156                 adminRestService.addToBlacklist("120.120.120.120");
157                 fail("Was expecting an exception to be thrown");
158         }
159
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));
163
164                 adminRestService.addToBlacklist("120.120.120.120");
165                 fail("Was expecting an exception to be thrown");
166         }
167
168         @Test
169         public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException {
170
171                 adminRestService.removeFromBlacklist("120.120.120.120");
172
173         }
174
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));
178
179                 adminRestService.removeFromBlacklist("120.120.120.120");
180                 fail("Was expecting an exception to be thrown");
181         }
182
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));
186
187                 adminRestService.removeFromBlacklist("120.120.120.120");
188                 fail("Was expecting an exception to be thrown");
189         }
190
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));
194
195                 adminRestService.removeFromBlacklist("120.120.120.120");
196                 fail("Was expecting an exception to be thrown");
197         }
198
199 }