Skip to content

Commit

Permalink
no need to get Lines' positions
Browse files Browse the repository at this point in the history
Signed-off-by: maissa SOUISSI <[email protected]>
  • Loading branch information
souissimai committed Jun 12, 2024
1 parent 3f1d20b commit 1f4b960
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 195 deletions.
39 changes: 0 additions & 39 deletions src/main/java/com/powsybl/sld/server/GeoDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.iidm.network.Line;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Substation;
import com.powsybl.iidm.network.extensions.LinePosition;
import com.powsybl.iidm.network.extensions.LinePositionAdder;
import com.powsybl.iidm.network.extensions.SubstationPosition;
import com.powsybl.iidm.network.extensions.SubstationPositionAdder;
import com.powsybl.sld.server.dto.Coordinate;
import com.powsybl.sld.server.dto.LineGeoData;
import com.powsybl.sld.server.dto.SubstationGeoData;
import com.powsybl.sld.server.utils.GeoDataUtils;
import lombok.Setter;
Expand Down Expand Up @@ -60,25 +56,6 @@ private String getGeoDataServerURI() {
return this.geoDataServerBaseUri + DELIMITER + GEO_DATA_API_VERSION + DELIMITER;
}

public String getLinesGraphics(UUID networkUuid, String variantId, List<String> linesIds) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(getGeoDataServerURI() + LINES)
.queryParam(NETWORK_UUID, networkUuid);

if (!StringUtils.isBlank(variantId)) {
uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId);
}

if (linesIds != null) {
uriComponentsBuilder.queryParam(QUERY_PARAM_LINE_ID, linesIds);
}

var path = uriComponentsBuilder
.buildAndExpand()
.toUriString();

return restTemplate.getForObject(path, String.class);
}

public String getSubstationsGraphics(UUID networkUuid, String variantId, List<String> substationsIds) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(getGeoDataServerURI() + SUBSTATIONS)
.queryParam(NETWORK_UUID, networkUuid);
Expand Down Expand Up @@ -118,22 +95,6 @@ public void assignSubstationGeoData(Network network, UUID networkUuid, String va
}
}

