From 67438fc1e8e222a74cf8f3eef75dc9217f9f2d81 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Mon, 14 Oct 2024 18:42:40 +0200 Subject: [PATCH 1/2] Migrate tests to JUnit5 * remove unused log4j2-mock * remove deprecated mockito-inline (since v5.3.0) * replace guava-testlib by assertj (already included) Signed-off-by: Tristan <135599584+Tristan-WorkGH@users.noreply.github.com> Signed-off-by: Tristan Chuine --- pom.xml | 58 +++------------- .../parsers/cgmes/CgmesFileName.java | 1 + .../parsers/entsoe/EntsoeFileName.java | 1 + .../caseserver/CaseControllerTest.java | 68 +++++++------------ .../caseserver/CaseFileNameParserTests.java | 37 +++++----- .../CaseInfosELRepositoryTests.java | 26 +++---- .../caseserver/ScheduledCaseCleanerTest.java | 32 ++++----- .../caseserver/SupervisionControllerTest.java | 55 ++++++--------- .../util/CaseDataSourceControllerTest.java | 40 +++++------ .../elasticsearch/EmbeddedElasticsearch.java | 5 +- .../powsybl/caseserver/utils/TestUtils.java | 6 +- 11 files changed, 115 insertions(+), 214 deletions(-) diff --git a/pom.xml b/pom.xml index 6073310..cca1b85 100644 --- a/pom.xml +++ b/pom.xml @@ -45,11 +45,7 @@ com.powsybl.caseserver.repository - - 3.11.1 - 0.0.2 2.15.0 - 1.18.3 @@ -84,6 +80,8 @@ + + com.powsybl @@ -93,18 +91,7 @@ import - - - de.dentrassi.elasticsearch - log4j2-mock - ${log4j2-mock-version} - - - org.testcontainers - elasticsearch - ${testcontainers.version} - test - + @@ -168,13 +155,13 @@ org.springdoc springdoc-openapi-starter-webmvc-ui + org.liquibase liquibase-core runtime - org.postgresql postgresql @@ -215,7 +202,7 @@ powsybl-ucte-converter runtime - + org.slf4j log4j-over-slf4j runtime @@ -230,12 +217,8 @@ micrometer-registry-prometheus runtime + - - com.google.guava - guava-testlib - test - com.google.jimfs jimfs @@ -251,21 +234,6 @@ powsybl-config-test test - - de.dentrassi.elasticsearch - log4j2-mock - test - - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - org.springframework.boot spring-boot-starter-test @@ -286,17 +254,9 @@ spring-integration-test test - - - - - org.mockito - mockito-inline - ${mockito-inline.version} + + org.testcontainers + junit-jupiter test diff --git a/src/main/java/com/powsybl/caseserver/parsers/cgmes/CgmesFileName.java b/src/main/java/com/powsybl/caseserver/parsers/cgmes/CgmesFileName.java index 2068876..b309181 100644 --- a/src/main/java/com/powsybl/caseserver/parsers/cgmes/CgmesFileName.java +++ b/src/main/java/com/powsybl/caseserver/parsers/cgmes/CgmesFileName.java @@ -27,6 +27,7 @@ public class CgmesFileName implements FileNameInfos { @NonNull private String tso; @NonNull private Integer version; + @Override public Type getType() { return Type.CGMES; } diff --git a/src/main/java/com/powsybl/caseserver/parsers/entsoe/EntsoeFileName.java b/src/main/java/com/powsybl/caseserver/parsers/entsoe/EntsoeFileName.java index 6fde85f..aa26181 100644 --- a/src/main/java/com/powsybl/caseserver/parsers/entsoe/EntsoeFileName.java +++ b/src/main/java/com/powsybl/caseserver/parsers/entsoe/EntsoeFileName.java @@ -28,6 +28,7 @@ public class EntsoeFileName implements FileNameInfos { @NonNull private EntsoeGeographicalCode geographicalCode; @NonNull private Integer version; + @Override public Type getType() { return Type.ENTSOE; } diff --git a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java index bdcfb5d..c6bcf54 100644 --- a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java @@ -17,10 +17,9 @@ import com.powsybl.caseserver.repository.CaseMetadataRepository; import com.powsybl.caseserver.utils.TestUtils; import com.powsybl.computation.ComputationManager; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -31,7 +30,6 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -48,21 +46,11 @@ import java.util.List; import java.util.UUID; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.junit.jupiter.api.Assertions.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -70,12 +58,10 @@ * @author Abdelsalem Hedhili * @author Franck Lecuyer */ -@RunWith(SpringRunner.class) @AutoConfigureMockMvc @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, properties = {"case-store-directory=/cases"}) @ContextConfigurationWithTestChannel -public class CaseControllerTest { - +class CaseControllerTest { private static final String TEST_CASE = "testCase.xiidm"; private static final String TEST_CASE_FORMAT = "XIIDM"; private static final String NOT_A_NETWORK = "notANetwork.txt"; @@ -108,20 +94,16 @@ public class CaseControllerTest { private final String caseImportDestination = "case.import.destination"; - @Before - public void setUp() { + @BeforeEach + void setUp() { fileSystem = Jimfs.newFileSystem(Configuration.unix()); caseService.setFileSystem(fileSystem); caseService.setComputationManager(Mockito.mock(ComputationManager.class)); - cleanDB(); - outputDestination.clear(); - } - - private void cleanDB() { caseMetadataRepository.deleteAll(); + outputDestination.clear(); } - @After + @AfterEach public void tearDown() throws Exception { fileSystem.close(); List destinations = List.of(caseImportDestination); @@ -142,14 +124,14 @@ private static MockMultipartFile createMockMultipartFile(String fileName) throws } @Test - public void testStorageNotCreated() throws Exception { + void testStorageNotCreated() throws Exception { // expect a fail since the storage dir. is not created mvc.perform(delete("/v1/cases")) .andExpect(status().isUnprocessableEntity()); } @Test - public void testDeleteCases() throws Exception { + void testDeleteCases() throws Exception { // create the storage dir createStorageDir(); @@ -158,7 +140,7 @@ public void testDeleteCases() throws Exception { } @Test - public void testCheckNonExistingCase() throws Exception { + void testCheckNonExistingCase() throws Exception { // create the storage dir createStorageDir(); @@ -170,7 +152,7 @@ public void testCheckNonExistingCase() throws Exception { } @Test - public void testImportValidCase() throws Exception { + void testImportValidCase() throws Exception { createStorageDir(); // import a case @@ -225,7 +207,7 @@ public void testImportValidCase() throws Exception { } @Test - public void testImportInvalidFile() throws Exception { + void testImportInvalidFile() throws Exception { createStorageDir(); // import a non valid case and expect a fail @@ -244,7 +226,7 @@ public void testImportInvalidFile() throws Exception { } @Test - public void testDownloadNonExistingCase() throws Exception { + void testDownloadNonExistingCase() throws Exception { createStorageDir(); // download a non existing case @@ -254,7 +236,7 @@ public void testDownloadNonExistingCase() throws Exception { } @Test - public void testExportNonExistingCaseFromat() throws Exception { + void testExportNonExistingCaseFromat() throws Exception { createStorageDir(); // import a case @@ -267,7 +249,7 @@ public void testExportNonExistingCaseFromat() throws Exception { } @Test - public void deleteNonExistingCase() throws Exception { + void deleteNonExistingCase() throws Exception { createStorageDir(); // import a case @@ -286,7 +268,7 @@ public void deleteNonExistingCase() throws Exception { } @Test - public void test() throws Exception { + void test() throws Exception { // create the storage dir createStorageDir(); @@ -475,7 +457,7 @@ public void test() throws Exception { } @Test - public void testDuplicateNonIndexedCase() throws Exception { + void testDuplicateNonIndexedCase() throws Exception { // create the storage dir createStorageDir(); @@ -516,7 +498,7 @@ private UUID importCase(String testCase, Boolean withExpiration) throws Exceptio } @Test - public void validateCaseNameTest() { + void validateCaseNameTest() { CaseService.validateCaseName("test.xiidm"); CaseService.validateCaseName("test-case.7zip"); CaseService.validateCaseName("testcase1.7zip"); @@ -541,7 +523,7 @@ public void validateCaseNameTest() { } @Test - public void searchCaseTest() throws Exception { + void searchCaseTest() throws Exception { // create the storage dir createStorageDir(); @@ -771,13 +753,13 @@ public void searchCaseTest() throws Exception { assertFalse(response.contains("\"name\":\"20200424T1330Z_2D_RTEFRANCE_001.zip\"")); } - private String getDateSearchTerm(String entsoeFormatDate) { + private static String getDateSearchTerm(String entsoeFormatDate) { String utcFormattedDate = EntsoeFileNameParser.parseDateTime(entsoeFormatDate).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); return "date:\"" + utcFormattedDate + "\""; } @Test - public void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception { + void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception { createStorageDir(); Path filePath = fileSystem.getPath(rootDirectory).resolve("randomFile.txt"); Files.createFile(filePath); diff --git a/src/test/java/com/powsybl/caseserver/CaseFileNameParserTests.java b/src/test/java/com/powsybl/caseserver/CaseFileNameParserTests.java index 9676bae..f284dcc 100644 --- a/src/test/java/com/powsybl/caseserver/CaseFileNameParserTests.java +++ b/src/test/java/com/powsybl/caseserver/CaseFileNameParserTests.java @@ -16,26 +16,22 @@ import com.powsybl.caseserver.parsers.entsoe.EntsoeFileNameParser; import com.powsybl.entsoe.util.EntsoeGeographicalCode; import com.powsybl.iidm.network.Country; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import java.nio.file.Path; import java.util.Optional; import java.util.UUID; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * @author Slimane Amar */ -@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) @DisableElasticsearch -public class CaseFileNameParserTests { - +class CaseFileNameParserTests { private static final String SN_UCTE_CASE_FILE_NAME = "20200103_0915_SN5_D80.UCT"; private static final String ID_UCTE_CASE_FILE_NAME = "20200424_1330_135_CH2.UCT"; private static final String D1_UCTE_CASE_FILE_NAME = "20200110_0430_FO5_FR0.uct"; @@ -48,7 +44,7 @@ public class CaseFileNameParserTests { private CaseService caseService; @Test - public void testValidNameUcteSN() { + void testValidNameUcteSN() { EntsoeCaseInfos caseInfos = (EntsoeCaseInfos) createInfos(SN_UCTE_CASE_FILE_NAME); assertEquals(SN_UCTE_CASE_FILE_NAME, caseInfos.getName()); assertEquals("UCTE", caseInfos.getFormat()); @@ -60,7 +56,7 @@ public void testValidNameUcteSN() { } @Test - public void testValidNameUcteID() { + void testValidNameUcteID() { EntsoeCaseInfos caseInfos = (EntsoeCaseInfos) createInfos(ID_UCTE_CASE_FILE_NAME); assertEquals(ID_UCTE_CASE_FILE_NAME, caseInfos.getName()); assertEquals("UCTE", caseInfos.getFormat()); @@ -72,7 +68,7 @@ public void testValidNameUcteID() { } @Test - public void testValidNameUcte1D() { + void testValidNameUcte1D() { EntsoeCaseInfos caseInfos = (EntsoeCaseInfos) createInfos(D1_UCTE_CASE_FILE_NAME); assertEquals(D1_UCTE_CASE_FILE_NAME, caseInfos.getName()); assertEquals("UCTE", caseInfos.getFormat()); @@ -84,7 +80,7 @@ public void testValidNameUcte1D() { } @Test - public void testValidNameUcte2D() { + void testValidNameUcte2D() { EntsoeCaseInfos caseInfos = (EntsoeCaseInfos) createInfos(D2_UCTE_CASE_FILE_NAME); assertEquals(D2_UCTE_CASE_FILE_NAME, caseInfos.getName()); assertEquals("UCTE", caseInfos.getFormat()); @@ -96,7 +92,7 @@ public void testValidNameUcte2D() { } @Test - public void testValidNameCgmes() { + void testValidNameCgmes() { CgmesCaseInfos caseInfos = (CgmesCaseInfos) createInfos(TEST_CGMES_CASE_FILE_NAME); assertEquals(TEST_CGMES_CASE_FILE_NAME, caseInfos.getName()); assertEquals("CGMES", caseInfos.getFormat()); @@ -106,7 +102,8 @@ public void testValidNameCgmes() { assertEquals(Integer.valueOf(1), caseInfos.getVersion()); } - public void testNonValidNameEntsoe() { + @Test + void testNonValidNameEntsoe() { CaseInfos caseInfos = createInfos(TEST_OTHER_CASE_FILE_NAME); assertEquals(TEST_OTHER_CASE_FILE_NAME, caseInfos.getName()); assertEquals("XIIDM", caseInfos.getFormat()); @@ -120,13 +117,13 @@ private CaseInfos createInfos(String fileName) { } @Test - public void testCreateDefaultCaseInfo() { + void testCreateDefaultCaseInfo() { CaseInfos infos = caseService.createInfos(TEST_OTHER_CASE_FILE_NAME, UUID.randomUUID(), "UNKNOW"); - assertEquals(infos.getClass(), CaseInfos.class); + assertEquals(CaseInfos.class, infos.getClass()); } @Test - public void testFileNameIncorrect() { + void testFileNameIncorrect() { Path casePath = Path.of(this.getClass().getResource("/" + CASE_FILE_NAME_INCORRECT).getPath()); String fileBaseName = casePath.getFileName().toString(); FileNameParser parser = new EntsoeFileNameParser(); @@ -134,9 +131,9 @@ public void testFileNameIncorrect() { assertTrue(fileNameInfos.isEmpty()); } - @Test(expected = UnsupportedOperationException.class) - public void testFileNameParserIncorrect() { - FileNameParser parserIncorrect = fileBaseName -> false; - parserIncorrect.parse(TEST_OTHER_CASE_FILE_NAME); + @Test + void testFileNameParserIncorrect() { + final FileNameParser parserIncorrect = fileBaseName -> false; + assertThrows(UnsupportedOperationException.class, () -> parserIncorrect.parse(TEST_OTHER_CASE_FILE_NAME)); } } diff --git a/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java b/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java index 6c35717..b9df8eb 100644 --- a/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java +++ b/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java @@ -6,31 +6,27 @@ */ package com.powsybl.caseserver; -import com.google.common.testing.EqualsTester; import com.powsybl.caseserver.dto.CaseInfos; import com.powsybl.caseserver.dto.cgmes.CgmesCaseInfos; import com.powsybl.caseserver.dto.entsoe.EntsoeCaseInfos; import com.powsybl.caseserver.elasticsearch.CaseInfosService; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.UUID; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; /** * @author Slimane Amar */ -@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) -public class CaseInfosELRepositoryTests { - +class CaseInfosELRepositoryTests { private static final String SN_UCTE_CASE_FILE_NAME = "20200103_0915_SN5_D80.UCT"; private static final String ID1_UCTE_CASE_FILE_NAME = "20200103_0915_135_CH2.UCT"; private static final String ID2_UCTE_CASE_FILE_NAME = "20200424_1330_135_CH2.UCT"; @@ -46,18 +42,18 @@ public class CaseInfosELRepositoryTests { private CaseInfosService caseInfosService; @Test - public void testAddDeleteCaseInfos() { + void testAddDeleteCaseInfos() { EntsoeCaseInfos caseInfos1 = (EntsoeCaseInfos) caseInfosService.addCaseInfos(createInfos(SN_UCTE_CASE_FILE_NAME)); Optional caseInfosAfter1 = caseInfosService.getCaseInfosByUuid(caseInfos1.getUuid().toString()); assertFalse(caseInfosAfter1.isEmpty()); assertEquals(caseInfos1, caseInfosAfter1.get()); - testEquals(caseInfos1, caseInfosAfter1.get()); + assertThat(caseInfosAfter1.get()).usingRecursiveAssertion().isEqualTo(caseInfos1); EntsoeCaseInfos caseInfos2 = (EntsoeCaseInfos) caseInfosService.addCaseInfos(createInfos(ID2_UCTE_CASE_FILE_NAME)); Optional caseInfosAfter2 = caseInfosService.getCaseInfosByUuid(caseInfos2.getUuid().toString()); assertFalse(caseInfosAfter2.isEmpty()); assertEquals(caseInfos2, caseInfosAfter2.get()); - testEquals(caseInfos2, caseInfosAfter2.get()); + assertThat(caseInfosAfter2.get()).usingRecursiveAssertion().isEqualTo(caseInfos2); caseInfosService.deleteCaseInfosByUuid(caseInfos1.getUuid().toString()); caseInfosAfter1 = caseInfosService.getCaseInfosByUuid(caseInfos1.getUuid().toString()); @@ -85,7 +81,7 @@ public void testAddDeleteCaseInfos() { } @Test - public void searchCaseInfos() { + void searchCaseInfos() { caseInfosService.deleteAllCaseInfos(); List all = caseInfosService.getAllCaseInfos(); assertTrue(all.isEmpty()); @@ -167,12 +163,6 @@ public void searchCaseInfos() { assertTrue(list.size() == 1 && list.contains(ucte1)); } - private void testEquals(CaseInfos c1, CaseInfos c2) { - new EqualsTester() - .addEqualityGroup(c1, c2) - .testEquals(); - } - private CaseInfos createInfos(String fileName) { Path casePath = Path.of(this.getClass().getResource("/" + fileName).getPath()); String fileBaseName = casePath.getFileName().toString(); diff --git a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java index 23e7724..0cae911 100644 --- a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java +++ b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java @@ -10,54 +10,47 @@ import com.powsybl.caseserver.elasticsearch.DisableElasticsearch; import com.powsybl.caseserver.repository.CaseMetadataEntity; import com.powsybl.caseserver.repository.CaseMetadataRepository; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * @author Abdelsalem Hedhili */ -@RunWith(SpringRunner.class) @SpringBootTest @DisableElasticsearch -public class ScheduledCaseCleanerTest { +class ScheduledCaseCleanerTest { @Autowired - public CaseMetadataRepository caseMetadataRepository; + private CaseMetadataRepository caseMetadataRepository; @Autowired - public ScheduledCaseCleaner scheduledCaseCleaner; + private ScheduledCaseCleaner scheduledCaseCleaner; @MockBean - CaseInfosRepository caseInfosRepository; + private CaseInfosRepository caseInfosRepository; @MockBean - public CaseService caseService; + private CaseService caseService; - @Before - public void setUp() { - cleanDB(); - } - - private void cleanDB() { + @BeforeEach + void setUp() { caseMetadataRepository.deleteAll(); } @Test - public void test() { + void test() { Instant now = Instant.now(); Instant yesterday = now.minus(1, ChronoUnit.DAYS); CaseMetadataEntity shouldNotExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), now.plus(1, ChronoUnit.HOURS), false); @@ -74,5 +67,4 @@ public void test() { assertTrue(caseMetadataRepository.findById(shouldExpireEntity.getId()).isEmpty()); verify(caseService, times(1)).deleteCase(shouldExpireEntity.getId()); } - } diff --git a/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java b/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java index 454aba8..2a43eda 100644 --- a/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java @@ -11,11 +11,9 @@ import com.powsybl.caseserver.repository.CaseMetadataRepository; import com.powsybl.caseserver.services.SupervisionService; import com.powsybl.computation.ComputationManager; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -23,8 +21,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import java.io.IOException; @@ -33,23 +29,22 @@ import java.nio.file.Files; import java.nio.file.Path; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * @author Jamal KHEYYAD */ -@RunWith(SpringRunner.class) @AutoConfigureMockMvc -@SpringBootTest(properties = {"case-store-directory=/cases"}) -@ContextConfiguration(classes = {CaseApplication.class}) -public class SupervisionControllerTest { +@SpringBootTest(classes = {CaseApplication.class}, properties = {"case-store-directory=/cases"}) +class SupervisionControllerTest { @Autowired - SupervisionService supervisionService; + private SupervisionService supervisionService; @Autowired - CaseMetadataRepository caseMetadataRepository; + private CaseMetadataRepository caseMetadataRepository; @Autowired - CaseService caseService; + private CaseService caseService; @Autowired private MockMvc mockMvc; @@ -61,21 +56,18 @@ public class SupervisionControllerTest { private FileSystem fileSystem; @Test - public void testGetCaseInfosCount() throws Exception { + void testGetCaseInfosCount() throws Exception { createStorageDir(); importCase(true); importCase(true); importCase(false); - mockMvc.perform(post("/v1/supervision/cases/reindex")) .andExpect(status().isOk()); - - Assert.assertEquals(2, supervisionService.getIndexedCasesCount()); - + assertEquals(2, supervisionService.getIndexedCasesCount()); } @Test - public void testReindexAll() throws Exception { + void testReindexAll() throws Exception { createStorageDir(); importCase(true); importCase(true); @@ -84,7 +76,7 @@ public void testReindexAll() throws Exception { mockMvc.perform(delete("/v1/supervision/cases/indexation")) .andExpect(status().isOk()); - Assert.assertEquals(0, supervisionService.getIndexedCasesCount()); + assertEquals(0, supervisionService.getIndexedCasesCount()); //reindex mockMvc.perform(post("/v1/supervision/cases/reindex")) @@ -93,17 +85,16 @@ public void testReindexAll() throws Exception { String countStr = mockMvc.perform(get("/v1/supervision/cases/indexation-count")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); - Assert.assertEquals("2", countStr); - Assert.assertEquals(2, supervisionService.getIndexedCasesCount()); + assertEquals("2", countStr); + assertEquals(2, supervisionService.getIndexedCasesCount()); } @Test - public void testGetIndexName() throws Exception { + void testGetIndexName() throws Exception { String result = mockMvc.perform(get("/v1/supervision/cases/index-name")) .andReturn().getResponse().getContentAsString(); - - Assert.assertEquals("cases", result); + assertEquals("cases", result); } private void importCase(Boolean indexed) throws Exception { @@ -120,20 +111,16 @@ private static MockMultipartFile createMockMultipartFile() throws IOException { } } - @Before - public void setUp() { + @BeforeEach + void setUp() { fileSystem = Jimfs.newFileSystem(Configuration.unix()); caseService.setFileSystem(fileSystem); caseService.setComputationManager(Mockito.mock(ComputationManager.class)); } - @After - public void tearDown() throws Exception { + @AfterEach + void tearDown() throws Exception { fileSystem.close(); - cleanDB(); - } - - private void cleanDB() { caseMetadataRepository.deleteAll(); } diff --git a/src/test/java/com/powsybl/caseserver/datasource/util/CaseDataSourceControllerTest.java b/src/test/java/com/powsybl/caseserver/datasource/util/CaseDataSourceControllerTest.java index c1ba9ba..03848f9 100644 --- a/src/test/java/com/powsybl/caseserver/datasource/util/CaseDataSourceControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/datasource/util/CaseDataSourceControllerTest.java @@ -14,9 +14,8 @@ import com.powsybl.caseserver.repository.CaseMetadataRepository; import com.powsybl.commons.datasource.DataSource; import jakarta.persistence.EntityManager; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -25,43 +24,37 @@ import org.springframework.cloud.stream.function.StreamBridge; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextHierarchy; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.Set; import java.util.UUID; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * @author Abdelsalem Hedhili */ -@RunWith(SpringRunner.class) @EnableWebMvc -@WebMvcTest(CaseDataSourceController.class) -@TestPropertySource(properties = {"case-store-directory=test"}) +@WebMvcTest(value = CaseDataSourceController.class, properties = {"case-store-directory=test"}) @ContextHierarchy({@ContextConfiguration(classes = {CaseApplication.class, TestChannelBinderConfiguration.class})}) -public class CaseDataSourceControllerTest { +class CaseDataSourceControllerTest { private FileSystem fileSystem = Jimfs.newFileSystem(); @MockBean - StreamBridge streamBridge; + private StreamBridge streamBridge; @MockBean - EntityManager entityManager; + private EntityManager entityManager; @Autowired private MockMvc mvc; @@ -70,7 +63,7 @@ public class CaseDataSourceControllerTest { private CaseMetadataRepository caseMetadataRepository; @MockBean - CaseInfosRepository caseInfosRepository; + private CaseInfosRepository caseInfosRepository; @Autowired private CaseService caseService; @@ -88,8 +81,8 @@ public class CaseDataSourceControllerTest { @Autowired private ObjectMapper mapper; - @Before - public void setUp() throws URISyntaxException, IOException { + @BeforeEach + void setUp() throws Exception { Path path = fileSystem.getPath(rootDirectory); if (!Files.exists(path)) { Files.createDirectories(path); @@ -109,7 +102,7 @@ public void setUp() throws URISyntaxException, IOException { } @Test - public void testBaseName() throws Exception { + void testBaseName() throws Exception { MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/baseName", CASE_UUID)) .andExpect(status().isOk()) .andReturn(); @@ -118,7 +111,7 @@ public void testBaseName() throws Exception { } @Test - public void testListName() throws Exception { + void testListName() throws Exception { MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/list", CASE_UUID) .param("regex", ".*")) .andExpect(status().isOk()) @@ -129,7 +122,7 @@ public void testListName() throws Exception { } @Test - public void testInputStreamWithFileName() throws Exception { + void testInputStreamWithFileName() throws Exception { MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", CASE_UUID) .param("fileName", fileName)) .andExpect(status().isOk()) @@ -147,7 +140,7 @@ public void testInputStreamWithFileName() throws Exception { } @Test - public void testInputStreamWithSuffixExt() throws Exception { + void testInputStreamWithSuffixExt() throws Exception { String suffix = "/MicroGridTestConfiguration_BC_BE_DL_V2"; String ext = "xml"; MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", CASE_UUID) @@ -168,7 +161,7 @@ public void testInputStreamWithSuffixExt() throws Exception { } @Test - public void testExistsWithFileName() throws Exception { + void testExistsWithFileName() throws Exception { MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/exists", CASE_UUID) .param("fileName", fileName)) .andExpect(status().isOk()) @@ -187,7 +180,7 @@ public void testExistsWithFileName() throws Exception { } @Test - public void testExistsWithSuffixExt() throws Exception { + void testExistsWithSuffixExt() throws Exception { String suffix = "random"; String ext = "uct"; MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/exists", CASE_UUID) @@ -199,5 +192,4 @@ public void testExistsWithSuffixExt() throws Exception { Boolean res = mapper.readValue(mvcResult.getResponse().getContentAsString(), Boolean.class); assertEquals(dataSource.exists(suffix, ext), res); } - } diff --git a/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java b/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java index c060fa6..babeae8 100644 --- a/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java +++ b/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java @@ -6,11 +6,10 @@ */ package com.powsybl.caseserver.elasticsearch; -import org.springframework.stereotype.Component; -import org.testcontainers.elasticsearch.ElasticsearchContainer; - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; +import org.springframework.stereotype.Component; +import org.testcontainers.elasticsearch.ElasticsearchContainer; import java.util.Map; diff --git a/src/test/java/com/powsybl/caseserver/utils/TestUtils.java b/src/test/java/com/powsybl/caseserver/utils/TestUtils.java index f4fd835..b12b18e 100644 --- a/src/test/java/com/powsybl/caseserver/utils/TestUtils.java +++ b/src/test/java/com/powsybl/caseserver/utils/TestUtils.java @@ -11,7 +11,7 @@ import java.util.List; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Jamal KHEYYAD @@ -21,12 +21,12 @@ public final class TestUtils { private static final long TIMEOUT = 100; private TestUtils() { - + throw new IllegalStateException("Utility class"); } public static void assertQueuesEmptyThenClear(List destinations, OutputDestination output) { try { - destinations.forEach(destination -> assertNull("Should not be any messages in queue " + destination + " : ", output.receive(TIMEOUT, destination))); + destinations.forEach(destination -> assertNull(output.receive(TIMEOUT, destination), "Should not be any messages in queue " + destination + " : ")); } catch (NullPointerException e) { // Ignoring } finally { From ebc8fe8a4aa457d68a76f6b257bc6e3071fac754 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Thu, 17 Oct 2024 17:55:17 +0200 Subject: [PATCH 2/2] fix Signed-off-by: Tristan Chuine --- .../java/com/powsybl/caseserver/CaseControllerTest.java | 2 +- .../com/powsybl/caseserver/CaseInfosELRepositoryTests.java | 2 +- .../com/powsybl/caseserver/ScheduledCaseCleanerTest.java | 6 +++--- src/test/java/com/powsybl/caseserver/utils/TestUtils.java | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java index c6bcf54..b84dc12 100644 --- a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java @@ -46,7 +46,7 @@ import java.util.List; import java.util.UUID; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java b/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java index b9df8eb..166f815 100644 --- a/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java +++ b/src/test/java/com/powsybl/caseserver/CaseInfosELRepositoryTests.java @@ -19,7 +19,7 @@ import java.util.Optional; import java.util.UUID; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; /** diff --git a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java index 0cae911..9a90c1c 100644 --- a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java +++ b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java @@ -10,7 +10,7 @@ import com.powsybl.caseserver.elasticsearch.DisableElasticsearch; import com.powsybl.caseserver.repository.CaseMetadataEntity; import com.powsybl.caseserver.repository.CaseMetadataRepository; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -44,8 +44,8 @@ class ScheduledCaseCleanerTest { @MockBean private CaseService caseService; - @BeforeEach - void setUp() { + @AfterEach + void cleanDB() { caseMetadataRepository.deleteAll(); } diff --git a/src/test/java/com/powsybl/caseserver/utils/TestUtils.java b/src/test/java/com/powsybl/caseserver/utils/TestUtils.java index b12b18e..bb0332f 100644 --- a/src/test/java/com/powsybl/caseserver/utils/TestUtils.java +++ b/src/test/java/com/powsybl/caseserver/utils/TestUtils.java @@ -4,7 +4,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - package com.powsybl.caseserver.utils; import org.springframework.cloud.stream.binder.test.OutputDestination;