diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataException.java b/src/main/java/org/gridsuite/geodata/server/GeoDataException.java deleted file mode 100644 index 1fb102df..00000000 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataException.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (c) 2022, 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 org.gridsuite.geodata.server; - -import org.springframework.http.HttpStatus; - -import java.util.Objects; - -/** - * @author Franck Lecuyer - */ -public class GeoDataException extends RuntimeException { - - public enum Type { - PARSING_ERROR(HttpStatus.INTERNAL_SERVER_ERROR), - FAILED_LINES_LOADING(HttpStatus.INTERNAL_SERVER_ERROR), - FAILED_SUBSTATIONS_LOADING(HttpStatus.INTERNAL_SERVER_ERROR); - - public final HttpStatus status; - - HttpStatus getStatus() { - return status; - } - - Type(HttpStatus status) { - this.status = status; - } - } - - private final Type type; - - public GeoDataException(Type type, Exception cause) { - super(Objects.requireNonNull(type.name()) + " : " + ((cause.getMessage() == null) ? cause.getClass().getName() : cause.getMessage()), cause); - this.type = type; - } - - Type getType() { - return type; - } -} diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index ebd8c998..4c272b66 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.io.UncheckedIOException; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.CompletableFuture; @@ -38,8 +39,6 @@ import java.util.stream.Stream; import static java.lang.Math.round; -import static org.gridsuite.geodata.server.GeoDataException.Type.FAILED_LINES_LOADING; -import static org.gridsuite.geodata.server.GeoDataException.Type.FAILED_SUBSTATIONS_LOADING; /** * @author Chamseddine Benhamed @@ -429,7 +428,7 @@ void saveLines(List linesGeoData) { } lineRepository.saveAll(linesEntities); } catch (JsonProcessingException e) { - throw new GeoDataException(GeoDataException.Type.PARSING_ERROR, e); + throw new UncheckedIOException("Parsing error", e); } } @@ -546,34 +545,26 @@ private Pair getSubstations(Identifiable identifiable public CompletableFuture> getSubstationsData(Network network, Set countrySet, List substationIds) { return geoDataExecutionService.supplyAsync(() -> { - try { - if (substationIds != null) { - if (!countrySet.isEmpty()) { - LOGGER.warn("Countries will not be taken into account to filter substation position."); - } - return getSubstationsByIds(network, new HashSet<>(substationIds)); - } else { - return getSubstationsByCountries(network, countrySet); + if (substationIds != null) { + if (!countrySet.isEmpty()) { + LOGGER.warn("Countries will not be taken into account to filter substation position."); } - } catch (Exception e) { - throw new GeoDataException(FAILED_SUBSTATIONS_LOADING, e); + return getSubstationsByIds(network, new HashSet<>(substationIds)); + } else { + return getSubstationsByCountries(network, countrySet); } }); } public CompletableFuture> getLinesData(Network network, Set countrySet, List lineIds) { return geoDataExecutionService.supplyAsync(() -> { - try { - if (lineIds != null) { - if (!countrySet.isEmpty()) { - LOGGER.warn("Countries will not be taken into account to filter line position."); - } - return getLinesByIds(network, new HashSet<>(lineIds)); - } else { - return getLinesByCountries(network, countrySet); + if (lineIds != null) { + if (!countrySet.isEmpty()) { + LOGGER.warn("Countries will not be taken into account to filter line position."); } - } catch (Exception e) { - throw new GeoDataException(FAILED_LINES_LOADING, e); + return getLinesByIds(network, new HashSet<>(lineIds)); + } else { + return getLinesByCountries(network, countrySet); } }); } @@ -618,7 +609,7 @@ public LineGeoData toDto(LineEntity lineEntity) { toDto(lineEntity.getCoordinates()) ); } catch (JsonProcessingException e) { - throw new GeoDataException(GeoDataException.Type.PARSING_ERROR, e); + throw new UncheckedIOException("Parsing error", e); } } diff --git a/src/main/java/org/gridsuite/geodata/server/RestResponseEntityExceptionHandler.java b/src/main/java/org/gridsuite/geodata/server/RestResponseEntityExceptionHandler.java deleted file mode 100644 index ea2d53db..00000000 --- a/src/main/java/org/gridsuite/geodata/server/RestResponseEntityExceptionHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright (c) 2022, 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 org.gridsuite.geodata.server; - -import com.powsybl.commons.PowsyblException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; - -/** - * @author Franck Lecuyer - */ -@ControllerAdvice -public class RestResponseEntityExceptionHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class); - - @ExceptionHandler(value = {GeoDataException.class, PowsyblException.class}) - protected ResponseEntity handleException(Exception exception) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(exception.getMessage(), exception); - } - if (exception instanceof GeoDataException) { - GeoDataException geoDataException = (GeoDataException) exception; - return ResponseEntity - .status(geoDataException.getType().getStatus()) - .body(geoDataException.getMessage()); - } - return ResponseEntity.internalServerError().body(exception.getMessage()); - } -} diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java index d9cca9ce..81288d4c 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java @@ -16,19 +16,23 @@ import com.powsybl.network.store.client.NetworkStoreService; import com.powsybl.network.store.client.PreloadingStrategy; import com.powsybl.network.store.client.RestClientImpl; +import com.powsybl.ws.commons.error.BaseExceptionHandler; import org.gridsuite.geodata.server.dto.LineGeoData; import org.gridsuite.geodata.server.dto.SubstationGeoData; import org.gridsuite.geodata.server.repositories.LineRepository; import org.gridsuite.geodata.server.repositories.SubstationRepository; +import org.hamcrest.core.StringContains; 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.context.annotation.Import; import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; @@ -47,6 +51,7 @@ * @author Chamseddine Benhamed */ @WebMvcTest(GeoDataController.class) +@Import(BaseExceptionHandler.class) class GeoDataControllerTest { @Autowired @@ -116,7 +121,7 @@ void test() throws Exception { mvc.perform(post("/" + VERSION + "/substations/infos?networkUuid=" + networkUuid + "&variantId=" + WRONG_VARIANT_ID) .contentType(APPLICATION_JSON)) - .andExpect(content().string("Variant '" + WRONG_VARIANT_ID + "' not found")) + .andExpect(content().string(StringContains.containsString("Variant '" + WRONG_VARIANT_ID + "' not found"))) .andExpect(status().isInternalServerError()); mockMvcResultActions = mvc.perform(post("/" + VERSION + "/lines/infos?networkUuid=" + networkUuid) @@ -137,7 +142,7 @@ void test() throws Exception { mvc.perform(post("/" + VERSION + "/lines/infos?networkUuid=" + networkUuid + "&variantId=" + WRONG_VARIANT_ID) .contentType(APPLICATION_JSON)) - .andExpect(content().string("Variant '" + WRONG_VARIANT_ID + "' not found")) + .andExpect(content().string(StringContains.containsString("Variant '" + WRONG_VARIANT_ID + "' not found"))) .andExpect(status().isInternalServerError()); String substationJson = objectMapper.writeValueAsString(Collections.singleton( @@ -217,7 +222,7 @@ void testGetLinesError() throws Exception { Network testNetwork = EurostagTutorialExample1Factory.create(); given(service.getNetwork(networkUuid)).willReturn(testNetwork); given(service.getNetwork(networkUuid, PreloadingStrategy.COLLECTION)).willReturn(testNetwork); - given(lineRepository.findAllById(any())).willThrow(new GeoDataException(GeoDataException.Type.PARSING_ERROR, new RuntimeException("Error parsing"))); + given(lineRepository.findAllById(any())).willThrow(new UncheckedIOException("Parsing error", new IOException("IO"))); MvcResult mvcResult = mvc.perform(post("/" + VERSION + "/lines/infos?networkUuid=" + networkUuid) .contentType(APPLICATION_JSON)) diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataServerNameProvider.java b/src/test/java/org/gridsuite/geodata/server/GeoDataServerNameProvider.java new file mode 100644 index 00000000..d2fde17c --- /dev/null +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataServerNameProvider.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2025, 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 org.gridsuite.geodata.server; + +import com.powsybl.ws.commons.error.ServerNameProvider; +import org.springframework.stereotype.Component; + +/** + * @author Mohamed Ben-rejeb {@literal } + */ +@Component +public class GeoDataServerNameProvider implements ServerNameProvider { + + @Override + public String serverName() { + return "geo-data-server"; + } +} diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java index df61aa8d..c21b195d 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java @@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.io.UncheckedIOException; import java.util.*; import static org.junit.jupiter.api.Assertions.*; @@ -521,7 +522,7 @@ void testLineCoordinatesError() { .substationEnd("substation2") .build(), true, "coordinates_error"); - assertThrows(GeoDataException.class, () -> + assertThrows(UncheckedIOException.class, () -> geoDataService.toDto(lineEntity)); }