Skip to content

Commit

Permalink
Migrate tests to JUnit5 (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH authored Oct 17, 2024
1 parent e925300 commit 9beaeac
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 134 deletions.
19 changes: 0 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
<relativePath/>
</parent>


<artifactId>gridsuite-geo-data-server</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Geo data server</name>
<description>Geographical data server</description>

<url>http://www.gridsuite.org/</url>

<licenses>
Expand Down Expand Up @@ -193,27 +191,11 @@
</dependency>

<!-- test scope -->
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-config-test</artifactId>
Expand All @@ -229,6 +211,5 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 org.gridsuite.geodata.server.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.gridsuite.geodata.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.ByteStreams;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VariantManagerConstants;
Expand All @@ -18,22 +19,17 @@
import org.gridsuite.geodata.server.dto.SubstationGeoData;
import org.gridsuite.geodata.server.repositories.LineRepository;
import org.gridsuite.geodata.server.repositories.SubstationRepository;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.UUID;

import static com.powsybl.network.store.model.NetworkStoreApi.VERSION;
Expand All @@ -48,9 +44,8 @@
/**
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
*/
@RunWith(SpringRunner.class)
@WebMvcTest(GeoDataController.class)
public class GeoDataControllerTest {
class GeoDataControllerTest {

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -73,16 +68,12 @@ public class GeoDataControllerTest {
private static final String VARIANT_ID = "First_variant";
private static final String WRONG_VARIANT_ID = "Wrong_variant";

public String toString(String resourceName) {
try {
return new String(ByteStreams.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream(resourceName))), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
private static String toString(String resourceName) throws IOException {
return new String(ByteStreams.toByteArray(Objects.requireNonNull(GeoDataControllerTest.class.getResourceAsStream(resourceName))), StandardCharsets.UTF_8);
}

@Test
public void test() throws Exception {
void test() throws Exception {
UUID networkUuid = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");

Network testNetwork = EurostagTutorialExample1Factory.create();
Expand Down Expand Up @@ -185,7 +176,7 @@ public void test() throws Exception {
}

@Test
public void testGetLinesError() throws Exception {
void testGetLinesError() throws Exception {
UUID networkUuid = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
Network testNetwork = EurostagTutorialExample1Factory.create();
given(service.getNetwork(networkUuid)).willReturn(testNetwork);
Expand Down
78 changes: 38 additions & 40 deletions src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,30 @@
*/
package org.gridsuite.geodata.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.powsybl.iidm.network.test.NoEquipmentNetworkFactory;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.Coordinate;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.iidm.network.test.NoEquipmentNetworkFactory;
import org.gridsuite.geodata.server.dto.LineGeoData;
import org.gridsuite.geodata.server.dto.SubstationGeoData;
import org.gridsuite.geodata.server.repositories.*;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
*/
@RunWith(SpringRunner.class)
@ContextHierarchy({
@ContextConfiguration(classes = GeoDataApplication.class)
})
public class GeoDataServiceTest {
@SpringBootTest(classes = GeoDataApplication.class)
class GeoDataServiceTest {

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -47,15 +41,13 @@ public class GeoDataServiceTest {
private LineRepository lineRepository;

@Autowired
GeoDataService geoDataService;
private GeoDataService geoDataService;

@Autowired
private DefaultSubstationGeoDataByCountry defaultSubstationsGeoData;

@Before
public void setUp() throws JsonProcessingException {
lineRepository.deleteAll();
substationRepository.deleteAll();
@BeforeEach
void setUp() throws Exception {
List<SubstationEntity> substationEntities = new ArrayList<>();

substationEntities.add(SubstationEntity.builder()
Expand Down Expand Up @@ -125,14 +117,20 @@ public void setUp() throws JsonProcessingException {
lineRepository.saveAll(lineEntities);
}

static LineGeoData getFromList(List<LineGeoData> list, String id) {
@AfterEach
void cleanDb() {
lineRepository.deleteAll();
substationRepository.deleteAll();
}

private static LineGeoData getFromList(List<LineGeoData> list, String id) {
Optional<LineGeoData> res = list.stream().filter(l -> l.getId().equals(id)).findAny();
assertTrue(res.isPresent());
return res.get();
}

@Test
public void test() {
void test() {
Network network = createGeoDataNetwork();
List<SubstationGeoData> substationsGeoData = geoDataService.getSubstationsByCountries(network, new HashSet<>(Collections.singletonList(Country.FR)));

Expand Down Expand Up @@ -189,7 +187,7 @@ public void test() {
}

@Test
public void testCgmesCase() {
void testCgmesCase() {
Network network = createCgmesGeoDataNetwork();

List<SubstationGeoData> substationsGeoData = geoDataService.getSubstationsByCountries(network, new HashSet<>(Collections.singletonList(Country.FR)));
Expand All @@ -202,7 +200,7 @@ public void testCgmesCase() {
}

@Test
public void testNonExisting() {
void testNonExisting() {
Network network = EurostagTutorialExample1Factory.create();
Substation notexistsub1 = network.newSubstation()
.setId("NOTEXISTSUB1")
Expand Down Expand Up @@ -248,20 +246,20 @@ public void testNonExisting() {
.setB2(386E-6 / 2)
.add();
List<SubstationGeoData> substationsGeoData = geoDataService.getSubstationsByCountries(network, new HashSet<>(Collections.singletonList(Country.FR)));
assertFalse("Must not contain nulls", substationsGeoData.stream().anyMatch(Objects::isNull));
assertFalse("Must not contain unknown substation " + notexistsub1.getId(),
substationsGeoData.stream().anyMatch(s -> notexistsub1.getId().equals(s.getId())));
assertFalse("Must not contain unknown substation " + notexistsub2.getId(),
substationsGeoData.stream().anyMatch(s -> notexistsub2.getId().equals(s.getId())));
assertFalse(substationsGeoData.stream().anyMatch(Objects::isNull), "Must not contain nulls");
assertFalse(substationsGeoData.stream().anyMatch(s -> notexistsub1.getId().equals(s.getId())),
"Must not contain unknown substation " + notexistsub1.getId());
assertFalse(substationsGeoData.stream().anyMatch(s -> notexistsub2.getId().equals(s.getId())),
"Must not contain unknown substation " + notexistsub2.getId());

List<LineGeoData> linesGeoData = geoDataService.getLinesByCountries(network, new HashSet<>(Collections.singletonList(Country.FR)));
assertFalse("Must not contain nulls", linesGeoData.stream().anyMatch(Objects::isNull));
assertFalse("Must not contain unknown lines " + notexistline.getId(),
linesGeoData.stream().anyMatch(s -> notexistline.getId().equals(s.getId())));
assertFalse(linesGeoData.stream().anyMatch(Objects::isNull), "Must not contain nulls");
assertFalse(linesGeoData.stream().anyMatch(s -> notexistline.getId().equals(s.getId())),
"Must not contain unknown lines " + notexistline.getId());
}

@Test
public void testSimilarNeighborhoodOffset() {
void testSimilarNeighborhoodOffset() {
Network network = EurostagTutorialExample1Factory.create();
Substation p4 = network.newSubstation()
.setId("P4")
Expand Down Expand Up @@ -363,7 +361,7 @@ public void testSimilarNeighborhoodOffset() {
}

@Test
public void testCalculatedDefaultSubstations() {
void testCalculatedDefaultSubstations() {
Network network = EurostagTutorialExample1Factory.create();
Substation p4 = network.newSubstation()
.setId("P4")
Expand Down Expand Up @@ -514,7 +512,7 @@ public void testCalculatedDefaultSubstations() {
}

@Test
public void testLineCoordinatesError() {
void testLineCoordinatesError() {
LineEntity lineEntity = LineEntity.create(LineGeoData.builder()
.id("idLine")
.country1(Country.FR)
Expand All @@ -527,7 +525,7 @@ public void testLineCoordinatesError() {
geoDataService.toDto(lineEntity));
}

private Network createGeoDataNetwork() {
private static Network createGeoDataNetwork() {
Network network = EurostagTutorialExample1Factory.create();

Substation p3 = network.newSubstation()
Expand Down Expand Up @@ -846,7 +844,7 @@ private Network createGeoDataNetwork() {
return network;
}

private Network createCgmesGeoDataNetwork() {
private static Network createCgmesGeoDataNetwork() {
Network network = NoEquipmentNetworkFactory.create();

Substation s1 = network.newSubstation()
Expand Down Expand Up @@ -903,7 +901,7 @@ private Network createCgmesGeoDataNetwork() {
}

@Test
public void testGetSubstationsGeodataById() {
void testGetSubstationsGeodataById() {
Network network = createGeoDataNetwork();
List<SubstationGeoData> substationsGeoData = geoDataService.getSubstationsByIds(network, Set.of("P1", "P3"));

Expand Down Expand Up @@ -1018,7 +1016,7 @@ public void testGetSubstationsGeodataById() {
}

@Test
public void testGetLinesGeodataById() {
void testGetLinesGeodataById() {
Network network = createGeoDataNetwork();

List<LineGeoData> linesGeoData = geoDataService.getLinesByIds(network, Set.of("NHV2_NHV5", "NHV1_NHV2_1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@

import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.extensions.Coordinate;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;

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;

/**
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
*/
public class LineGeoDataTest {

class LineGeoDataTest {
@Test
public void test() {
void test() {
LineGeoData lineGeoData = new LineGeoData("l", Country.FR, Country.FR, "sub1", "sub2", new ArrayList<>());

assertEquals("l", lineGeoData.getId());
Expand All @@ -41,6 +40,5 @@ public void test() {
assertEquals("LineGeoData.LineGeoDataBuilder(id=testId, country1=FR, country2=FR, substationStart=sub1, substationEnd=sub2, coordinates=[Coordinate(lat=1.0, lon=2.0), Coordinate(lat=3.0, lon=4.0)])",
lineGeoDataBuilder.toString());
assertEquals("LineGeoData(id=testId, country1=FR, country2=FR, substationStart=sub1, substationEnd=sub2, coordinates=[Coordinate(lat=1.0, lon=2.0), Coordinate(lat=3.0, lon=4.0)])", lineGeoDataBuilder.build().toString());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.extensions.Coordinate;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
*/
public class SubstationGeoDataTest {

class SubstationGeoDataTest {
@Test
public void test() {
void test() {
SubstationGeoData substationGeoData = new SubstationGeoData("id", Country.FR, new Coordinate(1, 1));

assertEquals("id", substationGeoData.getId());
Expand All @@ -32,6 +31,5 @@ public void test() {
substationGeoDataBuilder.coordinate(new Coordinate(3, 4));
assertEquals("SubstationGeoData.SubstationGeoDataBuilder(id=testID, country=FR, coordinate=Coordinate(lat=3.0, lon=4.0))", substationGeoDataBuilder.toString());
assertEquals("SubstationGeoData(id=testID, country=FR, coordinate=Coordinate(lat=3.0, lon=4.0))", substationGeoDataBuilder.build().toString());

}
}
Loading

0 comments on commit 9beaeac

Please sign in to comment.