Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to JUnit5. #44

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 9 additions & 49 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@

<properties>
<liquibase-hibernate-package>com.powsybl.caseserver.repository</liquibase-hibernate-package>

<mockito-inline.version>3.11.1</mockito-inline.version>
<log4j2-mock-version>0.0.2</log4j2-mock-version>
<powsybl-ws-dependencies.version>2.15.0</powsybl-ws-dependencies.version>
<testcontainers.version>1.18.3</testcontainers.version>
</properties>

<build>
Expand Down Expand Up @@ -84,6 +80,8 @@

<dependencyManagement>
<dependencies>
<!-- overrides of imports -->

<!-- imports -->
<dependency>
<groupId>com.powsybl</groupId>
Expand All @@ -93,18 +91,7 @@
<scope>import</scope>
</dependency>

<!-- project specific dependencies (also overrides imports, but separate for clarity) -->
<dependency>
<groupId>de.dentrassi.elasticsearch</groupId>
<artifactId>log4j2-mock</artifactId>
<version>${log4j2-mock-version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<!-- project specific dependencies -->
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -168,13 +155,13 @@
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>

<!-- Runtime dependencies -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down Expand Up @@ -215,7 +202,7 @@
<artifactId>powsybl-ucte-converter</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<dependency><!--needed because powsybl-entsoe-util depends on poi lib who use log4j api-->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<scope>runtime</scope>
Expand All @@ -230,12 +217,8 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
Expand All @@ -251,21 +234,6 @@
<artifactId>powsybl-config-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.dentrassi.elasticsearch</groupId>
<artifactId>log4j2-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -286,17 +254,9 @@
<artifactId>spring-integration-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Needed to mock StreamBridge (which happens to be a final class) in CaseDataSourceControllerTest which doesn't load the full spring context -->
<!-- see "mockito-inline" in https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing.spring-boot-applications.mocking-beans -->
<!-- otherwise we get the following exception:
Cannot mock/spy class org.springframework.cloud.stream.function.StreamBridge
Mockito cannot mock/spy because :
- final class
-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-inline.version}</version>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CgmesFileName implements FileNameInfos {
@NonNull private String tso;
@NonNull private Integer version;

@Override
public Type getType() {
return Type.CGMES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class EntsoeFileName implements FileNameInfos {
@NonNull private EntsoeGeographicalCode geographicalCode;
@NonNull private Integer version;

@Override
public Type getType() {
return Type.ENTSOE;
}
Expand Down
66 changes: 24 additions & 42 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,31 +49,19 @@
import static org.assertj.core.api.Assertions.assertThat;
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;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
*/
@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";
Expand Down Expand Up @@ -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<String> destinations = List.of(caseImportDestination);
Expand All @@ -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();

Expand All @@ -158,7 +140,7 @@ public void testDeleteCases() throws Exception {
}

@Test
public void testCheckNonExistingCase() throws Exception {
void testCheckNonExistingCase() throws Exception {
// create the storage dir
createStorageDir();

Expand All @@ -170,7 +152,7 @@ public void testCheckNonExistingCase() throws Exception {
}

@Test
public void testImportValidCase() throws Exception {
void testImportValidCase() throws Exception {
createStorageDir();

// import a case
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -254,7 +236,7 @@ public void testDownloadNonExistingCase() throws Exception {
}

@Test
public void testExportNonExistingCaseFromat() throws Exception {
void testExportNonExistingCaseFromat() throws Exception {
createStorageDir();

// import a case
Expand All @@ -267,7 +249,7 @@ public void testExportNonExistingCaseFromat() throws Exception {
}

@Test
public void deleteNonExistingCase() throws Exception {
void deleteNonExistingCase() throws Exception {
createStorageDir();

// import a case
Expand All @@ -286,7 +268,7 @@ public void deleteNonExistingCase() throws Exception {
}

@Test
public void test() throws Exception {
void test() throws Exception {
// create the storage dir
createStorageDir();

Expand Down Expand Up @@ -475,7 +457,7 @@ public void test() throws Exception {
}

@Test
public void testDuplicateNonIndexedCase() throws Exception {
void testDuplicateNonIndexedCase() throws Exception {
// create the storage dir
createStorageDir();

Expand Down Expand Up @@ -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");
Expand All @@ -541,7 +523,7 @@ public void validateCaseNameTest() {
}

@Test
public void searchCaseTest() throws Exception {
void searchCaseTest() throws Exception {
// create the storage dir
createStorageDir();

Expand Down Expand Up @@ -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);
Expand Down
Loading
Loading