From c48edac8228b6f128f8d73cc3ed4b69dc104b3cf Mon Sep 17 00:00:00 2001 From: mebo4b Date: Tue, 3 Nov 2020 13:17:21 +0100 Subject: [PATCH] feat: prevent live event trigger for empty lists and for enquiries --- .../service/liveevents/LiveEventNotificationService.java | 7 +++++++ .../RelevantUserAccountIdsBySessionProvider.java | 2 +- .../liveevents/LiveEventNotificationServiceTest.java | 8 ++++---- .../RelevantUserAccountIdsBySessionProviderTest.java | 9 +++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationService.java b/src/main/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationService.java index 12faf2926..94cb96f59 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationService.java @@ -1,6 +1,7 @@ package de.caritas.cob.userservice.api.service.liveevents; import static de.caritas.cob.userservice.liveservice.generated.web.model.EventType.DIRECTMESSAGE; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; import static org.apache.commons.lang3.StringUtils.isNotBlank; import de.caritas.cob.userservice.api.helper.AuthenticatedUser; @@ -36,6 +37,12 @@ public void sendLiveDirectMessageEventToUsers(String rcGroupId) { .collectUserIds(rcGroupId).stream() .filter(this::notInitiatingUser) .collect(Collectors.toList()); + triggerLiveEvent(rcGroupId, userIds); + } + } + + private void triggerLiveEvent(String rcGroupId, List userIds) { + if (isNotEmpty(userIds)) { try { this.liveControllerApi.sendLiveEvent(userIds, DIRECTMESSAGE.toString()); } catch (RestClientException e) { diff --git a/src/main/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProvider.java b/src/main/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProvider.java index 8d81b9711..1de698a79 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProvider.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProvider.java @@ -38,7 +38,7 @@ public List collectUserIds(String rcGroupId) { } private List extractDependentUserIds(Session session) { - if (isNull(session)) { + if (isNull(session) || isNull(session.getConsultant())) { return emptyList(); } return Stream.of(session.getUser().getUserId(), session.getConsultant().getId()) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationServiceTest.java index c5a2e7d2a..bc4a55559 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/liveevents/LiveEventNotificationServiceTest.java @@ -3,7 +3,7 @@ import static de.caritas.cob.userservice.liveservice.generated.web.model.EventType.DIRECTMESSAGE; import static java.util.Arrays.asList; -import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -87,6 +87,7 @@ public void sendLiveDirectMessageEventToUsers_Should_doNothing_When_rcGroupIdIsN @Test public void sendLiveDirectMessageEventToUsers_Should_logError_When_apiCallFailes() { when(this.userIdsProviderFactory.byRocketChatGroup(any())).thenReturn(bySessionProvider); + when(this.bySessionProvider.collectUserIds(any())).thenReturn(singletonList("test")); doThrow(new RestClientException("")).when(this.liveControllerApi) .sendLiveEvent(any(), anyString()); @@ -110,13 +111,12 @@ public void sendLiveDirectMessageEventToUsers_Should_sendEventToAllUsersInsteadO } @Test - public void sendLiveDirectMessageEventToUsers_Should_sendEmptyList_When_noIdsAreProvided() { + public void sendLiveDirectMessageEventToUsers_Should_sendNothing_When_noIdsAreProvided() { when(this.userIdsProviderFactory.byRocketChatGroup(any())).thenReturn(this.byChatProvider); this.liveEventNotificationService.sendLiveDirectMessageEventToUsers("group id"); - verify(this.liveControllerApi, times(1)).sendLiveEvent(eq(emptyList()), - eq(DIRECTMESSAGE.toString())); + verifyZeroInteractions(this.liveControllerApi); } @Test diff --git a/src/test/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProviderTest.java b/src/test/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProviderTest.java index 9d2ca481b..f6ce75139 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProviderTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/liveevents/RelevantUserAccountIdsBySessionProviderTest.java @@ -113,4 +113,13 @@ public void collectUserIds_Should_returnAllUserIds_When_authenticatedUserIsOther assertThat(userIds.get(1), is("consultant")); } + @Test + public void collectUserIds_Should_returnEmptyList_When_sessionHasNoConsultant() { + when(sessionRepository.findByFeedbackGroupId(any())).thenReturn(Optional.of(new Session())); + + List userIds = this.bySessionProvider.collectUserIds("rcGroupId"); + + assertThat(userIds, hasSize(0)); + } + }