diff --git a/archive-manager-server/src/main/java/de/ozgcloud/archive/ArchiveManagerConfiguration.java b/archive-manager-server/src/main/java/de/ozgcloud/archive/ArchiveManagerConfiguration.java index cae0d47eb139ac4eb1733ee9802a8276dccf5b29..90f158d548309ca8961473fb78cafec16219e6d4 100644 --- a/archive-manager-server/src/main/java/de/ozgcloud/archive/ArchiveManagerConfiguration.java +++ b/archive-manager-server/src/main/java/de/ozgcloud/archive/ArchiveManagerConfiguration.java @@ -81,7 +81,6 @@ public class ArchiveManagerConfiguration { public static final String CALL_CONTEXT_PROVIDER_NAME = "archive_CallContextProvider"; public static final String CALL_CONTEXT_MAPPER_NAME = "archive_CallContextMapper"; - public static final String CLIENT_ATTRIBUTE_SERVICE_NAME = "archive_ClientAttributeService"; public static final String OZGCLOUD_CLIENT_ATTRIBUTE_SERVICE_NAME = "archive_OzgCloudClientAttributeService"; public static final String KOMMENTAR_SERVICE_NAME = "archive_KommentarService"; diff --git a/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/ArchiveEventListener.java b/archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveEventListener.java similarity index 91% rename from archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/ArchiveEventListener.java rename to archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveEventListener.java index e863cb332126c190d4808cdc5c65a64777c0a2ad..a9f8f1597ecea5baf104af9ec71cd9df88ed9157 100644 --- a/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/ArchiveEventListener.java +++ b/archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveEventListener.java @@ -21,7 +21,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.archive.vorgang; +package de.ozgcloud.archive.archivierung; import java.util.function.Consumer; import java.util.function.Predicate; @@ -34,12 +34,11 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.stereotype.Component; import de.ozgcloud.archive.ArchiveManagerConfiguration; -import de.ozgcloud.archive.attributes.ClientAttributeService; import de.ozgcloud.archive.common.callcontext.CallContextUser; import de.ozgcloud.archive.common.callcontext.CurrentUserService; import de.ozgcloud.archive.common.command.CommandService; import de.ozgcloud.archive.common.errorhandling.TimeoutException; -import de.ozgcloud.archive.export.ExportService; +import de.ozgcloud.archive.vorgang.VorgangService; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEvent; import de.ozgcloud.command.CommandFailedEvent; @@ -69,15 +68,14 @@ class ArchiveEventListener { @Qualifier(ArchiveManagerConfiguration.CURRENT_USER_SERVICE_NAME) // NOSONAR private final CurrentUserService currentUserService; - @Qualifier(ArchiveManagerConfiguration.CLIENT_ATTRIBUTE_SERVICE_NAME) // NOSONAR - private final ClientAttributeService clientAttributeService; - @Qualifier(ArchiveManagerConfiguration.VORGANG_SERVICE_NAME) // NOSONAR private final VorgangService vorgangService; private final ApplicationEventPublisher eventPublisher; + + @Qualifier(ArchiveManagerConfiguration.COMMAND_SERVICE_NAME) // NOSONAR private final CommandService commandService; - private final ExportService exportService; + private final ArchiveService archiveService; @EventListener(condition = IS_ARCHIVE_VORGANG_EVENT) void onArchiveVorgangEvent(CommandCreatedEvent event) { @@ -85,14 +83,14 @@ class ArchiveEventListener { } void doLockVorgang(Command command) { - clientAttributeService.setVorgangArchiving(command.getVorgangId()); + archiveService.setVorgangArchiving(command.getVorgangId()); vorgangService.lockVorgang(command); } @EventListener(condition = IS_LOCKED_BY_ARCHIVE_MANAGER_EVENT) public void onVorgangLockedEvent(VorgangLockedEvent event) { waitForPendingCommandsToFinish(event.getCommand().getVorgangId(), WAIT_INTERVAL); - runWithSecurityContext(event.getCommand(), exportService::archiveVorgang); + runWithSecurityContext(event.getCommand(), archiveService::archiveVorgang); } void waitForPendingCommandsToFinish(String vorgangId, long waitIntervalInMillis) { diff --git a/archive-manager-server/src/main/java/de/ozgcloud/archive/attributes/ClientAttributeService.java b/archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveService.java similarity index 59% rename from archive-manager-server/src/main/java/de/ozgcloud/archive/attributes/ClientAttributeService.java rename to archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveService.java index 21d24f804eb7d430b399243e9af5baaa8affecb8..dc45d255352044fa6b333ff08bc590851cb88274 100644 --- a/archive-manager-server/src/main/java/de/ozgcloud/archive/attributes/ClientAttributeService.java +++ b/archive-manager-server/src/main/java/de/ozgcloud/archive/archivierung/ArchiveService.java @@ -21,7 +21,14 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.archive.attributes; +package de.ozgcloud.archive.archivierung; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +import jakarta.activation.DataHandler; +import jakarta.activation.FileDataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -32,17 +39,22 @@ import de.ozgcloud.apilib.client_attribute.OzgCloudClientAttributeService; import de.ozgcloud.apilib.vorgang.OzgCloudVorgangIdMapper; import de.ozgcloud.archive.ArchiveManagerConfiguration; import de.ozgcloud.archive.common.callcontext.CallContextUser; +import de.ozgcloud.archive.common.xta.XtaService; +import de.ozgcloud.archive.export.ExportService; +import de.ozgcloud.command.Command; +import de.ozgcloud.common.errorhandling.TechnicalException; +import de.ozgcloud.xta.client.model.XtaFile; import lombok.RequiredArgsConstructor; -@Service(ArchiveManagerConfiguration.CLIENT_ATTRIBUTE_SERVICE_NAME) // NOSONAR +@Service @RequiredArgsConstructor -public class ClientAttributeService { +class ArchiveService { public static final String ATTRIBUTE_NAME_ANTRAG_ARCHIVING = "ARCHIVING"; - + private final XtaService xtaService; + private final ExportService exportService; @Qualifier(ArchiveManagerConfiguration.OZGCLOUD_CLIENT_ATTRIBUTE_SERVICE_NAME) // NOSONAR private final OzgCloudClientAttributeService ozgCloudAttributesService; - private final OzgCloudVorgangIdMapper vorgangIdMapper; public void setVorgangArchiving(String vorgangId) { @@ -58,4 +70,37 @@ public class ClientAttributeService { .build(); } + public void archiveVorgang(Command command) { + xtaService.sendXdomeaFile(buildXdomeaXtaFile(command.getVorgangId())); + } + + XtaFile buildXdomeaXtaFile(String vorgangId) { + var fileNameId = createFileNameId(); + return XtaFile.builder() + .name(exportService.buildXdomeaFileName(fileNameId)) + .content(createFileContent(vorgangId, fileNameId)) + .contentType("application/zip") + .build(); + } + + String createFileNameId() { + return UUID.randomUUID().toString(); + } + + DataHandler createFileContent(String vorgangId, String fileNameId) { + try { + var tempFile = createTempFile(fileNameId); + var fileDataSource = new FileDataSource(tempFile); + exportService.writeXdomeaFileContent(vorgangId, fileNameId, fileDataSource.getOutputStream()); + return new DataHandler(fileDataSource); + } catch (IOException e) { + throw new TechnicalException("Error on creating file content for xDomea file!", e); + } + } + + File createTempFile(String fileNameId) throws IOException { + var tempFile = File.createTempFile(fileNameId, ".zip"); + tempFile.deleteOnExit(); + return tempFile; + } } diff --git a/archive-manager-server/src/main/java/de/ozgcloud/archive/export/ExportService.java b/archive-manager-server/src/main/java/de/ozgcloud/archive/export/ExportService.java index eb57751785352bedddc4dae01ae3de7a864f894e..555dd08ff192966cb4f0a90396053f8cec20be61 100644 --- a/archive-manager-server/src/main/java/de/ozgcloud/archive/export/ExportService.java +++ b/archive-manager-server/src/main/java/de/ozgcloud/archive/export/ExportService.java @@ -23,22 +23,17 @@ */ package de.ozgcloud.archive.export; -import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.Set; -import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import jakarta.activation.DataHandler; -import jakarta.activation.FileDataSource; - import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -46,7 +41,6 @@ import org.springframework.stereotype.Service; import de.ozgcloud.archive.ArchiveManagerConfiguration; import de.ozgcloud.archive.bescheid.ExportBescheidService; import de.ozgcloud.archive.common.ExportFilenameGenerator; -import de.ozgcloud.archive.common.xta.XtaService; import de.ozgcloud.archive.file.ExportFileService; import de.ozgcloud.archive.file.OzgFile; import de.ozgcloud.archive.historie.ExportHistorieService; @@ -56,9 +50,7 @@ import de.ozgcloud.archive.vorgang.Eingang; import de.ozgcloud.archive.vorgang.EingangHeader; import de.ozgcloud.archive.vorgang.VorgangService; import de.ozgcloud.archive.vorgang.VorgangWithEingang; -import de.ozgcloud.command.Command; import de.ozgcloud.common.errorhandling.TechnicalException; -import de.ozgcloud.xta.client.model.XtaFile; import de.xoev.xdomea.AbgabeAbgabe0401; import lombok.RequiredArgsConstructor; @@ -80,46 +72,11 @@ public class ExportService { private final ExportKommentarService exportKommentarService; private final ExportNachrichtService exportNachrichtService; private final ExportBescheidService exportBescheidService; - private final XtaService xtaService; - - public void archiveVorgang(Command command) { - xtaService.sendXdomeaFile(buildXdomeaXtaFile(command.getVorgangId())); - } - - XtaFile buildXdomeaXtaFile(String vorgangId) { - var fileNameId = createFileNameId(); - return XtaFile.builder() - .name(buildXdomeaFileName(fileNameId)) - .content(createFileContent(vorgangId, fileNameId)) - .contentType("application/zip") - .build(); - } - - String createFileNameId() { - return UUID.randomUUID().toString(); - } public String buildXdomeaFileName(String fileNameId) { return String.format(EXPORT_XDOMEA_FILENAME_TEMPLATE, fileNameId); } - DataHandler createFileContent(String vorgangId, String fileNameId) { - try { - var tempFile = createTempFile(fileNameId); - var fileDataSource = new FileDataSource(tempFile); - writeXdomeaFileContent(vorgangId, fileNameId, fileDataSource.getOutputStream()); - return new DataHandler(fileDataSource); - } catch (IOException e) { - throw new TechnicalException("Error on creating file content for xDomea file!", e); - } - } - - File createTempFile(String fileNameId) throws IOException { - var tempFile = File.createTempFile(fileNameId, ".zip"); - tempFile.deleteOnExit(); - return tempFile; - } - public void writeXdomeaFileContent(String vorgangId, String filenameId, OutputStream outputStream) { var exportData = collectExportData(vorgangId, filenameId); writeZipFile(exportData, outputStream); diff --git a/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/VorgangService.java b/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/VorgangService.java index d7661cc1354422de0c561d102954e31fe7a462b2..44c5f661718557c8c10b7f625e06b965cc4f2cd3 100644 --- a/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/VorgangService.java +++ b/archive-manager-server/src/main/java/de/ozgcloud/archive/vorgang/VorgangService.java @@ -98,8 +98,8 @@ public class VorgangService { return allgemeineMetadatenType; } - public void lockVorgang(Command command) { - ozgCloudCommandService.addSubCommands(buildCreateSubCommandsRequest(command.getId(), buildLockVorgangSubCommand(command))); + public void lockVorgang(Command parentCommand) { + ozgCloudCommandService.addSubCommands(buildCreateSubCommandsRequest(parentCommand.getId(), buildLockVorgangSubCommand(parentCommand))); } OzgCloudCommand buildLockVorgangSubCommand(Command command) { diff --git a/archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerITCase.java b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerITCase.java similarity index 98% rename from archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerITCase.java rename to archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerITCase.java index e72c6347f66a6c5e27cf33583a37198bcb84a71a..cffcb269ead361bc2637320cc9831cb7c232563b 100644 --- a/archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerITCase.java +++ b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerITCase.java @@ -21,7 +21,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.archive.vorgang; +package de.ozgcloud.archive.archivierung; import static org.mockito.Mockito.*; diff --git a/archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerTest.java b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerTest.java similarity index 95% rename from archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerTest.java rename to archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerTest.java index 8f0318b145df6c6ea3b7fafbb73a5bafb74ae6dc..2e8379ee8653f773140c170fa5e453d706b0c9b0 100644 --- a/archive-manager-server/src/test/java/de/ozgcloud/archive/vorgang/ArchiveEventListenerTest.java +++ b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveEventListenerTest.java @@ -21,7 +21,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.archive.vorgang; +package de.ozgcloud.archive.archivierung; import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; @@ -40,11 +40,11 @@ import org.mockito.Spy; import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.core.context.SecurityContext; -import de.ozgcloud.archive.attributes.ClientAttributeService; import de.ozgcloud.archive.common.callcontext.CurrentUserService; import de.ozgcloud.archive.common.command.CommandService; import de.ozgcloud.archive.common.errorhandling.TimeoutException; -import de.ozgcloud.archive.export.ExportService; +import de.ozgcloud.archive.vorgang.VorgangService; +import de.ozgcloud.archive.vorgang.VorgangWithEingangTestFactory; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEventTestFactory; import de.ozgcloud.command.CommandFailedEvent; @@ -62,15 +62,13 @@ class ArchiveEventListenerTest { @Mock private CurrentUserService currentUserService; @Mock - private ClientAttributeService clientAttributeService; - @Mock private VorgangService vorgangService; @Mock private ApplicationEventPublisher eventPublisher; @Mock private CommandService commandService; @Mock - private ExportService exportService; + private ArchiveService archiveService; @Captor private ArgumentCaptor<Consumer<Command>> commandExecutorCapture; @@ -102,7 +100,7 @@ class ArchiveEventListenerTest { void shouldCallSetVorgangArchiving() { doLockVorgang(); - verify(clientAttributeService).setVorgangArchiving(CommandTestFactory.VORGANG_ID); + verify(archiveService).setVorgangArchiving(CommandTestFactory.VORGANG_ID); } @Test @@ -214,7 +212,7 @@ class ArchiveEventListenerTest { verify(eventListener).runWithSecurityContext(eq(command), commandExecutorCapture.capture()); commandExecutorCapture.getValue().accept(command); - verify(exportService).archiveVorgang(command); + verify(archiveService).archiveVorgang(command); } } diff --git a/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveServiceTest.java b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4e1213a6c62b4966f2d1299ef6fa42b02dff2db5 --- /dev/null +++ b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/ArchiveServiceTest.java @@ -0,0 +1,333 @@ +package de.ozgcloud.archive.archivierung; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.UUID; + +import jakarta.activation.DataHandler; +import jakarta.activation.FileDataSource; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedConstruction; +import org.mockito.Spy; + +import de.ozgcloud.apilib.client_attribute.OzgCloudAccessPermission; +import de.ozgcloud.apilib.client_attribute.OzgCloudClientAttributeService; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangIdMapper; +import de.ozgcloud.archive.common.callcontext.CallContextUser; +import de.ozgcloud.archive.common.xta.XtaService; +import de.ozgcloud.archive.export.ExportService; +import de.ozgcloud.archive.vorgang.VorgangWithEingangTestFactory; +import de.ozgcloud.archive.vorgang.XdomeaXtaFileTestFactory; +import de.ozgcloud.command.Command; +import de.ozgcloud.command.CommandTestFactory; +import de.ozgcloud.common.errorhandling.TechnicalException; +import de.ozgcloud.xta.client.model.XtaFile; +import lombok.SneakyThrows; + +class ArchiveServiceTest { + + @Spy + @InjectMocks + private ArchiveService service; + + @Mock + private ExportService exportService; + @Mock + private XtaService xtaService; + @Mock + private OzgCloudClientAttributeService ozgCloudAttributesService; + @Mock + private OzgCloudVorgangIdMapper vorgangIdMapper; + + @Nested + class TestSetVorgangArchiving { + + @Test + void shouldBuildBooleanClientAttribute() { + setVorgangArchiving(); + + verify(service).buildBooleanClientAttribute(true); + } + + @Test + void shouldCallVorgangIdMapper() { + setVorgangArchiving(); + + verify(vorgangIdMapper).fromString(VorgangWithEingangTestFactory.ID); + } + + @Test + void shouldSetClientAttribute() { + var attribute = OzgCloudBooleanClientAttributeTestFactory.create(); + doReturn(attribute).when(service).buildBooleanClientAttribute(true); + when(vorgangIdMapper.fromString(VorgangWithEingangTestFactory.ID)).thenReturn(OzgCloudVorgangId.from(VorgangWithEingangTestFactory.ID)); + + setVorgangArchiving(); + + verify(ozgCloudAttributesService).setClientAttribute(attribute, OzgCloudVorgangId.from(VorgangWithEingangTestFactory.ID)); + } + + private void setVorgangArchiving() { + service.setVorgangArchiving(VorgangWithEingangTestFactory.ID); + } + } + + @Nested + class TestBuildBooleanClientAttribute { + + @Test + void shouldSetClientName() { + var attribute = service.buildBooleanClientAttribute(false); + + assertThat(attribute.getClientName()).isEqualTo(CallContextUser.ARCHIVE_MANAGER_CLIENT_NAME); + } + + @Test + void shouldSetAccess() { + var attribute = service.buildBooleanClientAttribute(false); + + assertThat(attribute.getAccess()).isEqualTo(OzgCloudAccessPermission.READ_ONLY); + } + + @Test + void shouldSetAttributeName() { + var attribute = service.buildBooleanClientAttribute(false); + + assertThat(attribute.getAttributeName()).isEqualTo(ArchiveService.ATTRIBUTE_NAME_ANTRAG_ARCHIVING); + } + + @ParameterizedTest + @ValueSource(booleans = { true, false }) + void shouldSetValue(boolean isArchiving) { + var attribute = service.buildBooleanClientAttribute(isArchiving); + + assertThat(attribute.getValue()).isEqualTo(isArchiving); + } + } + + @Nested + class TestArchiveVorgang { + + private final Command command = CommandTestFactory.create(); + private final XtaFile xdomeaFile = XdomeaXtaFileTestFactory.create(); + + @BeforeEach + void mock() { + doReturn(xdomeaFile).when(service).buildXdomeaXtaFile(CommandTestFactory.VORGANG_ID); + } + + @Test + void shouldCallBuildXdomeaXtaFile() { + archiveVorgang(); + + verify(service).buildXdomeaXtaFile(CommandTestFactory.VORGANG_ID); + } + + @Test + void shouldCallSendXdomeaFile() { + archiveVorgang(); + + verify(xtaService).sendXdomeaFile(xdomeaFile); + } + + private void archiveVorgang() { + service.archiveVorgang(command); + } + } + + @Nested + class TestBuildXdomeaXtaFile { + + private final String fileNameId = UUID.randomUUID().toString(); + + @BeforeEach + void mock() { + doReturn(fileNameId).when(service).createFileNameId(); + when(exportService.buildXdomeaFileName(any())).thenReturn(XdomeaXtaFileTestFactory.FILE_NAME); + doReturn(XdomeaXtaFileTestFactory.DATA_HANDLER).when(service).createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); + } + + @Test + void shouldCallCreateFileNameId() { + buildXdomeaXtaFile(); + + verify(service).createFileNameId(); + } + + @Test + void shouldCallBuildXdomeaFileName() { + buildXdomeaXtaFile(); + + verify(exportService).buildXdomeaFileName(fileNameId); + } + + @Test + void shouldCallCreateFileContent() { + buildXdomeaXtaFile(); + + verify(service).createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); + } + + @Test + void shouldReturnXdomeaFile() { + var xdomeaFile = buildXdomeaXtaFile(); + + assertThat(xdomeaFile).usingRecursiveComparison().isEqualTo(XdomeaXtaFileTestFactory.create()); + } + + private XtaFile buildXdomeaXtaFile() { + return service.buildXdomeaXtaFile(VorgangWithEingangTestFactory.ID); + } + } + + @Nested + class TestCreateFileNameId { + + private final UUID uuid = UUID.randomUUID(); + + @Test + void shouldReturnRandomUUID() { + try (var mockedStaticUUID = mockStatic(UUID.class)) { + mockedStaticUUID.when(UUID::randomUUID).thenReturn(uuid); + + var returnedFileNameId = service.createFileNameId(); + + assertThat(returnedFileNameId).isEqualTo(uuid.toString()); + } + } + } + + @Nested + class TestCreateFileContent { + + private final String fileNameId = UUID.randomUUID().toString(); + + @Nested + class TestOnNoExceptionThrown { + private MockedConstruction<FileDataSource> fileDataSourceConstruction; + private FileDataSource fileDataSource; + private File dataSourceFile; + + private MockedConstruction<DataHandler> dataHandlerConstruction; + private DataHandler dataHandler; + private FileDataSource dataHandlerSource; + + @Mock + private File tempFile; + @Mock + private OutputStream outputStream; + + @BeforeEach + @SneakyThrows + void setUpMock() { + doReturn(tempFile).when(service).createTempFile(fileNameId); + fileDataSourceConstruction = mockConstruction(FileDataSource.class, + (fileDataSource, context) -> { + dataSourceFile = (File) context.arguments().get(0); + this.fileDataSource = fileDataSource; + when(fileDataSource.getOutputStream()).thenReturn(outputStream); + }); + dataHandlerConstruction = mockConstruction(DataHandler.class, (dataHandler, context) -> { + this.dataHandler = dataHandler; + dataHandlerSource = (FileDataSource) context.arguments().get(0); + }); + } + + @AfterEach + void cleanUp() { + fileDataSourceConstruction.close(); + dataHandlerConstruction.close(); + } + + @Test + @SneakyThrows + void shouldCallCreateTempFile() { + createFileContent(); + + verify(service).createTempFile(fileNameId); + } + + @Test + void shouldCreateFileDataSourceWithTempFile() { + createFileContent(); + + assertThat(dataSourceFile).isEqualTo(tempFile); + } + + @Test + @SneakyThrows + void shouldGetOutputStreamOfFileDataSource() { + createFileContent(); + + verify(fileDataSource).getOutputStream(); + } + + @Test + void shouldWriteXdomeaContentToOutputStream() { + createFileContent(); + + verify(exportService).writeXdomeaFileContent(VorgangWithEingangTestFactory.ID, fileNameId, outputStream); + } + + @Test + void shouldConstructDataHandlerFromFileDataSource() { + createFileContent(); + + assertThat(dataHandlerConstruction.constructed()).hasSize(1); + assertThat(dataHandlerSource).isEqualTo(fileDataSource); + } + + @Test + void shouldReturnDataHandler() { + var resultDataHandler = createFileContent(); + + assertThat(resultDataHandler).isEqualTo(dataHandler); + } + } + + @Nested + class TestOnIOExceptionThrown { + @Test + @SneakyThrows + void shouldThrowTechnicalExcpetion() { + doThrow(IOException.class).when(service).createTempFile(any()); + + assertThrows(TechnicalException.class, () -> createFileContent()); + } + } + + private DataHandler createFileContent() { + return service.createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); + } + } + + @Nested + class TestCreateTempFile { + + private final String fileNameId = UUID.randomUUID().toString(); + + @Test + @SneakyThrows + void shouldReturnFile() { + var file = service.createTempFile(fileNameId); + + assertThat(file).isNotNull().isInstanceOf(File.class); + } + } + +} diff --git a/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/OzgCloudBooleanClientAttributeTestFactory.java b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/OzgCloudBooleanClientAttributeTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..954b23b8ace8a76a6951b1e091d090c8c233fee9 --- /dev/null +++ b/archive-manager-server/src/test/java/de/ozgcloud/archive/archivierung/OzgCloudBooleanClientAttributeTestFactory.java @@ -0,0 +1,29 @@ +package de.ozgcloud.archive.archivierung; + +import com.thedeanda.lorem.LoremIpsum; + +import de.ozgcloud.apilib.client_attribute.OzgCloudAccessPermission; +import de.ozgcloud.apilib.client_attribute.OzgCloudBooleanClientAttribute; +import de.ozgcloud.apilib.client_attribute.OzgCloudBooleanClientAttribute.OzgCloudBooleanClientAttributeBuilder; +import de.ozgcloud.archive.common.callcontext.CallContextUserTestFactory; + +public class OzgCloudBooleanClientAttributeTestFactory { + + private static final boolean VALUE = true; + private static final String ATTRIBUTE_NAME = LoremIpsum.getInstance().getWords(1); + private static final OzgCloudAccessPermission ACCESS = OzgCloudAccessPermission.READ_ONLY; + private static final String CLIENT_NAME = CallContextUserTestFactory.CLIENT_NAME; + + public static OzgCloudBooleanClientAttribute create() { + return createBuilder().build(); + } + + private static OzgCloudBooleanClientAttributeBuilder<?, ?> createBuilder() { + return OzgCloudBooleanClientAttribute.builder() + .clientName(CLIENT_NAME) + .access(ACCESS) + .attributeName(ATTRIBUTE_NAME) + .value(VALUE); + } + +} diff --git a/archive-manager-server/src/test/java/de/ozgcloud/archive/export/ExportServiceTest.java b/archive-manager-server/src/test/java/de/ozgcloud/archive/export/ExportServiceTest.java index 4375dda1089806d13699708d560ef050937882d3..08221c17a7dff76148d00a4dc20c13cc8886560a 100644 --- a/archive-manager-server/src/test/java/de/ozgcloud/archive/export/ExportServiceTest.java +++ b/archive-manager-server/src/test/java/de/ozgcloud/archive/export/ExportServiceTest.java @@ -25,11 +25,9 @@ package de.ozgcloud.archive.export; import static de.ozgcloud.archive.common.XDomeaTestUtils.*; import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; @@ -38,9 +36,6 @@ import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import jakarta.activation.DataHandler; -import jakarta.activation.FileDataSource; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -50,7 +45,6 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockedConstruction; import org.mockito.MockedStatic; import org.mockito.Spy; @@ -77,11 +71,7 @@ import de.ozgcloud.archive.vorgang.VorgangService; import de.ozgcloud.archive.vorgang.VorgangTypeTestFactory; import de.ozgcloud.archive.vorgang.VorgangWithEingang; import de.ozgcloud.archive.vorgang.VorgangWithEingangTestFactory; -import de.ozgcloud.archive.vorgang.XdomeaXtaFileTestFactory; -import de.ozgcloud.command.Command; -import de.ozgcloud.command.CommandTestFactory; import de.ozgcloud.common.errorhandling.TechnicalException; -import de.ozgcloud.xta.client.model.XtaFile; import de.xoev.xdomea.AbgabeAbgabe0401; import de.xoev.xdomea.AkteType; import de.xoev.xdomea.DokumentType; @@ -692,215 +682,4 @@ class ExportServiceTest { } } - @Nested - class TestArchiveVorgang { - - private final Command command = CommandTestFactory.create(); - private final XtaFile xdomeaFile = XdomeaXtaFileTestFactory.create(); - - @BeforeEach - void mock() { - doReturn(xdomeaFile).when(service).buildXdomeaXtaFile(CommandTestFactory.VORGANG_ID); - } - - @Test - void shouldCallBuildXdomeaXtaFile() { - archiveVorgang(); - - verify(service).buildXdomeaXtaFile(CommandTestFactory.VORGANG_ID); - } - - @Test - void shouldCallSendXdomeaFile() { - archiveVorgang(); - - verify(xtaService).sendXdomeaFile(xdomeaFile); - } - - private void archiveVorgang() { - service.archiveVorgang(command); - } - } - - @Nested - class TestBuildXdomeaXtaFile { - - private final String fileNameId = UUID.randomUUID().toString(); - - @BeforeEach - void mock() { - doReturn(fileNameId).when(service).createFileNameId(); - doReturn(XdomeaXtaFileTestFactory.FILE_NAME).when(service).buildXdomeaFileName(any()); - doReturn(XdomeaXtaFileTestFactory.DATA_HANDLER).when(service).createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); - } - - @Test - void shouldCallCreateFileNameId() { - buildXdomeaXtaFile(); - - verify(service).createFileNameId(); - } - - @Test - void shouldCallBuildXdomeaFileName() { - buildXdomeaXtaFile(); - - verify(service).buildXdomeaFileName(fileNameId); - } - - @Test - void shouldCallCreateFileContent() { - buildXdomeaXtaFile(); - - verify(service).createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); - } - - @Test - void shouldReturnXdomeaFile() { - var xdomeaFile = buildXdomeaXtaFile(); - - assertThat(xdomeaFile).usingRecursiveComparison().isEqualTo(XdomeaXtaFileTestFactory.create()); - } - - private XtaFile buildXdomeaXtaFile() { - return service.buildXdomeaXtaFile(VorgangWithEingangTestFactory.ID); - } - } - - @Nested - class TestCreateFileNameId { - - private final UUID uuid = UUID.randomUUID(); - - @Test - void shouldReturnRandomUUID() { - try (var mockedStaticUUID = mockStatic(UUID.class)) { - mockedStaticUUID.when(UUID::randomUUID).thenReturn(uuid); - - var returnedFileNameId = service.createFileNameId(); - - assertThat(returnedFileNameId).isEqualTo(uuid.toString()); - } - } - } - - @Nested - class TestCreateFileContent { - - private final String fileNameId = UUID.randomUUID().toString(); - - @Nested - class TestOnNoExceptionThrown { - private MockedConstruction<FileDataSource> fileDataSourceConstruction; - private FileDataSource fileDataSource; - private File dataSourceFile; - - private MockedConstruction<DataHandler> dataHandlerConstruction; - private DataHandler dataHandler; - private FileDataSource dataHandlerSource; - - @Mock - private File tempFile; - @Mock - private OutputStream outputStream; - - @BeforeEach - @SneakyThrows - void setUpMock() { - doReturn(tempFile).when(service).createTempFile(fileNameId); - fileDataSourceConstruction = mockConstruction(FileDataSource.class, - (fileDataSource, context) -> { - dataSourceFile = (File) context.arguments().get(0); - this.fileDataSource = fileDataSource; - when(fileDataSource.getOutputStream()).thenReturn(outputStream); - }); - dataHandlerConstruction = mockConstruction(DataHandler.class, (dataHandler, context) -> { - this.dataHandler = dataHandler; - dataHandlerSource = (FileDataSource) context.arguments().get(0); - }); - doNothing().when(service).writeXdomeaFileContent(any(), any(), any()); - } - - @AfterEach - void cleanUp() { - fileDataSourceConstruction.close(); - dataHandlerConstruction.close(); - } - - @Test - @SneakyThrows - void shouldCallCreateTempFile() { - createFileContent(); - - verify(service).createTempFile(fileNameId); - } - - @Test - void shouldCreateFileDataSourceWithTempFile() { - createFileContent(); - - assertThat(dataSourceFile).isEqualTo(tempFile); - } - - @Test - @SneakyThrows - void shouldGetOutputStreamOfFileDataSource() { - createFileContent(); - - verify(fileDataSource).getOutputStream(); - } - - @Test - void shouldWriteXdomeaContentToOutputStream() { - createFileContent(); - - verify(service).writeXdomeaFileContent(VorgangWithEingangTestFactory.ID, fileNameId, outputStream); - } - - @Test - void shouldConstructDataHandlerFromFileDataSource() { - createFileContent(); - - assertThat(dataHandlerConstruction.constructed()).hasSize(1); - assertThat(dataHandlerSource).isEqualTo(fileDataSource); - } - - @Test - void shouldReturnDataHandler() { - var resultDataHandler = createFileContent(); - - assertThat(resultDataHandler).isEqualTo(dataHandler); - } - } - - @Nested - class TestOnIOExceptionThrown { - @Test - @SneakyThrows - void shouldThrowTechnicalExcpetion() { - doThrow(IOException.class).when(service).createTempFile(any()); - - assertThrows(TechnicalException.class, () -> createFileContent()); - } - } - - private DataHandler createFileContent() { - return service.createFileContent(VorgangWithEingangTestFactory.ID, fileNameId); - } - } - - @Nested - class TestCreateTempFile { - - private final String fileNameId = UUID.randomUUID().toString(); - - @Test - @SneakyThrows - void shouldReturnFile() { - var file = service.createTempFile(fileNameId); - - assertThat(file).isNotNull().isInstanceOf(File.class); - } - } - } \ No newline at end of file