update the package name
[dmaap/messagerouter/messageservice.git] / src / test / java / com / att / nsa / dmaap / service / ApiKeysRestServiceTest.java
index 225d434..c784f90 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 org.onap.dmaap.dmf.mr.CambriaApiException;\r
+import org.onap.dmaap.dmf.mr.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 org.onap.dmaap.dmf.mr.beans.DMaaPContext;\r
+\r
+import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;\r
+import org.onap.dmaap.dmf.mr.service.ApiKeysService;\r
+import org.onap.dmaap.dmf.mr.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