Add testing for file uploading and correct minors bugs in service
This commit is contained in:
		| @ -0,0 +1,103 @@ | ||||
| package ovh.herisson.Clyde.Endpoints; | ||||
|  | ||||
| import com.github.dockerjava.api.model.ExposedPort; | ||||
| import com.github.dockerjava.api.model.HostConfig; | ||||
| import com.github.dockerjava.api.model.PortBinding; | ||||
| import com.github.dockerjava.api.model.Ports; | ||||
| import io.restassured.RestAssured; | ||||
| import io.restassured.http.ContentType; | ||||
| import org.junit.Assert; | ||||
| import org.junit.jupiter.api.*; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.boot.test.web.server.LocalServerPort; | ||||
| import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.mock.web.MockMultipartFile; | ||||
| import org.testcontainers.containers.PostgreSQLContainer; | ||||
| import org.testcontainers.junit.jupiter.Container; | ||||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||||
| import ovh.herisson.Clyde.ClydeApplication; | ||||
| import ovh.herisson.Clyde.EndPoints.StorageController; | ||||
| import ovh.herisson.Clyde.Repositories.FileRepository; | ||||
| import ovh.herisson.Clyde.Services.StorageService; | ||||
| import ovh.herisson.Clyde.Tables.FileType; | ||||
| import ovh.herisson.Clyde.Tables.StorageFile; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.FileWriter; | ||||
| import java.io.IOException; | ||||
| import java.util.ArrayList; | ||||
|  | ||||
| import static io.restassured.RestAssured.with; | ||||
|  | ||||
| @Testcontainers | ||||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||||
| public class StorageControllerTest { | ||||
|     @LocalServerPort | ||||
|     private Integer port; | ||||
|  | ||||
|     @Autowired | ||||
|     private ClydeApplication controller; | ||||
|  | ||||
|     @Autowired | ||||
|     private StorageController storageController; | ||||
|  | ||||
|     @Autowired | ||||
|     private StorageService storageService; | ||||
|     @Autowired | ||||
|     private FileRepository fileRepository; | ||||
|     @Container | ||||
|     @ServiceConnection | ||||
|     static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:alpine") | ||||
|             .withDatabaseName("clyde") | ||||
|             .withUsername("devel") | ||||
|             .withPassword("devel") | ||||
|             .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(5432), new ExposedPort(5432))))); | ||||
|  | ||||
|     @BeforeAll | ||||
|     static void beforeAll(){ | ||||
|         postgres.start(); | ||||
|     } | ||||
|  | ||||
|     @AfterAll | ||||
|     static void afterAll(){ | ||||
|         postgres.stop(); | ||||
|     } | ||||
|  | ||||
|     @BeforeEach | ||||
|     void setup(){ | ||||
|         RestAssured.baseURI = "http://localhost:" + port; | ||||
|     } | ||||
|  | ||||
|     @AfterEach | ||||
|     void afterEach(){ | ||||
|         fileRepository.deleteAll(); | ||||
|     } | ||||
|     @Test | ||||
|     public void uploadFileTest() throws IOException { | ||||
|         File mmf = new File("test.txt"); | ||||
|         mmf.createNewFile(); | ||||
|  | ||||
|         FileWriter fw = new FileWriter(mmf); | ||||
|         fw.write("Ceci est un test"); | ||||
|         fw.close(); | ||||
|  | ||||
|         with().multiPart("file", mmf).pathParam("fileType", FileType.ProfilePicture).when().request("POST", "/upload/{fileType}").then().statusCode(200); | ||||
|  | ||||
|         StorageFile sf = fileRepository.getStorageFileByName("test.txt"); | ||||
|  | ||||
|         //On vérifie que le fichier a bien été stocké dans la db | ||||
|         Assert.assertFalse(sf == null); | ||||
|  | ||||
|         //On vérifie que le fichier a bel et bien été stocké dans le dossier cdn | ||||
|         File extFile = new File(sf.getUrl()); | ||||
|         Assert.assertTrue(extFile.exists()); | ||||
|  | ||||
|         Assert.assertEquals("cdn", extFile.getParent()); | ||||
|  | ||||
|         //On delete tranquillement les fichiers (la méthode delete a déja été testée dans StorageServiceTest) | ||||
|         storageService.delete(sf); | ||||
|         mmf.delete(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user