diff --git a/src/main/java/com/powsybl/sld/server/GeoDataService.java b/src/main/java/com/powsybl/sld/server/GeoDataService.java index 264f90d..58d18eb 100644 --- a/src/main/java/com/powsybl/sld/server/GeoDataService.java +++ b/src/main/java/com/powsybl/sld/server/GeoDataService.java @@ -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; @@ -60,25 +56,6 @@ private String getGeoDataServerURI() { return this.geoDataServerBaseUri + DELIMITER + GEO_DATA_API_VERSION + DELIMITER; } - public String getLinesGraphics(UUID networkUuid, String variantId, List 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 substationsIds) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(getGeoDataServerURI() + SUBSTATIONS) .queryParam(NETWORK_UUID, networkUuid); @@ -118,22 +95,6 @@ public void assignSubstationGeoData(Network network, UUID networkUuid, String va } } - public void assignLineGeoData(Network network, UUID networkUuid, String variantId, List lines) { - List linesGeoData = GeoDataUtils.fromStringToLineGeoData(getLinesGraphics(networkUuid, variantId, null), new ObjectMapper()); - Map> lineGeoDataMap = linesGeoData.stream() - .collect(Collectors.toMap(LineGeoData::getId, LineGeoData::getCoordinates)); - for (Line line : lines) { - if (network.getLine(line.getId()).getExtension(LinePosition.class) == null) { - List coordinates = lineGeoDataMap.get(line.getId()); - if (coordinates != null) { - network.getLine(line.getId()) - .newExtension(LinePositionAdder.class) - .withCoordinates(coordinates) - .add(); - } - } - } - } } diff --git a/src/main/java/com/powsybl/sld/server/NetworkAreaDiagramService.java b/src/main/java/com/powsybl/sld/server/NetworkAreaDiagramService.java index 02418e8..422fc29 100644 --- a/src/main/java/com/powsybl/sld/server/NetworkAreaDiagramService.java +++ b/src/main/java/com/powsybl/sld/server/NetworkAreaDiagramService.java @@ -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; @@ -32,7 +31,6 @@ import java.io.StringWriter; import java.io.UncheckedIOException; import java.util.*; -import java.util.stream.StreamSupport; /** * @author Etienne Homer @@ -85,7 +83,6 @@ private void getGeoDataCoordinates(Network network, UUID networkUuid, String var // Geographical Position for lines and substations related to voltageLevels List substations = getSubstations(voltageLevels); geoDataService.assignSubstationGeoData(network, networkUuid, variantId, substations); - geoDataService.assignLineGeoData(network, networkUuid, variantId, getLines(voltageLevels)); } private List getSubstations(List voltages) { @@ -95,12 +92,6 @@ private List getSubstations(List voltages) { .toList(); } - private List getLines(List voltages) { - return voltages.stream() - .flatMap(voltageLevel -> StreamSupport.stream(voltageLevel.getLines().spliterator(), false)) - .toList(); - } - private Map computeAdditionalMetadata(Network network, List voltageLevelsIds, int depth) { VoltageLevelFilter vlFilter = VoltageLevelFilter.createVoltageLevelsDepthFilter(network, voltageLevelsIds, depth); diff --git a/src/main/java/com/powsybl/sld/server/dto/LineGeoData.java b/src/main/java/com/powsybl/sld/server/dto/LineGeoData.java deleted file mode 100644 index be8fe33..0000000 --- a/src/main/java/com/powsybl/sld/server/dto/LineGeoData.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright (c) 2024, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * 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.sld.server.dto; - -import com.powsybl.iidm.network.Country; -import lombok.*; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Maissa SOUISSI - */ -@NoArgsConstructor -@AllArgsConstructor -@Getter -@Builder -@ToString -public class LineGeoData { - - private String id; - - private Country country1; - - private Country country2; - - String substationStart; - - String substationEnd; - - private List coordinates = new ArrayList<>(); -} diff --git a/src/main/java/com/powsybl/sld/server/utils/GeoDataUtils.java b/src/main/java/com/powsybl/sld/server/utils/GeoDataUtils.java index 0ae2a40..4689214 100644 --- a/src/main/java/com/powsybl/sld/server/utils/GeoDataUtils.java +++ b/src/main/java/com/powsybl/sld/server/utils/GeoDataUtils.java @@ -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; @@ -33,15 +32,6 @@ public static List fromStringToSubstationGeoData(String jsonR } } - public static List fromStringToLineGeoData(String jsonResponse, ObjectMapper objectMapper) { - try { - return objectMapper.readValue(jsonResponse, new TypeReference<>() { - }); - } catch (JsonProcessingException e) { - throw new PowsyblException("Failed to parse JSON response", e); - } - } - } diff --git a/src/test/java/com/powsybl/sld/server/GeoDataServiceTest.java b/src/test/java/com/powsybl/sld/server/GeoDataServiceTest.java index 8acf8f3..2bf2b21 100644 --- a/src/test/java/com/powsybl/sld/server/GeoDataServiceTest.java +++ b/src/test/java/com/powsybl/sld/server/GeoDataServiceTest.java @@ -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; @@ -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; @@ -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); @@ -121,26 +102,11 @@ public void setUp() { geoDataService.setGeoDataServerBaseUri(baseUri); } - @Test - public void testGetLinesGraphics() { - UUID networkUuid = UUID.randomUUID(); - String variantId = "variant1"; - List 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 substationsIds = List.of("substation1", "substation2"); + List substationsIds = List.of("subFr1", "subFr2"); String expectedResponse = "Substations graphics data"; when(restTemplate.getForObject(ArgumentMatchers.anyString(), ArgumentMatchers.eq(String.class))) @@ -151,26 +117,15 @@ public void testGetSubstationsGraphics() { assertEquals(expectedResponse, response); } - @Test - public void testGetLinesGraphicsWithoutVariantId() { - UUID networkUuid = UUID.randomUUID(); - List 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 substationsIds = List.of("substation1", "substation2"); - - String expectedResponse = "Substations graphics data without variant"; + List 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); @@ -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 linesGeoData = GeoDataUtils.fromStringToLineGeoData(lineGeoDataJson, new ObjectMapper()); - Map> lineGeoDataMap = linesGeoData.stream() - .collect(Collectors.toMap(LineGeoData::getId, LineGeoData::getCoordinates)); - - // Convert Iterable to List - List 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); } + } diff --git a/src/test/java/com/powsybl/sld/server/SingleLineDiagramTest.java b/src/test/java/com/powsybl/sld/server/SingleLineDiagramTest.java index ba3e383..14d588e 100644 --- a/src/test/java/com/powsybl/sld/server/SingleLineDiagramTest.java +++ b/src/test/java/com/powsybl/sld/server/SingleLineDiagramTest.java @@ -286,7 +286,6 @@ 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 { @@ -294,7 +293,6 @@ public void testNetworkAreaDiagram() throws Exception { 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()); @@ -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(); @@ -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(); diff --git a/src/test/resources/geo_data_lines.json b/src/test/resources/geo_data_lines.json deleted file mode 100644 index 008207c..0000000 --- a/src/test/resources/geo_data_lines.json +++ /dev/null @@ -1,23 +0,0 @@ -[ - { - "id": "lineFR", - "country1": "FR", - "country2": "FR", - "substationStart": "subFr1", - "substationEnd": "subFr1", - "coordinates": [ - { - "lat": 50.9169, - "lon": 4.34509 - }, - { - "lat": 51.0448, - "lon": 4.29565 - }, - { - "lat": 51.157, - "lon": 4.38354 - } - ] - } -]