/
Chebarik
/
PackageManagement
Обзор
Документация
Войти
/
Chebarik
/
PackageManagement
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/packagemanagement/infrastructure/storage/FileStorageServiceTest.java
234 строки
7 KB
Николай Мехоношин
added unit tests
13 янв 2026, 20:50
13 янв 2026, 20:50
d22b6a7
Код
Авторство
О чём код?
package com.packagemanagement.infrastructure.storage; import com.packagemanagement.domain.exception.VersionNotFoundException; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.*; @DisplayName("FileStorageService") class FileStorageServiceTest { @TempDir Path tempDir; private FileStorageService storageService; @BeforeEach void setUp() { storageService = new FileStorageService(tempDir.toString()); storageService.init(); } @Nested @DisplayName("store") class Store { @Test @DisplayName("should store content successfully") void shouldStoreContentSuccessfully() { byte[] content = "test content".getBytes(); String path = storageService.store("package-1", "1.0.0", content); assertNotNull(path); assertTrue(storageService.exists("package-1", "1.0.0")); } @Test @DisplayName("should create directory structure") void shouldCreateDirectoryStructure() { byte[] content = "test".getBytes(); storageService.store("my-package", "2.0.0", content); Path expectedDir = tempDir.resolve("my-package").resolve("2.0.0"); assertTrue(Files.isDirectory(expectedDir)); } @Test @DisplayName("should overwrite existing file") void shouldOverwriteExistingFile() { byte[] content1 = "first content".getBytes(); byte[] content2 = "second content".getBytes(); storageService.store("package-1", "1.0.0", content1); storageService.store("package-1", "1.0.0", content2); byte[] retrieved = storageService.retrieve("package-1", "1.0.0"); assertArrayEquals(content2, retrieved); } @Test @DisplayName("should sanitize path to prevent traversal") void shouldSanitizePathToPreventTraversal() { byte[] content = "test".getBytes(); storageService.store("../../../etc", "passwd", content); assertFalse(Files.exists(tempDir.resolve("../../../etc"))); } } @Nested @DisplayName("retrieve") class Retrieve { @Test @DisplayName("should retrieve stored content") void shouldRetrieveStoredContent() { byte[] content = "Hello, World!".getBytes(); storageService.store("package-1", "1.0.0", content); byte[] retrieved = storageService.retrieve("package-1", "1.0.0"); assertArrayEquals(content, retrieved); } @Test @DisplayName("should throw exception when not found") void shouldThrowExceptionWhenNotFound() { assertThrows(VersionNotFoundException.class, () -> storageService.retrieve("non-existent", "1.0.0") ); } @Test @DisplayName("should retrieve large content") void shouldRetrieveLargeContent() { byte[] content = new byte[1024 * 1024]; for (int i = 0; i < content.length; i++) { content[i] = (byte) (i % 256); } storageService.store("large-package", "1.0.0", content); byte[] retrieved = storageService.retrieve("large-package", "1.0.0"); assertArrayEquals(content, retrieved); } } @Nested @DisplayName("delete") class Delete { @Test @DisplayName("should delete existing file") void shouldDeleteExistingFile() { byte[] content = "test".getBytes(); storageService.store("package-1", "1.0.0", content); storageService.delete("package-1", "1.0.0"); assertFalse(storageService.exists("package-1", "1.0.0")); } @Test @DisplayName("should not throw when deleting non-existent file") void shouldNotThrowWhenDeletingNonExistent() { assertDoesNotThrow(() -> storageService.delete("non-existent", "1.0.0") ); } @Test @DisplayName("should clean up empty directories") void shouldCleanUpEmptyDirectories() throws IOException { byte[] content = "test".getBytes(); storageService.store("cleanup-test", "1.0.0", content); Path packageDir = tempDir.resolve("cleanup-test"); assertTrue(Files.isDirectory(packageDir)); storageService.delete("cleanup-test", "1.0.0"); assertFalse(Files.exists(packageDir.resolve("1.0.0"))); } } @Nested @DisplayName("deleteAll") class DeleteAll { @Test @DisplayName("should delete all versions of package") void shouldDeleteAllVersions() { storageService.store("package-1", "1.0.0", "v1".getBytes()); storageService.store("package-1", "2.0.0", "v2".getBytes()); storageService.store("package-1", "3.0.0", "v3".getBytes()); storageService.deleteAll("package-1"); assertFalse(storageService.exists("package-1", "1.0.0")); assertFalse(storageService.exists("package-1", "2.0.0")); assertFalse(storageService.exists("package-1", "3.0.0")); } @Test @DisplayName("should not affect other packages") void shouldNotAffectOtherPackages() { storageService.store("package-1", "1.0.0", "p1".getBytes()); storageService.store("package-2", "1.0.0", "p2".getBytes()); storageService.deleteAll("package-1"); assertFalse(storageService.exists("package-1", "1.0.0")); assertTrue(storageService.exists("package-2", "1.0.0")); } @Test @DisplayName("should not throw when package doesn't exist") void shouldNotThrowWhenPackageNotExists() { assertDoesNotThrow(() -> storageService.deleteAll("non-existent") ); } } @Nested @DisplayName("exists") class Exists { @Test @DisplayName("should return true for existing file") void shouldReturnTrueForExistingFile() { storageService.store("package-1", "1.0.0", "test".getBytes()); assertTrue(storageService.exists("package-1", "1.0.0")); } @Test @DisplayName("should return false for non-existing file") void shouldReturnFalseForNonExistingFile() { assertFalse(storageService.exists("package-1", "1.0.0")); } } @Nested @DisplayName("getSize") class GetSize { @Test @DisplayName("should return correct file size") void shouldReturnCorrectFileSize() { byte[] content = "Hello, World!".getBytes(); storageService.store("package-1", "1.0.0", content); long size = storageService.getSize("package-1", "1.0.0"); assertEquals(content.length, size); } @Test @DisplayName("should return 0 for non-existing file") void shouldReturnZeroForNonExistingFile() { long size = storageService.getSize("non-existent", "1.0.0"); assertEquals(0, size); } } }