Leo/Backend/UnitTest #138
@ -11,7 +11,6 @@ import ovh.herisson.Clyde.Tables.StorageFile;
 | 
				
			|||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
public class StorageController {
 | 
					public class StorageController {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    private final StorageService storageServ;
 | 
					    private final StorageService storageServ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public StorageController(StorageService storageServ){
 | 
					    public StorageController(StorageService storageServ){
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,9 @@ import org.springframework.data.repository.CrudRepository;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import ovh.herisson.Clyde.Tables.StorageFile;
 | 
					import ovh.herisson.Clyde.Tables.StorageFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public interface FileRepository extends CrudRepository<StorageFile,Long> {
 | 
					public interface FileRepository extends CrudRepository<StorageFile,Long> {
 | 
				
			||||||
 | 
					    public StorageFile getStorageFileByName(String name);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -17,8 +17,6 @@ import java.util.UUID;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@Service
 | 
					@Service
 | 
				
			||||||
public class StorageService {
 | 
					public class StorageService {
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private final Path rootLocation = Paths.get("cdn/");
 | 
					    private final Path rootLocation = Paths.get("cdn/");
 | 
				
			||||||
    private final FileRepository fileRepo;
 | 
					    private final FileRepository fileRepo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -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;
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
					
					Maxime marked this conversation as resolved
					
				 
				 | 
				|||||||
 | 
					
 | 
				
			||||||
 | 
					    @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
	
normalement tu peux aussi @ Autowired le StorageService
Edit : j'ai testé et ca fait crash les tests