Skip to content
Snippets Groups Projects
Commit 2ee70f55 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-7037 create archivierung package

parent 78fd1324
Branches
Tags
2 merge requests!2Ozg 7037 refactor package structure,!1Ozg 7037 vorgang an dms
......@@ -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;
......@@ -39,7 +39,7 @@ 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;
......@@ -77,7 +77,7 @@ class ArchiveEventListener {
private final ApplicationEventPublisher eventPublisher;
private final CommandService commandService;
private final ExportService exportService;
private final ArchiveService archiveService;
@EventListener(condition = IS_ARCHIVE_VORGANG_EVENT)
void onArchiveVorgangEvent(CommandCreatedEvent event) {
......@@ -92,7 +92,7 @@ class ArchiveEventListener {
@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) {
......
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.stereotype.Service;
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
@RequiredArgsConstructor
class ArchiveService {
private final XtaService xtaService;
private final ExportService exportService;
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;
}
}
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;
......@@ -23,7 +18,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;
......@@ -33,9 +27,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;
......@@ -57,46 +49,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);
......
......@@ -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.*;
......
......@@ -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.*;
......@@ -44,7 +44,8 @@ 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;
......@@ -70,7 +71,7 @@ class ArchiveEventListenerTest {
@Mock
private CommandService commandService;
@Mock
private ExportService exportService;
private ArchiveService archiveService;
@Captor
private ArgumentCaptor<Consumer<Command>> commandExecutorCapture;
......@@ -214,7 +215,7 @@ class ArchiveEventListenerTest {
verify(eventListener).runWithSecurityContext(eq(command), commandExecutorCapture.capture());
commandExecutorCapture.getValue().accept(command);
verify(exportService).archiveVorgang(command);
verify(archiveService).archiveVorgang(command);
}
}
......
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.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.Spy;
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;
@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);
}
}
}
......@@ -2,11 +2,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;
......@@ -15,9 +13,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;
......@@ -27,7 +22,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;
......@@ -54,11 +48,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;
......@@ -669,215 +659,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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment