Skip to content

Commit

Permalink
feat: new message format
Browse files Browse the repository at this point in the history
  • Loading branch information
daho4b committed Sep 21, 2021
1 parent 79812c5 commit 4dc0c15
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 43 deletions.
13 changes: 11 additions & 2 deletions services/statisticsservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,28 @@ components:
- "ASSIGN_SESSION"
- "CREATE_MESSAGE"

UserRole:
type: string
enum:
- "CONSULTANT"
- "ASKER"

StatisticsEventMessage:
type: object
required:
- eventType
- consultantId
- userId
- userRole
- timestamp
properties:
eventType:
$ref: '#/components/schemas/EventType'
consultantId:
userId:
type: string
description: The keycloak id of the consultant
example: d63f4cc0-215d-40e2-a866-2d3e910f0590
userRole:
$ref: '#/components/schemas/UserRole'
timestamp:
type: string
format: date-time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.caritas.cob.messageservice.api.service.RocketChatService;
import de.caritas.cob.messageservice.api.service.statistics.StatisticsService;
import de.caritas.cob.messageservice.api.service.statistics.event.CreateMessageStatisticsEvent;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -71,10 +72,17 @@ public void postGroupMessage(
emailNotificationFacade.sendEmailNotification(rcGroupId);
}

if (AuthenticatedUserHelper.isConsultant(authenticatedUser)) {
statisticsService.fireEvent(
new CreateMessageStatisticsEvent(authenticatedUser.getUserId(), rcGroupId, false));
}
statisticsService.fireEvent(
new CreateMessageStatisticsEvent(
authenticatedUser.getUserId(),
resolveUserRole(authenticatedUser),
rcGroupId,
false));
}

