Skip to content

Commit

Permalink
feat: optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
daho4b committed Sep 15, 2021
1 parent 63c7265 commit e1b60b1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

/** Special {@link OffsetDateTime} serializer with ISO_DATE_TIME format. */
public class OffsetDateTimeToStringSerializer extends JsonSerializer<OffsetDateTime> {

@Override
public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator,
SerializerProvider serializers) throws IOException {
jsonGenerator.writeObject(offsetDateTime.toString());
public void serialize(
OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializers)
throws IOException {
jsonGenerator.writeObject(offsetDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 java.time.OffsetDateTime;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,7 +16,6 @@
public class CreateMessageStatisticsEvent implements StatisticsEvent {

private static final EventType EVENT_TYPE = EventType.CREATE_MESSAGE;
private static final OffsetDateTime TIMESTAMP = CustomOffsetDateTime.nowInUtc();

private @NonNull String consultantId;
private @NonNull String rcGroupId;
Expand All @@ -42,6 +40,6 @@ private CreateMessageStatisticsEventMessage createCreateMessageStatisticsEventMe
.consultantId(consultantId)
.rcGroupId(rcGroupId)
.hasAttachment(hasAttachment)
.timestamp(TIMESTAMP);
.timestamp(CustomOffsetDateTime.nowInUtc());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ public void convertStringToAliasMessageDTO_Should_returnOptionalEmpty_When_jsonS
@Test
public void serialize_Should_returnOptionalWithSerializedObject() {

OffsetDateTime offsetDateTime = CustomOffsetDateTime.nowInUtc();

CreateMessageStatisticsEventMessage createMessageStatisticsEventMessage =
new CreateMessageStatisticsEventMessage()
.eventType(EventType.CREATE_MESSAGE)
.rcGroupId(RC_GROUP_ID)
.consultantId(CONSULTANT_ID)
.timestamp(offsetDateTime)
.timestamp(CustomOffsetDateTime.nowInUtc())
.hasAttachment(false);

Optional<String> result =
Expand All @@ -64,15 +62,15 @@ public void serialize_Should_returnOptionalWithSerializedObject() {
+ CONSULTANT_ID
+ "\","
+ " \"timestamp\":\""
+ offsetDateTime
+ CustomOffsetDateTime.nowInUtc()
+ "\","
+ " \"eventType\":\""
+ EventType.CREATE_MESSAGE
+ "\","
+ " \"hasAttachment\": false"
+ "}";

assertThat(result.get(), jsonEquals(expectedJson));
assertThat(result.get(), jsonEquals(expectedJson).whenIgnoringPaths("timestamp"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.messageservice.MessageServiceApplication;
import de.caritas.cob.messageservice.api.helper.CustomOffsetDateTime;
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;
Expand Down Expand Up @@ -35,7 +36,6 @@
public class StatisticsServiceIT {

private static final long MAX_TIMEOUT_MILLIS = 5000;
private static final String TIMESTAMP_FIELD_NAME = "TIMESTAMP";

@Autowired StatisticsService statisticsService;
@Autowired AmqpTemplate amqpTemplate;
Expand All @@ -46,19 +46,13 @@ public void fireEvent_Should_Send_ExpectedCreateMessageStatisticsEventMessageToQ

CreateMessageStatisticsEvent createMessageStatisticsEvent =
new CreateMessageStatisticsEvent(CONSULTANT_ID, RC_GROUP_ID, false);
OffsetDateTime staticTimestamp =
(OffsetDateTime) Objects.requireNonNull(
ReflectionTestUtils.getField(
createMessageStatisticsEvent,
CreateMessageStatisticsEvent.class,
TIMESTAMP_FIELD_NAME));
CreateMessageStatisticsEventMessage createMessageStatisticsEventMessage =
new CreateMessageStatisticsEventMessage()
.eventType(EventType.CREATE_MESSAGE)
.consultantId(CONSULTANT_ID)
.rcGroupId(RC_GROUP_ID)
.hasAttachment(false)
.timestamp(staticTimestamp);
.timestamp(CustomOffsetDateTime.nowInUtc());

statisticsService.fireEvent(createMessageStatisticsEvent);
Message message =
Expand All @@ -74,7 +68,7 @@ public void fireEvent_Should_Send_ExpectedCreateMessageStatisticsEventMessageToQ
+ CONSULTANT_ID
+ "\","
+ " \"timestamp\":\""
+ staticTimestamp
+ CustomOffsetDateTime.nowInUtc()
+ "\","
+ " \"eventType\":\""
+ EventType.CREATE_MESSAGE
Expand All @@ -84,7 +78,7 @@ public void fireEvent_Should_Send_ExpectedCreateMessageStatisticsEventMessageToQ

assertThat(
extractBodyFromAmpQMessage(message),
jsonEquals(expectedJson));
jsonEquals(expectedJson).whenIgnoringPaths("timestamp"));
}

private String extractBodyFromAmpQMessage(Message message) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import de.caritas.cob.messageservice.api.helper.CustomOffsetDateTime;
import de.caritas.cob.messageservice.statisticsservice.generated.web.model.EventType;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -18,18 +19,11 @@
@RunWith(MockitoJUnitRunner.class)
public class CreateMessageStatisticsEventTest {

private final String TIMESTAMP_FIELD_NAME = "TIMESTAMP";
private CreateMessageStatisticsEvent createMessageStatisticsEvent;
private String staticTimestamp;

@Before
public void setup() throws NoSuchFieldException, IllegalAccessException {
createMessageStatisticsEvent = new CreateMessageStatisticsEvent(CONSULTANT_ID, RC_GROUP_ID, false);
staticTimestamp = Objects.requireNonNull(ReflectionTestUtils
.getField(createMessageStatisticsEvent,
CreateMessageStatisticsEvent.class,
TIMESTAMP_FIELD_NAME))
.toString();
}

@Test
Expand All @@ -45,14 +39,14 @@ public void getPayload_Should_ReturnValidJsonPayload() {
+ " \"rcGroupId\":\"" + RC_GROUP_ID + "\","
+ " \"consultantId\":\"" + CONSULTANT_ID + "\","
+ " \"hasAttachment\": false,"
+ " \"timestamp\":\"" + staticTimestamp + "\","
+ " \"timestamp\":\"" + CustomOffsetDateTime.nowInUtc() + "\","
+ " \"eventType\":\"" + EventType.CREATE_MESSAGE + "\""
+ "}";

Optional<String> result = createMessageStatisticsEvent.getPayload();

assertThat(result.isPresent(), is(true));
assertThat(result.get(), jsonEquals(expectedJson));
assertThat(result.get(), jsonEquals(expectedJson).whenIgnoringPaths("timestamp"));
}

}

0 comments on commit e1b60b1

Please sign in to comment.