public void assignLineGeoData(Network network, UUID networkUuid, String variantId, List<Line> lines) {
List<LineGeoData> linesGeoData = GeoDataUtils.fromStringToLineGeoData(getLinesGraphics(networkUuid, variantId, null), new ObjectMapper());
Map<String, List<com.powsybl.sld.server.dto.Coordinate>> lineGeoDataMap = linesGeoData.stream()
.collect(Collectors.toMap(LineGeoData::getId, LineGeoData::getCoordinates));
for (Line line : lines) {
if (network.getLine(line.getId()).getExtension(LinePosition.class) == null) {
List<com.powsybl.sld.server.dto.Coordinate> coordinates = lineGeoDataMap.get(line.getId());
if (coordinates != null) {
network.getLine(line.getId())
.newExtension(LinePositionAdder.class)
.withCoordinates(coordinates)
.add();
}
}
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.sld.server;

import com.powsybl.iidm.network.Line;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Substation;
import com.powsybl.iidm.network.VoltageLevel;
Expand All @@ -32,7 +31,6 @@
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.util.*;
import java.util.stream.StreamSupport;

/**
* @author Etienne Homer<etienne.homer at rte-france.com>
Expand Down Expand Up @@ -85,7 +83,6 @@ private void getGeoDataCoordinates(Network network, UUID networkUuid, String var
// Geographical Position for lines and substations related to voltageLevels
List<Substation> substations = getSubstations(voltageLevels);
geoDataService.assignSubstationGeoData(network, networkUuid, variantId, substations);
geoDataService.assignLineGeoData(network, networkUuid, variantId, getLines(voltageLevels));
}

private List<Substation> getSubstations(List<VoltageLevel> voltages) {
Expand All @@ -95,12 +92,6 @@ private List<Substation> getSubstations(List<VoltageLevel> voltages) {
.toList();
}

private List<Line> getLines(List<VoltageLevel> voltages) {
return voltages.stream()
.flatMap(voltageLevel -> StreamSupport.stream(voltageLevel.getLines().spliterator(), false))
.toList();
}

private Map<String, Object> computeAdditionalMetadata(Network network, List<String> voltageLevelsIds, int depth) {

VoltageLevelFilter vlFilter = VoltageLevelFilter.createVoltageLevelsDepthFilter(network, voltageLevelsIds, depth);
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/com/powsybl/sld/server/dto/LineGeoData.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/com/powsybl/sld/server/utils/GeoDataUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.PowsyblException;
import com.powsybl.sld.server.dto.LineGeoData;
import com.powsybl.sld.server.dto.SubstationGeoData;

import java.util.List;
Expand All @@ -33,15 +32,6 @@ public static List<SubstationGeoData> fromStringToSubstationGeoData(String jsonR
}
}

public static List<LineGeoData> fromStringToLineGeoData(String jsonResponse, ObjectMapper objectMapper) {
try {
return objectMapper.readValue(jsonResponse, new TypeReference<>() {
});
} catch (JsonProcessingException e) {
throw new PowsyblException("Failed to parse JSON response", e);
}
}

}


Expand Down
95 changes: 21 additions & 74 deletions src/test/java/com/powsybl/sld/server/GeoDataServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.LinePosition;
import com.powsybl.iidm.network.extensions.SubstationPosition;
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.sld.server.dto.LineGeoData;
import com.powsybl.sld.server.utils.GeoDataUtils;
import com.powsybl.sld.server.dto.Coordinate;
import com.powsybl.sld.server.dto.SubstationGeoData;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
Expand All @@ -24,13 +23,10 @@
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -94,21 +90,6 @@ public static Network createNetwork() {
.setId("busFr2A")
.setName("busFr2A")
.add();
Line lineFr1 = network.newLine()
.setId("lineFr1")
.setVoltageLevel1("vlFr2A")
.setBus1("busFr2A")
.setConnectableBus1("busFr2A")
.setVoltageLevel2("vlFr2A")
.setBus2("busFr2A")
.setConnectableBus2("busFr2A")
.setR(3.0)
.setX(33.0)
.setG1(0.0)
.setB1(386E-6 / 2)
.setG2(0.0)
.setB2(386E-6 / 2)
.add();
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID);
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID);

Expand All @@ -121,26 +102,11 @@ public void setUp() {
geoDataService.setGeoDataServerBaseUri(baseUri);
}

@Test
public void testGetLinesGraphics() {
UUID networkUuid = UUID.randomUUID();
String variantId = "variant1";
List<String> linesIds = List.of("line1", "line2");

String expectedResponse = "Lines graphics data";
when(restTemplate.getForObject(ArgumentMatchers.anyString(), ArgumentMatchers.eq(String.class)))
.thenReturn(expectedResponse);

String response = geoDataService.getLinesGraphics(networkUuid, variantId, linesIds);

assertEquals(expectedResponse, response);
}

@Test
public void testGetSubstationsGraphics() {
UUID networkUuid = UUID.randomUUID();
String variantId = "variant2";
List<String> substationsIds = List.of("substation1", "substation2");
List<String> substationsIds = List.of("subFr1", "subFr2");

String expectedResponse = "Substations graphics data";
when(restTemplate.getForObject(ArgumentMatchers.anyString(), ArgumentMatchers.eq(String.class)))
Expand All @@ -151,26 +117,15 @@ public void testGetSubstationsGraphics() {
assertEquals(expectedResponse, response);
}

@Test
public void testGetLinesGraphicsWithoutVariantId() {
UUID networkUuid = UUID.randomUUID();
List<String> linesIds = List.of("line1", "line2");

String expectedResponse = "Lines graphics data without variant";
when(restTemplate.getForObject(ArgumentMatchers.anyString(), ArgumentMatchers.eq(String.class)))
.thenReturn(expectedResponse);

String response = geoDataService.getLinesGraphics(networkUuid, null, linesIds);

assertEquals(expectedResponse, response);
}

@Test
public void testGetSubstationsGraphicsWithoutVariantId() {
UUID networkUuid = UUID.randomUUID();
List<String> substationsIds = List.of("substation1", "substation2");

String expectedResponse = "Substations graphics data without variant";
List<String> substationsIds = List.of("subFr1");
SubstationGeoData substationGeoData = new SubstationGeoData();
substationGeoData.setId("subFr1");
substationGeoData.setCoordinate(new Coordinate(48.8588443, 2.2943506));
substationGeoData.setCountry(Country.FR);
String expectedResponse = substationGeoData.toString();
when(restTemplate.getForObject(ArgumentMatchers.anyString(), ArgumentMatchers.eq(String.class)))
.thenReturn(expectedResponse);

Expand All @@ -184,31 +139,23 @@ public void testAssignSubstationGeoData() throws Exception {
UUID testNetworkId = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
Network network = networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION);

String substationGeoDataJson = "[{\"id\":\"subFr1\",\"coordinate\":{\"lat\":48.8588443,\"lon\":2.2943506}},{\"id\":\"subFr2\",\"coordinate\":{\"lat\":51.507351,\"lon\":1.127758}}]";
when(geoDataService.getSubstationsGraphics(testNetworkId, VARIANT_1_ID, null)).thenReturn(substationGeoDataJson);

geoDataService.assignSubstationGeoData(network, testNetworkId, VARIANT_1_ID, List.of(network.getSubstation("subFr1")));
assertEquals(network.getSubstation("subFr1").getExtension(SubstationPosition.class).getCoordinate(), new com.powsybl.iidm.network.extensions.Coordinate(48.8588443, 2.2943506));
assertEquals(network.getSubstation("subFr2").getExtension(SubstationPosition.class), null);
}

@Test
public void testAssignLineGeoData() throws Exception {
UUID testNetworkId = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
Network network = networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION);
String lineGeoDataJson = "[{\"id\":\"lineFr1\",\"coordinates\":[{\"lat\":48.8588443,\"lon\":2.2943506},{\"lat\":48.8588444,\"lon\":2.2943507}]},{\"id\":\"lineFr2\",\"coordinates\":[{\"lat\":51.507351,\"lon\":-0.127758},{\"lat\":51.507352,\"lon\":-0.127759}]}]";
when(geoDataService.getLinesGraphics(testNetworkId, VARIANT_1_ID, null)).thenReturn(lineGeoDataJson);
String faultSubstationGeoDataJson = "[{\"id\":\"subFr1\",\"coordinate\":{\"lat\":48.8588443,\"long\":2.2943506}}]";
when(geoDataService.getSubstationsGraphics(testNetworkId, VARIANT_1_ID, null)).thenReturn(faultSubstationGeoDataJson);
PowsyblException exception = assertThrows(PowsyblException.class, () -> {
geoDataService.assignSubstationGeoData(network, testNetworkId, VARIANT_1_ID, List.of(network.getSubstation("subFr2")));
});

List<LineGeoData> linesGeoData = GeoDataUtils.fromStringToLineGeoData(lineGeoDataJson, new ObjectMapper());
Map<String, List<com.powsybl.sld.server.dto.Coordinate>> lineGeoDataMap = linesGeoData.stream()
.collect(Collectors.toMap(LineGeoData::getId, LineGeoData::getCoordinates));

// Convert Iterable to List
List<Line> lines = StreamSupport.stream(network.getLines().spliterator(), false)
.collect(Collectors.toList());

geoDataService.assignLineGeoData(network, testNetworkId, VARIANT_1_ID, lines);
assertNotNull("Line position will not be null", network.getLine("lineFr1").getExtension(LinePosition.class));
// Assert the exception message
assertEquals("Failed to parse JSON response", exception.getMessage());
assertEquals(network.getSubstation("subFr2").getExtension(SubstationPosition.class), null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,13 @@ public void testComponentLibraries() throws Exception {
}

private static final String GEO_DATA_SUBSTATIONS = "/geo_data_substations.json";
private static final String GEO_DATA_LINES = "/geo_data_lines.json";

@Test
public void testNetworkAreaDiagram() throws Exception {
UUID testNetworkId = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
UUID notFoundNetworkId = UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");

given(geoDataService.getSubstationsGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_SUBSTATIONS));
given(geoDataService.getLinesGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_LINES));
given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
given(networkStoreService.getNetwork(notFoundNetworkId, PreloadingStrategy.COLLECTION)).willThrow(new PowsyblException());
Expand Down Expand Up @@ -329,7 +327,6 @@ public void testNetworkAreaDiagramAdditionalMetadata() {

given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
given(geoDataService.getSubstationsGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_SUBSTATIONS));
given(geoDataService.getLinesGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_LINES));

SvgAndMetadata svgAndMetadata = networkAreaDiagramService.generateNetworkAreaDiagramSvg(testNetworkId, VARIANT_2_ID, List.of("vlFr1A"), 2);
Object additionalMetadata = svgAndMetadata.getAdditionalMetadata();
Expand Down Expand Up @@ -545,7 +542,6 @@ public void testNetworkAreaDiagramWithMissingVoltageLevel() throws Exception {
UUID testNetworkId = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
given(networkStoreService.getNetwork(testNetworkId, PreloadingStrategy.COLLECTION)).willReturn(createNetwork());
given(geoDataService.getSubstationsGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_SUBSTATIONS));
given(geoDataService.getLinesGraphics(testNetworkId, VARIANT_2_ID, null)).willReturn(toString(GEO_DATA_LINES));

SvgAndMetadata svgAndMetadata = networkAreaDiagramService.generateNetworkAreaDiagramSvg(testNetworkId, VARIANT_2_ID, List.of("vlFr1A", "vlNotFound1"), 0);
Object additionalMetadata = svgAndMetadata.getAdditionalMetadata();
Expand Down
23 changes: 0 additions & 23 deletions src/test/resources/geo_data_lines.json

This file was deleted.

0 comments on commit 1f4b960

Please sign in to comment.