diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java b/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java index 8b6a80e61c811a7ba7cc1d83aacd5520c3900ae3..2e962704a048ef70eef7c1e5e81a7e269f4b7cdf 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java @@ -43,6 +43,7 @@ import de.ozgcloud.alfa.common.file.OzgFile; import de.ozgcloud.alfa.common.file.OzgFileService; import de.ozgcloud.alfa.common.zipdownload.ZipDownloadService; import de.ozgcloud.alfa.vorgang.VorgangController; +import de.ozgcloud.common.errorhandling.TechnicalException; import lombok.RequiredArgsConstructor; @RestController @@ -85,7 +86,7 @@ public class AttachmentByVorgangController { try { return Files.size(file.toPath()); } catch (IOException e) { - throw new RuntimeException(e); + throw new TechnicalException("Error getting file size", e); } } } diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java index 5c3ed501267851d27633e931cec02fcfaaea422a..b58f71556a561bf7092a5f183c3f86cbd233ca22 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java @@ -30,7 +30,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.io.File; +import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.Random; import java.util.UUID; @@ -43,6 +46,7 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Spy; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -61,6 +65,7 @@ import de.ozgcloud.alfa.vorgang.VorgangController; import de.ozgcloud.alfa.vorgang.VorgangHeaderTestFactory; import de.ozgcloud.alfa.vorgang.VorgangWithEingang; import de.ozgcloud.alfa.vorgang.VorgangWithEingangTestFactory; +import de.ozgcloud.common.errorhandling.TechnicalException; import lombok.SneakyThrows; class AttachmentByVorgangControllerTest { @@ -250,4 +255,43 @@ class AttachmentByVorgangControllerTest { controller.createZipFile(VorgangHeaderTestFactory.ID); } } + + @Nested + class TestGetFileSize { + + private final Path path = Path.of("/some/dummy/path/dummy-archive.zip"); + + @Test + void shouldCallFilesSize() { + try (MockedStatic<Files> mocked = mockStatic(Files.class)) { + controller.getFileSize(path.toFile()); + + mocked.verify(() -> Files.size(path)); + } + } + + @Test + void shouldReturnResultFileSize() { + var expectedFileSize = new Random().nextLong(); + try (MockedStatic<Files> mocked = mockStatic(Files.class)) { + mocked.when(() -> Files.size(any())).thenReturn(expectedFileSize); + + var fileSize = controller.getFileSize(path.toFile()); + + assertThat(fileSize).isEqualTo(expectedFileSize); + } + } + + @Test + void shouldThrowTechnicalException() { + try (MockedStatic<Files> mocked = mockStatic(Files.class)) { + mocked.when(() -> Files.size(any())).thenThrow(new IOException()); + + assertThatThrownBy(() -> controller.getFileSize(path.toFile())) + .isInstanceOf(TechnicalException.class) + .hasMessageStartingWith("Error getting file size") + .hasCauseInstanceOf(IOException.class); + } + } + } }