From 5093746ca522554be9d99e8042f5ecf53f620a14 Mon Sep 17 00:00:00 2001 From: Felix Reichenbach <felix.reichenbach@mgm-tp.com> Date: Thu, 20 Mar 2025 08:54:07 +0100 Subject: [PATCH] OZG-7573 only read content if EOF is false --- .../EingangStubReceiverStreamObserver.java | 13 ++-- ...EingangStubReceiverStreamObserverTest.java | 69 ++++++++++++------- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/forwarder/src/main/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserver.java b/forwarder/src/main/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserver.java index d5f1aa22a..1d355f5fd 100644 --- a/forwarder/src/main/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserver.java +++ b/forwarder/src/main/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserver.java @@ -156,13 +156,14 @@ public class EingangStubReceiverStreamObserver implements StreamObserver<GrpcRou if (Objects.isNull(currentFile)) { throw new IllegalStateException("File content received before metadata."); } - try { - pipedOutput.write(content.getContent().toByteArray()); - if (content.getIsEndOfFile()) { - handleEndOfFile(); + if (content.getIsEndOfFile()) { + handleEndOfFile(); + } else { + try { + pipedOutput.write(content.getContent().toByteArray()); + } catch (IOException e) { + throw new TechnicalException("Error when writing file content.", e); } - } catch (IOException e) { - throw new TechnicalException("Error when writing file content.", e); } } diff --git a/forwarder/src/test/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserverTest.java b/forwarder/src/test/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserverTest.java index f4bacd0b2..d14d99362 100644 --- a/forwarder/src/test/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserverTest.java +++ b/forwarder/src/test/java/de/ozgcloud/eingang/forwarder/EingangStubReceiverStreamObserverTest.java @@ -502,42 +502,61 @@ class EingangStubReceiverStreamObserverTest { setCurrentFile(incomingFile); } - @Test - @SneakyThrows - void shouldWriteContentToOutputStream() { - observer.storeFileContent(GrpcFileContentTestFactory.create()); + @Nested + class TestOnEndOfFile { - verify(pipedOutput).write(GrpcFileContentTestFactory.CONTENT); - } + private GrpcFileContent fileContent = GrpcFileContentTestFactory.createBuilder().setIsEndOfFile(true).build(); - @Test - void shouldCallHandleEndOfFile() { - doNothing().when(observer).handleEndOfFile(); - var fileContent = GrpcFileContentTestFactory.createBuilder().setIsEndOfFile(true).build(); + @BeforeEach + void setUp() { + doNothing().when(observer).handleEndOfFile(); + } + + @Test + void shouldCallHandleEndOfFile() { + observer.storeFileContent(fileContent); + + verify(observer).handleEndOfFile(); + } - observer.storeFileContent(fileContent); + @Test + @SneakyThrows + void shouldNotWriteContentToOutputStream() { + observer.storeFileContent(fileContent); - verify(observer).handleEndOfFile(); + verify(pipedOutput, never()).write(any()); + } } - @Test - void shouldNotCallHandleEndOfFile() { - var fileContent = GrpcFileContentTestFactory.createBuilder().setIsEndOfFile(false).build(); + @Nested + class TestOnNotEndOfFile { - observer.storeFileContent(fileContent); + private GrpcFileContent fileContent = GrpcFileContentTestFactory.createBuilder().setIsEndOfFile(false).build(); - verify(observer, never()).handleEndOfFile(); - } + @Test + @SneakyThrows + void shouldWriteContentToOutputStream() { + observer.storeFileContent(fileContent); - @Test - @SneakyThrows - void shouldThrowTechnicalExceptionOnIOException() { - doThrow(new IOException()).when(pipedOutput).write(any()); - var fileContent = GrpcFileContentTestFactory.create(); + verify(pipedOutput).write(GrpcFileContentTestFactory.CONTENT); + } - assertThrows(TechnicalException.class, () -> { + @Test + void shouldNotCallHandleEndOfFile() { observer.storeFileContent(fileContent); - }); + + verify(observer, never()).handleEndOfFile(); + } + + @Test + @SneakyThrows + void shouldThrowTechnicalExceptionOnIOException() { + doThrow(new IOException()).when(pipedOutput).write(any()); + + assertThrows(TechnicalException.class, () -> { + observer.storeFileContent(fileContent); + }); + } } } } -- GitLab