Skip to content
Snippets Groups Projects
Commit 9abbd562 authored by Felix Reichenbach's avatar Felix Reichenbach
Browse files

OZG-7573 refactor EingangStubReceiverStreamObserver

parent 7dc12961
Branches
Tags
1 merge request!9Ozg 7573 forward vorgang
...@@ -72,8 +72,7 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa ...@@ -72,8 +72,7 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa
private final Map<String, List<IncomingFile>> attachments = new HashMap<>(); private final Map<String, List<IncomingFile>> attachments = new HashMap<>();
private String groupName; private String groupName;
private ContentCollector attachmentCollector; private ContentCollector contentCollector;
private ContentCollector representationCollector;
@Override @Override
public synchronized void onNext(GrpcRouteForwardingRequest request) { public synchronized void onNext(GrpcRouteForwardingRequest request) {
...@@ -97,24 +96,28 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa ...@@ -97,24 +96,28 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa
void handleAttachment(GrpcAttachment attachment) { void handleAttachment(GrpcAttachment attachment) {
if (attachment.hasFile()) { if (attachment.hasFile()) {
attachmentCollector = buildContentCollector(incomingFileMapper.fromGrpcAttachmentFile(attachment.getFile())); verifyNoOngoingFileDownload();
contentCollector = buildContentCollector(incomingFileMapper.fromGrpcAttachmentFile(attachment.getFile()));
groupName = attachment.getFile().getGroupName(); groupName = attachment.getFile().getGroupName();
} else { } else {
if (Objects.isNull(attachmentCollector)) { verifyMetadataWasSent();
throw new IllegalStateException("File content received before metadata."); contentCollector.collect(attachment.getContent()).ifPresent(this::addAsAttachment);
}
attachmentCollector.collect(attachment.getContent()).ifPresent(this::addAsAttachment);
} }
} }
void handleRepresentation(GrpcRepresentation representation) { void handleRepresentation(GrpcRepresentation representation) {
if (representation.hasFile()) { if (representation.hasFile()) {
representationCollector = buildContentCollector(incomingFileMapper.fromGrpcRepresentationFile(representation.getFile())); verifyNoOngoingFileDownload();
contentCollector = buildContentCollector(incomingFileMapper.fromGrpcRepresentationFile(representation.getFile()));
} else { } else {
if (Objects.isNull(representationCollector)) { verifyMetadataWasSent();
throw new IllegalStateException("File content received before metadata."); contentCollector.collect(representation.getContent()).ifPresent(this::addAsRepresentation);
} }
representationCollector.collect(representation.getContent()).ifPresent(this::addAsRepresentation); }
void verifyNoOngoingFileDownload() {
if (Objects.nonNull(contentCollector)) {
throw new IllegalStateException("File metadata received before previous file content was completed.");
} }
} }
...@@ -125,21 +128,26 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa ...@@ -125,21 +128,26 @@ class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRouteForwa
.build(); .build();
} }
void verifyMetadataWasSent() {
if (Objects.isNull(contentCollector)) {
throw new IllegalStateException("File content received before metadata.");
}
}
void addAsAttachment(IncomingFile completedIncomingFile) { void addAsAttachment(IncomingFile completedIncomingFile) {
attachments.computeIfAbsent(groupName, s -> new ArrayList<>()).add(completedIncomingFile); attachments.computeIfAbsent(groupName, s -> new ArrayList<>()).add(completedIncomingFile);
attachmentCollector = null; contentCollector = null;
} }
void addAsRepresentation(IncomingFile completedIncomingFile) { void addAsRepresentation(IncomingFile completedIncomingFile) {
representations.add(completedIncomingFile); representations.add(completedIncomingFile);
representationCollector = null; contentCollector = null;
} }
@Override @Override
public synchronized void onError(Throwable t) { public synchronized void onError(Throwable t) {
LOG.error("Error happened. Receiving stream closed.", t); LOG.error("Error happened. Receiving stream closed.", t);
attachmentCollector.close(); contentCollector.close();
representationCollector.close();
} }
@Override @Override
......
...@@ -199,12 +199,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -199,12 +199,7 @@ class EingangStubReceiverStreamObserverTest {
class TestHandleAttachment { class TestHandleAttachment {
@Mock @Mock
private ContentCollector attachmentCollector; private ContentCollector contentCollector;
@BeforeEach
void setUp() {
setAttachmentCollector(attachmentCollector);
}
@Nested @Nested
class TestWithFile { class TestWithFile {
...@@ -213,8 +208,16 @@ class EingangStubReceiverStreamObserverTest { ...@@ -213,8 +208,16 @@ class EingangStubReceiverStreamObserverTest {
private final IncomingFile incomingFile = IncomingFileTestFactory.create(); private final IncomingFile incomingFile = IncomingFileTestFactory.create();
@BeforeEach @BeforeEach
void mock() { void initMock() {
when(incomingFileMapper.fromGrpcAttachmentFile(any())).thenReturn(incomingFile); when(incomingFileMapper.fromGrpcAttachmentFile(any())).thenReturn(incomingFile);
doNothing().when(observer).verifyNoOngoingFileDownload();
}
@Test
void shouldCallVerifyNoOngoingFileDownload() {
observer.handleAttachment(attachmentWithFile);
verify(observer).verifyNoOngoingFileDownload();
} }
@Test @Test
...@@ -225,12 +228,12 @@ class EingangStubReceiverStreamObserverTest { ...@@ -225,12 +228,12 @@ class EingangStubReceiverStreamObserverTest {
} }
@Test @Test
void shouldSetAttachmentCollector() { void shouldSetContentCollector() {
var expectedContentCollector = ContentCollector.builder().fileSaver(fileSaver).incomingFile(incomingFile).build(); var expectedContentCollector = ContentCollector.builder().fileSaver(fileSaver).incomingFile(incomingFile).build();
observer.handleAttachment(attachmentWithFile); observer.handleAttachment(attachmentWithFile);
assertThat(getAttachmentCollector()).usingRecursiveComparison().ignoringFields("pipedInput", "pipedOutput") assertThat(getContentCollector()).usingRecursiveComparison().ignoringFields("pipedInput", "pipedOutput")
.isEqualTo(expectedContentCollector); .isEqualTo(expectedContentCollector);
} }
...@@ -245,7 +248,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -245,7 +248,7 @@ class EingangStubReceiverStreamObserverTest {
void shouldNotCollectContent() { void shouldNotCollectContent() {
observer.handleAttachment(attachmentWithFile); observer.handleAttachment(attachmentWithFile);
verify(attachmentCollector, never()).collect(any()); verify(contentCollector, never()).collect(any());
} }
} }
...@@ -255,11 +258,23 @@ class EingangStubReceiverStreamObserverTest { ...@@ -255,11 +258,23 @@ class EingangStubReceiverStreamObserverTest {
private final GrpcAttachment attachmentWithContent = GrpcAttachmentTestFactory.createWithContent(); private final GrpcAttachment attachmentWithContent = GrpcAttachmentTestFactory.createWithContent();
private final IncomingFile incomingFile = IncomingFileTestFactory.create(); private final IncomingFile incomingFile = IncomingFileTestFactory.create();
@BeforeEach
void setUp() {
setContentCollector(contentCollector);
}
@Test
void shouldCallVerifyMetadataWasSent() {
observer.handleAttachment(attachmentWithContent);
verify(observer).verifyMetadataWasSent();
}
@Test @Test
void shouldCollectContent() { void shouldCollectContent() {
observer.handleAttachment(attachmentWithContent); observer.handleAttachment(attachmentWithContent);
verify(attachmentCollector).collect(GrpcAttachmentTestFactory.CONTENT); verify(contentCollector).collect(GrpcAttachmentTestFactory.CONTENT);
} }
@Test @Test
...@@ -278,7 +293,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -278,7 +293,7 @@ class EingangStubReceiverStreamObserverTest {
@Test @Test
void shouldCallAddAsAttachment() { void shouldCallAddAsAttachment() {
when(attachmentCollector.collect(any())).thenReturn(Optional.of(incomingFile)); when(contentCollector.collect(any())).thenReturn(Optional.of(incomingFile));
observer.handleAttachment(attachmentWithContent); observer.handleAttachment(attachmentWithContent);
...@@ -287,12 +302,13 @@ class EingangStubReceiverStreamObserverTest { ...@@ -287,12 +302,13 @@ class EingangStubReceiverStreamObserverTest {
@Test @Test
void shouldNotCallAddAsAttachment() { void shouldNotCallAddAsAttachment() {
when(attachmentCollector.collect(any())).thenReturn(Optional.empty()); when(contentCollector.collect(any())).thenReturn(Optional.empty());
observer.handleAttachment(attachmentWithContent); observer.handleAttachment(attachmentWithContent);
verify(observer, never()).addAsAttachment(any()); verify(observer, never()).addAsAttachment(any());
} }
} }
} }
...@@ -300,12 +316,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -300,12 +316,7 @@ class EingangStubReceiverStreamObserverTest {
class TestHandleRepresentation { class TestHandleRepresentation {
@Mock @Mock
private ContentCollector representationCollector; private ContentCollector contentCollector;
@BeforeEach
void setUp() {
setRepresentationCollector(representationCollector);
}
@Nested @Nested
class TestWithFile { class TestWithFile {
...@@ -316,6 +327,14 @@ class EingangStubReceiverStreamObserverTest { ...@@ -316,6 +327,14 @@ class EingangStubReceiverStreamObserverTest {
@BeforeEach @BeforeEach
void mock() { void mock() {
when(incomingFileMapper.fromGrpcRepresentationFile(any())).thenReturn(incomingFile); when(incomingFileMapper.fromGrpcRepresentationFile(any())).thenReturn(incomingFile);
doNothing().when(observer).verifyNoOngoingFileDownload();
}
@Test
void shouldCallVerifyNoOngoingFileDownload() {
observer.handleRepresentation(representationWithFile);
verify(observer).verifyNoOngoingFileDownload();
} }
@Test @Test
...@@ -326,12 +345,12 @@ class EingangStubReceiverStreamObserverTest { ...@@ -326,12 +345,12 @@ class EingangStubReceiverStreamObserverTest {
} }
@Test @Test
void shouldSetRepresentationCollector() { void shouldSetContentCollector() {
var expectedContentCollector = ContentCollector.builder().fileSaver(fileSaver).incomingFile(incomingFile).build(); var expectedContentCollector = ContentCollector.builder().fileSaver(fileSaver).incomingFile(incomingFile).build();
observer.handleRepresentation(representationWithFile); observer.handleRepresentation(representationWithFile);
assertThat(getRepresentationCollector()).usingRecursiveComparison().ignoringFields("pipedInput", "pipedOutput") assertThat(getContentCollector()).usingRecursiveComparison().ignoringFields("pipedInput", "pipedOutput")
.isEqualTo(expectedContentCollector); .isEqualTo(expectedContentCollector);
} }
...@@ -339,7 +358,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -339,7 +358,7 @@ class EingangStubReceiverStreamObserverTest {
void shouldNotCollectContent() { void shouldNotCollectContent() {
observer.handleRepresentation(representationWithFile); observer.handleRepresentation(representationWithFile);
verify(representationCollector, never()).collect(any()); verify(contentCollector, never()).collect(any());
} }
} }
...@@ -349,11 +368,23 @@ class EingangStubReceiverStreamObserverTest { ...@@ -349,11 +368,23 @@ class EingangStubReceiverStreamObserverTest {
private final GrpcRepresentation representationWithContent = GrpcRepresentationTestFactory.createWithContent(); private final GrpcRepresentation representationWithContent = GrpcRepresentationTestFactory.createWithContent();
private final IncomingFile incomingFile = IncomingFileTestFactory.create(); private final IncomingFile incomingFile = IncomingFileTestFactory.create();
@BeforeEach
void setUp() {
setContentCollector(contentCollector);
}
@Test
void shouldCallVerifyMetadataWasSent() {
observer.handleRepresentation(representationWithContent);
verify(observer).verifyMetadataWasSent();
}
@Test @Test
void shouldCollectContent() { void shouldCollectContent() {
observer.handleRepresentation(representationWithContent); observer.handleRepresentation(representationWithContent);
verify(representationCollector).collect(GrpcRepresentationTestFactory.CONTENT); verify(contentCollector).collect(GrpcRepresentationTestFactory.CONTENT);
} }
@Test @Test
...@@ -365,7 +396,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -365,7 +396,7 @@ class EingangStubReceiverStreamObserverTest {
@Test @Test
void shouldCallAddAsRepresentation() { void shouldCallAddAsRepresentation() {
when(representationCollector.collect(any())).thenReturn(Optional.of(incomingFile)); when(contentCollector.collect(any())).thenReturn(Optional.of(incomingFile));
observer.handleRepresentation(representationWithContent); observer.handleRepresentation(representationWithContent);
...@@ -374,7 +405,7 @@ class EingangStubReceiverStreamObserverTest { ...@@ -374,7 +405,7 @@ class EingangStubReceiverStreamObserverTest {
@Test @Test
void shouldNotCallAddAsRepresentation() { void shouldNotCallAddAsRepresentation() {
when(representationCollector.collect(any())).thenReturn(Optional.empty()); when(contentCollector.collect(any())).thenReturn(Optional.empty());
observer.handleRepresentation(representationWithContent); observer.handleRepresentation(representationWithContent);
...@@ -383,17 +414,49 @@ class EingangStubReceiverStreamObserverTest { ...@@ -383,17 +414,49 @@ class EingangStubReceiverStreamObserverTest {
} }
} }
@Nested
class TestVerifyNoOngoingFileDownload {
@Test
void shouldThrowIllegalStateExceptionIfContentCollectorIsNotNull() {
setContentCollector(mock(ContentCollector.class));
assertThrows(IllegalStateException.class, () -> observer.verifyNoOngoingFileDownload());
}
@Test
void shouldNotThrowExceptionIfContentCollectorIsNull() {
assertDoesNotThrow(() -> observer.verifyNoOngoingFileDownload());
}
}
@Nested
class TestVerifyMetadataWasSent {
@Test
void shouldThrowIllegalStateExceptionIfContentCollectorIsNull() {
assertThrows(IllegalStateException.class, () -> observer.verifyMetadataWasSent());
}
@Test
void shouldNotThrowExceptionIfContentCollectorIsNotNull() {
setContentCollector(mock(ContentCollector.class));
assertDoesNotThrow(() -> observer.verifyMetadataWasSent());
}
}
@Nested @Nested
class TestAddAsAttachment { class TestAddAsAttachment {
@Mock @Mock
private ContentCollector attachmentCollector; private ContentCollector contentCollector;
private final IncomingFile incomingFile = IncomingFileTestFactory.create(); private final IncomingFile incomingFile = IncomingFileTestFactory.create();
@BeforeEach @BeforeEach
void setUp() { void setUp() {
setRepresentationCollector(attachmentCollector); setContentCollector(contentCollector);
setGroupName(GrpcAttachmentFileTestFactory.GROUP_NAME); setGroupName(GrpcAttachmentFileTestFactory.GROUP_NAME);
} }
...@@ -406,10 +469,10 @@ class EingangStubReceiverStreamObserverTest { ...@@ -406,10 +469,10 @@ class EingangStubReceiverStreamObserverTest {
} }
@Test @Test
void shouldSetAttachmentCollectorToNull() { void shouldSetContentCollectorToNull() {
observer.addAsAttachment(incomingFile); observer.addAsAttachment(incomingFile);
assertThat(getAttachmentCollector()).isNull(); assertThat(getContentCollector()).isNull();
} }
} }
...@@ -417,13 +480,13 @@ class EingangStubReceiverStreamObserverTest { ...@@ -417,13 +480,13 @@ class EingangStubReceiverStreamObserverTest {
class TestAddAsRepresentation { class TestAddAsRepresentation {
@Mock @Mock
private ContentCollector representationCollector; private ContentCollector contentCollector;
private final IncomingFile incomingFile = IncomingFileTestFactory.create(); private final IncomingFile incomingFile = IncomingFileTestFactory.create();
@BeforeEach @BeforeEach
void setUp() { void setUp() {
setRepresentationCollector(representationCollector); setContentCollector(contentCollector);
} }
@Test @Test
...@@ -434,10 +497,10 @@ class EingangStubReceiverStreamObserverTest { ...@@ -434,10 +497,10 @@ class EingangStubReceiverStreamObserverTest {
} }
@Test @Test
void shouldSetRepresentationCollectorToNull() { void shouldSetContentCollectorToNull() {
observer.addAsRepresentation(incomingFile); observer.addAsRepresentation(incomingFile);
assertThat(getRepresentationCollector()).isNull(); assertThat(getContentCollector()).isNull();
} }
} }
...@@ -445,28 +508,19 @@ class EingangStubReceiverStreamObserverTest { ...@@ -445,28 +508,19 @@ class EingangStubReceiverStreamObserverTest {
class TestOnError { class TestOnError {
@Mock @Mock
private ContentCollector attachmentCollector; private ContentCollector contentCollector;
@Mock
private ContentCollector representationCollector;
@BeforeEach @BeforeEach
void mock() { void mock() {
setAttachmentCollector(attachmentCollector); setContentCollector(contentCollector);
setRepresentationCollector(representationCollector); setContentCollector(contentCollector);
} }
@Test @Test
void shouldCloseAttachmentCollector() { void shouldCloseContentCollector() {
observer.onError(new Exception()); observer.onError(new Exception());
verify(attachmentCollector).close(); verify(contentCollector).close();
}
@Test
void shouldCloseRepresentationCollector() {
observer.onError(new Exception());
verify(representationCollector).close();
} }
} }
...@@ -591,20 +645,12 @@ class EingangStubReceiverStreamObserverTest { ...@@ -591,20 +645,12 @@ class EingangStubReceiverStreamObserverTest {
ReflectionTestUtils.setField(observer, "attachments", attachments); ReflectionTestUtils.setField(observer, "attachments", attachments);
} }
private ContentCollector getAttachmentCollector() { private ContentCollector getContentCollector() {
return (ContentCollector) ReflectionTestUtils.getField(observer, "attachmentCollector"); return (ContentCollector) ReflectionTestUtils.getField(observer, "contentCollector");
}
private void setAttachmentCollector(ContentCollector attachmentCollector) {
ReflectionTestUtils.setField(observer, "attachmentCollector", attachmentCollector);
}
private ContentCollector getRepresentationCollector() {
return (ContentCollector) ReflectionTestUtils.getField(observer, "representationCollector");
} }
private void setRepresentationCollector(ContentCollector representationCollector) { private void setContentCollector(ContentCollector contentCollector) {
ReflectionTestUtils.setField(observer, "representationCollector", representationCollector); ReflectionTestUtils.setField(observer, "contentCollector", contentCollector);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment