Skip to content

Commit

Permalink
feat: integrate live event trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 26, 2020
1 parent 04486e0 commit ef5b276
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,25 @@
import de.caritas.cob.messageservice.api.model.MessageDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.group.GetGroupInfoDto;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.PostMessageResponseDTO;
import de.caritas.cob.messageservice.api.service.LiveEventNotificationService;
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.api.service.RocketChatService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/*
* Facade to encapsulate the steps for posting a (group) message to Rocket.Chat
*/
@Service
@RequiredArgsConstructor
public class PostGroupMessageFacade {

private static final String FEEDBACK_GROUP_IDENTIFIER = "feedback";
private final RocketChatService rocketChatService;
private final EmailNotificationFacade emailNotificationFacade;

/**
* Constructor
*
* @param rocketChatService {@link RocketChatService}
*/
@Autowired
public PostGroupMessageFacade(RocketChatService rocketChatService,
EmailNotificationFacade emailNotificationFacade) {
this.rocketChatService = rocketChatService;
this.emailNotificationFacade = emailNotificationFacade;
}
private final @NonNull RocketChatService rocketChatService;
private final @NonNull EmailNotificationFacade emailNotificationFacade;
private final @NonNull LiveEventNotificationService liveEventNotificationService;

/**
* Posts a message to the given Rocket.Chat group id and sends out a notification e-mail via the
Expand All @@ -51,6 +44,7 @@ public void postGroupMessage(String rcToken, String rcUserId, String rcGroupId,
MessageDTO message) {

postRocketChatGroupMessage(rcToken, rcUserId, rcGroupId, message.getMessage(), null);
this.liveEventNotificationService.sendLiveEvent(rcGroupId);

if (isTrue(message.getSendNotification())) {
emailNotificationFacade.sendEmailNotification(rcGroupId);
Expand All @@ -71,6 +65,7 @@ public void postFeedbackGroupMessage(String rcToken, String rcUserId, String rcF

validateFeedbackChatId(rcToken, rcUserId, rcFeedbackGroupId);
postRocketChatGroupMessage(rcToken, rcUserId, rcFeedbackGroupId, message, alias);
this.liveEventNotificationService.sendLiveEvent(rcFeedbackGroupId);
emailNotificationFacade.sendFeedbackEmailNotification(rcFeedbackGroupId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.caritas.cob.messageservice.api.service;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* Service class to provide live event triggers to the live proxy endpoint in user service.
*/
@Service
@RequiredArgsConstructor
public class LiveEventNotificationService {

private final @NonNull LiveproxyControllerApi liveproxyControllerApi;

/**
* Triggers a live event to proxy endpoint of user service.
*
* @param rcGroupId the rocket chat group id
*/
public void sendLiveEvent(String rcGroupId) {
if (isNotBlank(rcGroupId)) {
this.liveproxyControllerApi.sendLiveEvent(rcGroupId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.caritas.cob.messageservice.api.exception.InternalServerErrorException;
import de.caritas.cob.messageservice.api.exception.RocketChatPostMessageException;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.PostMessageResponseDTO;
import de.caritas.cob.messageservice.api.service.LiveEventNotificationService;
import de.caritas.cob.messageservice.api.service.RocketChatService;
import java.util.Date;
import org.junit.Test;
Expand All @@ -38,11 +39,16 @@ public class PostGroupMessageFacadeTest {

@InjectMocks
private PostGroupMessageFacade postGroupMessageFacade;

@Mock
private RocketChatService rocketChatService;

@Mock
private EmailNotificationFacade emailNotificationFacade;

@Mock
private LiveEventNotificationService liveEventNotificationService;

/**
* Tests for method: postGroupMessage
*/
Expand Down Expand Up @@ -223,4 +229,32 @@ public void postFeedbackGroupMessage_Should_ReturnBadRequestAndNotSendFeedbackNo

verify(emailNotificationFacade, times(0)).sendFeedbackEmailNotification(RC_GROUP_ID);
}

@Test
public void postGroupMessage_Should_sendLiveNotification_When_RocketChatServiceSucceeds()
throws CustomCryptoException {
when(rocketChatService.postGroupMessage(RC_TOKEN, RC_USER_ID, RC_GROUP_ID, MESSAGE, null))
.thenReturn(POST_MESSAGE_RESPONSE_DTO);

postGroupMessageFacade.postGroupMessage(
RC_TOKEN, RC_USER_ID, RC_GROUP_ID, MESSAGE_DTO_WITHOUT_NOTIFICATION);

verify(this.liveEventNotificationService, times(1)).sendLiveEvent(RC_GROUP_ID);
}

@Test
public void postFeedbackGroupMessage_Should_sendLiveNotification_When_RocketChatServiceSucceeds()
throws CustomCryptoException {
when(rocketChatService.getGroupInfo(RC_TOKEN, RC_USER_ID, RC_FEEDBACK_GROUP_ID))
.thenReturn(GET_GROUP_INFO_DTO_FEEDBACK_CHAT);
when(rocketChatService.postGroupMessage(
RC_TOKEN, RC_USER_ID, RC_FEEDBACK_GROUP_ID, MESSAGE, null))
.thenReturn(POST_MESSAGE_RESPONSE_DTO);

postGroupMessageFacade.postFeedbackGroupMessage(
RC_TOKEN, RC_USER_ID, RC_FEEDBACK_GROUP_ID, MESSAGE, null);

verify(this.liveEventNotificationService, times(1)).sendLiveEvent(RC_FEEDBACK_GROUP_ID);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.caritas.cob.messageservice.api.service;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;

import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class LiveEventNotificationServiceTest {

@InjectMocks
private LiveEventNotificationService liveEventNotificationService;

@Mock
private LiveproxyControllerApi liveproxyControllerApi;

@Test
public void sendLiveEvent_Should_notTriggerLiveEvent_When_rcGroupIdIsNull() {
this.liveEventNotificationService.sendLiveEvent(null);

verifyZeroInteractions(this.liveproxyControllerApi);
}

@Test
public void sendLiveEvent_Should_notTriggerLiveEvent_When_rcGroupIdIsEmpty() {
this.liveEventNotificationService.sendLiveEvent("");

verifyZeroInteractions(this.liveproxyControllerApi);
}

@Test
public void sendLiveEvent_Should_triggerLiveEvent_When_rcGroupIdIsValid() {
this.liveEventNotificationService.sendLiveEvent("valid");

verify(this.liveproxyControllerApi, times(1)).sendLiveEvent(eq("valid"));
}

}

0 comments on commit ef5b276

Please sign in to comment.