From 4135305cab6f00315cb1d6eb99d582e349e0371c Mon Sep 17 00:00:00 2001 From: sBouzols Date: Tue, 26 Nov 2024 11:52:38 +0100 Subject: [PATCH 1/4] fix(): manage null substationIds ou lineIds Signed-off-by: sBouzols --- .../geodata/server/GeoDataService.java | 36 ++++++++++++------- .../geodata/server/GeoDataControllerTest.java | 24 +++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index 4cd6714c..cd0a9237 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -178,7 +178,10 @@ private void prepareGeoDataForComputation(Network network, Map { if (geoDataForComputation.get(neighbourId) == null && !substationsToCalculate.contains(neighbourId)) { substationsToCalculate.add(neighbourId); - substations.add(network.getSubstation(neighbourId)); + Substation sub = network.getSubstation(neighbourId); + if (sub != null) { + substations.add(sub); + } } }); Map> newNeighbours = getNeighbours(substations); @@ -283,17 +286,19 @@ private void step(Step step, Network network, Map> sortedNei for (Iterator it = substationsToCalculate.iterator(); it.hasNext();) { String substationId = it.next(); Set neighbours = sortedNeighbours.get(substationId); - double neighborhoodOffset = calculatedSubstationsOffset.get(neighbours) != null ? nextNeighborhoodOffset(calculatedSubstationsOffset.get(neighbours)) : 0; - - // centroid calculation - Substation substation = network.getSubstation(substationId); - SubstationGeoData substationGeoData = calculateCentroidGeoData(substation, neighbours, step, substationsGeoData, neighborhoodOffset); - - if (substationGeoData != null) { - calculated++; - substationsGeoData.put(substationId, substationGeoData); - calculatedSubstationsOffset.put(neighbours, neighborhoodOffset); - it.remove(); + if (neighbours != null) { + double neighborhoodOffset = calculatedSubstationsOffset.get(neighbours) != null ? nextNeighborhoodOffset(calculatedSubstationsOffset.get(neighbours)) : 0; + + // centroid calculation + Substation substation = network.getSubstation(substationId); + SubstationGeoData substationGeoData = calculateCentroidGeoData(substation, neighbours, step, substationsGeoData, neighborhoodOffset); + + if (substationGeoData != null) { + calculated++; + substationsGeoData.put(substationId, substationGeoData); + calculatedSubstationsOffset.put(neighbours, neighborhoodOffset); + it.remove(); + } } } LOGGER.info("Step {}, iteration {}, {} substation's coordinates have been calculated, {} remains unknown", @@ -536,7 +541,12 @@ public List getLinesByIds(Network network, Set linesIds) { List lines = new ArrayList<>(); - linesIds.forEach(id -> lines.add(network.getLine(id))); + linesIds.forEach(id -> { + Line line = network.getLine(id); + if (line != null) { + lines.add(line); + } + }); // read lines from DB Map linesGeoDataDb = lineRepository.findAllById(linesIds).stream().collect(Collectors.toMap(LineEntity::getId, this::toDto)); diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java index 68ac624f..11e67058 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java @@ -162,6 +162,18 @@ void test() throws Exception { .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); + mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=null&substationId=P2") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(0))); + + mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=null") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(0))); + mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=NHV1_NHV2_2&lineId=NHV1_NHV2_1&country=" + Country.FR) .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) @@ -173,6 +185,18 @@ void test() throws Exception { .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); + + mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=NHV1_NHV2_2&lineId=null") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(0))); + + mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=null") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(0))); } @Test From b8d031e431643e82312ab7011fdba533e75e4163 Mon Sep 17 00:00:00 2001 From: sBouzols Date: Tue, 26 Nov 2024 11:59:27 +0100 Subject: [PATCH 2/4] better java code Signed-off-by: sBouzols --- .../org/gridsuite/geodata/server/GeoDataService.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index cd0a9237..7f909de6 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -539,14 +539,7 @@ public List getLinesByIds(Network network, Set linesIds) { StopWatch stopWatch = StopWatch.createStarted(); - List lines = new ArrayList<>(); - - linesIds.forEach(id -> { - Line line = network.getLine(id); - if (line != null) { - lines.add(line); - } - }); + List lines = linesIds.stream().map(network::getLine).filter(Objects::nonNull).collect(Collectors.toList()); // read lines from DB Map linesGeoDataDb = lineRepository.findAllById(linesIds).stream().collect(Collectors.toMap(LineEntity::getId, this::toDto)); From 72cdf4a029e961941b3f5c5ca40b76a80a4d9823 Mon Sep 17 00:00:00 2001 From: sBouzols Date: Tue, 26 Nov 2024 14:43:30 +0100 Subject: [PATCH 3/4] Add comment en LOGGER.debug Change null to notExistsId in TUs Signed-off-by: sBouzols --- .../gridsuite/geodata/server/GeoDataService.java | 15 +++++++++++++-- .../geodata/server/GeoDataControllerTest.java | 8 ++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index 7f909de6..790a6be5 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -179,8 +179,10 @@ private void prepareGeoDataForComputation(Network network, Map getLinesByIds(Network network, Set linesIds) { StopWatch stopWatch = StopWatch.createStarted(); - List lines = linesIds.stream().map(network::getLine).filter(Objects::nonNull).collect(Collectors.toList()); + List lines = new ArrayList<>(); + + linesIds.forEach(id -> { + Line line = network.getLine(id); + if (line != null) { // comes from a Request param string, could not exists in the network + lines.add(line); + } else { + LOGGER.debug("{} line doesn't exist in the newtwork, will be ignored.", id); + } + }); // read lines from DB Map linesGeoDataDb = lineRepository.findAllById(linesIds).stream().collect(Collectors.toMap(LineEntity::getId, this::toDto)); diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java index 11e67058..eb36a3d3 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java @@ -162,13 +162,13 @@ void test() throws Exception { .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); - mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=null&substationId=P2") + mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=notExistsId&substationId=P2") .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); - mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=null") + mvc.perform(get("/" + VERSION + "/substations?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&substationId=notExistsId") .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) @@ -186,13 +186,13 @@ void test() throws Exception { .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); - mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=NHV1_NHV2_2&lineId=null") + mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=NHV1_NHV2_2&lineId=notExistsId") .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$", hasSize(0))); - mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=null") + mvc.perform(get("/" + VERSION + "/lines?networkUuid=" + networkUuid + "&variantId=" + VARIANT_ID + "&lineId=notExistsId") .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) From 44f45a42ebad5ae94bc7d29804df2a41d1b751c0 Mon Sep 17 00:00:00 2001 From: sBouzols Date: Tue, 26 Nov 2024 14:47:52 +0100 Subject: [PATCH 4/4] comments Signed-off-by: sBouzols --- .../java/org/gridsuite/geodata/server/GeoDataService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index 790a6be5..2b6cd0d8 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -179,7 +179,7 @@ private void prepareGeoDataForComputation(Network network, Map> sortedNei for (Iterator it = substationsToCalculate.iterator(); it.hasNext();) { String substationId = it.next(); Set neighbours = sortedNeighbours.get(substationId); - if (neighbours != null) { + if (neighbours != null) { // substationsToCalculate comes from a Request param string, could not exists in the network double neighborhoodOffset = calculatedSubstationsOffset.get(neighbours) != null ? nextNeighborhoodOffset(calculatedSubstationsOffset.get(neighbours)) : 0; // centroid calculation @@ -545,7 +545,7 @@ public List getLinesByIds(Network network, Set linesIds) { linesIds.forEach(id -> { Line line = network.getLine(id); - if (line != null) { // comes from a Request param string, could not exists in the network + if (line != null) { // linesIds comes from a Request param string, could not exists in the network lines.add(line); } else { LOGGER.debug("{} line doesn't exist in the newtwork, will be ignored.", id);