testcases for code coverage 91/34391/1
authorSunil Unnava <su622b@att.com>
Wed, 7 Mar 2018 01:59:19 +0000 (20:59 -0500)
committerSunil Unnava <su622b@att.com>
Wed, 7 Mar 2018 01:59:33 +0000 (20:59 -0500)
Issue-ID: DMAAP-271
Change-Id: Ifee89b3800d0a78e7b7197ae0ea48dfbe7e37709
Signed-off-by: Sunil Unnava <su622b@att.com>
src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java
src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java

index 9f04a1f..eb76a96 100644 (file)
@@ -107,7 +107,7 @@ public class ApiKeysRestService {
                log.info("Inside ApiKeysRestService.getAllApiKeys");
 
                try {
-                       apiKeyService.getAllApiKeys(getDmaapContext());
+                       apiKeyService.getAllApiKeys(ServiceUtil.getDMaaPContext(configReader, request, response));
                        log.info("Fetching all API keys is Successful");
                } catch (ConfigDbException | IOException e) {
                        log.error("Error while retrieving API keys: " + e);
@@ -135,7 +135,7 @@ public class ApiKeysRestService {
                log.info("Fetching details of api key: " + apiKeyName);
 
                try {
-                       apiKeyService.getApiKey(getDmaapContext(), apiKeyName);
+                       apiKeyService.getApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName);
                        log.info("Fetching specific API key is Successful");
                } catch (ConfigDbException | IOException e) {
                        log.error("Error while retrieving API key details: " + e);
@@ -164,7 +164,7 @@ public class ApiKeysRestService {
                log.info("Creating Api Key.");
 
                try {
-                       apiKeyService.createApiKey(getDmaapContext(), nsaApiKey);
+                       apiKeyService.createApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), nsaApiKey);
                        log.info("Creating API key is Successful");
                } catch (KeyExistsException | ConfigDbException | IOException e) {
                        log.error("Error while Creating API key : " + e.getMessage(), e);
@@ -198,7 +198,7 @@ public class ApiKeysRestService {
                try {
                        
                        apiKeyService
-                                       .updateApiKey(getDmaapContext(), apiKeyName, nsaApiKey);
+                                       .updateApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName, nsaApiKey);
                        log.error("API key updated sucessfully");
                } catch (ConfigDbException | IOException | AccessDeniedException e) {
                        log.error("Error while Updating API key : " + apiKeyName, e);
@@ -225,7 +225,7 @@ public class ApiKeysRestService {
        public void deleteApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
                log.info("Deleting Api Key: " + apiKeyName);
                try {
-                       apiKeyService.deleteApiKey(getDmaapContext(), apiKeyName);
+                       apiKeyService.deleteApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName);
                        log.info("Api Key deleted successfully: " + apiKeyName);
                } catch (ConfigDbException | IOException | AccessDeniedException e) {
                        log.error("Error while deleting API key : " + apiKeyName, e);
@@ -239,16 +239,5 @@ public class ApiKeysRestService {
                }
        }
 
-       /**
-        * Create a dmaap context
-        * @return DMaaPContext
-        */
-       private DMaaPContext getDmaapContext() {
-               DMaaPContext dmaapContext = new DMaaPContext();
-               dmaapContext.setConfigReader(configReader);
-               dmaapContext.setRequest(request);
-               dmaapContext.setResponse(response);
-               return dmaapContext;
-       }
 
 }
\ No newline at end of file
index 225d434..f11593f 100644 (file)
@@ -22,25 +22,65 @@ package com.att.nsa.dmaap.service;
 \r
 import static org.junit.Assert.*;\r
 \r
+import java.io.IOException;\r
 import java.lang.reflect.InvocationTargetException;\r
 import java.lang.reflect.Method;\r
 \r
 import org.junit.After;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
 \r
 import com.att.nsa.cambria.CambriaApiException;\r
 import com.att.nsa.cambria.beans.ApiKeyBean;\r
 import com.att.nsa.configs.ConfigDbException;\r
 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
 \r
+import static org.junit.Assert.assertTrue;\r
+import static org.mockito.Mockito.when;\r
+import org.mockito.InjectMocks;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.json.JSONException;\r
+\r
+import org.powermock.api.mockito.PowerMockito;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import com.att.nsa.cambria.beans.DMaaPContext;\r
+\r
+import com.att.nsa.cambria.utils.ConfigurationReader;\r
+import com.att.nsa.cambria.service.ApiKeysService;\r
+import com.att.nsa.cambria.utils.ConfigurationReader;\r
+import com.att.nsa.configs.ConfigDbException;\r
+import com.att.nsa.security.db.NsaApiDb.KeyExistsException;\r
+\r
+@RunWith(PowerMockRunner.class)\r
+@PrepareForTest({ ServiceUtil.class })\r
 public class ApiKeysRestServiceTest {\r
 \r
-       private ApiKeysRestService service = null;\r
+       @InjectMocks\r
+       private ApiKeysRestService service;\r
+\r
+       @Mock\r
+       ApiKeysService apiKeyService;\r
+\r
+       @Mock\r
+       DMaaPContext dmaapContext;\r
+\r
+       @Mock\r
+       HttpServletRequest httpServReq;\r
+       @Mock\r
+       private HttpServletResponse response;\r
+       @Mock\r
+       private ConfigurationReader configReader;\r
 \r
        @Before\r
        public void setUp() throws Exception {\r
-               service = new ApiKeysRestService();\r
+               MockitoAnnotations.initMocks(this);\r
        }\r
 \r
        @After\r
@@ -60,7 +100,23 @@ public class ApiKeysRestServiceTest {
                }\r
 \r
        }\r
-       \r
+\r
+       @Test\r
+       public void testGetAllApiKeys_error() throws ConfigDbException, IOException {\r
+               PowerMockito.mockStatic(ServiceUtil.class);\r
+               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);\r
+               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext);\r
+               try {\r
+                       service.getAllApiKeys();\r
+               } catch (CambriaApiException e) {\r
+                       // TODO Auto-generated catch block\r
+                       assertTrue(true);\r
+               } catch (NullPointerException e) {\r
+                       assertTrue(true);\r
+               }\r
+\r
+       }\r
+\r
        @Test\r
        public void testGetApiKey() {\r
 \r
@@ -74,7 +130,24 @@ public class ApiKeysRestServiceTest {
                }\r
 \r
        }\r
-       \r
+\r
+       @Test\r
+       public void testGetApiKey_error() throws ConfigDbException, IOException {\r
+               PowerMockito.mockStatic(ServiceUtil.class);\r
+               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);\r
+               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName");\r
+\r
+               try {\r
+                       service.getApiKey("apikeyName");\r
+               } catch (CambriaApiException e) {\r
+                       // TODO Auto-generated catch block\r
+                       assertTrue(true);\r
+               } catch (NullPointerException e) {\r
+                       assertTrue(true);\r
+               }\r
+\r
+       }\r
+\r
        @Test\r
        public void testCreateApiKey() {\r
 \r
@@ -89,7 +162,25 @@ public class ApiKeysRestServiceTest {
 \r
        }\r
 \r
-       \r
+       @Test\r
+       public void testCreateApiKey_error()\r
+                       throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {\r
+\r
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");\r
+               PowerMockito.mockStatic(ServiceUtil.class);\r
+               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);\r
+               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean);\r
+\r
+               try {\r
+                       service.createApiKey(bean);\r
+               } catch (CambriaApiException e) {\r
+                       assertTrue(true);\r
+               } catch (NullPointerException e) {\r
+                       assertTrue(true);\r
+               }\r
+\r
+       }\r
+\r
        @Test\r
        public void testUpdateApiKey() {\r
 \r
@@ -103,12 +194,18 @@ public class ApiKeysRestServiceTest {
                }\r
 \r
        }\r
