updating test cases for more coverage 83/82283/1
authorsawantmandar <ms5838@att.com>
Thu, 14 Mar 2019 18:18:15 +0000 (13:18 -0500)
committersawantmandar <ms5838@att.com>
Thu, 14 Mar 2019 18:18:35 +0000 (13:18 -0500)
Issue-ID: DMAAP-1097
Change-Id: I5973532bc53e814127d8439f3155c9fce869eee6
Signed-off-by: sawantmandar <ms5838@att.com>
src/test/java/org/onap/dmaap/service/AdminRestServiceTest.java
src/test/java/org/onap/dmaap/service/EventsRestServiceTest.java
src/test/java/org/onap/dmaap/util/ContentLengthInterceptorTest.java

index 0f99ff5..b766581 100644 (file)
 
  package org.onap.dmaap.service;
 
+import static org.junit.Assert.assertTrue;
+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.junit.Test;
 import org.junit.runner.RunWith;
-
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.onap.dmaap.dmf.mr.CambriaApiException;
-
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.when;
-
+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.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
-
-import java.io.IOException;
-import java.util.Enumeration;
-import org.onap.dmaap.dmf.mr.service.AdminService;
 import com.att.nsa.configs.ConfigDbException;
 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
-import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
-import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
-import org.powermock.core.classloader.annotations.PrepareForTest;
 
 @RunWith(PowerMockRunner.class)
 @PrepareForTest({ ServiceUtil.class })
@@ -145,9 +143,12 @@ public class AdminRestServiceTest {
 
        @Test
        public void testGetBlacklist() throws CambriaApiException, AccessDeniedException {
+               Vector headers = new Vector();
+               headers.add("Content-type");
+               Enumeration headerNms = headers.elements();
 
                when(dmaapContext.getRequest()).thenReturn(httpServReq);
-               when(httpServReq.getHeaderNames()).thenReturn(headerNames);
+               when(httpServReq.getHeaderNames()).thenReturn(headerNms);
                when(headerNames.nextElement()).thenReturn("key");
                when(httpServReq.getHeader("key")).thenReturn("value");
 
@@ -155,37 +156,32 @@ public class AdminRestServiceTest {
 
        }
        
-       //@Test
+       @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);
+               PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
                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
+       @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);
+               PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(any(DMaaPContext.class));
                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();
                }
index 7f30cf4..f7e701a 100644 (file)
@@ -256,6 +256,24 @@ public class EventsRestServiceTest {
 
        }
 
+       @Test
+       public void testGetEventsToException() throws CambriaApiException {
+               try {
+                       eventsRestRestService.getEventsToException("/topic");
+               } catch (CambriaApiException e) {
+                       assertTrue(true);
+               }
+       }
+       
+       @Test
+       public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
+               try {
+                       eventsRestRestService.getEventsToException("/topic", "1234");
+               } catch (CambriaApiException e) {
+                       assertTrue(true);
+               }               
+       }
+       
        @Test
        public void testPushEvents_TopicExistException() throws CambriaApiException {
 
index 0608d2e..26c9305 100644 (file)
@@ -70,6 +70,25 @@ public class ContentLengthInterceptorTest {
                assertTrue(true);
        }
        
+       @Test
+       public void testAllowOrRejectChunked() throws Exception {
+               PowerMockito.when(req.getHeader("Transfer-Encoding")).thenReturn("chunked");
+               PowerMockito.when(req.getHeader("Content-Length")).thenReturn("1027");
+               System.setProperty("maxcontentlength", "1024");
+
+               interceptor.allowOrReject(req, res, map);
+               assertTrue(true);
+       }       
+       
+       @Test
+       public void testAllowOrRejectNullTransferEncoding() throws Exception {
+               PowerMockito.when(req.getHeader("Transfer-Encoding")).thenReturn(null);
+               PowerMockito.when(req.getHeader("Content-Length")).thenReturn("1027");
+               System.setProperty("maxcontentlength", "1024");
+
+               interceptor.allowOrReject(req, res, map);
+               assertTrue(true);
+       }               
        //@Test(expected = NullPointerException.class) 
        public void testAllowOrRejectWithException() throws Exception {
                PowerMockito.when(req.getHeader("Transfer-Encoding")).thenThrow(new NumberFormatException());