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 d2de2f10ab57d1bad01b53bb5ad79f3d30eb7d3d..ef8d1ea7f2d5e9d28499e8f05badc783b0854fc5 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 cbe2fdff4f392a30782854bece3495f48a474d00..2d568dfd0338f5be3b9d56292b6c3a3e0fbfc0d3 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); }