From 63ed43d60999433b57d1ca65b4ffc2d26380d925 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Mon, 7 Oct 2024 19:04:55 +0200 Subject: [PATCH] Migrate tests to JUnit5 --- pom.xml | 21 +------ .../caseimport/server/CaseService.java | 2 - .../caseimport/server/DirectoryService.java | 2 - .../caseimport/server/CaseImportTest.java | 58 ++++++++++--------- 4 files changed, 33 insertions(+), 50 deletions(-) diff --git a/pom.xml b/pom.xml index ed7a9de..e66244e 100644 --- a/pom.xml +++ b/pom.xml @@ -99,6 +99,7 @@ org.projectlombok lombok + provided com.fasterxml.jackson.core @@ -138,31 +139,11 @@ - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - - - org.mockito - mockito-core - test - org.springframework.boot spring-boot-starter-test test - - com.squareup.okhttp3 - mockwebserver - test - org.wiremock wiremock-jetty12 diff --git a/src/main/java/org/gridsuite/caseimport/server/CaseService.java b/src/main/java/org/gridsuite/caseimport/server/CaseService.java index 2431943..cef74f3 100644 --- a/src/main/java/org/gridsuite/caseimport/server/CaseService.java +++ b/src/main/java/org/gridsuite/caseimport/server/CaseService.java @@ -6,7 +6,6 @@ */ package org.gridsuite.caseimport.server; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.http.client.MultipartBodyBuilder; @@ -33,7 +32,6 @@ public class CaseService { private final RestTemplate restTemplate; private String caseServerBaseUri; - @Autowired public CaseService(@Value("${powsybl.services.case-server.base-uri:http://case-server/}") String caseServerBaseUri, RestTemplate restTemplate) { this.caseServerBaseUri = caseServerBaseUri; diff --git a/src/main/java/org/gridsuite/caseimport/server/DirectoryService.java b/src/main/java/org/gridsuite/caseimport/server/DirectoryService.java index 97bad14..1249060 100644 --- a/src/main/java/org/gridsuite/caseimport/server/DirectoryService.java +++ b/src/main/java/org/gridsuite/caseimport/server/DirectoryService.java @@ -7,7 +7,6 @@ package org.gridsuite.caseimport.server; import org.gridsuite.caseimport.server.dto.ElementAttributes; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -36,7 +35,6 @@ public class DirectoryService { private static final String HEADER_USER_ID = "userId"; public static final String ELEMENT = "ELEMENT"; - @Autowired public DirectoryService( @Value("${gridsuite.services.directory-server.base-uri:http://directory-server/}") String directoryServerBaseUri, RestTemplate restTemplate) { this.directoryServerBaseUri = directoryServerBaseUri; diff --git a/src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java b/src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java index 13ea5d2..5cd7cf2 100644 --- a/src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java +++ b/src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java @@ -6,37 +6,32 @@ */ package org.gridsuite.caseimport.server; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.WireMockServer; import org.gridsuite.caseimport.server.utils.WireMockUtils; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.ResourceUtils; import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * @author Abdelsalem Hedhili */ -@RunWith(SpringRunner.class) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) @AutoConfigureMockMvc @SpringBootTest -public class CaseImportTest { +class CaseImportTest { private static final String TEST_FILE = "testCase.xiidm"; private static final String TEST_CASE_NAME = "testCase"; private static final String TEST_FILE_WITH_ERRORS = "testCase_with_errors.xiidm"; @@ -52,8 +47,6 @@ public class CaseImportTest { private WireMockUtils wireMockUtils; - @Autowired - ObjectMapper objectMapper; @Autowired private MockMvc mockMvc; @Autowired @@ -61,24 +54,38 @@ public class CaseImportTest { @Autowired private CaseService caseService; - @Before - public void setup() throws IOException { + @BeforeAll + void initialize() { wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); wireMockUtils = new WireMockUtils(wireMockServer); // Start the server. wireMockServer.start(); + } + + @AfterAll + void tearDown() { + wireMockServer.shutdown(); + } + @BeforeEach + void setup() { + wireMockServer.resetAll(); directoryService.setDirectoryServerBaseUri(wireMockServer.baseUrl()); caseService.setBaseUri(wireMockServer.baseUrl()); } + @AfterAll + void cleanup() { + wireMockServer.checkForUnmatchedRequests(); + } + @Test - public void testImportCase() throws Exception { + void testImportCase() throws Exception { wireMockUtils.stubImportCase(TEST_FILE); wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -89,11 +96,11 @@ public void testImportCase() throws Exception { } @Test - public void testImportCaseWithBadRequestError() throws Exception { + void testImportCaseWithBadRequestError() throws Exception { wireMockUtils.stubImportCaseWithErrorInvalid(TEST_FILE_WITH_ERRORS); wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE_WITH_ERRORS, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE_WITH_ERRORS, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -104,11 +111,11 @@ public void testImportCaseWithBadRequestError() throws Exception { } @Test - public void testImportCaseWithUnprocessableEntityError() throws Exception { + void testImportCaseWithUnprocessableEntityError() throws Exception { wireMockUtils.stubImportCaseWithErrorBadExtension(TEST_INCORRECT_FILE); wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_INCORRECT_FILE, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_INCORRECT_FILE, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -119,11 +126,11 @@ public void testImportCaseWithUnprocessableEntityError() throws Exception { } @Test - public void testImportCaseWithInvalidOrigin() throws Exception { + void testImportCaseWithInvalidOrigin() throws Exception { wireMockUtils.stubImportCaseWithErrorInvalid(TEST_FILE); wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -135,12 +142,12 @@ public void testImportCaseWithInvalidOrigin() throws Exception { } @Test - public void testImportCaseWithValidOrigin() throws Exception { + void testImportCaseWithValidOrigin() throws Exception { final String caseName = "testCase"; wireMockUtils.stubImportCase(TEST_FILE); wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -155,11 +162,11 @@ public void testImportCaseWithValidOrigin() throws Exception { } @Test - public void testGivenEmptyCaseNameUseFilename() throws Exception { + void testGivenEmptyCaseNameUseFilename() throws Exception { wireMockUtils.stubImportCase(TEST_FILE); wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY); try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) { - MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is); + MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is); mockMvc.perform(multipart("/v1/cases").file(mockFile) .header("userId", USER1) @@ -171,6 +178,5 @@ public void testGivenEmptyCaseNameUseFilename() throws Exception { jsonPath("caseName").value(TEST_CASE_NAME), jsonPath("parentDirectory").value(CASE_ORIGIN_1_DIRECTORY)); } - } }