summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java21
-rw-r--r--src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java154
2 files changed, 118 insertions, 57 deletions
diff --git a/src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java b/src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java
index 9f04a1f..eb76a96 100644
--- a/src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java
+++ b/src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java
@@ -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
diff --git a/src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java
index 225d434..f11593f 100644
--- a/src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java
+++ b/src/test/java/com/att/nsa/dmaap/service/ApiKeysRestServiceTest.java
@@ -22,25 +22,65 @@ package com.att.nsa.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 com.att.nsa.cambria.CambriaApiException;
import com.att.nsa.cambria.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 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.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 com.att.nsa.cambria.utils.ConfigurationReader;
+import com.att.nsa.cambria.service.ApiKeysService;
+import com.att.nsa.cambria.utils.ConfigurationReader;
+import com.att.nsa.configs.ConfigDbException;
+import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ ServiceUtil.class })
public class ApiKeysRestServiceTest {
- private ApiKeysRestService service = null;
+ @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 {
- service = new ApiKeysRestService();
+ MockitoAnnotations.initMocks(this);
}
@After
@@ -60,7 +100,23 @@ 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
public void testGetApiKey() {
@@ -74,7 +130,24 @@ public class ApiKeysRestServiceTest {
}
}
-
+
+ @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");
+
+ try {
+ service.getApiKey("apikeyName");
+ } catch (CambriaApiException e) {
+ // TODO Auto-generated catch block
+ assertTrue(true);
+ } catch (NullPointerException e) {
+ assertTrue(true);
+ }
+
+ }
+
@Test
public void testCreateApiKey() {
@@ -89,7 +162,25 @@ public class ApiKeysRestServiceTest {
}
-
+ @Test
+ public void testCreateApiKey_error()
+ 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);
+ }
+
+ }
+
@Test
public void testUpdateApiKey() {
@@ -103,12 +194,18 @@ public class ApiKeysRestServiceTest {
}
}
-
- @Test
- public void testDeleteApiKey() {
+ @Test
+ public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
+ 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.deleteApiKey("apikeyName");
+ service.updateApiKey("apikeyName", bean);
} catch (CambriaApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -117,44 +214,19 @@ public class ApiKeysRestServiceTest {
}
}
-
+
@Test
- public void testGetDmaapContext() {
- Class clazz = null;
- Method method = null;
+ public void testDeleteApiKey() {
+
try {
- clazz = Class.forName("com.att.nsa.dmaap.service.ApiKeysRestService");
- Object obj = clazz.newInstance();
- method = clazz.getDeclaredMethod("getDmaapContext", null);
- method.setAccessible(true);
- method.invoke(obj, null);
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
+ service.deleteApiKey("apikeyName");
+ } catch (CambriaApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
+ } catch (NullPointerException e) {
+ assertTrue(true);
}
- assertTrue(true);
}
-
-
-
} \ No newline at end of file