From fbe26a20e749c3177f84cd05c67e757ac107b9a8 Mon Sep 17 00:00:00 2001 From: Sunil Unnava Date: Wed, 28 Feb 2018 14:21:40 -0500 Subject: changes for code coverage Issue-ID: DMAAP-271 Change-Id: Ib8fd245fdbe9ef3457e8303c5379977b237ec09a Signed-off-by: Sunil Unnava --- .../nsa/dmaap/service/EventsRestServiceTest.java | 216 +++++++++-- .../att/nsa/dmaap/service/MMRestServiceTest.java | 45 ++- .../nsa/dmaap/service/TopicRestServiceTest.java | 411 ++++++++++++++++++++- .../dmaap/service/TransactionRestServiceTest.java | 40 ++ 4 files changed, 673 insertions(+), 39 deletions(-) (limited to 'src/test') diff --git a/src/test/java/com/att/nsa/dmaap/service/EventsRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/EventsRestServiceTest.java index ff90bd7..05d39ba 100644 --- a/src/test/java/com/att/nsa/dmaap/service/EventsRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/EventsRestServiceTest.java @@ -19,6 +19,8 @@ */ package com.att.nsa.dmaap.service; +import java.util.Date; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -28,20 +30,26 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import static org.mockito.Mockito.when; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.api.mockito.PowerMockito; import com.att.ajsc.beans.PropertiesMapBean; import com.att.nsa.cambria.CambriaApiException; import com.att.nsa.cambria.backends.ConsumerFactory.UnavailableException; +import com.att.nsa.cambria.exception.DMaaPErrorMessages; import com.att.nsa.cambria.service.EventsService; import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.cambria.utils.Utils; +import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting; import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; -import static org.mockito.Mockito.when; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.InputStream; +import java.util.Date; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; @@ -61,10 +69,10 @@ public class EventsRestServiceTest { @Mock private EventsService eventsService; - + @Mock ErrorResponse errorResponse; - + @Mock DMaaPContext dmaapContext; @@ -75,7 +83,10 @@ public class EventsRestServiceTest { ServletInputStream servletInputStream; @Mock - HttpServletRequest httpServReq; + HttpServletRequest request; + + @Mock + private DMaaPErrorMessages errorMessages; @Before public void setUp() throws Exception { @@ -93,6 +104,59 @@ public class EventsRestServiceTest { } + @Test + public void testGetEvents_error() { + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(dmaapContext, "topicName", + "consumergroup", "consumerid"); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(dmaapContext, "topicName", + "consumergroup", "consumerid"); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(dmaapContext, + "topicName", "consumergroup", "consumerid"); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + @Test(expected = TopicExistsException.class) public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException { @@ -103,10 +167,10 @@ public class EventsRestServiceTest { eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); } - + @Test(expected = DMaaPAccessDeniedException.class) - public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException, TopicExistsException, - UnavailableException, IOException, AccessDeniedException { + public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException, + TopicExistsException, UnavailableException, IOException, AccessDeniedException { Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); @@ -114,17 +178,22 @@ public class EventsRestServiceTest { eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); } - - /*@Test(expected = DMaaPAccessDeniedException.class) - public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException, TopicExistsException, - UnavailableException, IOException, AccessDeniedException { - Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(dmaapContext, - "topicName", "consumergroup", "consumerid"); - - eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); - - }*/ + /* + * @Test(expected = DMaaPAccessDeniedException.class) public void + * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, + * ConfigDbException, TopicExistsException, UnavailableException, + * IOException, AccessDeniedException { + * + * Mockito.doThrow(new + * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents( + * dmaapContext, "topicName", "consumergroup", "consumerid"); + * + * eventsService.getEvents(dmaapContext, "topicName", "consumergroup", + * "consumerid"); + * + * } + */ @Test public void testPushEvents() throws CambriaApiException { @@ -132,7 +201,59 @@ public class EventsRestServiceTest { eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); } - + + @Test + public void testPushEvents_error() { + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(dmaapContext, "topicName", iStream, + "partitionKey", null); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(dmaapContext, "topicName", + iStream, "partitionKey", null); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(dmaapContext, + "topicName", iStream, "partitionKey", null); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } @Test public void testPushEvents_TopicExistException() throws CambriaApiException { @@ -143,11 +264,64 @@ public class EventsRestServiceTest { @Test public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException { - // when(dmaapContext.getRequest()).th - // thenReturn(httpServReq); - when(httpServReq.getInputStream()).thenReturn(servletInputStream); + when(request.getInputStream()).thenReturn(servletInputStream); eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); } + @Test + public void tesTPushEventsWithTransaction_error() throws IOException { + when(request.getInputStream()).thenReturn(servletInputStream); + ServletInputStream stream = request.getInputStream(); + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(dmaapContext, + "topicName", stream, "partitionKey", Utils.getFormattedDate(new Date())); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(dmaapContext, "topicName", + stream, "partitionKey", Utils.getFormattedDate(new Date())); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(dmaapContext, "topicName", stream, + "partitionKey", Utils.getFormattedDate(new Date())); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + } diff --git a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java index 6fa5592..d85ef9b 100644 --- a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java @@ -163,19 +163,23 @@ public class MMRestServiceTest { public void testCallCreateMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, JSONException, ConfigDbException { prepareForTestCommon(); - - String sampleJson = "{\"test\":\"test\"}"; + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); - mmRestService.callCreateMirrorMaker(iStream); + mmRestService.callCreateMirrorMaker(inputSteam); assertTrue(true); + } @Test public void testCallListAllMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, JSONException, ConfigDbException { prepareForTestCommon(); - - String sampleJson = "{\"test\":\"test\"}"; + + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); mmRestService.callListAllMirrorMaker(inputSteam); assertTrue(true); @@ -184,24 +188,28 @@ public class MMRestServiceTest { @Test public void testCallUpdateMirrorMaker() throws ConfigDbException, CambriaApiException { prepareForTestCommon(); - - - mmRestService.callUpdateMirrorMaker(iStream); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); assertTrue(true); } @Test public void testCallDeleteMirrorMaker() throws ConfigDbException, CambriaApiException { prepareForTestCommon(); - mmRestService.callDeleteMirrorMaker(iStream); + + String sampleJson = "{ messageID:\"test\", deleteMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callDeleteMirrorMaker(inputSteam); assertTrue(true); } @Test public void testListWhiteList() throws ConfigDbException { prepareForTestCommon(); - - String sampleJson = "{\"test\":\"test\"}"; + + String sampleJson = "{ name:\"test\", namespace:\"test\"}}"; InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); mmRestService.listWhiteList(inputSteam); assertTrue(true); @@ -210,9 +218,9 @@ public class MMRestServiceTest { @Test public void testCreateWhiteList() throws ConfigDbException { prepareForTestCommon(); - String sampleJson = "{\"test\":\"test\"}"; + String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}"; InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); - + mmRestService.createWhiteList(inputSteam); assertTrue(true); } @@ -220,13 +228,13 @@ public class MMRestServiceTest { @Test public void testDeleteWhiteList() throws ConfigDbException { prepareForTestCommon(); - - String sampleJson = "{\"test\":\"test\"}"; + + String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}"; InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); mmRestService.deleteWhiteList(inputSteam); assertTrue(true); } - + private void prepareForTestCommon() throws ConfigDbException { Assert.assertNotNull(mmRestService); PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq); @@ -239,6 +247,9 @@ public class MMRestServiceTest { PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf")) .thenReturn("admin"); + PowerMockito + .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create")) + .thenReturn("aafcreate"); PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf")) .thenReturn("admin"); @@ -265,6 +276,8 @@ public class MMRestServiceTest { PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true); PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin"); PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "aafcreatetest|create")) + .thenReturn(true); PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker); diff --git a/src/test/java/com/att/nsa/dmaap/service/TopicRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/TopicRestServiceTest.java index abaac0f..8778602 100644 --- a/src/test/java/com/att/nsa/dmaap/service/TopicRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/TopicRestServiceTest.java @@ -185,6 +185,40 @@ public class TopicRestServiceTest { topicService.getTopics(); } + @Test + public void testGetTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException { + + Assert.assertNotNull(topicService); + + PowerMockito.mockStatic(PropertiesMapBean.class); + + assertTrue(true); + when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf")) + .thenReturn("namespace"); + + PowerMockito.mockStatic(DMaaPResponseBuilder.class); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeader("Authorization")).thenReturn(null); + + when(dmaapContext.getResponse()).thenReturn(httpServRes); + String perms = "namespace" + "|" + "*" + "|" + "view"; + when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true); + + when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null); + try { + PowerMockito.doThrow(new IOException()).when(tService).getTopics(dmaapContext); + } catch (JSONException | ConfigDbException | IOException excp) { + assertTrue(false); + } + + try { + topicService.getTopics(); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test(expected = DMaaPAccessDeniedException.class) public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, JSONException, ConfigDbException { @@ -228,6 +262,36 @@ public class TopicRestServiceTest { topicService.getAllTopics(); } + @Test + public void testGetAllTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException { + + Assert.assertNotNull(topicService); + PowerMockito.mockStatic(PropertiesMapBean.class); + + assertTrue(true); + when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf")) + .thenReturn("namespace"); + + PowerMockito.mockStatic(DMaaPResponseBuilder.class); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeader("Authorization")).thenReturn(null); + + when(dmaapContext.getResponse()).thenReturn(httpServRes); + + try { + PowerMockito.doThrow(new IOException()).when(tService).getAllTopics(dmaapContext); + } catch (JSONException | ConfigDbException | IOException excp) { + assertTrue(false); + } + + try { + topicService.getAllTopics(); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test(expected = DMaaPAccessDeniedException.class) public void testGetTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, JSONException, ConfigDbException { @@ -272,6 +336,37 @@ public class TopicRestServiceTest { topicService.getTopic("topicName"); } + @Test + public void testGetTopic_error() throws DMaaPAccessDeniedException, ConfigDbException { + + Assert.assertNotNull(topicService); + + PowerMockito.mockStatic(PropertiesMapBean.class); + + assertTrue(true); + when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF")) + .thenReturn("enfTopicName"); + + PowerMockito.mockStatic(DMaaPResponseBuilder.class); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeader("Authorization")).thenReturn(null); + + when(dmaapContext.getResponse()).thenReturn(httpServRes); + + try { + PowerMockito.doThrow(new IOException()).when(tService).getTopic(dmaapContext, "topicName"); + } catch (TopicExistsException | ConfigDbException | IOException excp) { + assertTrue(false); + } + + try { + topicService.getTopic("topicName"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testCreateTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { @@ -289,6 +384,62 @@ public class TopicRestServiceTest { topicService.createTopic(topicBean); } + @Test + public void testCreateTopic_error() { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).createTopic(dmaapContext, topicBean); + } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) { + assertTrue(false); + } catch (CambriaApiException excp) { + assertTrue(false); + } + + try { + topicService.createTopic(topicBean); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(tService).createTopic(dmaapContext, topicBean); + } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) { + assertTrue(false); + } catch (CambriaApiException excp) { + assertTrue(false); + } + + try { + topicService.createTopic(topicBean); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(tService).createTopic(dmaapContext, topicBean); + } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) { + assertTrue(false); + } catch (CambriaApiException excp) { + assertTrue(false); + } + + try { + topicService.createTopic(topicBean); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testDeleteTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { @@ -306,6 +457,48 @@ public class TopicRestServiceTest { topicService.deleteTopic("enfTopicNamePlusExtra"); } + @Test + public void testDeleteTopic_error() + throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).deleteTopic(dmaapContext, "enfTopicNamePlusExtra"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.deleteTopic("enfTopicNamePlusExtra"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(tService).deleteTopic(dmaapContext, + "enfTopicNamePlusExtra"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.deleteTopic("enfTopicNamePlusExtra"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testGetPublishersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { @@ -323,6 +516,33 @@ public class TopicRestServiceTest { topicService.getPublishersByTopicName("enfTopicNamePlusExtra"); } + @Test + public void testGetPublishersByTopicName_error() { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).getPublishersByTopicName(dmaapContext, + "enfTopicNamePlusExtra"); + } catch (TopicExistsException | ConfigDbException | IOException e) { + assertTrue(false); + } + + try { + topicService.getPublishersByTopicName("enfTopicNamePlusExtra"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testPermitPublisherForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { @@ -340,6 +560,49 @@ public class TopicRestServiceTest { topicService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID"); } + @Test + public void testPermitPublisherForTopic_error() + throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).permitPublisherForTopic(dmaapContext, + "enfTopicNamePlusExtra", "producerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(tService).permitPublisherForTopic(dmaapContext, + "enfTopicNamePlusExtra", "producerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testDenyPublisherForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { @@ -357,6 +620,50 @@ public class TopicRestServiceTest { topicService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID"); } + @Test + public void testDenyPublisherForTopic_error() + throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).denyPublisherForTopic(dmaapContext, + "enfTopicNamePlusExtra", "producerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(tService).denyPublisherForTopic(dmaapContext, + "enfTopicNamePlusExtra", "producerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + } + @Test public void testGetConsumersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, AccessDeniedException { @@ -374,6 +681,34 @@ public class TopicRestServiceTest { topicService.getConsumersByTopicName("enfTopicNamePlusExtra"); } + @Test + public void testGetConsumersByTopicName_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, AccessDeniedException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).getConsumersByTopicName(dmaapContext, + "enfTopicNamePlusExtra"); + } catch (TopicExistsException | ConfigDbException | IOException excp) { + assertTrue(false); + } + + try { + topicService.getConsumersByTopicName("enfTopicNamePlusExtra"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + @Test public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, AccessDeniedException { @@ -390,9 +725,9 @@ public class TopicRestServiceTest { topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); } - + @Test - public void testPermitConsumerForTopicWithException() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + public void testPermitConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException, AccessDeniedException { Assert.assertNotNull(topicService); @@ -405,6 +740,35 @@ public class TopicRestServiceTest { TopicBean topicBean = new TopicBean(); topicBean.setTopicName("enfTopicNamePlusExtra"); + try { + PowerMockito.doThrow(new IOException()).when(tService).permitConsumerForTopic(dmaapContext, + "enfTopicNamePlusExtra", "consumerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + + @Test + public void testPermitConsumerForTopicWithException() throws DMaaPAccessDeniedException, CambriaApiException, + IOException, TopicExistsException, AccessDeniedException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); } @@ -425,4 +789,47 @@ public class TopicRestServiceTest { topicService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); } + @Test + public void testDenyConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException, + TopicExistsException, AccessDeniedException { + + Assert.assertNotNull(topicService); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey); + when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator); + when(dmaapContext.getConfigReader()).thenReturn(configReader); + + TopicBean topicBean = new TopicBean(); + topicBean.setTopicName("enfTopicNamePlusExtra"); + + try { + PowerMockito.doThrow(new IOException()).when(tService).denyConsumerForTopic(dmaapContext, + "enfTopicNamePlusExtra", "consumerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(tService).denyConsumerForTopic(dmaapContext, + "enfTopicNamePlusExtra", "consumerID"); + } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException + | DMaaPAccessDeniedException excp) { + assertTrue(false); + } + + try { + topicService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID"); + } catch (CambriaApiException excp) { + assertTrue(true); + } + } + } diff --git a/src/test/java/com/att/nsa/dmaap/service/TransactionRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/TransactionRestServiceTest.java index 701cb3c..0952c25 100644 --- a/src/test/java/com/att/nsa/dmaap/service/TransactionRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/TransactionRestServiceTest.java @@ -22,6 +22,7 @@ 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; @@ -42,6 +43,8 @@ import com.att.nsa.cambria.service.EventsService; import com.att.nsa.cambria.service.TransactionService; import com.att.nsa.configs.ConfigDbException; import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; +import com.att.aft.dme2.internal.jettison.json.JSONException; +import org.powermock.api.mockito.PowerMockito; @RunWith(PowerMockRunner.class) @PrepareForTest({ PropertiesMapBean.class }) @@ -70,6 +73,7 @@ public class TransactionRestServiceTest { public void testGetAllTransactionObjs() throws CambriaApiException { transactionRestService.getAllTransactionObjs(); + assertTrue(true); } @@ -77,6 +81,42 @@ public class TransactionRestServiceTest { public void testGetTransactionObj() throws CambriaApiException { transactionRestService.getTransactionObj("transactionId"); + assertTrue(true); + + } + + @Test + public void testGetAllTransactionObjsError() throws CambriaApiException { + + try { + PowerMockito.doThrow(new IOException()).when(transactionService).getAllTransactionObjs(dmaapContext); + } catch (ConfigDbException | IOException e) { + assertTrue(false); + } + + try { + transactionRestService.getAllTransactionObjs(); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testGetTransactionObjError() { + + try { + PowerMockito.doThrow(new IOException()).when(transactionService).getTransactionObj(dmaapContext, + "transactionId"); + } catch (ConfigDbException | JSONException | IOException e) { + assertTrue(false); + } + + try { + transactionRestService.getTransactionObj("transactionId"); + } catch (CambriaApiException e) { + assertTrue(true); + } } -- cgit 1.2.3-korg