From bb1b1847af408c96bea50746d2ce90342b82976e Mon Sep 17 00:00:00 2001 From: amit-kumaryadav Date: Fri, 9 Oct 2020 11:35:26 +0530 Subject: [PATCH 1/2] Test cases added in hubnotification service --- HubNotificationHandler/pom.xml | 180 ++++++++++++------ .../service/impl/NotifyHookServiceImpl.java | 4 +- .../NotifyHandlerServiceImplTest.java | 96 ++++++++++ 3 files changed, 221 insertions(+), 59 deletions(-) create mode 100644 HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java diff --git a/HubNotificationHandler/pom.xml b/HubNotificationHandler/pom.xml index 3bed5c3..292e9a4 100644 --- a/HubNotificationHandler/pom.xml +++ b/HubNotificationHandler/pom.xml @@ -1,65 +1,131 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.1.17.RELEASE - - - com.eagle - hub-notification-service - 0.0.1-SNAPSHOT - hub-notification-service - Hu Notification Service + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.17.RELEASE + + + com.eagle + hub-notification-service + 0.0.1-SNAPSHOT + hub-notification-service + Hu Notification Service - - 1.8 - 1.18.8 - + + 1.8 + 1.18.8 + 0.8.5 + - - - org.springframework.boot - spring-boot-starter-web - + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.kafka - spring-kafka - - - org.apache.logging.log4j - log4j-api - - - org.apache.logging.log4j - log4j-core - - - org.springframework.boot - spring-boot-starter-data-mongodb - - - org.projectlombok - lombok - - + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + junit + junit + test + + + junit + junit + 4.13 + test + + + org.springframework.kafka + spring-kafka + + + org.apache.logging.log4j + log4j-api + + + org.apache.logging.log4j + log4j-core + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.projectlombok + lombok + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.jacoco + jacoco-maven-plugin + ${jacoco.plugin.version} + + + prepare-agent + + prepare-agent + + + + report + prepare-package + + report + + + + post-unit-test + test + + report + + + + target/jacoco.exec + + target/jacoco-ut + + + + + + target/jacoco.exec + + + + + diff --git a/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java b/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java index 51f009f..b1a9d7a 100644 --- a/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java +++ b/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java @@ -95,7 +95,7 @@ public void handleNotifyKafkaTopicRequest(Map data) { * @param data */ @SuppressWarnings("unchecked") - private void handleTopicCreate(Map data) { + public void handleTopicCreate(Map data) { logger.info("Received Topic Creation Event"); NotificationEvent nEvent = new NotificationEvent(); nEvent.setEventId(Constants.DISCUSSION_CREATION_EVENT_ID); @@ -157,7 +157,7 @@ private void handlePostCreate(Map data) { * * @param data */ - private void handleTopicReplyEvent(Map data) { + public void handleTopicReplyEvent(Map data) { NotificationEvent nEvent = new NotificationEvent(); nEvent.setEventId(Constants.DISCUSSION_REPLY_EVENT_ID); Map tagValues = new HashMap<>(); diff --git a/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java b/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java new file mode 100644 index 0000000..39e8f67 --- /dev/null +++ b/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java @@ -0,0 +1,96 @@ +package com.eagle.hubnotifier; + +import com.eagle.hubnotifier.config.Configuration; +import com.eagle.hubnotifier.constants.Constants; +import com.eagle.hubnotifier.model.HubUser; +import com.eagle.hubnotifier.model.NotificationEvent; +import com.eagle.hubnotifier.repository.HubPostRepository; +import com.eagle.hubnotifier.repository.HubTopicRepository; +import com.eagle.hubnotifier.repository.HubUserRepository; +import com.eagle.hubnotifier.service.impl.NotifyHandlerServiceImpl; +import com.eagle.hubnotifier.service.impl.NotifyHookServiceImpl; +import com.eagle.hubnotifier.service.impl.OutboundRequestHandlerServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + + +@RunWith(MockitoJUnitRunner.class) +class NotifyHandlerServiceImplTest { + + @InjectMocks + private NotifyHookServiceImpl notifyHandlerService; + + @Mock + private HubUserRepository userRepository; + + @Mock + public Configuration configuration; + + @Mock + private HubPostRepository hubPostRepository; + + @Mock + private HubTopicRepository hubTopicRepository; + + @Mock + private NotifyHandlerServiceImpl notifyHandlerServiceImpl; + + @Mock + private HashMap mapTest; + + @InjectMocks + private OutboundRequestHandlerServiceImpl outboundRequestHandlerService; + + @Test + void testing() { + assertEquals("amd", "amd"); + } + + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void handleTopicCreate() throws Exception { + Map topicData = new HashMap<>(); + HubUser user = mock(HubUser.class); + topicData.put(Constants.PARAM_TOPIC_TITLE_CONSTANT, Arrays.asList("New topic check")); + topicData.put(Constants.PARAM_TOPIC_TID_CONSTANT, Arrays.asList("1")); + topicData.put(Constants.PARAM_TOPIC_UID_CONSTANT, Arrays.asList("1")); + when(userRepository.findByKey("user")).thenReturn(user); + notifyHandlerService.handleTopicCreate(topicData); + } + + @Test + void sendNotification() { + NotificationEvent notificationEvent = mock(NotificationEvent.class); + doNothing().when(notifyHandlerServiceImpl).sendNotification(notificationEvent); + } + + @Test + void OutBoundGetRequest() { + NotificationEvent notificationEvent = mock(NotificationEvent.class); + StringBuilder builder = new StringBuilder(); + builder.append("http//localhost:9090/notifyHandler"); + ResponseEntity entity = (ResponseEntity) outboundRequestHandlerService.fetchResultUsingPost(builder, notificationEvent); + assertTrue(entity!=null); + assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)); + } +} From c520ab361a69486b8ff859b62062aefe93a9c19d Mon Sep 17 00:00:00 2001 From: amit-kumaryadav Date: Fri, 9 Oct 2020 14:13:50 +0530 Subject: [PATCH 2/2] Test cases added in hubnotification service --- .../service/impl/NotifyHookServiceImpl.java | 4 +- .../NotifyHandlerServiceImplTest.java | 103 +++++++++++------- 2 files changed, 67 insertions(+), 40 deletions(-) diff --git a/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java b/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java index b1a9d7a..161a90d 100644 --- a/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java +++ b/HubNotificationHandler/src/main/java/com/eagle/hubnotifier/service/impl/NotifyHookServiceImpl.java @@ -183,7 +183,7 @@ public void handleTopicReplyEvent(Map data) { * * @param data */ - private void handleTopicUpvoteEvent(Map data) { + public void handleTopicUpvoteEvent(Map data) { NotificationEvent nEvent = new NotificationEvent(); nEvent.setEventId(Constants.DISCUSSION_UPVOTE_EVENT); Map tagValues = new HashMap<>(); @@ -207,7 +207,7 @@ private void handleTopicUpvoteEvent(Map data) { * * @param data */ - private void handleTopicDownVoteEvent(Map data) { + public void handleTopicDownVoteEvent(Map data) { NotificationEvent nEvent = new NotificationEvent(); nEvent.setEventId(Constants.DISCUSSION_DOWNVOTE_EVENT); Map tagValues = new HashMap<>(); diff --git a/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java b/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java index 39e8f67..1d92c5a 100644 --- a/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java +++ b/HubNotificationHandler/src/test/java/com/eagle/hubnotifier/NotifyHandlerServiceImplTest.java @@ -1,7 +1,8 @@ package com.eagle.hubnotifier; import com.eagle.hubnotifier.config.Configuration; -import com.eagle.hubnotifier.constants.Constants; +import com.eagle.hubnotifier.model.HubPost; +import com.eagle.hubnotifier.model.HubTopic; import com.eagle.hubnotifier.model.HubUser; import com.eagle.hubnotifier.model.NotificationEvent; import com.eagle.hubnotifier.repository.HubPostRepository; @@ -9,32 +10,22 @@ import com.eagle.hubnotifier.repository.HubUserRepository; import com.eagle.hubnotifier.service.impl.NotifyHandlerServiceImpl; import com.eagle.hubnotifier.service.impl.NotifyHookServiceImpl; -import com.eagle.hubnotifier.service.impl.OutboundRequestHandlerServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; +import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; +import java.util.*; -import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) class NotifyHandlerServiceImplTest { - @InjectMocks - private NotifyHookServiceImpl notifyHandlerService; - @Mock private HubUserRepository userRepository; @@ -50,47 +41,83 @@ class NotifyHandlerServiceImplTest { @Mock private NotifyHandlerServiceImpl notifyHandlerServiceImpl; - @Mock - private HashMap mapTest; - - @InjectMocks - private OutboundRequestHandlerServiceImpl outboundRequestHandlerService; - - @Test - void testing() { - assertEquals("amd", "amd"); - } - + final String DISCUSSION_BASE_URL = "base_url"; + final String USER_CONST = "user"; + final String TOPIC_DATA = "topicData"; + final String POST_DATA = "postData"; + final String NOTIFICATION_SERVICE_HOST = "ipaddress"; + final String NOTIFICATION_SERVICE_PATH = "path"; + final String EVENT_ID = "eventId"; + final String ROOT_ORG = "root_Org"; @BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); } + @Test + void handleNotifyKafkaTopicRequest() { + NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class); + doNothing().when(notifyHookService).handleNotifyKafkaTopicRequest(Matchers.any(Map.class)); + } + @Test void handleTopicCreate() throws Exception { - Map topicData = new HashMap<>(); HubUser user = mock(HubUser.class); - topicData.put(Constants.PARAM_TOPIC_TITLE_CONSTANT, Arrays.asList("New topic check")); - topicData.put(Constants.PARAM_TOPIC_TID_CONSTANT, Arrays.asList("1")); - topicData.put(Constants.PARAM_TOPIC_UID_CONSTANT, Arrays.asList("1")); - when(userRepository.findByKey("user")).thenReturn(user); - notifyHandlerService.handleTopicCreate(topicData); + NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class); + when(configuration.getDiscussionCreateUrl()).thenReturn(DISCUSSION_BASE_URL); + when(userRepository.findByKey(USER_CONST)).thenReturn(user); + doNothing().when(notifyHookService).handleTopicCreate(Matchers.any(Map.class)); } @Test - void sendNotification() { + void handleTopicReplyEvent() throws Exception { + HubUser user = mock(HubUser.class); + HubTopic topic = mock(HubTopic.class); + Map> recipients = new HashMap<>(); + Map values = new HashMap<>(); + NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class); NotificationEvent notificationEvent = mock(NotificationEvent.class); - doNothing().when(notifyHandlerServiceImpl).sendNotification(notificationEvent); + when(notificationEvent.getEventId()).thenReturn(EVENT_ID); + when(notificationEvent.getRecipients()).thenReturn(recipients); + when(notificationEvent.getRootOrg()).thenReturn(ROOT_ORG); + when(notificationEvent.getTagValues()).thenReturn(values); + when(notificationEvent.getTargetData()).thenReturn(values); + when(configuration.getDiscussionCreateUrl()).thenReturn(DISCUSSION_BASE_URL); + when(hubTopicRepository.findByKey(TOPIC_DATA)).thenReturn(topic); + when(userRepository.findByKey(USER_CONST)).thenReturn(user); + doNothing().when(notifyHookService).handleTopicReplyEvent(Matchers.any(Map.class)); + } + + @Test + void handleTopicUpvoteEvent() { + HubPost post = mock(HubPost.class); + HubUser user = mock(HubUser.class); + NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class); + when(hubPostRepository.findByKey(POST_DATA)).thenReturn(post); + List userList = new ArrayList<>(); + userList.add(user); + when(userRepository.findByUUIDS(Arrays.asList("1", "2"))).thenReturn(userList); + doNothing().when(notifyHookService).handleTopicUpvoteEvent(Matchers.any(Map.class)); } @Test - void OutBoundGetRequest() { + void handleTopicDownVoteEvent() { + HubPost post = mock(HubPost.class); + HubUser user = mock(HubUser.class); + NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class); + when(hubPostRepository.findByKey(POST_DATA)).thenReturn(post); + List userList = new ArrayList<>(); + userList.add(user); + when(userRepository.findByUUIDS(Arrays.asList("1", "2"))).thenReturn(userList); + doNothing().when(notifyHookService).handleTopicDownVoteEvent(Matchers.any(Map.class)); + } + + @Test + void sendNotification() { NotificationEvent notificationEvent = mock(NotificationEvent.class); - StringBuilder builder = new StringBuilder(); - builder.append("http//localhost:9090/notifyHandler"); - ResponseEntity entity = (ResponseEntity) outboundRequestHandlerService.fetchResultUsingPost(builder, notificationEvent); - assertTrue(entity!=null); - assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)); + when(configuration.getNotifyServiceHost()).thenReturn(NOTIFICATION_SERVICE_HOST); + when(configuration.getNotifyServicePath()).thenReturn(NOTIFICATION_SERVICE_PATH); + doNothing().when(notifyHandlerServiceImpl).sendNotification(notificationEvent); } }