-       \r
-       @Test\r
-       public void testDeleteApiKey() {\r
 \r
+       @Test\r
+       public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,\r
+                       ConfigDbException, IOException, AccessDeniedException {\r
+\r
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");\r
+               PowerMockito.mockStatic(ServiceUtil.class);\r
+               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);\r
+               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName",\r
+                               bean);\r
                try {\r
-                       service.deleteApiKey("apikeyName");\r
+                       service.updateApiKey("apikeyName", bean);\r
                } catch (CambriaApiException e) {\r
                        // TODO Auto-generated catch block\r
                        e.printStackTrace();\r
@@ -117,44 +214,19 @@ public class ApiKeysRestServiceTest {
                }\r
 \r
        }\r
-       \r
+\r
        @Test\r
-       public void testGetDmaapContext() {\r
-               Class clazz = null;\r
-               Method method = null;\r
+       public void testDeleteApiKey() {\r
+\r
                try {\r
-                       clazz = Class.forName("com.att.nsa.dmaap.service.ApiKeysRestService");\r
-                       Object obj = clazz.newInstance();\r
-                       method = clazz.getDeclaredMethod("getDmaapContext", null);\r
-                       method.setAccessible(true);\r
-                       method.invoke(obj, null);\r
-               } catch (ClassNotFoundException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (NoSuchMethodException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (SecurityException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (InstantiationException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (IllegalAccessException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (IllegalArgumentException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-               } catch (InvocationTargetException e) {\r
+                       service.deleteApiKey("apikeyName");\r
+               } catch (CambriaApiException e) {\r
                        // TODO Auto-generated catch block\r
                        e.printStackTrace();\r
+               } catch (NullPointerException e) {\r
+                       assertTrue(true);\r
                }\r
 \r
-               assertTrue(true);\r
        }\r
 \r
-       \r
-       \r
-\r
 }
\ No newline at end of file