Add tests for Storage system
This commit is contained in:
		| @ -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); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user