package org.onap.dmaap.service;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
-import static org.mockito.Matchers.any;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.junit.After;
-import org.junit.Before;
+import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.onap.dmaap.dmf.mr.CambriaApiException;
import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
import org.onap.dmaap.dmf.mr.service.AdminService;
-import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PowerMockIgnore;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
-@RunWith(PowerMockRunner.class)
-@PowerMockIgnore("jdk.internal.reflect.*")
-@PrepareForTest({ ServiceUtil.class })
+@RunWith(MockitoJUnitRunner.class)
public class AdminRestServiceTest {
@InjectMocks
@Mock
HttpServletRequest httpServReq;
- @Mock
- private HttpServletResponse response;
@Mock
Enumeration headerNames;
- @Mock
- private DMaaPContext dmaaPContext;
- @Mock
- private ConfigurationReader configReader;
-
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
-
- }
-
- @After
- public void tearDown() throws Exception {
- }
@Test
public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException {
}
- @Test
- public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException {
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new IOException("error")).when(adminService).showConsumerCache(dmaaPContext);
- try {
- adminRestService.getConsumerCache();
- } catch (CambriaApiException e) {
- assertTrue(true);
- }
+ @Test(expected=CambriaApiException.class)
+ public void testGetConsumerCache_IOException() throws CambriaApiException, AccessDeniedException, IOException {
+ doThrow(new IOException("error")).when(adminService).showConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
+ adminRestService.getConsumerCache();
+ fail("Was expecting an exception to be thrown");
}
@Test
public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException {
adminRestService.dropConsumerCache();
-
}
-
- @Test
- public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new IOException("error")).when(adminService).dropConsumerCache(dmaaPContext);
- try {
- adminRestService.dropConsumerCache();
- }
- catch (CambriaApiException e) {
- assertTrue(true);
- }
-
+ @Test(expected=CambriaApiException.class)
+ public void testDropConsumerCach_IOException() throws CambriaApiException, AccessDeniedException ,IOException{
+ doThrow(new IOException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
+
+ adminRestService.dropConsumerCache();
+ fail("Was expecting an exception to be thrown");
}
- @Test
- public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException {
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(dmaaPContext);
- try {
+
+ @Test(expected=CambriaApiException.class)
+ public void testDropConsumerCache_AccessDeniedException() throws CambriaApiException, AccessDeniedException,IOException {
+ doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
+
adminRestService.dropConsumerCache();
- }
- catch (CambriaApiException e) {
- assertTrue(true);
- }
-
+ fail("Was expecting an exception to be thrown");
+ }
+
+ @Test(expected=CambriaApiException.class)
+ public void testDropConsumerCache_JSONException() throws CambriaApiException, AccessDeniedException,IOException {
+ doThrow(new JSONException("error")).when(adminService).dropConsumerCache(ArgumentMatchers.any(DMaaPContext.class));
+ adminRestService.dropConsumerCache();
+ fail("Was expecting an exception to be thrown");
}
@Test
- public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {
+ public void testGetBlacklist() throws CambriaApiException {
Vector headers = new Vector();
headers.add("Content-type");
Enumeration headerNms = headers.elements();
-
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
when(httpServReq.getHeaderNames()).thenReturn(headerNms);
- when(headerNames.nextElement()).thenReturn("key");
- when(httpServReq.getHeader("key")).thenReturn("value");
adminRestService.getBlacklist();
-
}
-
- @Test
- public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException {
-
- PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
+
+ @Test(expected=CambriaApiException.class)
+ public void testGetBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException, IOException {
+
when(httpServReq.getHeaderNames()).thenReturn(headerNames);
- when(headerNames.nextElement()).thenReturn("key");
- when(httpServReq.getHeader("key")).thenReturn("value");
+ doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(ArgumentMatchers.any(DMaaPContext.class));
- try {
adminRestService.getBlacklist();
- }
- catch (CambriaApiException e) {
- assertTrue(true);
- }
+ fail("Was expecting an exception to be thrown");
}
-
- @Test
- public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException {
-
- PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
+
+ @Test(expected=CambriaApiException.class)
+ public void testGetBlacklist_IOException() throws CambriaApiException, AccessDeniedException,IOException {
when(httpServReq.getHeaderNames()).thenReturn(headerNames);
- when(headerNames.nextElement()).thenReturn("key");
- when(httpServReq.getHeader("key")).thenReturn("value");
+ doThrow(new IOException("error")).when(adminService).getBlacklist(ArgumentMatchers.any(DMaaPContext.class));
- try {
adminRestService.getBlacklist();
- }
- catch (CambriaApiException e) {
- assertTrue(true);
- }
-
+ fail("Was expecting an exception to be thrown");
}
@Test
public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException {
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
-
adminRestService.addToBlacklist("120.120.120.120");
}
-
- @Test
- public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
- try {
- adminRestService.addToBlacklist("120.120.120.120");
- }catch (CambriaApiException e) {
- assertTrue(true);
- }
+ @Test(expected=CambriaApiException.class)
+ public void testAddToBlacklist_IOException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
+ doThrow(new IOException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+ adminRestService.addToBlacklist("120.120.120.120");
+ fail("Was expecting an exception to be thrown");
}
-
- @Test
- public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120");
-
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
- try {
+
+ @Test(expected=CambriaApiException.class)
+ public void testAddToBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
+ doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+
adminRestService.addToBlacklist("120.120.120.120");
- }catch (CambriaApiException e) {
- assertTrue(true);
- }
+ fail("Was expecting an exception to be thrown");
+ }
+
+ @Test(expected=CambriaApiException.class)
+ public void testAddToBlacklist_ConfigDbException() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException {
+ doThrow(new ConfigDbException("error")).when(adminService).addToBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+ adminRestService.addToBlacklist("120.120.120.120");
+ fail("Was expecting an exception to be thrown");
}
@Test
- public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
-
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException {
adminRestService.removeFromBlacklist("120.120.120.120");
}
-
- @Test
- public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
-
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
- try {
+ @Test(expected=CambriaApiException.class)
+ public void testRemoveFromBlacklist_ConfigDbException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
+ doThrow(new ConfigDbException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
adminRestService.removeFromBlacklist("120.120.120.120");
- }catch (CambriaApiException e) {
- assertTrue(true);
- }
- catch (AccessDeniedException e) {
- assertTrue(true);
- }
- catch (ConfigDbException e) {
- assertTrue(true);
- }
-
+ fail("Was expecting an exception to be thrown");
}
-
- @Test
- public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
-
- PowerMockito.mockStatic(ServiceUtil.class);
- PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext);
- PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120");
-
- when(dmaapContext.getRequest()).thenReturn(httpServReq);
- try {
+ @Test(expected=CambriaApiException.class)
+ public void testRemoveFromBlacklist_IOException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{
+ doThrow(new IOException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
adminRestService.removeFromBlacklist("120.120.120.120");
- }catch (CambriaApiException e) {
- assertTrue(true);
- }
- catch (AccessDeniedException e) {
- assertTrue(true);
- }
- catch (ConfigDbException e) {
- assertTrue(true);
- }
+ fail("Was expecting an exception to be thrown");
+ }
+
+ @Test(expected=CambriaApiException.class)
+ public void testRemoveFromBlacklist_AccessDeniedException() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException {
+ doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+ adminRestService.removeFromBlacklist("120.120.120.120");
+ fail("Was expecting an exception to be thrown");
}
}
\ No newline at end of file