private UserRole resolveUserRole(AuthenticatedUser authenticatedUser) {
return
(AuthenticatedUserHelper.isConsultant(authenticatedUser)) ? UserRole.CONSULTANT : UserRole.ASKER;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.api.service.statistics.event.StatisticsEvent;
import java.nio.charset.StandardCharsets;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -18,6 +21,7 @@ public class StatisticsService {

@Value("${statistics.enabled}")
private boolean statisticsEnabled;

@Value("${statistics.rabbitmq.exchange.name}")
private String rabbitMqExchangeName;

Expand All @@ -35,7 +39,11 @@ public void fireEvent(StatisticsEvent statisticsEvent) {
.ifPresentOrElse(
payload ->
amqpTemplate.convertAndSend(
rabbitMqExchangeName, statisticsEvent.getEventType().toString(), payload),
rabbitMqExchangeName,
statisticsEvent.getEventType().toString(),
MessageBuilder.withBody(payload.getBytes(StandardCharsets.UTF_8))
.setContentType(MessageProperties.CONTENT_TYPE_JSON)
.build()),
() ->
LogService.logStatisticsEventWarning(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.CreateMessageStatisticsEventMessage;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,7 +18,8 @@ public class CreateMessageStatisticsEvent implements StatisticsEvent {

private static final EventType EVENT_TYPE = EventType.CREATE_MESSAGE;

private @NonNull String consultantId;
private @NonNull String userId;
private @NonNull UserRole userRole;
private @NonNull String rcGroupId;
private @NonNull Boolean hasAttachment;

Expand All @@ -37,7 +39,8 @@ public EventType getEventType() {
private CreateMessageStatisticsEventMessage createCreateMessageStatisticsEventMessage() {
return new CreateMessageStatisticsEventMessage()
.eventType(EVENT_TYPE)
.consultantId(consultantId)
.userId(userId)
.userRole(userRole)
.rcGroupId(rcGroupId)
.hasAttachment(hasAttachment)
.timestamp(CustomOffsetDateTime.nowInUtc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import de.caritas.cob.messageservice.api.service.RocketChatService;
import de.caritas.cob.messageservice.api.service.statistics.StatisticsService;
import de.caritas.cob.messageservice.api.service.statistics.event.CreateMessageStatisticsEvent;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import java.util.Date;
import java.util.Objects;
import org.apache.commons.collections4.SetUtils;
import org.hamcrest.Matchers;
import org.jeasy.random.EasyRandom;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -84,6 +84,10 @@ public class PostGroupMessageFacadeTest {
@Before
public void setup() {
setField(this.postGroupMessageFacade, "rocketChatSystemUserId", RC_SYSTEM_USER_ID);
when(authenticatedUser.getRoles())
.thenReturn(SetUtils.unmodifiableSet(Role.CONSULTANT.getRoleName()));
when(authenticatedUser.getUserId())
.thenReturn(CONSULTANT_ID);
}

/**
Expand Down Expand Up @@ -323,10 +327,6 @@ public void postGroupMessage_Should_deleteDraftMessage_When_RocketChatServiceSuc
public void postGroupMessage_Should_FireCreateMessageStatisticsEvent()
throws CustomCryptoException {

when(authenticatedUser.getRoles())
.thenReturn(SetUtils.unmodifiableSet(Role.CONSULTANT.getRoleName()));
when(authenticatedUser.getUserId())
.thenReturn(CONSULTANT_ID);
when(rocketChatService.postGroupMessage(RC_TOKEN, RC_USER_ID, RC_GROUP_ID, MESSAGE, null))
.thenReturn(POST_MESSAGE_RESPONSE_DTO);

Expand All @@ -339,9 +339,12 @@ public void postGroupMessage_Should_FireCreateMessageStatisticsEvent()
ArgumentCaptor<CreateMessageStatisticsEvent> captor = ArgumentCaptor.forClass(
CreateMessageStatisticsEvent.class);
verify(statisticsService, times(1)).fireEvent(captor.capture());
String consultantId = Objects.requireNonNull(
ReflectionTestUtils.getField(captor.getValue(), "consultantId")).toString();
assertThat(consultantId, is(CONSULTANT_ID));
String userId = Objects.requireNonNull(
ReflectionTestUtils.getField(captor.getValue(), "userId")).toString();
assertThat(userId, is(CONSULTANT_ID));
String userRole = Objects.requireNonNull(
ReflectionTestUtils.getField(captor.getValue(), "userRole")).toString();
assertThat(userRole, is(UserRole.CONSULTANT.toString()));
String rcGroupId = Objects.requireNonNull(
ReflectionTestUtils.getField(captor.getValue(), "rcGroupId")).toString();
assertThat(rcGroupId, is(RC_GROUP_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.CreateMessageStatisticsEventMessage;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
Expand Down Expand Up @@ -46,7 +47,8 @@ public void serialize_Should_returnOptionalWithSerializedObject() {
new CreateMessageStatisticsEventMessage()
.eventType(EventType.CREATE_MESSAGE)
.rcGroupId(RC_GROUP_ID)
.consultantId(CONSULTANT_ID)
.userId(CONSULTANT_ID)
.userRole(UserRole.CONSULTANT)
.timestamp(offsetDateTime)
.hasAttachment(false);

Expand All @@ -61,9 +63,12 @@ public void serialize_Should_returnOptionalWithSerializedObject() {
+ " \"rcGroupId\":\""
+ RC_GROUP_ID
+ "\","
+ " \"consultantId\":\""
+ " \"userId\":\""
+ CONSULTANT_ID
+ "\","
+ " \"userRole\":\""
+ UserRole.CONSULTANT
+ "\","
+ " \"timestamp\":\""
+ offsetDateTime.format(DateTimeFormatter.ISO_DATE_TIME)
+ "\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.caritas.cob.messageservice.api.service.statistics.event.CreateMessageStatisticsEvent;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.CreateMessageStatisticsEventMessage;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import de.caritas.cob.messageservice.testConfig.RabbitMqTestConfig;
import java.io.IOException;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -45,14 +46,7 @@ public void fireEvent_Should_Send_ExpectedCreateMessageStatisticsEventMessageToQ
throws IOException {

CreateMessageStatisticsEvent createMessageStatisticsEvent =
new CreateMessageStatisticsEvent(CONSULTANT_ID, RC_GROUP_ID, false);
CreateMessageStatisticsEventMessage createMessageStatisticsEventMessage =
new CreateMessageStatisticsEventMessage()
.eventType(EventType.CREATE_MESSAGE)
.consultantId(CONSULTANT_ID)
.rcGroupId(RC_GROUP_ID)
.hasAttachment(false)
.timestamp(CustomOffsetDateTime.nowInUtc());
new CreateMessageStatisticsEvent(CONSULTANT_ID, UserRole.CONSULTANT, RC_GROUP_ID, false);

statisticsService.fireEvent(createMessageStatisticsEvent);
Message message =
Expand All @@ -64,9 +58,12 @@ public void fireEvent_Should_Send_ExpectedCreateMessageStatisticsEventMessageToQ
+ " \"rcGroupId\":\""
+ RC_GROUP_ID
+ "\","
+ " \"consultantId\":\""
+ " \"userId\":\""
+ CONSULTANT_ID
+ "\","
+ " \"userRole\":\""
+ UserRole.CONSULTANT
+ "\","
+ " \"timestamp\":\""
+ CustomOffsetDateTime.nowInUtc()
+ "\","
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.caritas.cob.messageservice.api.service.statistics;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
Expand All @@ -11,6 +12,7 @@
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.api.service.statistics.event.CreateMessageStatisticsEvent;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -21,6 +23,9 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.messaging.Message;

@RunWith(MockitoJUnitRunner.class)
public class StatisticsServiceTest {
Expand All @@ -29,16 +34,11 @@ public class StatisticsServiceTest {
private static final String FIELD_NAME_RABBIT_EXCHANGE_NAME = "rabbitMqExchangeName";
private static final String RABBIT_EXCHANGE_NAME = "exchange";
private static final String PAYLOAD = "payload";

@Mock Logger logger;
private CreateMessageStatisticsEvent createMessageStatisticsEvent;
private EventType eventType = EventType.ASSIGN_SESSION;

@InjectMocks
private StatisticsService statisticsService;
@Mock
private AmqpTemplate amqpTemplate;
@Mock
Logger logger;
@InjectMocks private StatisticsService statisticsService;
@Mock private AmqpTemplate amqpTemplate;

@Before
public void setup() {
Expand All @@ -55,7 +55,7 @@ public void fireEvent_Should_NotSendStatisticsMessage_WhenStatisticsIsDisabled()
setField(statisticsService, FIELD_NAME_STATISTICS_ENABLED, false);
statisticsService.fireEvent(createMessageStatisticsEvent);
verify(amqpTemplate, times(0))
.convertAndSend(eq(RABBIT_EXCHANGE_NAME), anyString(), anyString());
.convertAndSend(eq(RABBIT_EXCHANGE_NAME), anyString(), any(Message.class));
}

@Test
Expand All @@ -67,7 +67,11 @@ public void fireEvent_Should_SendStatisticsMessage_WhenStatisticsIsEnabled() {

statisticsService.fireEvent(createMessageStatisticsEvent);
verify(amqpTemplate, times(1))
.convertAndSend(eq(RABBIT_EXCHANGE_NAME), anyString(), anyString());
.convertAndSend(
eq(RABBIT_EXCHANGE_NAME),
anyString(),
eq(
buildPayloadMessage()));
}

@Test
Expand All @@ -85,7 +89,16 @@ public void fireEvent_Should_UseEventTypeAsTopicAndSendPayloadOfEvent() {
setField(statisticsService, FIELD_NAME_STATISTICS_ENABLED, true);
statisticsService.fireEvent(createMessageStatisticsEvent);
verify(amqpTemplate, times(1))
.convertAndSend(RABBIT_EXCHANGE_NAME, eventType.toString(), PAYLOAD);
.convertAndSend(
RABBIT_EXCHANGE_NAME,
eventType.toString(),
buildPayloadMessage());
}

private org.springframework.amqp.core.Message buildPayloadMessage() {
return MessageBuilder.withBody(PAYLOAD.getBytes(StandardCharsets.UTF_8))
.setContentType(MessageProperties.CONTENT_TYPE_JSON)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

import de.caritas.cob.messageservice.api.helper.CustomOffsetDateTime;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import java.util.Objects;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.UserRole;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class CreateMessageStatisticsEventTest {
Expand All @@ -23,7 +22,8 @@ public class CreateMessageStatisticsEventTest {

@Before
public void setup() throws NoSuchFieldException, IllegalAccessException {
createMessageStatisticsEvent = new CreateMessageStatisticsEvent(CONSULTANT_ID, RC_GROUP_ID, false);
createMessageStatisticsEvent =
new CreateMessageStatisticsEvent(CONSULTANT_ID, UserRole.CONSULTANT, RC_GROUP_ID, false);
}

@Test
Expand All @@ -37,7 +37,8 @@ public void getPayload_Should_ReturnValidJsonPayload() {

String expectedJson = "{"
+ " \"rcGroupId\":\"" + RC_GROUP_ID + "\","
+ " \"consultantId\":\"" + CONSULTANT_ID + "\","
+ " \"userId\":\"" + CONSULTANT_ID + "\","
+ " \"userRole\":\"" + UserRole.CONSULTANT + "\","
+ " \"hasAttachment\": false,"
+ " \"timestamp\":\"" + CustomOffsetDateTime.nowInUtc() + "\","
+ " \"eventType\":\"" + EventType.CREATE_MESSAGE + "\""
Expand Down

0 comments on commit 4dc0c15

Please sign in to comment.