Skip to content
Snippets Groups Projects
Commit 7645c890 authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

OZG-6223 OZG-7381 Test for getFileSize

parent 3a5dd47d
No related branches found
No related tags found
1 merge request!2OZG-6223 OZG-7401 Timeout for async requests
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
...@@ -43,6 +43,7 @@ import de.ozgcloud.alfa.common.file.OzgFile; ...@@ -43,6 +43,7 @@ import de.ozgcloud.alfa.common.file.OzgFile;
import de.ozgcloud.alfa.common.file.OzgFileService; import de.ozgcloud.alfa.common.file.OzgFileService;
import de.ozgcloud.alfa.common.zipdownload.ZipDownloadService; import de.ozgcloud.alfa.common.zipdownload.ZipDownloadService;
import de.ozgcloud.alfa.vorgang.VorgangController; import de.ozgcloud.alfa.vorgang.VorgangController;
import de.ozgcloud.common.errorhandling.TechnicalException;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RestController @RestController
...@@ -85,7 +86,7 @@ public class AttachmentByVorgangController { ...@@ -85,7 +86,7 @@ public class AttachmentByVorgangController {
try { try {
return Files.size(file.toPath()); return Files.size(file.toPath());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new TechnicalException("Error getting file size", e);
} }
} }
} }
...@@ -30,7 +30,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder ...@@ -30,7 +30,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
...@@ -43,6 +46,7 @@ import org.junit.jupiter.api.Test; ...@@ -43,6 +46,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers; import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -61,6 +65,7 @@ import de.ozgcloud.alfa.vorgang.VorgangController; ...@@ -61,6 +65,7 @@ import de.ozgcloud.alfa.vorgang.VorgangController;
import de.ozgcloud.alfa.vorgang.VorgangHeaderTestFactory; import de.ozgcloud.alfa.vorgang.VorgangHeaderTestFactory;
import de.ozgcloud.alfa.vorgang.VorgangWithEingang; import de.ozgcloud.alfa.vorgang.VorgangWithEingang;
import de.ozgcloud.alfa.vorgang.VorgangWithEingangTestFactory; import de.ozgcloud.alfa.vorgang.VorgangWithEingangTestFactory;
import de.ozgcloud.common.errorhandling.TechnicalException;
import lombok.SneakyThrows; import lombok.SneakyThrows;
class AttachmentByVorgangControllerTest { class AttachmentByVorgangControllerTest {
...@@ -250,4 +255,43 @@ class AttachmentByVorgangControllerTest { ...@@ -250,4 +255,43 @@ class AttachmentByVorgangControllerTest {
controller.createZipFile(VorgangHeaderTestFactory.ID); 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);
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment