Skip to content
Snippets Groups Projects
Commit 7178a9ef authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5917 move/impl and cleanup persistNachricht

parent 1104296c
No related branches found
No related tags found
No related merge requests found
...@@ -84,6 +84,7 @@ interface AntragraumNachrichtMapper { ...@@ -84,6 +84,7 @@ interface AntragraumNachrichtMapper {
return ZonedDateTime.parse(sentAt); return ZonedDateTime.parse(sentAt);
} }
@Mapping(target = "sentAtBytes", ignore = true)
@Mapping(target = "mergeFrom", ignore = true) @Mapping(target = "mergeFrom", ignore = true)
@Mapping(target = "clearField", ignore = true) @Mapping(target = "clearField", ignore = true)
@Mapping(target = "clearOneof", ignore = true) @Mapping(target = "clearOneof", ignore = true)
......
package de.ozgcloud.nachrichten.postfach;
import org.mapstruct.ObjectFactory;
import de.ozgcloud.common.datatype.StringBasedValue;
class NachrichtId extends StringBasedValue {
NachrichtId(String vorgangId) {
super(vorgangId);
}
@ObjectFactory
public static NachrichtId from(String vorgangId) {
return new NachrichtId(vorgangId);
}
}
...@@ -29,7 +29,7 @@ import java.util.Map; ...@@ -29,7 +29,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.commons.codec.binary.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
...@@ -37,19 +37,24 @@ import org.springframework.stereotype.Service; ...@@ -37,19 +37,24 @@ import org.springframework.stereotype.Service;
import de.ozgcloud.apilib.common.command.OzgCloudCommand; import de.ozgcloud.apilib.common.command.OzgCloudCommand;
import de.ozgcloud.apilib.common.command.OzgCloudCommandService; import de.ozgcloud.apilib.common.command.OzgCloudCommandService;
import de.ozgcloud.apilib.user.OzgCloudUserId;
import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId; import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId;
import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration; import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration;
import de.ozgcloud.nachrichten.common.vorgang.VorgangService; import de.ozgcloud.nachrichten.common.vorgang.VorgangService;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Service @Service
@Primary @Primary
public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachrichtService { public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachrichtService {
private static final String NOT_IMPLEMENTED_MESSAGE = "Not implemented. Use gRPC API instead."; private static final String NOT_IMPLEMENTED_MESSAGE = "Not implemented. Use gRPC API instead.";
static final String CLIENT = "OzgCloud_NachrichtenManager"; static final String CLIENT = "OzgCloud_NachrichtenManager";
static final String ITEM_NAME = "PostfachMail"; static final String ITEM_NAME = "PostfachMail";
static final String CREATE_ATTACHED_ITEM_ORDER = "CREATE_ATTACHED_ITEM"; static final String CREATE_ATTACHED_ITEM_ORDER = "CREATE_ATTACHED_ITEM";
static final String UPDATE_ATTACHED_ITEM_ORDER = "UPDATE_ATTACHED_ITEM";
static final String CLIENT_FIELD = "client"; static final String CLIENT_FIELD = "client";
static final String VORGANG_ID_FIELD = "vorgangId"; static final String VORGANG_ID_FIELD = "vorgangId";
...@@ -75,11 +80,49 @@ public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachr ...@@ -75,11 +80,49 @@ public class PersistPostfachNachrichtServiceImpl implements PersistPostfachNachr
@Override @Override
public void persistNachricht(Optional<String> userId, PostfachNachricht nachricht) { public void persistNachricht(Optional<String> userId, PostfachNachricht nachricht) {
if (vorgangManagerNachrichtService != null) { var commandBuilder = createBaseCommandBuilder(userId, nachricht);
vorgangManagerNachrichtService.persistNachricht(userId, nachricht); commandService.create(buildNachrichtCommand(commandBuilder, nachricht));
return;
} }
throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE);
OzgCloudCommand.OzgCloudCommandBuilder createBaseCommandBuilder(Optional<String> userId, PostfachNachricht nachricht) {
var commandBuilder = OzgCloudCommand.builder().vorgangId(OzgCloudVorgangId.from(nachricht.getVorgangId()));
userId.map(OzgCloudUserId::from).ifPresent(commandBuilder::createdBy);
return commandBuilder;
}
OzgCloudCommand buildNachrichtCommand(OzgCloudCommand.OzgCloudCommandBuilder commandBuilder, PostfachNachricht nachricht) {
return StringUtils.isBlank(nachricht.getId()) ? buildCreateCommand(commandBuilder, nachricht) : buildUpdateCommand(commandBuilder, nachricht);
}
OzgCloudCommand buildCreateCommand(OzgCloudCommand.OzgCloudCommandBuilder builder, PostfachNachricht nachricht) {
return builder.order(CREATE_ATTACHED_ITEM_ORDER)
.relationId(OzgCloudVorgangId.from(nachricht.getVorgangId()))
.bodyObject(buildCreateItem(nachricht))
.build();
}
private Map<String, Object> buildCreateItem(PostfachNachricht nachricht) {
return Map.of(
CLIENT_FIELD, CLIENT,
VORGANG_ID_FIELD, nachricht.getVorgangId(),
ITEM_NAME_FIELD, ITEM_NAME,
ITEM_FIELD, postfachNachrichtMapper.mapToMap(nachricht));
}
OzgCloudCommand buildUpdateCommand(OzgCloudCommand.OzgCloudCommandBuilder builder, PostfachNachricht nachricht) {
return builder
.order(UPDATE_ATTACHED_ITEM_ORDER)// TOCHECK: Auf Patch umstellen
.relationId(NachrichtId.from(nachricht.getId()))
.bodyObject(buildUpdateItem(nachricht))
.build();
}
private Map<String, Object> buildUpdateItem(PostfachNachricht nachricht) {
return Map.of(
CLIENT_FIELD, CLIENT,
ITEM_FIELD, postfachNachrichtMapper.mapToMap(nachricht));
} }
@Override @Override
......
...@@ -104,6 +104,7 @@ public abstract class PostfachNachrichtMapper { ...@@ -104,6 +104,7 @@ public abstract class PostfachNachrichtMapper {
return (attachments instanceof String attachment && StringUtils.isNotBlank(attachment)) || attachments instanceof Collection; return (attachments instanceof String attachment && StringUtils.isNotBlank(attachment)) || attachments instanceof Collection;
} }
@SuppressWarnings("unchecked")
private Iterable<String> getAttachments(Object attachments) { private Iterable<String> getAttachments(Object attachments) {
if (attachments instanceof String attachment) { if (attachments instanceof String attachment) {
return Collections.singletonList(attachment); return Collections.singletonList(attachment);
......
...@@ -5,6 +5,7 @@ import static org.mockito.ArgumentMatchers.*; ...@@ -5,6 +5,7 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -38,6 +39,276 @@ class PersistPostfachNachrichtServiceImplTest { ...@@ -38,6 +39,276 @@ class PersistPostfachNachrichtServiceImplTest {
@Mock @Mock
private PostfachNachrichtMapper postfachNachrichtMapper; private PostfachNachrichtMapper postfachNachrichtMapper;
@DisplayName("Persist nachricht")
@Nested
class TestPersistNachricht {
private final String userId = UUID.randomUUID().toString();
private final Optional<String> userIdOpt = Optional.of(userId);
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.createBuilder().id(null).build();
private final OzgCloudCommand.OzgCloudCommandBuilder commandBuilder = OzgCloudCommand.builder();
private final OzgCloudCommand command = OzgCloudCommand.builder().build();
@BeforeEach
void mock() {
doReturn(commandBuilder).when(service).createBaseCommandBuilder(any(), any());
doReturn(command).when(service).buildNachrichtCommand(any(), any());
}
@Test
void shouldCreateBaseCommandBuilder() {
service.persistNachricht(userIdOpt, nachricht);
verify(service).createBaseCommandBuilder(userIdOpt, nachricht);
}
@Test
void shouldBuildNachrichtCommand() {
service.persistNachricht(userIdOpt, nachricht);
verify(service).buildNachrichtCommand(commandBuilder, nachricht);
}
@Test
void shouldCallCommandService() {
service.persistNachricht(userIdOpt, nachricht);
verify(commandService).create(command);
}
}
@DisplayName("Create base command builder")
@Nested
class TestCreateBaseComandBuilder {
private final String userId = UUID.randomUUID().toString();
private final Optional<String> userIdOpt = Optional.of(userId);
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.createBuilder().id(null).build();
@Test
void shouldContainVorgangId() {
var command = service.createBaseCommandBuilder(userIdOpt, nachricht).build();
assertThat(command.getVorgangId()).hasToString(MessageTestFactory.VORGANG_ID);
}
@DisplayName("created by")
@Nested
class TestCreatedBy {
@Test
void shouldBeAddIfIsPresent() {
var command = service.createBaseCommandBuilder(userIdOpt, nachricht).build();
assertThat(command.getCreatedBy()).hasToString(userId);
}
@Test
void shouldNotBeAddIfMissing() {
var command = service.createBaseCommandBuilder(Optional.empty(), nachricht).build();
assertThat(command.getCreatedBy()).isNull();
}
}
}
@DisplayName("Build nachricht command")
@Nested
class TestBuildNachrichtCommand {
private final OzgCloudCommand.OzgCloudCommandBuilder commandBuilder = OzgCloudCommand.builder();
private final OzgCloudCommand command = OzgCloudCommand.builder().build();
@DisplayName("on present id")
@Nested
class TestOnPresentId {
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.create();
@BeforeEach
void mock() {
doReturn(command).when(service).buildUpdateCommand(any(), any());
}
@Test
void shouldBuildUpdateCommand() {
service.buildNachrichtCommand(commandBuilder, nachricht);
verify(service).buildUpdateCommand(commandBuilder, nachricht);
}
@Test
void shouldReturnValue() {
var nachrichtCommand = service.buildNachrichtCommand(commandBuilder, nachricht);
assertThat(nachrichtCommand).isEqualTo(command);
}
}
@DisplayName("on missing id")
@Nested
class TestOnMissingId {
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.createBuilder().id(null).build();
@BeforeEach
void mock() {
doReturn(command).when(service).buildCreateCommand(any(), any());
}
@Test
void shouldBuildCreateCommand() {
service.buildNachrichtCommand(commandBuilder, nachricht);
verify(service).buildCreateCommand(commandBuilder, nachricht);
}
@Test
void shouldReturnValue() {
var nachrichtCommand = service.buildNachrichtCommand(commandBuilder, nachricht);
assertThat(nachrichtCommand).isEqualTo(command);
}
}
@DisplayName("for creation")
@Nested
class TestBuildCreateCommand {
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.createBuilder().id(null).build();
@Test
void shouldContainOrder() {
var command = buildCreateCommand();
assertThat(command.getOrder()).isEqualTo(PersistPostfachNachrichtServiceImpl.CREATE_ATTACHED_ITEM_ORDER);
}
@Test
void shouldContainRelationId() {
var command = buildCreateCommand();
assertThat(command.getRelationId()).hasToString(MessageTestFactory.VORGANG_ID);
}
@DisplayName("bodyObject")
@Nested
class TestBodyObject {
private final Map<String, Object> itemMap = Map.of("dummyKey", "dummyValue");
@BeforeEach
void mock() {
when(postfachNachrichtMapper.mapToMap(any())).thenReturn(itemMap);
}
@Test
void shouldContainClient() {
var command = buildCreateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.CLIENT_FIELD,
PersistPostfachNachrichtServiceImpl.CLIENT);
}
@Test
void shouldContainVorgangId() {
var command = buildCreateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.VORGANG_ID_FIELD,
MessageTestFactory.VORGANG_ID);
}
@Test
void shouldContainItemName() {
var command = buildCreateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.ITEM_NAME_FIELD,
PersistPostfachNachrichtServiceImpl.ITEM_NAME);
}
@Test
void shouldCallMapper() {
buildCreateCommand();
verify(postfachNachrichtMapper).mapToMap(nachricht);
}
@Test
void shouldContainItem() {
var command = buildCreateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.ITEM_FIELD, itemMap);
}
}
private OzgCloudCommand buildCreateCommand() {
return service.buildCreateCommand(commandBuilder, nachricht);
}
}
@DisplayName("for update")
@Nested
class TestBuildUpdateCommand {
private final PostfachNachricht nachricht = PostfachNachrichtTestFactory.create();
@Test
void shouldContainOrder() {
var command = buildUpdateCommand();
assertThat(command.getOrder()).isEqualTo(PersistPostfachNachrichtServiceImpl.UPDATE_ATTACHED_ITEM_ORDER);
}
@Test
void shouldContainRelationId() {
var command = buildUpdateCommand();
assertThat(command.getRelationId()).hasToString(PostfachNachrichtTestFactory.ID);
}
@DisplayName("bodyObject")
@Nested
class TestBodyObject {
private final Map<String, Object> itemMap = Map.of("dummyKey", "dummyValue");
@BeforeEach
void mock() {
when(postfachNachrichtMapper.mapToMap(any())).thenReturn(itemMap);
}
@Test
void shouldContainClient() {
var command = buildUpdateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.CLIENT_FIELD,
PersistPostfachNachrichtServiceImpl.CLIENT);
}
@Test
void shouldCallMapper() {
buildUpdateCommand();
verify(postfachNachrichtMapper).mapToMap(nachricht);
}
@Test
void shouldContainItem() {
var command = buildUpdateCommand();
assertThat(command.getBodyObject()).containsEntry(PersistPostfachNachrichtServiceImpl.ITEM_FIELD, itemMap);
}
}
private OzgCloudCommand buildUpdateCommand() {
return service.buildUpdateCommand(commandBuilder, nachricht);
}
}
}
@DisplayName("Persist answer") @DisplayName("Persist answer")
@Nested @Nested
class TestPersistAnswer { class TestPersistAnswer {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment