From 189e664f37070d01f6f7442ad611041926f8c94e Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Fri, 15 Mar 2024 18:49:41 +0100 Subject: [PATCH] Add tests for Storage system --- .../Clyde/EndPoints/StorageController.java | 1 - .../Clyde/Repositories/FileRepository.java | 3 + .../Clyde/Services/StorageService.java | 2 - .../Clyde/Services/StorageServiceTest.java | 75 +++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java index 724ae10..5ad2052 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -11,7 +11,6 @@ import ovh.herisson.Clyde.Tables.StorageFile; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") public class StorageController { - private final StorageService storageServ; public StorageController(StorageService storageServ){ diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java index 2240c92..b736a1d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java @@ -4,6 +4,9 @@ import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.StorageFile; +import java.util.ArrayList; + public interface FileRepository extends CrudRepository { + public StorageFile getStorageFileByName(String name); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index c7f8d1b..14a41d9 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -17,8 +17,6 @@ import java.util.UUID; @Service public class StorageService { - - private final Path rootLocation = Paths.get("cdn/"); private final FileRepository fileRepo; diff --git a/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java b/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java new file mode 100644 index 0000000..b44454b --- /dev/null +++ b/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java @@ -0,0 +1,75 @@ +package ovh.herisson.Clyde.Services; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.TestPropertySource; +import org.springframework.web.multipart.MultipartFile; +import org.testcontainers.shaded.com.google.common.net.MediaType; +import ovh.herisson.Clyde.Repositories.FileRepository; +import ovh.herisson.Clyde.Tables.FileType; +import ovh.herisson.Clyde.Tables.StorageFile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; + +@DataJpaTest +@TestPropertySource(properties = { + "spring.test.database.replace=none", + "spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db" +}) +public class StorageServiceTest { + @Autowired + FileRepository fileRepo; + + StorageService ss; + + @BeforeEach + public void setup(){ + if (ss == null){ + ss = new StorageService(fileRepo); + } + } + + @Test + //Check si le fichier est bien sauvegardé dans la DB et si le fichier est bien sauvegardé au bon endroit + public void saveFile(){ + //Test si le directory a bien été crée a l'init du fileService + Path rootloc = Paths.get("cdn/"); + + Assert.assertTrue(Files.exists(rootloc)); + } + + @Test + public void deleteFile() throws IOException { + File file = new File("cdn/test.txt"); + file.createNewFile(); + + //On vérifie que le fichier a bien été crée + Assert.assertTrue(file.exists()); + + //StorageFile représentant le fichier + StorageFile sf = new StorageFile("testfile",file.getPath(), FileType.ProfilePicture); + fileRepo.save(sf); + + //Check that the storagefile is properly saved + StorageFile resp = fileRepo.getStorageFileByName("testfile"); + Assert.assertEquals(sf, resp); + + ss.delete(sf); + + //On vérifie que le fichier a bien été delete et que le StorageFile a été delete de la DB + Assert.assertFalse(file.exists()); + + resp = fileRepo.getStorageFileByName("testfile"); + Assert.assertEquals(null, resp); + } +}