Skip to content

Commit

Permalink
Merge pull request #51 from CaritasDeutschland/feature-optimize-livee…
Browse files Browse the repository at this point in the history
…vent-trigger

Feature optimize liveevent trigger
  • Loading branch information
mobo4b authored Nov 3, 2020
2 parents 9630e9d + e72f4ac commit 0364d59
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> userIds) {
if (isNotEmpty(userIds)) {
try {
this.liveControllerApi.sendLiveEvent(userIds, DIRECTMESSAGE.toString());
} catch (RestClientException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<String> collectUserIds(String rcGroupId) {
}

private List<String> extractDependentUserIds(Session session) {
if (isNull(session)) {
if (isNull(session) || isNull(session.getConsultant())) {
return emptyList();
}
return Stream.of(session.getUser().getUserId(), session.getConsultant().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> userIds = this.bySessionProvider.collectUserIds("rcGroupId");

assertThat(userIds, hasSize(0));
}

}

0 comments on commit 0364d59

Please sign in to comment.