diff --git a/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java b/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java index 732600269b23577374e983b60b9b04fb3a649223..9a41471b91fb20bc65131a501291f4d55867016e 100644 --- a/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java +++ b/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java @@ -48,8 +48,8 @@ public class GrpcBinaryFileServerDownloader<T> { static final int CHUNK_SIZE = 255 * 1024; - private static final int END_OF_STREAM = -1; - private static final int NOTHING_READ = 0; + static final int END_OF_STREAM = -1; + static final int NOTHING_READ = 0; private final CallStreamObserver<T> callObserver; private final Function<ByteString, T> chunkBuilder; @@ -126,7 +126,7 @@ public class GrpcBinaryFileServerDownloader<T> { try { doSendChunks(); } catch (Exception e) { - completeRequestWithError(new TechnicalException("Error while sending chunks", e)); + handleError(new TechnicalException("Error while sending chunks", e)); } } @@ -144,36 +144,37 @@ public class GrpcBinaryFileServerDownloader<T> { var bytesRead = inputStream.read(buffer); switch (bytesRead) { case END_OF_STREAM: - completeRequest(); + finishProcessing(); break; case NOTHING_READ: break; default: - callObserver.onNext(chunkBuilder.apply(ByteString.copyFrom(buffer, 0, bytesRead))); - LOG.debug("Sent {} bytes", bytesRead); + sendBytesToCallObserver(bytesRead); } } - void completeRequest() { + void sendBytesToCallObserver(int bytesRead) { + var bytes = ByteString.copyFrom(buffer, 0, bytesRead); + var chunk = chunkBuilder.apply(bytes); + callObserver.onNext(chunk); + LOG.debug("Sent {} bytes", bytesRead); + } + + void finishProcessing() { if (Objects.nonNull(downloadError.get())) { throw downloadError.get(); } else { - completeRequestNormally(); + finishRequest(); + callObserver.onCompleted(); } } - void completeRequestWithError(TechnicalException e) { + void handleError(TechnicalException e) { LOG.debug("Complete download request with error"); finishRequest(); throw e; } - void completeRequestNormally() { - LOG.debug("Complete download request"); - finishRequest(); - callObserver.onCompleted(); - } - private void finishRequest() { requestFinished.set(true); closeInputStream(); diff --git a/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderITCase.java b/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderITCase.java index 039f873a97eee187224a497865b55efdc69e7d3b..fed3cc387ce54f90b1d4dec72c515cc2556f7ee5 100644 --- a/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderITCase.java +++ b/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderITCase.java @@ -65,7 +65,7 @@ class GrpcBinaryFileServerDownloaderITCase { @Nested class OnNoError { - private static final int DOWNLOAD_DATA_LENGTH = Double.valueOf(GrpcBinaryFileServerDownloader.CHUNK_SIZE * 1.5).intValue(); + private static final int DOWNLOAD_DATA_LENGTH = (int) (GrpcBinaryFileServerDownloader.CHUNK_SIZE * 1.5); @Captor private ArgumentCaptor<GrpcResponseDummy> captor; diff --git a/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderTest.java b/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderTest.java index ba9b67ab7e44121f7cccda3268c737fa91c9d14c..1817466f0245e6a85caf5bc7c5244277c965c119 100644 --- a/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderTest.java +++ b/ozgcloud-common-lib/src/test/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloaderTest.java @@ -23,6 +23,7 @@ */ package de.ozgcloud.common.binaryfile; +import static de.ozgcloud.common.binaryfile.GrpcBinaryFileServerDownloader.*; import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -337,14 +338,14 @@ class GrpcBinaryFileServerDownloaderTest { @BeforeEach void init() { doThrow(exception).when(downloader).doSendChunks(); - doNothing().when(downloader).completeRequestWithError(any()); + doNothing().when(downloader).handleError(any()); } @Test - void shouldCompleteRequestWithError() { + void shouldHandleError() { downloader.sendChunks(); - verify(downloader).completeRequestWithError(argumentCaptor.capture()); + verify(downloader).handleError(argumentCaptor.capture()); assertThat(argumentCaptor.getValue()).isInstanceOf(TechnicalException.class).hasCause(exception); } } @@ -397,25 +398,25 @@ class GrpcBinaryFileServerDownloaderTest { @SneakyThrows @BeforeEach void init() { - doNothing().when(downloader).completeRequest(); - when(inputStream.read(any())).thenReturn(-1); + doNothing().when(downloader).finishProcessing(); + when(inputStream.read(any())).thenReturn(END_OF_STREAM); setInputStreamField(inputStream); } @SneakyThrows @Test - void shouldCompleteRequest() { + void shouldFinishProcessing() { downloader.processDataFromInputStream(); - verify(downloader).completeRequest(); + verify(downloader).finishProcessing(); } @SneakyThrows @Test - void shouldNotCallCallObserver() { + void shouldNotSendBytesToCallObserver() { downloader.processDataFromInputStream(); - verifyNoInteractions(callObserver); + verify(downloader, never()).sendBytesToCallObserver(anyInt()); } } @@ -425,56 +426,73 @@ class GrpcBinaryFileServerDownloaderTest { @SneakyThrows @BeforeEach void init() { - when(inputStream.read(any())).thenReturn(0); + when(inputStream.read(any())).thenReturn(NOTHING_READ); setInputStreamField(inputStream); } @SneakyThrows @Test - void shouldNotCallCallObserver() { + void shouldNotSendBytesToCallObserver() { downloader.processDataFromInputStream(); - verifyNoInteractions(callObserver); + verify(downloader, never()).sendBytesToCallObserver(anyInt()); } } @Nested class OnBytesWereReceived { - @Captor - private ArgumentCaptor<ByteString> byteStringCaptor; - - private final int readBytes = 20; - private final byte[] buffer = new byte[readBytes]; - private final GrpcResponseDummy grpcResponseDummy = new GrpcResponseDummy(); + private final int bytesRead = 20; @SneakyThrows @BeforeEach void mock() { - when(inputStream.read(any())).thenReturn(readBytes); + when(inputStream.read(any())).thenReturn(bytesRead); setInputStreamField(inputStream); - new Random().nextBytes(buffer); - ReflectionTestUtils.setField(downloader, "buffer", buffer); } @SneakyThrows @Test - void shouldCallChunkBuilder() { + void shouldSendBytesToCallObserver() { downloader.processDataFromInputStream(); - verify(chunkBuilder).apply(byteStringCaptor.capture()); - assertThat(byteStringCaptor.getValue().toByteArray()).isEqualTo(buffer); + verify(downloader).sendBytesToCallObserver(bytesRead); } + } + } - @SneakyThrows - @Test - void shouldCallOnNext() { - when(chunkBuilder.apply(any())).thenReturn(grpcResponseDummy); + @Nested + class TestSendBytesToCallObserver { - downloader.processDataFromInputStream(); + @Captor + private ArgumentCaptor<ByteString> byteStringCaptor; + private final int bytesRead = 20; + private final byte[] buffer = new byte[bytesRead]; + private final GrpcResponseDummy grpcResponseDummy = new GrpcResponseDummy(); - verify(callObserver).onNext(grpcResponseDummy); - } + @BeforeEach + void init() { + new Random().nextBytes(buffer); + ReflectionTestUtils.setField(downloader, "buffer", buffer); + } + + @SneakyThrows + @Test + void shouldCallChunkBuilder() { + downloader.sendBytesToCallObserver(bytesRead); + + verify(chunkBuilder).apply(byteStringCaptor.capture()); + assertThat(byteStringCaptor.getValue().toByteArray()).isEqualTo(buffer); + } + + @SneakyThrows + @Test + void shouldCallOnNext() { + when(chunkBuilder.apply(any())).thenReturn(grpcResponseDummy); + + downloader.sendBytesToCallObserver(bytesRead); + + verify(callObserver).onNext(grpcResponseDummy); } } @@ -503,7 +521,7 @@ class GrpcBinaryFileServerDownloaderTest { } @Nested - class TestCompleteRequest { + class TestFinishProcessing { @Nested class OnError { @@ -517,7 +535,7 @@ class GrpcBinaryFileServerDownloaderTest { @Test void shouldThrowException() { - assertThatThrownBy(downloader::completeRequest).isSameAs(exception); + assertThatThrownBy(downloader::finishProcessing).isSameAs(exception); } } @@ -526,59 +544,43 @@ class GrpcBinaryFileServerDownloaderTest { @BeforeEach void init() { - doNothing().when(downloader).completeRequestNormally(); + doNothing().when(downloader).closeInputStream(); } @Test void shouldNotCompleteRequestWithError() { - downloader.completeRequest(); + downloader.finishProcessing(); - verify(downloader, never()).completeRequestWithError(any()); + verify(downloader, never()).handleError(any()); } @Test - void shouldCompleteRequestNormally() { - downloader.completeRequest(); + void shouldSetRequestFinished() { + assertThat(getRequestFinished()).isFalse(); - verify(downloader).completeRequestNormally(); - } - } - } + downloader.finishProcessing(); - @Nested - class TestCompleteRequestNormally { - - @BeforeEach - void init() { - doNothing().when(downloader).closeInputStream(); - } - - @Test - void shouldSetRequestFinished() { - assertThat(getRequestFinished()).isFalse(); - - downloader.completeRequestNormally(); - - assertThat(getRequestFinished()).isTrue(); - } + assertThat(getRequestFinished()).isTrue(); + } - @Test - void shouldCloseInputStream() { - downloader.completeRequestNormally(); + @Test + void shouldCloseInputStream() { + downloader.finishProcessing(); - verify(downloader).closeInputStream(); - } + verify(downloader).closeInputStream(); + } - @Test - void shouldNotifyObserver() { - downloader.completeRequestNormally(); + @Test + void shouldNotifyObserver() { + downloader.finishProcessing(); - verify(callObserver).onCompleted(); + verify(callObserver).onCompleted(); + } } } @Nested - class TestCompleteRequestWithError { + class TestHandleError { private final TechnicalException error = new TechnicalException("error"); @@ -591,21 +593,21 @@ class GrpcBinaryFileServerDownloaderTest { void shouldSetRequestFinished() { assertThat(getRequestFinished()).isFalse(); - catchException(() -> downloader.completeRequestWithError(error)); + catchException(() -> downloader.handleError(error)); assertThat(getRequestFinished()).isTrue(); } @Test void shouldCloseInputStream() { - catchException(() -> downloader.completeRequestWithError(error)); + catchException(() -> downloader.handleError(error)); verify(downloader).closeInputStream(); } @Test void shouldThrowException() { - assertThatThrownBy(() -> downloader.completeRequestWithError(error)).isSameAs(error); + assertThatThrownBy(() -> downloader.handleError(error)).isSameAs(error); } }