From 32f39b943e70c79ae951939e5f1bc7f150b38698 Mon Sep 17 00:00:00 2001 From: Jan Zickermann <jan.zickermann@dataport.de> Date: Mon, 3 Feb 2025 16:13:48 +0100 Subject: [PATCH] OZG-4095 receive: Avoid exception on any null value --- .../osiv2/transfer/Osi2ResponseMapper.java | 33 ++++++++++--------- .../transfer/Osi2ResponseMapperTest.java | 22 ++++++++++++- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapper.java b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapper.java index d2de2f1..ef8d1ea 100644 --- a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapper.java +++ b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapper.java @@ -51,12 +51,11 @@ public interface Osi2ResponseMapper { @Named("mapOffsetDateTimeToZoned") default ZonedDateTime mapOffsetDateTimeToZoned(OffsetDateTime offsetDateTime) { - return offsetDateTime.toZonedDateTime(); + return offsetDateTime == null ? null : offsetDateTime.toZonedDateTime(); } @Named("mapMailBody") default String mapMailBody(V1ReplyMessage message) { - // According to the API spec `body` is nullable var body = mapNullToEmpty(message.getBody()); return Boolean.TRUE.equals(message.getIsHtml()) ? Osi2HtmlDocument.renderToPlainText(body) @@ -64,23 +63,27 @@ public interface Osi2ResponseMapper { } default PostfachAddress buildPostfachAddressByPostfachId(UUID messageBox) { - return PostfachAddress.builder() - .type(POSTFACH_ADDRESS_TYPE) - .version(POSTFACH_ADDRESS_VERSION) - .identifier(StringBasedIdentifier.builder() - .mailboxId(messageBox.toString()) - .build()) - .serviceKontoType(OsiPostfachRemoteService.POSTFACH_TYPE_OSI) - .build(); + return messageBox == null + ? null + : PostfachAddress.builder() + .type(POSTFACH_ADDRESS_TYPE) + .version(POSTFACH_ADDRESS_VERSION) + .identifier(StringBasedIdentifier.builder() + .mailboxId(messageBox.toString()) + .build()) + .serviceKontoType(OsiPostfachRemoteService.POSTFACH_TYPE_OSI) + .build(); } default PostfachNachricht.ReplyOption mapReplyAction(V1ReplyBehavior replyOption) { - return switch (replyOption) { - case REPLYFORBIDDEN -> PostfachNachricht.ReplyOption.FORBIDDEN; - case REPLYPOSSIBLE -> PostfachNachricht.ReplyOption.POSSIBLE; - case REPLYMANDATORY -> PostfachNachricht.ReplyOption.MANDATORY; - }; + return replyOption == null + ? PostfachNachricht.ReplyOption.FORBIDDEN + : switch (replyOption) { + case REPLYFORBIDDEN -> PostfachNachricht.ReplyOption.FORBIDDEN; + case REPLYPOSSIBLE -> PostfachNachricht.ReplyOption.POSSIBLE; + case REPLYMANDATORY -> PostfachNachricht.ReplyOption.MANDATORY; + }; } @Builder diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapperTest.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapperTest.java index cbe2fdf..2d568df 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapperTest.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2ResponseMapperTest.java @@ -102,7 +102,8 @@ class Osi2ResponseMapperTest { return Stream.of( Arguments.of(V1ReplyBehavior.REPLYPOSSIBLE, PostfachNachricht.ReplyOption.POSSIBLE), Arguments.of(V1ReplyBehavior.REPLYMANDATORY, PostfachNachricht.ReplyOption.MANDATORY), - Arguments.of(V1ReplyBehavior.REPLYFORBIDDEN, PostfachNachricht.ReplyOption.FORBIDDEN) + Arguments.of(V1ReplyBehavior.REPLYFORBIDDEN, PostfachNachricht.ReplyOption.FORBIDDEN), + Arguments.of(null, PostfachNachricht.ReplyOption.FORBIDDEN) ); } @@ -126,6 +127,25 @@ class Osi2ResponseMapperTest { assertThat(result.getMessageId()).isEqualTo(MESSAGE_ID); } + @DisplayName("should not fail if all fields are null") + @Test + void shouldNotFailIfAllFieldsAreNull() { + var nullMessage = new V1ReplyMessage(); + + assertThatCode(() -> mapper.toPostfachNachricht(nullMessage)) + .doesNotThrowAnyException(); + } + + @DisplayName("should not fail if all fields are null and isHtml") + @Test + void shouldNotFailIfAllFieldsAreNullAndIsHtml() { + var nullMessage = new V1ReplyMessage() + .isHtml(true); + + assertThatCode(() -> mapper.toPostfachNachricht(nullMessage)) + .doesNotThrowAnyException(); + } + private PostfachNachricht doMapping() { return mapper.toPostfachNachricht(message); } -- GitLab