diff --git a/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto b/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto index e87eec8e340a95af400bcfa5692cfafd8b9e10c2..d3cfc107e10928b5897a651ae549a2dbf746072f 100644 --- a/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto +++ b/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto @@ -100,6 +100,7 @@ message GrpcPostfachMail { string replyOption = 12; repeated string attachment = 13; GrpcPostfachAddress address = 15; + string referencedNachricht = 16; } message GrpcPostfachAddress { diff --git a/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java b/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java index 1ef63b22e64243eec02001eaa6a22aaee3255360..cb6eccfb3ba9f3e01d5b6917de337b63aaed8ffe 100644 --- a/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java +++ b/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java @@ -30,6 +30,7 @@ public class GrpcPostfachMailTestFactory { public static final String ID = UUID.randomUUID().toString(); public static final String VORGANG_ID = UUID.randomUUID().toString(); + public static final String REFERENCED_NACHRICHT_ID = UUID.randomUUID().toString(); public static final String POSTFACH_ID = UUID.randomUUID().toString(); public static final String CREATED_AT = "2020-04-01T10:30:10Z"; public static final String CREATED_BY = UUID.randomUUID().toString(); @@ -49,6 +50,7 @@ public class GrpcPostfachMailTestFactory { public static GrpcPostfachMail.Builder createBuilder() { return GrpcPostfachMail.newBuilder() .setId(ID) + .setReferencedNachricht(REFERENCED_NACHRICHT_ID) .setVorgangId(VORGANG_ID) .setPostfachId(POSTFACH_ID) .setPostfachAddress(GrpcPostfachAddressTestFactory.create()) diff --git a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java index 5468779b919023df12dbaf2b2d7fcabebccede81..584087077f71ab050b40ded62ea45d73886d2e82 100644 --- a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java +++ b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java @@ -45,6 +45,7 @@ public class PostfachNachricht { public static final String FIELD_POSTFACH_ID = "postfachId"; public static final String POSTFACH_ADDRESS_FIELD = "postfachAddress"; + public static final String REFERENCED_NACHRICHT_FIELD = "referencedNachricht"; public static final String FIELD_MESSAGE_ID = "messageId"; public static final String FIELD_CREATED_AT = "createdAt"; @@ -69,6 +70,7 @@ public class PostfachNachricht { private String postfachId; private PostfachAddress postfachAddress; private String messageId; + private String referencedNachricht; @Builder.Default private ZonedDateTime createdAt = ZonedDateTime.now().withNano(0); diff --git a/nachrichten-manager-server/pom.xml b/nachrichten-manager-server/pom.xml index b5c34fb7b42aa818fcb03fcc3881bc181ba241b2..3bb00de248426a8efea4d9759bb033d1520a6dbc 100644 --- a/nachrichten-manager-server/pom.xml +++ b/nachrichten-manager-server/pom.xml @@ -46,6 +46,7 @@ <ozg-info-manager-interface.version>0.1.0-SNAPSHOT</ozg-info-manager-interface.version> <bayernid-proxy-interface.version>0.1.0</bayernid-proxy-interface.version> <vorgang-manager.version>2.8.0</vorgang-manager.version> + <ozgcloud-starter.version>0.9.0</ozgcloud-starter.version> </properties> <dependencies> @@ -79,6 +80,12 @@ <artifactId>bayernid-proxy-interface</artifactId> <version>${bayernid-proxy-interface.version}</version> </dependency> + + <dependency> + <groupId>de.ozgcloud.api-lib</groupId> + <artifactId>ozg-cloud-spring-boot-starter</artifactId> + <version>${ozgcloud-starter.version}</version> + </dependency> <dependency> <groupId>de.ozgcloud.info</groupId> diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerCallContextProvider.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerCallContextProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..c64af3e512b11fece4881f0875c96b738561622d --- /dev/null +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerCallContextProvider.java @@ -0,0 +1,18 @@ +package de.ozgcloud.nachrichten; + +import org.springframework.stereotype.Component; + +import de.ozgcloud.apilib.common.callcontext.CallContext; +import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider; +import de.ozgcloud.nachrichten.common.grpc.NachrichtenCallContextAttachingInterceptor; +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +class NachrichtenManagerCallContextProvider implements OzgCloudCallContextProvider { + + @Override + public CallContext provideContext() { + return CallContext.builder().clientName(NachrichtenCallContextAttachingInterceptor.NACHRICHTEN_MANAGER_CLIENT_NAME).build(); + } +} \ No newline at end of file diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..6ecace8ab50e3303e56ee0f77f42d36144c9827a --- /dev/null +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java @@ -0,0 +1,25 @@ +package de.ozgcloud.nachrichten; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import de.ozgcloud.apilib.common.command.OzgCloudCommandService; +import de.ozgcloud.apilib.common.command.grpc.CommandMapper; +import de.ozgcloud.apilib.common.command.grpc.GrpcOzgCloudCommandService; +import de.ozgcloud.vorgang.grpc.command.CommandServiceGrpc; +import net.devh.boot.grpc.client.inject.GrpcClient; + +@Configuration +public class NachrichtenManagerConfiguration { + + public static final String OZG_CLOUD_COMMAND_SERVICE_NAME = "nachrichten_OzgCloudCommandService"; + + @GrpcClient("command-manager") + private CommandServiceGrpc.CommandServiceBlockingStub commandServiceStub; + + @Bean(OZG_CLOUD_COMMAND_SERVICE_NAME) + OzgCloudCommandService grpcOzgCloudCommandService(CommandMapper commandMapper, NachrichtenManagerCallContextProvider contextProvider) { + return new GrpcOzgCloudCommandService(commandServiceStub, commandMapper, contextProvider, + GrpcOzgCloudCommandService.DEFAULT_COMMAND_REQUEST_THRESHOLD_MILLIS); + } +} \ No newline at end of file diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtService.java index e02e46a2bf98107a070736fc6b5b6b30e9ec6af4..27c1e0c97e882c7b53af05d37a6927aed810025b 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtService.java @@ -47,16 +47,16 @@ public interface PersistPostfachNachrichtService { /** * Persists the given answer - * + * * @param answer The given answer * @param rueckfrageId The ID of the original Rueckfrage * @return */ - String persistAnswer(String rueckfrageId, PostfachNachricht answer); + String persistAnswer(String referencedNachrichtId, PostfachNachricht answer); /** * Returns all Answers given for the Rueckfrage identified by id - * + * * @param rueckfrageId ID of the Rueckfrage to load answers for. * @return all Answers */ diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImpl.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImpl.java index 378725d403ee2b8514352e30e1f4c519ed107b17..fb49382a0c83d6d3b66c7cdea5c24fe905b6882e 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImpl.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImpl.java @@ -34,18 +34,38 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; +import de.ozgcloud.apilib.common.command.OzgCloudCommand; +import de.ozgcloud.apilib.common.command.OzgCloudCommandService; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId; +import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration; + @Service @Primary public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachrichtService { private static final String NOT_IMPLEMENTED_MESSAGE = "Not implemented. Use gRPC API instead."; + static final String CLIENT = "OzgCloud_NachrichtenManager"; + static final String ITEM_NAME = "PostfachMail"; + + static final String CREATE_ATTACHED_ITEM_ORDER = "CREATE_ATTACHED_ITEM"; + + static final String CLIENT_FIELD = "client"; + static final String VORGANG_ID_FIELD = "vorgangId"; + static final String ITEM_NAME_FIELD = "itemName"; + static final String ITEM_FIELD = "item"; @Autowired @Qualifier("findVorgangRemoteService") private VorgangRemoteService vorgangRemoteService; + @Autowired + @Qualifier(NachrichtenManagerConfiguration.OZG_CLOUD_COMMAND_SERVICE_NAME) + private OzgCloudCommandService commandService; + @Autowired private AttachedItemRemoteService attachedItemRemoteService; + @Autowired + private PostfachNachrichtMapper postfachNachrichtMapper; @Autowired(required = false) @Qualifier("persistPostfachNachrichtByCommandService") @@ -103,15 +123,31 @@ public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachr @Override public Stream<PostfachNachricht> findRueckfragen(String servicekontoType, String postfachId) { - return vorgangRemoteService.findVorgangIds(servicekontoType, postfachId).flatMap(attachedItemRemoteService::findReplyAllowedPostfachNachrichts); + return vorgangRemoteService.findVorgangIds(servicekontoType, postfachId) + .flatMap(attachedItemRemoteService::findReplyAllowedPostfachNachrichts); } @Override - public String persistAnswer(String rueckfrageId, PostfachNachricht answer) { - if (nonNull(vorgangManagerNachrichtService)) { - return vorgangManagerNachrichtService.persistAnswer(rueckfrageId, answer); - } - throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE); + public String persistAnswer(String referencedNachrichtId, PostfachNachricht answer) { + answer = answer.toBuilder().referencedNachricht(referencedNachrichtId).build(); + + return commandService.createAndWaitUntilDone(buildCommand(answer)).getId().toString(); + } + + OzgCloudCommand buildCommand(PostfachNachricht nachricht) { + return OzgCloudCommand.builder().order(CREATE_ATTACHED_ITEM_ORDER) + .vorgangId(OzgCloudVorgangId.from(nachricht.getVorgangId())) + .relationId(OzgCloudVorgangId.from(nachricht.getVorgangId())) + .bodyObject(buildCommandBody(nachricht)) + .build(); + } + + Map<String, Object> buildCommandBody(PostfachNachricht nachricht) { + return Map.of( + CLIENT_FIELD, CLIENT, + VORGANG_ID_FIELD, nachricht.getVorgangId(), + ITEM_NAME_FIELD, ITEM_NAME, + ITEM_FIELD, postfachNachrichtMapper.mapToMap(nachricht)); } @Override diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java index 780e100abcc0d767d61445a7de5a8b600f057ed0..85b189a2d8e9369053bcacbe8c4681b136bed65a 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java @@ -24,10 +24,13 @@ package de.ozgcloud.nachrichten.postfach; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import org.apache.commons.collections.MapUtils; @@ -43,7 +46,9 @@ import de.ozgcloud.nachrichten.postfach.PostfachNachricht.Direction; import de.ozgcloud.vorgang.common.GrpcObject; import de.ozgcloud.vorgang.common.grpc.GrpcObjectMapper; import de.ozgcloud.vorgang.vorgangAttachedItem.GrpcVorgangAttachedItem; +import lombok.extern.log4j.Log4j2; +@Log4j2 @Mapper(unmappedTargetPolicy = ReportingPolicy.WARN) public abstract class PostfachNachrichtMapper { @@ -71,6 +76,7 @@ public abstract class PostfachNachrichtMapper { var postfachMailBuilder = GrpcPostfachMail.newBuilder() .setId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_ID)) .setVorgangId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_VORGANG_ID)) + .setReferencedNachricht(MapUtils.getString(mailMap, PostfachNachricht.REFERENCED_NACHRICHT_FIELD, StringUtils.EMPTY)) .setPostfachId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_POSTFACH_ID, StringUtils.EMPTY)) .setCreatedAt(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_AT)) .setCreatedBy(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_BY, StringUtils.EMPTY)) @@ -109,6 +115,7 @@ public abstract class PostfachNachrichtMapper { var postfachMailBuilder = PostfachNachricht.builder() .id(MapUtils.getString(mailMap, PostfachNachricht.FIELD_ID)) .vorgangId(MapUtils.getString(mailMap, (PostfachNachricht.FIELD_VORGANG_ID))) + .referencedNachricht(MapUtils.getString(mailMap, PostfachNachricht.REFERENCED_NACHRICHT_FIELD, StringUtils.EMPTY)) .postfachId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_POSTFACH_ID, StringUtils.EMPTY)) .messageId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_MESSAGE_ID)) .createdAt(ZonedDateTime.parse(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_AT))) @@ -169,4 +176,58 @@ public abstract class PostfachNachrichtMapper { Map<String, Object> getAsMap(Map<String, Object> map, String fieldName) { return MapUtils.getMap(map, fieldName, Collections.emptyMap()); } + + public Map<String, Object> mapToMap(PostfachNachricht nachricht) { + var result = new HashMap<>(Map.of( + PostfachNachricht.FIELD_VORGANG_ID, nachricht.getVorgangId(), + PostfachNachricht.FIELD_CREATED_AT, nachricht.getCreatedAt().format(DateTimeFormatter.ISO_DATE_TIME), + PostfachNachricht.FIELD_DIRECTION, nachricht.getDirection().name(), + PostfachNachricht.FIELD_REPLY_OPTION, nachricht.getReplyOption().name(), + PostfachNachricht.FIELD_SUBJECT, nachricht.getSubject(), + PostfachNachricht.FIELD_MAIL_BODY, nachricht.getMailBody(), + PostfachNachricht.FIELD_ATTACHMENTS, nachricht.getAttachments())); + + putIfNonNull(result, PostfachNachricht.FIELD_POSTFACH_ID, nachricht.getPostfachId()); + putIfNonNull(result, PostfachNachricht.FIELD_MESSAGE_ID, nachricht.getMessageId()); + putIfNonNull(result, PostfachNachricht.FIELD_CREATED_BY, nachricht.getCreatedBy()); + putIfNonNull(result, PostfachNachricht.FIELD_SENT_SUCCESSFUL, nachricht.getSentSuccessful()); + putIfNonNull(result, PostfachNachricht.FIELD_MESSAGE_CODE, nachricht.getMessageCode()); + putIfNonNull(result, PostfachNachricht.REFERENCED_NACHRICHT_FIELD, nachricht.getReferencedNachricht()); + putDateIfNonNull(result, PostfachNachricht.FIELD_SENT_AT, nachricht.getSentAt()); + + Optional.ofNullable(nachricht.getPostfachAddress()).map(this::buildPostfachAddressMap) + .ifPresent(postfachMaiMap -> result.put(PostfachNachricht.POSTFACH_ADDRESS_FIELD, postfachMaiMap)); + + return Collections.unmodifiableMap(result); + } + + private Map<String, Object> putIfNonNull(Map<String, Object> mapIn, String key, Object value) { + if (Objects.nonNull(value)) { + mapIn.put(key, value); + } + return mapIn; + } + + private Map<String, Object> putDateIfNonNull(Map<String, Object> mapIn, String key, Object value) { + if (Objects.nonNull(value)) { + mapIn.put(key, ((ZonedDateTime) value).format(DateTimeFormatter.ISO_DATE_TIME)); + } + return mapIn; + } + + private Map<String, Object> buildPostfachAddressMap(PostfachAddress postfachAddress) { + var resultMap = new HashMap<String, Object>(); + resultMap.put(PostfachAddress.IDENTIFIER_FIELD, buildPostfachAddressIdentifierMap(postfachAddress)); + resultMap.put(PostfachAddress.VERSION_FIELD, postfachAddress.getVersion()); + resultMap.put(PostfachAddress.TYPE_FIELD, postfachAddress.getType()); + Optional.ofNullable(postfachAddress.getServiceKontoType()).ifPresentOrElse( + serviceKontoType -> resultMap.put(PostfachAddress.SERVICEKONTO_TYPE_FIELD, serviceKontoType), + () -> LOG.warn("ServiceKontoType is null")); + return Collections.unmodifiableMap(resultMap); + } + + private Map<String, Object> buildPostfachAddressIdentifierMap(PostfachAddress postfachAddress) { + var identifier = (StringBasedIdentifier) postfachAddress.getIdentifier(); + return Map.of(PostfachNachricht.FIELD_POSTFACH_ID, identifier.getPostfachId()); + } } \ No newline at end of file diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImplTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..15cd5e32054a2dffaf8cc06eab8888d27eeda5cb --- /dev/null +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PersistPostfachNachrichtServiceImplTest.java @@ -0,0 +1,163 @@ +package de.ozgcloud.nachrichten.postfach; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.Map; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; + +import de.ozgcloud.apilib.common.command.OzgCloudCommand; +import de.ozgcloud.apilib.common.command.OzgCloudCommandId; +import de.ozgcloud.apilib.common.command.OzgCloudCommandService; +import de.ozgcloud.nachrichten.postfach.osi.MessageTestFactory; + +class PersistPostfachNachrichtServiceImplTest { + + @Spy + @InjectMocks + private PersistPostfachNachrichtServiceImpl service; + @Mock + private OzgCloudCommandService commandService; + @Mock + private AttachedItemRemoteService attachedItemRemoteService; + @Mock + private PostfachNachrichtMapper postfachNachrichtMapper; + + @DisplayName("Persist answer") + @Nested + class TestPersistAnswer { + + @Captor + private ArgumentCaptor<PostfachNachricht> postfachNachrichtCaptor; + + private final OzgCloudCommand command = OzgCloudCommand.builder().id(OzgCloudCommandId.from(UUID.randomUUID().toString())).build(); + private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.createBuilder().referencedNachricht(null).build(); + + @BeforeEach + void mock() { + doReturn(command).when(service).buildCommand(any()); + when(commandService.createAndWaitUntilDone(any())).thenReturn(command); + } + + @Test + void shouldCallBuildCommand() { + service.persistAnswer(GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID, postfachNachricht); + + verify(service).buildCommand(postfachNachrichtCaptor.capture()); + assertThat(postfachNachrichtCaptor.getValue().getReferencedNachricht()).isEqualTo(GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID); + } + + @Test + void shouldCallCommandService() { + service.persistAnswer(GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID, postfachNachricht); + + verify(commandService).createAndWaitUntilDone(any()); + } + } + + @DisplayName("Build command") + @Nested + class TestBuildCommand { + + private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); + + @BeforeEach + void mock() { + doReturn(Map.of("dummyKey", "dummyValue")).when(service).buildCommandBody(any()); + } + + @Test + void shouldHaveSetOrder() { + var command = service.buildCommand(postfachNachricht); + + assertThat(command.getOrder()).isEqualTo(PersistPostfachNachrichtServiceImpl.CREATE_ATTACHED_ITEM_ORDER); + } + + @Test + void shouldHaveSetVorgangId() { + var command = service.buildCommand(postfachNachricht); + + assertThat(command.getVorgangId()).hasToString(MessageTestFactory.VORGANG_ID); + } + + @Test + void shouldHaveSetRelationId() { + var command = service.buildCommand(postfachNachricht); + + assertThat(command.getRelationId()).hasToString(MessageTestFactory.VORGANG_ID); + } + + @Test + void shouldCallBuildCommandBody() { + service.buildCommand(postfachNachricht); + + verify(service).buildCommandBody(postfachNachricht); + } + + @Test + void shouldHaveSetBodyObjet() { + var command = service.buildCommand(postfachNachricht); + + assertThat(command.getBodyObject()).isNotEmpty(); + } + } + + @DisplayName("Build command body") + @Nested + class TestBuildCommandBody { + + private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); + private Map<String, Object> postfachNachrichtAsMap = Map.of("dummyKey", "dummyValue"); + + @BeforeEach + void mock() { + when(postfachNachrichtMapper.mapToMap(any())).thenReturn(postfachNachrichtAsMap); + } + + @Test + void shouldHaveSetClient() { + var body = service.buildCommandBody(postfachNachricht); + + assertThat(body).containsEntry(PersistPostfachNachrichtServiceImpl.CLIENT_FIELD, PersistPostfachNachrichtServiceImpl.CLIENT); + } + + @Test + void shouldHaveSetVorgangId() { + var body = service.buildCommandBody(postfachNachricht); + + assertThat(body).containsEntry(PersistPostfachNachrichtServiceImpl.VORGANG_ID_FIELD, MessageTestFactory.VORGANG_ID); + } + + @Test + void shouldHaveSetItemName() { + var body = service.buildCommandBody(postfachNachricht); + + assertThat(body).containsEntry(PersistPostfachNachrichtServiceImpl.ITEM_NAME_FIELD, PersistPostfachNachrichtServiceImpl.ITEM_NAME); + } + + @Test + void shouldCallMapper() { + service.buildCommandBody(postfachNachricht); + + verify(postfachNachrichtMapper).mapToMap(postfachNachricht); + } + + @Test + void shouldHaveSetItem() { + var body = service.buildCommandBody(postfachNachricht); + + assertThat(body).containsEntry(PersistPostfachNachrichtServiceImpl.ITEM_FIELD, postfachNachrichtAsMap); + } + } +} diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java index 3964f981ad91a24b27d679420b55d43e0dbc8ed2..adfc03a3811ecb8059370480d93df84c496def8e 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java @@ -82,7 +82,8 @@ class PostfachEventListenerTest { verify(service).sendMail(eq(COMMAND_ID), eq(USER_ID), nachrichtCaptor.capture()); assertThat(nachrichtCaptor.getValue()) - .usingRecursiveComparison().ignoringFields("id", "createdAt", "messageCode", "messageId", "sentAt", "sentSuccessful") + .usingRecursiveComparison() + .ignoringFields("id", "createdAt", "messageCode", "messageId", "sentAt", "sentSuccessful", "referencedNachricht") .isEqualTo(PostfachNachrichtTestFactory.createBuilder().direction(Direction.OUT).build()); } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java index ad4c9544f7cf39b6f5f22ffa9e13444e5b520b71..344a5073a639ccc488b9d38a62ea5fbc792a9f44 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java @@ -283,4 +283,52 @@ class PostfachNachrichtMapperTest { return field.get(obj); } + + @DisplayName("Map to map") + @Nested + class TestMapToMap { + + @Test + void shouldContainAllFieldsIgnoringId() { + var map = mapper.mapToMap(PostfachNachrichtTestFactory.createBuilder().id(null).build()); + + var mapWithoutId = PostfachNachrichtTestFactory.asMap(); + mapWithoutId.remove("id"); + + assertThat(map).containsAllEntriesOf(mapWithoutId); + } + + @Test + void shouldIgnoreEmptyMessageId() { + assertDoesNotThrow(() -> mapper.mapToMap(PostfachNachrichtTestFactory.createBuilder().messageId(null).build())); + } + + @Test + void shouldIgnoreEmptyCreatedBy() { + assertDoesNotThrow(() -> mapper.mapToMap(PostfachNachrichtTestFactory.createBuilder().createdBy(null).build())); + } + + @Test + void shouldHandleNullAsPostfachId() { + var postfachMail = PostfachNachrichtTestFactory.createBuilder().postfachId(null).build(); + + assertDoesNotThrow(() -> mapper.mapToMap(postfachMail)); + } + + @Test + void shouldProceedWithEmptyPostfachAddress() { + var postfachMail = PostfachNachrichtTestFactory.createBuilder().postfachAddress(null).build(); + + assertDoesNotThrow(() -> mapper.mapToMap(postfachMail)); + } + + @Test + void shouldHandleEmptyReferencedNachrichtId() { + var postfachNachricht = PostfachNachrichtTestFactory.createBuilder().id(null) + .referencedNachricht(null) + .build(); + + assertDoesNotThrow(() -> mapper.mapToMap(postfachNachricht)); + } + } } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java index 23276159b678b7b0501794021e79c7fbe2241283..d58ab79a63e7b6984c5c9d641bef9460086e2117 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java @@ -60,6 +60,7 @@ public class PostfachNachrichtTestFactory { public static PostfachNachricht.PostfachNachrichtBuilder createBuilder() { return PostfachNachricht.builder() .id(ID) + .referencedNachricht(GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID) .postfachId(POSTFACH_ID) .postfachAddress(PostfachAddressTestFactory.create()) .messageId(MESSAGE_ID) @@ -79,6 +80,7 @@ public class PostfachNachrichtTestFactory { public static Map<String, Object> asMap() { var map = new HashMap<String, Object>(); map.put(PostfachNachricht.FIELD_ID, ID); + map.put(PostfachNachricht.REFERENCED_NACHRICHT_FIELD, GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID); map.put(PostfachNachricht.FIELD_POSTFACH_ID, POSTFACH_ID); map.put(PostfachNachricht.POSTFACH_ADDRESS_FIELD, getPostfachAddressAsMap()); map.put(PostfachNachricht.FIELD_MESSAGE_ID, MESSAGE_ID);