diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/EingangFilesRepository.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/EingangFilesRepository.java index c572496f9c31df5739bcfa42f89542490b0bc8aa..5a1517e9a38c2202fb65d56a700906f0e1ebcd0a 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/EingangFilesRepository.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/EingangFilesRepository.java @@ -24,6 +24,7 @@ package de.ozgcloud.vorgang.files; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -45,12 +46,10 @@ class EingangFilesRepository { private static final String DB_FIELDNAME_EINGANGS = "eingangs"; private static final String DB_FIELDNAME_ATTACHMENTS = "attachments"; private static final String DB_FIELDNAME_FILES = "files"; - private static final String DB_FIELDNAME_REPRESENTATIONS = "representations"; + private static final String FIELD_ATTACHMENT_FILES = DB_FIELDNAME_EINGANGS + "." + DB_FIELDNAME_ATTACHMENTS + "." + DB_FIELDNAME_FILES; - private static final String DB_FIELDNAME_FILE_ID = "id"; - private static final String DB_FIELDNAME_FILE_NAME = "name"; - private static final String DB_FIELDNAME_FILE_SIZE = "size"; - private static final String DB_FIELDNAME_FILE_CONTENT_TYPE = "contentType"; + private static final String DB_FIELDNAME_REPRESENTATIONS = "representations"; + private static final String FIELD_REPRESENTATION_FILES = DB_FIELDNAME_EINGANGS + "." + DB_FIELDNAME_REPRESENTATIONS; public List<OzgFile> findAttachmentsByEingangId(String eingangId) { @@ -58,23 +57,26 @@ class EingangFilesRepository { operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId))); operations.addAll(buildFindAttachmentsAggregation()); - operations.add(Aggregation.project(DB_FIELDNAME_FILE_ID, DB_FIELDNAME_FILE_NAME, DB_FIELDNAME_FILE_SIZE, DB_FIELDNAME_FILE_CONTENT_TYPE)); return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults(); } - private List<AggregationOperation> buildFindAttachmentsAggregation() { + public List<OzgFile> findAttachmentsByVorgangId(String vorgangId) { + return Collections.emptyList(); + } + public List<OzgFile> findAttachmentsByVorgangIdAndEingangId(final String vorgangId, final String eingangId) { + return Collections.emptyList(); + } + + private List<AggregationOperation> buildFindAttachmentsAggregation() { return List.of( - Aggregation.project(DB_FIELDNAME_EINGANGS), - Aggregation.unwind(DB_FIELDNAME_EINGANGS), - Aggregation.replaceRoot(DB_FIELDNAME_EINGANGS), - Aggregation.project(DB_FIELDNAME_ATTACHMENTS), - Aggregation.unwind(DB_FIELDNAME_ATTACHMENTS), - Aggregation.replaceRoot(DB_FIELDNAME_ATTACHMENTS), - Aggregation.project(DB_FIELDNAME_FILES), + Aggregation.project().andExpression(FIELD_ATTACHMENT_FILES).as(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES), - Aggregation.replaceRoot(DB_FIELDNAME_FILES)); + Aggregation.unwind(DB_FIELDNAME_FILES), + Aggregation.unwind(DB_FIELDNAME_FILES), + Aggregation.replaceRoot(DB_FIELDNAME_FILES) + ); } List<OzgFile> findRepresentationsByEingangId(String eingangId) { @@ -83,19 +85,24 @@ class EingangFilesRepository { operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId))); operations.addAll(buildFindRepresentationsAggregation()); - operations.add(Aggregation.project(DB_FIELDNAME_FILE_ID, DB_FIELDNAME_FILE_NAME, DB_FIELDNAME_FILE_SIZE, DB_FIELDNAME_FILE_CONTENT_TYPE)); return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults(); } + public List<OzgFile> findRepresentationsByVorgangId(String vorgangId) { + return Collections.emptyList(); + } + + public List<OzgFile> findRepresentationsByVorgangIdAndEingangId(String vorgangId, String eingangId) { + return Collections.emptyList(); + } + private List<AggregationOperation> buildFindRepresentationsAggregation() { return List.of( - Aggregation.project(DB_FIELDNAME_EINGANGS), - Aggregation.unwind(DB_FIELDNAME_EINGANGS), - Aggregation.replaceRoot(DB_FIELDNAME_EINGANGS), - Aggregation.project(DB_FIELDNAME_REPRESENTATIONS), - Aggregation.unwind(DB_FIELDNAME_REPRESENTATIONS), - Aggregation.replaceRoot(DB_FIELDNAME_REPRESENTATIONS)); + Aggregation.project().andExpression(FIELD_REPRESENTATION_FILES).as(DB_FIELDNAME_FILES), + Aggregation.unwind(DB_FIELDNAME_FILES), + Aggregation.unwind(DB_FIELDNAME_FILES), + Aggregation.replaceRoot(DB_FIELDNAME_FILES)); } } diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/FileService.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/FileService.java index 0d7b3bce1e8afec53d0c9a0cf082376d95f31b1c..bcee3f3f64d46f5913c6d1ee974fa83821f4d2d9 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/FileService.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/files/FileService.java @@ -26,6 +26,7 @@ package de.ozgcloud.vorgang.files; import java.io.InputStream; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; @@ -52,12 +53,22 @@ public class FileService implements BinaryFileService { @Autowired private FileIdMapper fileIdMapper; - public List<OzgFile> getAttachments(String eingangId) { - return repository.findAttachmentsByEingangId(eingangId); + public List<OzgFile> getAttachments(String vorgangId, String eingangId) { + if (Objects.isNull(vorgangId)) { + return repository.findAttachmentsByEingangId(eingangId); + } + return Objects.isNull(eingangId) + ? repository.findAttachmentsByVorgangId(vorgangId) + : repository.findAttachmentsByVorgangIdAndEingangId(vorgangId, eingangId); } - public List<OzgFile> getRepresentations(String eingangId) { - return repository.findRepresentationsByEingangId(eingangId); + public List<OzgFile> getRepresentations(String vorgangId, String eingangId) { + if (Objects.isNull(vorgangId)) { + return repository.findRepresentationsByEingangId(eingangId); + } + return Objects.isNull(eingangId) + ? repository.findRepresentationsByVorgangId(vorgangId) + : repository.findRepresentationsByVorgangIdAndEingangId(vorgangId, eingangId); } public FileId uploadFileStream(UploadedFilesReference ref, OzgFile file, Optional<String> userId, InputStream content) { diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/files/FileServiceTest.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/files/FileServiceTest.java index 014f2626f235ae0be3919bd5efe3f29dd25b110c..7c4c6255541d135aa5c3d2e2887bed976fa9fed2 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/files/FileServiceTest.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/files/FileServiceTest.java @@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; @@ -41,6 +42,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; +import de.ozgcloud.vorgang.vorgang.EingangTestFactory; import de.ozgcloud.vorgang.vorgang.IncomingFileTestFactory; import de.ozgcloud.vorgang.vorgang.VorgangTestFactory; @@ -131,7 +133,7 @@ class FileServiceTest { @Nested class TestFindFilesMetaData { - private Collection<FileId> ids = Collections.singleton(OzgFileTestFactory.ID); + private final Collection<FileId> ids = Collections.singleton(OzgFileTestFactory.ID); @BeforeEach void mockFileMapper() { @@ -162,4 +164,114 @@ class FileServiceTest { verify(binaryFileRepository).deleteAllByVorgang(VorgangTestFactory.ID); } } + + @Nested + class TestGetAttachments { + + @Test + void shouldCallFindAttachmentsByEingangId() { + service.getAttachments(null, EingangTestFactory.ID); + + verify(repository).findAttachmentsByEingangId(EingangTestFactory.ID); + } + + @Test + void shouldReturnAttachmentsByEingangId() { + var attachment = OzgFileTestFactory.create(); + when(repository.findAttachmentsByEingangId(anyString())).thenReturn(List.of(attachment)); + + var result = service.getAttachments(null, EingangTestFactory.ID); + + assertThat(result).containsExactly(attachment); + } + + @Test + void shouldCallFindAttachmentsByVorgangId() { + service.getAttachments(VorgangTestFactory.ID, null); + + verify(repository).findAttachmentsByVorgangId(VorgangTestFactory.ID); + } + + @Test + void shouldReturnAttachmentsByVorgangId() { + var attachment = OzgFileTestFactory.create(); + when(repository.findAttachmentsByVorgangId(anyString())).thenReturn(List.of(attachment)); + + var result = service.getAttachments(VorgangTestFactory.ID, null); + + assertThat(result).containsExactly(attachment); + } + + @Test + void shouldCallFindAttachmentsByVorgangIdAndEingangId() { + service.getAttachments(VorgangTestFactory.ID, EingangTestFactory.ID); + + verify(repository).findAttachmentsByVorgangIdAndEingangId(VorgangTestFactory.ID, EingangTestFactory.ID); + } + + @Test + void shouldReturnAttachmentsByVorgangIdAndEingangId() { + var attachment = OzgFileTestFactory.create(); + when(repository.findAttachmentsByVorgangIdAndEingangId(anyString(), anyString())).thenReturn(List.of(attachment)); + + var result = service.getAttachments(VorgangTestFactory.ID, EingangTestFactory.ID); + + assertThat(result).containsExactly(attachment); + } + } + + @Nested + class TestGetRepresentations { + + @Test + void shouldCallFindRepresentationsByEingangId() { + service.getRepresentations(null, EingangTestFactory.ID); + + verify(repository).findRepresentationsByEingangId(EingangTestFactory.ID); + } + + @Test + void shouldReturnRepresentationsByEingangId() { + var representation = OzgFileTestFactory.create(); + when(repository.findRepresentationsByEingangId(anyString())).thenReturn(List.of(representation)); + + var result = service.getRepresentations(null, EingangTestFactory.ID); + + assertThat(result).containsExactly(representation); + } + + @Test + void shouldCallFindRepresentationsByVorgangId() { + service.getRepresentations(VorgangTestFactory.ID, null); + + verify(repository).findRepresentationsByVorgangId(VorgangTestFactory.ID); + } + + @Test + void shouldReturnRepresentationsByVorgangId() { + var representation = OzgFileTestFactory.create(); + when(repository.findRepresentationsByVorgangId(anyString())).thenReturn(List.of(representation)); + + var result = service.getRepresentations(VorgangTestFactory.ID, null); + + assertThat(result).containsExactly(representation); + } + + @Test + void shouldCallFindRepresentationsByVorgangIdAndEingangId() { + service.getRepresentations(VorgangTestFactory.ID, EingangTestFactory.ID); + + verify(repository).findRepresentationsByVorgangIdAndEingangId(VorgangTestFactory.ID, EingangTestFactory.ID); + } + + @Test + void shouldReturnRepresentationsByVorgangIdAndEingangId() { + var representation = OzgFileTestFactory.create(); + when(repository.findRepresentationsByVorgangIdAndEingangId(anyString(), anyString())).thenReturn(List.of(representation)); + + var result = service.getRepresentations(VorgangTestFactory.ID, EingangTestFactory.ID); + + assertThat(result).containsExactly(representation); + } + } } \ No newline at end of file