Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 123 additions & 57 deletions HubNotificationHandler/pom.xml
Original file line number Diff line number Diff line change
@@ -1,65 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.17.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.eagle</groupId>
<artifactId>hub-notification-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hub-notification-service</name>
<description>Hu Notification Service</description>
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.17.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eagle</groupId>
<artifactId>hub-notification-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hub-notification-service</name>
<description>Hu Notification Service</description>

<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.8</lombok.version>
</properties>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.8</lombok.version>
<jacoco.plugin.version>0.8.5</jacoco.plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void handleNotifyKafkaTopicRequest(Map<String, Object> data) {
* @param data
*/
@SuppressWarnings("unchecked")
private void handleTopicCreate(Map<String, Object> data) {
public void handleTopicCreate(Map<String, Object> data) {
logger.info("Received Topic Creation Event");
NotificationEvent nEvent = new NotificationEvent();
nEvent.setEventId(Constants.DISCUSSION_CREATION_EVENT_ID);
Expand Down Expand Up @@ -157,7 +157,7 @@ private void handlePostCreate(Map<String, Object> data) {
*
* @param data
*/
private void handleTopicReplyEvent(Map<String, Object> data) {
public void handleTopicReplyEvent(Map<String, Object> data) {
NotificationEvent nEvent = new NotificationEvent();
nEvent.setEventId(Constants.DISCUSSION_REPLY_EVENT_ID);
Map<String, Object> tagValues = new HashMap<>();
Expand All @@ -183,7 +183,7 @@ private void handleTopicReplyEvent(Map<String, Object> data) {
*
* @param data
*/
private void handleTopicUpvoteEvent(Map<String, Object> data) {
public void handleTopicUpvoteEvent(Map<String, Object> data) {
NotificationEvent nEvent = new NotificationEvent();
nEvent.setEventId(Constants.DISCUSSION_UPVOTE_EVENT);
Map<String, Object> tagValues = new HashMap<>();
Expand All @@ -207,7 +207,7 @@ private void handleTopicUpvoteEvent(Map<String, Object> data) {
*
* @param data
*/
private void handleTopicDownVoteEvent(Map<String, Object> data) {
public void handleTopicDownVoteEvent(Map<String, Object> data) {
NotificationEvent nEvent = new NotificationEvent();
nEvent.setEventId(Constants.DISCUSSION_DOWNVOTE_EVENT);
Map<String, Object> tagValues = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.eagle.hubnotifier;

import com.eagle.hubnotifier.config.Configuration;
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;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.*;

import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
class NotifyHandlerServiceImplTest {

@Mock
private HubUserRepository userRepository;

@Mock
public Configuration configuration;

@Mock
private HubPostRepository hubPostRepository;

@Mock
private HubTopicRepository hubTopicRepository;

@Mock
private NotifyHandlerServiceImpl notifyHandlerServiceImpl;

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 {
HubUser user = mock(HubUser.class);
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 handleTopicReplyEvent() throws Exception {
HubUser user = mock(HubUser.class);
HubTopic topic = mock(HubTopic.class);
Map<String, List<String>> recipients = new HashMap<>();
Map<String, Object> values = new HashMap<>();
NotifyHookServiceImpl notifyHookService = mock(NotifyHookServiceImpl.class);
NotificationEvent notificationEvent = mock(NotificationEvent.class);
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<HubUser> userList = new ArrayList<>();
userList.add(user);
when(userRepository.findByUUIDS(Arrays.asList("1", "2"))).thenReturn(userList);
doNothing().when(notifyHookService).handleTopicUpvoteEvent(Matchers.any(Map.class));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look into this again. using this code it will not do any operation when notifyHookService class will be called.

}

@Test
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<HubUser> 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);
when(configuration.getNotifyServiceHost()).thenReturn(NOTIFICATION_SERVICE_HOST);
when(configuration.getNotifyServicePath()).thenReturn(NOTIFICATION_SERVICE_PATH);
doNothing().when(notifyHandlerServiceImpl).sendNotification(notificationEvent);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look into this again. using this code it will not do any operation when notifyHandlerServiceImpl class will be called.

}
}