DMAAP-MR Unit test improvements
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / service / ApiKeysRestServiceTest.java
index b66ccc1..3d96755 100644 (file)
 
  package org.onap.dmaap.service;
 
-import static org.junit.Assert.*;
 
-import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doThrow;
 
-import org.onap.dmaap.dmf.mr.CambriaApiException;
-import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
 import com.att.nsa.configs.ConfigDbException;
 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
-
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.when;
+import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
+import java.io.IOException;
+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.json.JSONException;
-
-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 javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.dmaap.dmf.mr.CambriaApiException;
+import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
-
-import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
-import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
-import com.att.nsa.configs.ConfigDbException;
-import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
 
-@RunWith(PowerMockRunner.class)
-@PowerMockIgnore("jdk.internal.reflect.*")
-@PrepareForTest({ ServiceUtil.class })
+@RunWith(MockitoJUnitRunner.class)
 public class ApiKeysRestServiceTest {
-
        @InjectMocks
        private ApiKeysRestService service;
 
        @Mock
        ApiKeysService apiKeyService;
 
-       @Mock
-       DMaaPContext dmaapContext;
-
-       @Mock
-       HttpServletRequest httpServReq;
-       @Mock
-       private HttpServletResponse response;
-       @Mock
-       private ConfigurationReader configReader;
-
-       @Before
-       public void setUp() throws Exception {
-               MockitoAnnotations.initMocks(this);
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-
        @Test
        public void testGetAllApiKeys() {
-
                try {
                        service.getAllApiKeys();
                } catch (CambriaApiException e) {
@@ -103,25 +62,22 @@ public class ApiKeysRestServiceTest {
 
        }
 
-       @Test
-       public void testGetAllApiKeys_error() throws ConfigDbException, IOException {
-               PowerMockito.mockStatic(ServiceUtil.class);
-               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
-               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext);
-               try {
-                       service.getAllApiKeys();
-               } catch (CambriaApiException e) {
-                       // TODO Auto-generated catch block
-                       assertTrue(true);
-               } catch (NullPointerException e) {
-                       assertTrue(true);
-               }
+       @Test(expected=CambriaApiException.class)
+       public void testGetAllApiKeys_IOException() throws ConfigDbException, IOException, CambriaApiException {
+               doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(ArgumentMatchers.any(DMaaPContext.class));
+               service.getAllApiKeys();
+               fail("Was expecting an exception to be thrown");
+       }
 
+       @Test(expected=CambriaApiException.class)
+       public void testGetAllApiKeys_ConfigDBException() throws ConfigDbException, IOException, CambriaApiException {
+               doThrow(new ConfigDbException("error")).when(apiKeyService).getAllApiKeys(ArgumentMatchers.any(DMaaPContext.class));
+               service.getAllApiKeys();
+               fail("Was expecting an exception to be thrown");
        }
 
        @Test
        public void testGetApiKey() {
-
                try {
                        service.getApiKey("apikeyName");
                } catch (CambriaApiException e) {
@@ -130,29 +86,28 @@ public class ApiKeysRestServiceTest {
                } catch (NullPointerException e) {
                        assertTrue(true);
                }
-
        }
 
-       @Test
-       public void testGetApiKey_error() throws ConfigDbException, IOException {
-               PowerMockito.mockStatic(ServiceUtil.class);
-               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
-               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName");
+       @Test(expected=CambriaApiException.class)
+       public void testGetApiKey_IOException() throws ConfigDbException, IOException, CambriaApiException {
+               String apikeyName = "apikeyName";
+               doThrow(new IOException("error")).when(apiKeyService).getApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
 
-               try {
-                       service.getApiKey("apikeyName");
-               } catch (CambriaApiException e) {
-                       // TODO Auto-generated catch block
-                       assertTrue(true);
-               } catch (NullPointerException e) {
-                       assertTrue(true);
-               }
+               service.getApiKey(apikeyName);
+               fail("Was expecting an exception to be thrown");
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testGetApiKey_ConfigDBException() throws ConfigDbException, IOException, CambriaApiException {
+               String apikeyName = "apikeyName";
+               doThrow(new ConfigDbException("error")).when(apiKeyService).getApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
 
+               service.getApiKey(apikeyName);
+               fail("Was expecting an exception to be thrown");
        }
 
        @Test
        public void testCreateApiKey() {
-
                try {
                        service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
                } catch (CambriaApiException e) {
@@ -161,31 +116,46 @@ public class ApiKeysRestServiceTest {
                } catch (NullPointerException e) {
                        assertTrue(true);
                }
-
        }
 
-       @Test
-       public void testCreateApiKey_error()
+       @Test(expected=CambriaApiException.class)
+       public void testCreateApiKey_ConfigDbException()
                        throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
 
                ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
-               PowerMockito.mockStatic(ServiceUtil.class);
-               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
-               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean);
 
-               try {
-                       service.createApiKey(bean);
-               } catch (CambriaApiException e) {
-                       assertTrue(true);
-               } catch (NullPointerException e) {
-                       assertTrue(true);
-               }
+               doThrow(new ConfigDbException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
+
+               service.createApiKey(bean);
+               fail("Was expecting an exception to be thrown");
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testCreateApiKey_IOException()
+               throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
 
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+
+               doThrow(new IOException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
+
+               service.createApiKey(bean);
+               fail("Was expecting an exception to be thrown");
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testCreateApiKey_KeyExistsException()
+               throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+
+               doThrow(new KeyExistsException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
+
+               service.createApiKey(bean);
+               fail("Was expecting an exception to be thrown");
        }
 
        @Test
        public void testUpdateApiKey() {
-
                try {
                        service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
                } catch (CambriaApiException e) {
@@ -194,27 +164,39 @@ public class ApiKeysRestServiceTest {
                } catch (NullPointerException e) {
                        assertTrue(true);
                }
-
        }
 
-       @Test
-       public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
+       @Test(expected=CambriaApiException.class)
+       public void testUpdateApiKey_ConfigDbException() throws CambriaApiException, JSONException,
                        ConfigDbException, IOException, AccessDeniedException {
 
                ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
-               PowerMockito.mockStatic(ServiceUtil.class);
-               PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
-               PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName",
-                               bean);
-               try {
-                       service.updateApiKey("apikeyName", bean);
-               } catch (CambriaApiException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               } catch (NullPointerException e) {
-                       assertTrue(true);
-               }
+               doThrow(new ConfigDbException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
+
+               service.updateApiKey("apikeyName", bean);
+               fail("Was expecting an exception to be thrown");
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testUpdateApiKey_IOException() throws CambriaApiException, JSONException,
+               ConfigDbException, IOException, AccessDeniedException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+               doThrow(new IOException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
+
+               service.updateApiKey("apikeyName", bean);
+               fail("Was expecting an exception to be thrown");
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testUpdateApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
+               ConfigDbException, IOException, AccessDeniedException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+               doThrow(new AccessDeniedException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
 
+               service.updateApiKey("apikeyName", bean);
+               fail("Was expecting an exception to be thrown");
        }
 
        @Test
@@ -228,7 +210,38 @@ public class ApiKeysRestServiceTest {
                } catch (NullPointerException e) {
                        assertTrue(true);
                }
+       }
+
+       @Test(expected=CambriaApiException.class)
+       public void testDeleteApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
+               ConfigDbException, IOException, AccessDeniedException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+               doThrow(new AccessDeniedException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+
+               service.deleteApiKey("apikeyName");
+               fail("Was expecting an exception to be thrown");
+       }
 
+       @Test(expected=CambriaApiException.class)
+       public void testDeleteApiKey_IOException() throws CambriaApiException, JSONException,
+               ConfigDbException, IOException, AccessDeniedException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+               doThrow(new IOException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+
+               service.deleteApiKey("apikeyName");
+               fail("Was expecting an exception to be thrown");
        }
 
+       @Test(expected=CambriaApiException.class)
+       public void testDeleteApiKey_ConfigDbException() throws CambriaApiException, JSONException,
+               ConfigDbException, IOException, AccessDeniedException {
+
+               ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
+               doThrow(new ConfigDbException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
+
+               service.deleteApiKey("apikeyName");
+               fail("Was expecting an exception to be thrown");
+       }
 }
\ No newline at end of file