Skip to content

Commit

Permalink
Migrate tests to JUnit5 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH authored Oct 23, 2024
1 parent 47741c1 commit a9d6c7d
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 252 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ target/

# IntelliJ
/.idea
*.iml
*.iml
47 changes: 5 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<properties>
<gridsuite-dependencies.version>34</gridsuite-dependencies.version>
<liquibase-hibernate-package>org.gridsuite.shortcircuit.server</liquibase-hibernate-package>
<mockito-inline.version>3.11.1</mockito-inline.version>
<db-util.version>1.0.5</db-util.version>
<!-- FIXME: powsybl-network-store modules'version is overloaded in the dependencies section.The overloads and this property below have to be removed at next powsybl-ws-dependencies.version upgrade -->
<powsybl-network-store.version>1.18.2</powsybl-network-store.version>
Expand Down Expand Up @@ -116,7 +115,11 @@
</dependency>

<!-- project specific dependencies -->

<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>db-util</artifactId>
<version>${db-util.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -209,35 +212,9 @@
<artifactId>powsybl-iidm-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>db-util</artifactId>
<version>${db-util.version}</version>
<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.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -255,19 +232,5 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!-- Needed to mock ShortCircuitAnalysis.Runner (which happens to be a final class) in ShortCircuitAnalysisControllerTest -->
<!-- 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 com.powsybl.shortcircuit.ShortCircuitAnalysis.Runner
Mockito cannot mock/spy because :
- final class
-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-inline.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.*;
import jakarta.persistence.Embeddable;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private void deleteShortCircuitResult(UUID resultUuid) {
resultRepository.deleteByResultUuid(resultUuid);
}

@Override
@Transactional
public void deleteAll() {
globalStatusRepository.deleteAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
import com.powsybl.security.LimitViolation;
import com.powsybl.security.LimitViolationType;
import com.powsybl.shortcircuit.*;
import lombok.SneakyThrows;
import com.powsybl.ws.commons.computation.service.ReportService;
import com.powsybl.ws.commons.computation.service.UuidGeneratorService;
import org.gridsuite.shortcircuit.server.dto.CsvTranslation;
import org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisStatus;
import org.gridsuite.shortcircuit.server.entities.FaultEmbeddable;
import org.gridsuite.shortcircuit.server.entities.FaultResultEntity;
import org.gridsuite.shortcircuit.server.repositories.FaultResultRepository;
import org.gridsuite.shortcircuit.server.service.ShortCircuitAnalysisResultService;
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.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
Expand All @@ -50,7 +47,6 @@
import org.springframework.messaging.Message;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

Expand All @@ -60,14 +56,15 @@
import java.util.stream.Collectors;

import static com.powsybl.network.store.model.NetworkStoreApi.VERSION;
import static com.powsybl.ws.commons.computation.service.NotificationService.HEADER_USER_ID;
import static com.powsybl.ws.commons.computation.service.NotificationService.getCancelFailedMessage;
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
import static org.gridsuite.shortcircuit.server.TestUtils.unzip;
import static com.powsybl.ws.commons.computation.service.NotificationService.HEADER_USER_ID;
import static com.powsybl.ws.commons.computation.service.NotificationService.getCancelFailedMessage;
import static org.gridsuite.shortcircuit.server.service.ShortCircuitResultContext.HEADER_BUS_ID;
import static org.gridsuite.shortcircuit.server.service.ShortCircuitWorkerService.COMPUTATION_TYPE;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doAnswer;
Expand All @@ -79,11 +76,10 @@
/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
@ContextHierarchy({@ContextConfiguration(classes = {ShortCircuitApplication.class, TestChannelBinderConfiguration.class})})
public class ShortCircuitAnalysisControllerTest {
class ShortCircuitAnalysisControllerTest {

private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
private static final UUID NETWORK1_UUID = UUID.fromString("faa0f351-f664-4771-951b-fa3b565c4d37");
Expand Down Expand Up @@ -220,13 +216,10 @@ public String getVersion() {
private UuidGeneratorService uuidGeneratorService;

@MockBean
ShortCircuitAnalysis.Runner runner;

@Autowired
ShortCircuitAnalysisResultService shortCircuitAnalysisResultRepository;
private ShortCircuitAnalysis.Runner runner;

@Autowired
FaultResultRepository faultResultRepository;
private FaultResultRepository faultResultRepository;

private final RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
private final ObjectMapper mapper = restTemplateConfig.objectMapper();
Expand Down Expand Up @@ -293,8 +286,8 @@ private static void assertFaultResultsEquals(List<FaultResult> faultResults, Lis
}
}

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);

// network store service mocking
Expand Down Expand Up @@ -330,23 +323,17 @@ public void setUp() throws Exception {
given(uuidGeneratorService.generate()).willReturn(RESULT_UUID);
}

@SneakyThrows
@After
public void tearDown() {
cleanDB();
@AfterEach
void tearDown() throws Exception {
mockMvc.perform(delete("/" + VERSION + "/results")).andExpect(status().isOk());

TestUtils.assertQueuesEmptyThenClear(List.of(shortCircuitAnalysisResultDestination, shortCircuitAnalysisRunDestination,
shortCircuitAnalysisCancelDestination, shortCircuitAnalysisStoppedDestination,
shortCircuitAnalysisFailedDestination, shortCircuitAnalysisCancelFailedDestination), output);
}

@SneakyThrows
private void cleanDB() {
mockMvc.perform(delete("/" + VERSION + "/results")).andExpect(status().isOk());
}

@Test
public void runTest() throws Exception {
void runTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {

shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
Expand Down Expand Up @@ -503,7 +490,7 @@ public void runTest() throws Exception {
}

@Test
public void testDeterministicResults() throws Exception {
void testDeterministicResults() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {

shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
Expand Down Expand Up @@ -556,7 +543,7 @@ public void testDeterministicResults() throws Exception {
}

@Test
public void runWithBusIdTest() throws Exception {
void runWithBusIdTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL));
Expand Down Expand Up @@ -617,7 +604,7 @@ public void runWithBusIdTest() throws Exception {
}

@Test
public void runWithBusBarSectionIdTest() throws Exception {
void runWithBusBarSectionIdTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(nodeBreakerNetwork), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL));
Expand Down Expand Up @@ -659,7 +646,7 @@ public void runWithBusBarSectionIdTest() throws Exception {
}

@Test
public void runWithBusBarSectionIdAndErrorTest() throws Exception {
void runWithBusBarSectionIdAndErrorTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(nodeBreakerNetwork), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_MAGNITUDE_FULL));
Expand Down Expand Up @@ -690,7 +677,7 @@ public void runWithBusBarSectionIdAndErrorTest() throws Exception {
}

@Test
public void stopTest() throws Exception {
void stopTest() throws Exception {
// this test breaks following tests executing shortcircuit analysis if they run with the same resultUUID by adding it to cancelComputationRequests
// for now, we need to run it with a different resultUuid
given(uuidGeneratorService.generate()).willReturn(RESULT_UUID_TO_STOP);
Expand Down Expand Up @@ -726,9 +713,8 @@ public void stopTest() throws Exception {
}
}

@SneakyThrows
@Test
public void testStatus() {
void testStatus() throws Exception {
MvcResult result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/status", RESULT_UUID))
.andExpect(status().isOk())
Expand All @@ -746,19 +732,17 @@ public void testStatus() {
assertEquals(ShortCircuitAnalysisStatus.NOT_DONE.name(), result.getResponse().getContentAsString());
}

@SneakyThrows
@Test
public void testGetFaultTypes() {
void testGetFaultTypes() throws Exception {
MvcResult result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/fault-types", RESULT_UUID))
.andExpect(status().isOk())
.andReturn();
assertEquals("[]", result.getResponse().getContentAsString());
}

@SneakyThrows
@Test
public void testGetBranchSides() {
void testGetBranchSides() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL));
Expand Down Expand Up @@ -806,19 +790,17 @@ public void testGetBranchSides() {
}
}

@SneakyThrows
@Test
public void testGetLimitTypes() {
void testGetLimitTypes() throws Exception {
MvcResult result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/limit-violation-types", RESULT_UUID))
.andExpect(status().isOk())
.andReturn();
assertEquals("[]", result.getResponse().getContentAsString());
}

@SneakyThrows
@Test
public void runWithReportTest() {
void runWithReportTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = TestUtils.injectShortCircuitAnalysisProvider(new ShortCircuitAnalysisProviderMock())) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(RESULT));
Expand All @@ -840,9 +822,8 @@ public void runWithReportTest() {
}
}

@SneakyThrows
@Test
public void runWithNoShortcircuitDataTest() {
void runWithNoShortCircuitDataTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = TestUtils.injectShortCircuitAnalysisProvider(new ShortCircuitAnalysisProviderMock())) {
shortCircuitAnalysisMockedStatic.when(() ->
ShortCircuitAnalysis.runAsync(
Expand Down Expand Up @@ -879,7 +860,7 @@ public void runWithNoShortcircuitDataTest() {
}

@Test
public void checkShortCircuitLimitsTest() throws Exception {
void checkShortCircuitLimitsTest() throws Exception {
try (MockedStatic<ShortCircuitAnalysis> shortCircuitAnalysisMockedStatic = Mockito.mockStatic(ShortCircuitAnalysis.class)) {
shortCircuitAnalysisMockedStatic.when(() -> ShortCircuitAnalysis.runAsync(eq(network), anyList(), any(ShortCircuitParameters.class), any(ComputationManager.class), anyList(), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_MAGNITUDE_FULL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
Expand All @@ -46,7 +44,7 @@
class ShortCircuitParametersControllerTest implements WithAssertions {
private final String defaultParametersJson;

public ShortCircuitParametersControllerTest() throws URISyntaxException, IOException {
public ShortCircuitParametersControllerTest() throws Exception {
this.defaultParametersJson = Files.readString(Paths.get(this.getClass().getResource(this.getClass().getSimpleName() + ".json").toURI())).replaceAll("\\s+", "");
}

Expand Down Expand Up @@ -127,10 +125,10 @@ void testDuplicateNonExistingParameters() throws Exception {
assertThat(uuidCaptor.getValue()).isEqualTo(arg);
}

static Stream<Arguments> testParametersArgs() {
private static Stream<Arguments> testParametersArgs() {
return Stream.of(
Arguments.arguments(true, status().isNoContent()),
Arguments.arguments(false, status().isNotFound())
Arguments.arguments(true, status().isNoContent()),
Arguments.arguments(false, status().isNotFound())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ public ShortCircuitParametersITest() throws Exception {
}

@Autowired
ObjectMapper objectMapper;
private ObjectMapper objectMapper;

@Autowired
MockMvc mockMvc;
private MockMvc mockMvc;

@Autowired
ParametersRepository parametersRepository;
private ParametersRepository parametersRepository;

@MockBean
NotificationService notificationService; //to keep; for not testing notification part, it's tested in another class test
private NotificationService notificationService; //to keep; for not testing notification part, it's tested in another class test

@BeforeAll
void checkDatabaseClean() {
Expand Down
Loading

0 comments on commit a9d6c7d

Please sign in to comment.