From 63548522dfacfdc11826facecf6d2a06a95f8295 Mon Sep 17 00:00:00 2001 From: Sunil Unnava Date: Tue, 6 Mar 2018 19:38:54 -0500 Subject: testcases for code coverage Issue-ID: DMAAP-271 Change-Id: Ifacdeeaca0176bf3a5e509cb0d27e7ec1e89bd8c Signed-off-by: Sunil Unnava --- .../nsa/dmaap/service/AdminRestServiceTest.java | 185 ++++++++++++++++++++- .../att/nsa/dmaap/service/MMRestServiceTest.java | 94 +++++++++++ 2 files changed, 277 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java index d9b39dc..e9a14c7 100644 --- a/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java @@ -29,22 +29,32 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import com.att.nsa.cambria.CambriaApiException; + +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import com.att.nsa.cambria.beans.DMaaPContext; + +import java.io.IOException; import java.util.Enumeration; import com.att.nsa.cambria.service.AdminService; import com.att.nsa.configs.ConfigDbException; import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; +import com.att.nsa.cambria.beans.DMaaPContext; +import com.att.nsa.cambria.utils.ConfigurationReader; +import org.powermock.core.classloader.annotations.PrepareForTest; @RunWith(PowerMockRunner.class) +@PrepareForTest({ ServiceUtil.class }) public class AdminRestServiceTest { - + @InjectMocks AdminRestService adminRestService; @@ -56,13 +66,20 @@ public class AdminRestServiceTest { @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 @@ -75,10 +92,55 @@ public class AdminRestServiceTest { } + @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 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 + 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 { + adminRestService.dropConsumerCache(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + } @Test @@ -92,6 +154,46 @@ public class AdminRestServiceTest { adminRestService.getBlacklist(); } + + //@Test + public void testGetBlacklist_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).getBlacklist(dmaaPContext); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeaderNames()).thenReturn(headerNames); + when(headerNames.nextElement()).thenReturn("key"); + when(httpServReq.getHeader("key")).thenReturn("value"); + when(headerNames.hasMoreElements()).thenReturn(false); + try { + adminRestService.getBlacklist(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + } + + ////@Test + public void testGetBlacklist_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).getBlacklist(dmaaPContext); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeaderNames()).thenReturn(headerNames); + when(headerNames.nextElement()).thenReturn("key"); + when(httpServReq.getHeader("key")).thenReturn("value"); + when(headerNames.hasMoreElements()).thenReturn(false); + try { + adminRestService.getBlacklist(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + } @Test public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException { @@ -103,11 +205,90 @@ public class AdminRestServiceTest { } @Test - public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException { + 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 + 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 { + adminRestService.addToBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException { + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + + 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 { + + adminRestService.removeFromBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + catch (AccessDeniedException e) { + assertTrue(true); + } + catch (ConfigDbException e) { + assertTrue(true); + } + + } + + @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 { adminRestService.removeFromBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + catch (AccessDeniedException e) { + assertTrue(true); + } + catch (ConfigDbException e) { + assertTrue(true); + } } diff --git a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java index d85ef9b..b25578c 100644 --- a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java @@ -172,6 +172,63 @@ public class MMRestServiceTest { mmRestService.callCreateMirrorMaker(inputSteam); assertTrue(true); + } + @Test + public void testCallCreateMirrorMaker_error4() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, JSONException, ConfigDbException { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test@#\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + @Test + public void testCallCreateMirrorMaker_3() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, JSONException, ConfigDbException { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + @Test + public void testCallCreateMirrorMaker_error2() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, JSONException, ConfigDbException { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\",whitelist:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + + @Test + public void testCallCreateMirrorMaker_error1() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, JSONException, ConfigDbException { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\"}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + } @Test @@ -194,6 +251,43 @@ public class MMRestServiceTest { mmRestService.callUpdateMirrorMaker(inputSteam); assertTrue(true); } + + @Test + public void testCallUpdateMirrorMaker_error1() throws ConfigDbException, CambriaApiException { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test@1\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error2() throws ConfigDbException, CambriaApiException { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error3() throws ConfigDbException, CambriaApiException { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error4() throws ConfigDbException, CambriaApiException { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } @Test public void testCallDeleteMirrorMaker() throws ConfigDbException, CambriaApiException { -- cgit 1.2.3-korg