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

OZG-6508 [wip] simplify aggregation pipeline for attachments and representations

parent 57e213c3
Branches
Tags
No related merge requests found
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
package de.ozgcloud.vorgang.files; package de.ozgcloud.vorgang.files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -45,12 +46,10 @@ class EingangFilesRepository { ...@@ -45,12 +46,10 @@ class EingangFilesRepository {
private static final String DB_FIELDNAME_EINGANGS = "eingangs"; private static final String DB_FIELDNAME_EINGANGS = "eingangs";
private static final String DB_FIELDNAME_ATTACHMENTS = "attachments"; private static final String DB_FIELDNAME_ATTACHMENTS = "attachments";
private static final String DB_FIELDNAME_FILES = "files"; 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_REPRESENTATIONS = "representations";
private static final String DB_FIELDNAME_FILE_NAME = "name"; private static final String FIELD_REPRESENTATION_FILES = DB_FIELDNAME_EINGANGS + "." + DB_FIELDNAME_REPRESENTATIONS;
private static final String DB_FIELDNAME_FILE_SIZE = "size";
private static final String DB_FIELDNAME_FILE_CONTENT_TYPE = "contentType";
public List<OzgFile> findAttachmentsByEingangId(String eingangId) { public List<OzgFile> findAttachmentsByEingangId(String eingangId) {
...@@ -58,23 +57,26 @@ class EingangFilesRepository { ...@@ -58,23 +57,26 @@ class EingangFilesRepository {
operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId))); operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
operations.addAll(buildFindAttachmentsAggregation()); 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(); 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( return List.of(
Aggregation.project(DB_FIELDNAME_EINGANGS), Aggregation.project().andExpression(FIELD_ATTACHMENT_FILES).as(DB_FIELDNAME_FILES),
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.unwind(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) { List<OzgFile> findRepresentationsByEingangId(String eingangId) {
...@@ -83,19 +85,24 @@ class EingangFilesRepository { ...@@ -83,19 +85,24 @@ class EingangFilesRepository {
operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId))); operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
operations.addAll(buildFindRepresentationsAggregation()); 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(); 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() { private List<AggregationOperation> buildFindRepresentationsAggregation() {
return List.of( return List.of(
Aggregation.project(DB_FIELDNAME_EINGANGS), Aggregation.project().andExpression(FIELD_REPRESENTATION_FILES).as(DB_FIELDNAME_FILES),
Aggregation.unwind(DB_FIELDNAME_EINGANGS), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.replaceRoot(DB_FIELDNAME_EINGANGS), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.project(DB_FIELDNAME_REPRESENTATIONS), Aggregation.replaceRoot(DB_FIELDNAME_FILES));
Aggregation.unwind(DB_FIELDNAME_REPRESENTATIONS),
Aggregation.replaceRoot(DB_FIELDNAME_REPRESENTATIONS));
} }
} }
...@@ -26,6 +26,7 @@ package de.ozgcloud.vorgang.files; ...@@ -26,6 +26,7 @@ package de.ozgcloud.vorgang.files;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -52,13 +53,23 @@ public class FileService implements BinaryFileService { ...@@ -52,13 +53,23 @@ public class FileService implements BinaryFileService {
@Autowired @Autowired
private FileIdMapper fileIdMapper; private FileIdMapper fileIdMapper;
public List<OzgFile> getAttachments(String eingangId) { public List<OzgFile> getAttachments(String vorgangId, String eingangId) {
if (Objects.isNull(vorgangId)) {
return repository.findAttachmentsByEingangId(eingangId); return repository.findAttachmentsByEingangId(eingangId);
} }
return Objects.isNull(eingangId)
? repository.findAttachmentsByVorgangId(vorgangId)
: repository.findAttachmentsByVorgangIdAndEingangId(vorgangId, eingangId);
}
public List<OzgFile> getRepresentations(String eingangId) { public List<OzgFile> getRepresentations(String vorgangId, String eingangId) {
if (Objects.isNull(vorgangId)) {
return repository.findRepresentationsByEingangId(eingangId); 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) { public FileId uploadFileStream(UploadedFilesReference ref, OzgFile file, Optional<String> userId, InputStream content) {
return uploadFile(ref, file, userId, content); return uploadFile(ref, file, userId, content);
......
...@@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream; ...@@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
...@@ -41,6 +42,7 @@ import org.mockito.InjectMocks; ...@@ -41,6 +42,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import de.ozgcloud.vorgang.vorgang.EingangTestFactory;
import de.ozgcloud.vorgang.vorgang.IncomingFileTestFactory; import de.ozgcloud.vorgang.vorgang.IncomingFileTestFactory;
import de.ozgcloud.vorgang.vorgang.VorgangTestFactory; import de.ozgcloud.vorgang.vorgang.VorgangTestFactory;
...@@ -131,7 +133,7 @@ class FileServiceTest { ...@@ -131,7 +133,7 @@ class FileServiceTest {
@Nested @Nested
class TestFindFilesMetaData { class TestFindFilesMetaData {
private Collection<FileId> ids = Collections.singleton(OzgFileTestFactory.ID); private final Collection<FileId> ids = Collections.singleton(OzgFileTestFactory.ID);
@BeforeEach @BeforeEach
void mockFileMapper() { void mockFileMapper() {
...@@ -162,4 +164,114 @@ class FileServiceTest { ...@@ -162,4 +164,114 @@ class FileServiceTest {
verify(binaryFileRepository).deleteAllByVorgang(VorgangTestFactory.ID); 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment