diff --git a/pom.xml b/pom.xml index 39e77703..2ff5e847 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,8 @@ org.gridsuite.mapping.server gridsuite org.gridsuite:dynamic-mapping-server + + 1.34.0 @@ -68,6 +70,11 @@ + + com.powsybl + powsybl-ws-commons + ${powsybl-ws-commons.version} + diff --git a/src/main/java/org/gridsuite/mapping/server/DynamicMappingException.java b/src/main/java/org/gridsuite/mapping/server/DynamicMappingException.java deleted file mode 100644 index 9c5ca03f..00000000 --- a/src/main/java/org/gridsuite/mapping/server/DynamicMappingException.java +++ /dev/null @@ -1,34 +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 org.gridsuite.mapping.server; - -import lombok.Getter; - -/** - * @author Thang PHAM - */ -@Getter -public class DynamicMappingException extends RuntimeException { - - public enum Type { - URI_SYNTAX, - MAPPING_NAME_NOT_PROVIDED, - GET_FILTER_ERROR, - CREATE_FILTER_ERROR, - UPDATE_FILTER_ERROR, - DUPLICATE_FILTER_ERROR, - DELETE_FILTER_ERROR, - FILTER_NOT_FOUND - } - - private final Type type; - - public DynamicMappingException(Type type, String message) { - super(message); - this.type = type; - } -} diff --git a/src/main/java/org/gridsuite/mapping/server/PropertyServerNameProvider.java b/src/main/java/org/gridsuite/mapping/server/PropertyServerNameProvider.java new file mode 100644 index 00000000..317787b6 --- /dev/null +++ b/src/main/java/org/gridsuite/mapping/server/PropertyServerNameProvider.java @@ -0,0 +1,28 @@ +/** + * 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.mapping.server; + +import com.powsybl.ws.commons.error.ServerNameProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * @author Hugo Marcellin + */ +@Component +public class PropertyServerNameProvider implements ServerNameProvider { + private final String name; + + public PropertyServerNameProvider(@Value("${spring.application.name:dynamic-mapping-server}") String name) { + this.name = name; + } + + @Override + public String serverName() { + return name; + } +} diff --git a/src/main/java/org/gridsuite/mapping/server/RestResponseEntityExceptionHandler.java b/src/main/java/org/gridsuite/mapping/server/RestResponseEntityExceptionHandler.java deleted file mode 100644 index f1d98a6c..00000000 --- a/src/main/java/org/gridsuite/mapping/server/RestResponseEntityExceptionHandler.java +++ /dev/null @@ -1,52 +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 org.gridsuite.mapping.server; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.client.HttpClientErrorException; - -/** - * @author Thang PHAM - */ -@ControllerAdvice -public class RestResponseEntityExceptionHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class); - - @ExceptionHandler(HttpClientErrorException.class) - protected ResponseEntity handleHttpClientErrorException(HttpClientErrorException exception) { - return ResponseEntity.status(exception.getStatusCode()).body(exception.getStatusText()); - } - - @ExceptionHandler(DynamicMappingException.class) - protected ResponseEntity handleDynamicSimulationException(DynamicMappingException exception) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(exception.getMessage(), exception); - } - - DynamicMappingException.Type type = exception.getType(); - return switch (type) { - case MAPPING_NAME_NOT_PROVIDED - -> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getMessage()); - case FILTER_NOT_FOUND - -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.getMessage()); - case URI_SYNTAX, - GET_FILTER_ERROR, - CREATE_FILTER_ERROR, - UPDATE_FILTER_ERROR, - DUPLICATE_FILTER_ERROR, - DELETE_FILTER_ERROR - -> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(exception.getMessage()); - }; - } -} diff --git a/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingErrorBusinessCode.java b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingErrorBusinessCode.java new file mode 100644 index 00000000..2023c589 --- /dev/null +++ b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingErrorBusinessCode.java @@ -0,0 +1,27 @@ +/** + * 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package org.gridsuite.mapping.server.error; + +import com.powsybl.ws.commons.error.BusinessErrorCode; + +/** + * @author Hugo Marcellin + */ +public enum DynamicMappingErrorBusinessCode implements BusinessErrorCode { + MAPPING_NAME_NOT_PROVIDED("dynamicMapping.mappingNameNotProvided"); + + private final String code; + + DynamicMappingErrorBusinessCode(String code) { + this.code = code; + } + + public String value() { + return code; + } +} diff --git a/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingException.java b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingException.java new file mode 100644 index 00000000..0733e490 --- /dev/null +++ b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingException.java @@ -0,0 +1,30 @@ +/** + * 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 org.gridsuite.mapping.server.error; + +import com.powsybl.ws.commons.error.AbstractBusinessException; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; + +/** + * @author Thang PHAM + */ +@Getter +public class DynamicMappingException extends AbstractBusinessException { + private final DynamicMappingErrorBusinessCode errorCode; + + public DynamicMappingException(DynamicMappingErrorBusinessCode errorCode, String message) { + super(message); + this.errorCode = errorCode; + } + + @NotNull + @Override + public DynamicMappingErrorBusinessCode getBusinessErrorCode() { + return errorCode; + } +} diff --git a/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandler.java b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandler.java new file mode 100644 index 00000000..3c3ce333 --- /dev/null +++ b/src/main/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandler.java @@ -0,0 +1,45 @@ +/* + * 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 org.gridsuite.mapping.server.error; + +import com.powsybl.ws.commons.error.AbstractBusinessExceptionHandler; +import com.powsybl.ws.commons.error.PowsyblWsProblemDetail; +import com.powsybl.ws.commons.error.ServerNameProvider; +import jakarta.servlet.http.HttpServletRequest; +import lombok.NonNull; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +/** + * @author Thang PHAM + */ +@ControllerAdvice +public class DynamicMappingExceptionHandler extends AbstractBusinessExceptionHandler { + protected DynamicMappingExceptionHandler(ServerNameProvider serverNameProvider) { + super(serverNameProvider); + } + + @Override + protected @NonNull DynamicMappingErrorBusinessCode getBusinessCode(DynamicMappingException e) { + return e.getBusinessErrorCode(); + } + + protected HttpStatus mapStatus(DynamicMappingErrorBusinessCode businessErrorCode) { + return switch (businessErrorCode) { + case MAPPING_NAME_NOT_PROVIDED -> HttpStatus.BAD_REQUEST; + }; + } + + @ExceptionHandler(DynamicMappingException.class) + protected ResponseEntity handleDynamicMappingException( + DynamicMappingException exception, HttpServletRequest request) { + return super.handleDomainException(exception, request); + } +} diff --git a/src/main/java/org/gridsuite/mapping/server/service/client/filter/impl/FilterClientImpl.java b/src/main/java/org/gridsuite/mapping/server/service/client/filter/impl/FilterClientImpl.java index 709ee0a3..a4e92423 100644 --- a/src/main/java/org/gridsuite/mapping/server/service/client/filter/impl/FilterClientImpl.java +++ b/src/main/java/org/gridsuite/mapping/server/service/client/filter/impl/FilterClientImpl.java @@ -9,15 +9,16 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.collections4.CollectionUtils; import org.gridsuite.filter.expertfilter.ExpertFilter; -import org.gridsuite.mapping.server.DynamicMappingException; import org.gridsuite.mapping.server.service.client.AbstractRestClient; import org.gridsuite.mapping.server.service.client.filter.FilterClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.*; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; -import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -26,8 +27,6 @@ import java.util.Map; import java.util.UUID; -import static org.gridsuite.mapping.server.DynamicMappingException.Type.*; -import static org.gridsuite.mapping.server.service.client.utils.ExceptionUtils.handleHttpError; import static org.gridsuite.mapping.server.service.client.utils.UrlUtils.buildEndPointUrl; /** @@ -57,20 +56,12 @@ public List getFilters(List filterUuids) { uriComponentsBuilder.queryParam("ids", filterUuids); // call filter server Rest API - try { - return getRestTemplate().exchange( - uriComponentsBuilder.build().toUriString(), - HttpMethod.GET, - null, - new ParameterizedTypeReference>() { }).getBody(); - - } catch (HttpStatusCodeException e) { - if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) { - throw new DynamicMappingException(FILTER_NOT_FOUND, "Some filters have not been found"); - } else { - throw handleHttpError(e, GET_FILTER_ERROR, getObjectMapper()); - } - } + return getRestTemplate().exchange( + uriComponentsBuilder.build().toUriString(), + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + }).getBody(); } @Override @@ -89,15 +80,13 @@ public List createFilters(Map filtersToCreateM HttpEntity> httpEntity = new HttpEntity<>(filtersToCreateMap, headers); // call filter server Rest API - try { - return getRestTemplate().exchange( - uriComponentsBuilder.build().toUriString(), - HttpMethod.POST, - httpEntity, - new ParameterizedTypeReference>() { }).getBody(); - } catch (HttpStatusCodeException e) { - throw handleHttpError(e, CREATE_FILTER_ERROR, getObjectMapper()); - } + return getRestTemplate().exchange( + uriComponentsBuilder.build().toUriString(), + HttpMethod.POST, + httpEntity, + new ParameterizedTypeReference>() { + }).getBody(); + } @Override @@ -116,15 +105,13 @@ public List updateFilters(Map filtersToUpdateM HttpEntity> httpEntity = new HttpEntity<>(filtersToUpdateMap, headers); // call filter server Rest API - try { - return getRestTemplate().exchange( - uriComponentsBuilder.build().toUriString(), - HttpMethod.PUT, - httpEntity, - new ParameterizedTypeReference>() { }).getBody(); - } catch (HttpStatusCodeException e) { - throw handleHttpError(e, UPDATE_FILTER_ERROR, getObjectMapper()); - } + return getRestTemplate().exchange( + uriComponentsBuilder.build().toUriString(), + HttpMethod.PUT, + httpEntity, + new ParameterizedTypeReference>() { + }).getBody(); + } @Override @@ -143,15 +130,13 @@ public Map duplicateFilters(List filterUuids) { HttpEntity> httpEntity = new HttpEntity<>(filterUuids, headers); // call filter server Rest API - try { - return getRestTemplate().exchange( - uriComponentsBuilder.build().toUriString(), - HttpMethod.POST, - httpEntity, - new ParameterizedTypeReference>() { }).getBody(); - } catch (HttpStatusCodeException e) { - throw handleHttpError(e, DUPLICATE_FILTER_ERROR, getObjectMapper()); - } + return getRestTemplate().exchange( + uriComponentsBuilder.build().toUriString(), + HttpMethod.POST, + httpEntity, + new ParameterizedTypeReference>() { + }).getBody(); + } @Override @@ -170,14 +155,11 @@ public void deleteFilters(List filterUuids) { HttpEntity> httpEntity = new HttpEntity<>(filterUuids, headers); // call filter server Rest API - try { - getRestTemplate().exchange( - uriComponentsBuilder.build().toUriString(), - HttpMethod.DELETE, - httpEntity, - Void.class); - } catch (HttpStatusCodeException e) { - throw handleHttpError(e, DELETE_FILTER_ERROR, getObjectMapper()); - } + getRestTemplate().exchange( + uriComponentsBuilder.build().toUriString(), + HttpMethod.DELETE, + httpEntity, + Void.class); + } } diff --git a/src/main/java/org/gridsuite/mapping/server/service/client/utils/ExceptionUtils.java b/src/main/java/org/gridsuite/mapping/server/service/client/utils/ExceptionUtils.java deleted file mode 100644 index 2864ace4..00000000 --- a/src/main/java/org/gridsuite/mapping/server/service/client/utils/ExceptionUtils.java +++ /dev/null @@ -1,52 +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 org.gridsuite.mapping.server.service.client.utils; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.gridsuite.mapping.server.DynamicMappingException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.client.HttpStatusCodeException; - -/** - * The implementation of class {@link ExceptionUtils} is taken from class {@code StudyUtils} in study-server - * - * @author Thang PHAM - */ -public final class ExceptionUtils { - private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionUtils.class); - - private ExceptionUtils() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - public static DynamicMappingException handleHttpError(HttpStatusCodeException httpException, DynamicMappingException.Type type, ObjectMapper objectMapper) { - String responseBody = httpException.getResponseBodyAsString(); - - String errorMessage = responseBody.isEmpty() ? httpException.getStatusCode().toString() : parseHttpError(responseBody, objectMapper); - - LOGGER.error(errorMessage, httpException); - - return new DynamicMappingException(type, errorMessage); - } - - private static String parseHttpError(String responseBody, ObjectMapper objectMapper) { - try { - JsonNode node = objectMapper.readTree(responseBody).path("message"); - if (!node.isMissingNode()) { - return node.asText(); - } - } catch (JsonProcessingException e) { - // status code or responseBody by default - } - - return responseBody; - } -} diff --git a/src/main/java/org/gridsuite/mapping/server/service/client/utils/UrlUtils.java b/src/main/java/org/gridsuite/mapping/server/service/client/utils/UrlUtils.java index 665b1535..c092366c 100644 --- a/src/main/java/org/gridsuite/mapping/server/service/client/utils/UrlUtils.java +++ b/src/main/java/org/gridsuite/mapping/server/service/client/utils/UrlUtils.java @@ -7,13 +7,12 @@ package org.gridsuite.mapping.server.service.client.utils; +import com.powsybl.commons.exceptions.UncheckedUriSyntaxException; import org.apache.logging.log4j.util.Strings; -import org.gridsuite.mapping.server.DynamicMappingException; import java.net.URI; import java.net.URISyntaxException; -import static org.gridsuite.mapping.server.DynamicMappingException.Type.URI_SYNTAX; import static org.gridsuite.mapping.server.service.client.RestClient.URL_DELIMITER; /** @@ -45,7 +44,7 @@ public static String buildEndPointUrl(String baseUri, String apiVersion, String // normalize before return return new URI(url).normalize().toString(); } catch (URISyntaxException e) { - throw new DynamicMappingException(URI_SYNTAX, e.getMessage()); + throw new UncheckedUriSyntaxException(e); } } } diff --git a/src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java b/src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java index 11d23975..300f0e61 100644 --- a/src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java +++ b/src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java @@ -9,7 +9,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.gridsuite.filter.expertfilter.ExpertFilter; -import org.gridsuite.mapping.server.DynamicMappingException; +import org.gridsuite.mapping.server.error.DynamicMappingException; import org.gridsuite.mapping.server.dto.InputMapping; import org.gridsuite.mapping.server.dto.RenameObject; import org.gridsuite.mapping.server.dto.Rule; @@ -34,8 +34,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.gridsuite.mapping.server.DynamicMappingException.Type.MAPPING_NAME_NOT_PROVIDED; import static org.gridsuite.mapping.server.MappingConstants.DEFAULT_MAPPING_NAME; +import static org.gridsuite.mapping.server.error.DynamicMappingErrorBusinessCode.MAPPING_NAME_NOT_PROVIDED; /** * @author Mathieu Scalbert diff --git a/src/test/java/org/gridsuite/mapping/server/PropertyServerNameProviderTest.java b/src/test/java/org/gridsuite/mapping/server/PropertyServerNameProviderTest.java new file mode 100644 index 00000000..6367fd26 --- /dev/null +++ b/src/test/java/org/gridsuite/mapping/server/PropertyServerNameProviderTest.java @@ -0,0 +1,23 @@ +/** + * 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.mapping.server; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Mohamed Ben-rejeb {@literal } + */ +class PropertyServerNameProviderTest { + + @Test + void returnsProvidedName() { + PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server"); + assertThat(provider.serverName()).isEqualTo("custom-server"); + } +} diff --git a/src/test/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandlerTest.java b/src/test/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandlerTest.java new file mode 100644 index 00000000..74012622 --- /dev/null +++ b/src/test/java/org/gridsuite/mapping/server/error/DynamicMappingExceptionHandlerTest.java @@ -0,0 +1,41 @@ +/** + * 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.mapping.server.error; + +import com.powsybl.ws.commons.error.PowsyblWsProblemDetail; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockHttpServletRequest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.gridsuite.mapping.server.error.DynamicMappingErrorBusinessCode.MAPPING_NAME_NOT_PROVIDED; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Hugo Marcellin + */ +class DynamicMappingExceptionHandlerTest { + private DynamicMappingExceptionHandler handler; + + @BeforeEach + void setUp() { + handler = new DynamicMappingExceptionHandler(() -> "dynamic-mapping"); + } + + @Test + void mapsInteralErrorBusinessErrorToStatus() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/results-endpoint/uuid"); + DynamicMappingException exception = new DynamicMappingException(MAPPING_NAME_NOT_PROVIDED, "mapping name not provided"); + ResponseEntity response = handler.handleDynamicMappingException(exception, request); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertEquals("dynamicMapping.mappingNameNotProvided", response.getBody().getBusinessErrorCode()); + } +} diff --git a/src/test/java/org/gridsuite/mapping/server/service/client/filter/FilterClientTest.java b/src/test/java/org/gridsuite/mapping/server/service/client/filter/FilterClientTest.java index 198616da..6b21f803 100644 --- a/src/test/java/org/gridsuite/mapping/server/service/client/filter/FilterClientTest.java +++ b/src/test/java/org/gridsuite/mapping/server/service/client/filter/FilterClientTest.java @@ -19,11 +19,11 @@ import org.gridsuite.filter.utils.expertfilter.CombinatorType; import org.gridsuite.filter.utils.expertfilter.FieldType; import org.gridsuite.filter.utils.expertfilter.OperatorType; -import org.gridsuite.mapping.server.DynamicMappingException; import org.gridsuite.mapping.server.service.client.AbstractWireMockRestClientTest; import org.gridsuite.mapping.server.service.client.filter.impl.FilterClientImpl; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; @@ -34,7 +34,6 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.assertj.core.api.Assertions.catchThrowableOfType; -import static org.gridsuite.mapping.server.DynamicMappingException.Type.*; import static org.gridsuite.mapping.server.service.client.filter.FilterClient.*; import static org.gridsuite.mapping.server.service.client.utils.UrlUtils.buildEndPointUrl; import static org.gridsuite.mapping.server.utils.assertions.Assertions.assertThat; @@ -61,7 +60,7 @@ private List createFilterList() { loadRules.add(loadRule); CombinatorExpertRule loadRootCombinator = CombinatorExpertRule.builder() - .combinator(CombinatorType.AND).rules(loadRules).build(); + .combinator(CombinatorType.AND).rules(loadRules).build(); ExpertFilter loadExpertFilter = new ExpertFilter(UUID.randomUUID(), null, EquipmentType.LOAD, loadRootCombinator); @@ -72,7 +71,7 @@ private List createFilterList() { generatorRules.add(generatorRule); CombinatorExpertRule generatorRootCombinator = CombinatorExpertRule.builder() - .combinator(CombinatorType.AND).rules(generatorRules).build(); + .combinator(CombinatorType.AND).rules(generatorRules).build(); ExpertFilter generatorExpertFilter = new ExpertFilter(UUID.randomUUID(), null, EquipmentType.GENERATOR, generatorRootCombinator); @@ -133,15 +132,9 @@ public void testCreateFiltersGivenException() throws JsonProcessingException { .withBody(ERROR_MESSAGE_JSON) )); - DynamicMappingException exception = catchThrowableOfType( - () -> filterClient.createFilters(filtersToCreate), - DynamicMappingException.class); - + HttpServerErrorException exception = catchThrowableOfType(HttpServerErrorException.class, () -> filterClient.createFilters(filtersToCreate)); // check result - assertThat(exception.getType()) - .isEqualTo(CREATE_FILTER_ERROR); - assertThat(exception.getMessage()) - .isEqualTo(ERROR_MESSAGE); + assertThat(exception.getMessage()).contains(ERROR_MESSAGE); } public void testUpdateFilters() throws JsonProcessingException { @@ -182,15 +175,10 @@ public void testUpdateFiltersGivenException() throws JsonProcessingException { .withBody(ERROR_MESSAGE_JSON) )); - DynamicMappingException exception = catchThrowableOfType( - () -> filterClient.updateFilters(filtersToUpdate), - DynamicMappingException.class); - + HttpServerErrorException exception = catchThrowableOfType(HttpServerErrorException.class, () -> filterClient.updateFilters(filtersToUpdate)); // check result - assertThat(exception.getType()) - .isEqualTo(UPDATE_FILTER_ERROR); assertThat(exception.getMessage()) - .isEqualTo(ERROR_MESSAGE); + .contains(ERROR_MESSAGE); } @Test @@ -230,15 +218,9 @@ public void testDuplicateFiltersGivenException() throws JsonProcessingException .withBody(ERROR_MESSAGE_JSON) )); - DynamicMappingException exception = catchThrowableOfType( - () -> filterClient.duplicateFilters(sourceUuids), - DynamicMappingException.class); - + HttpServerErrorException exception = catchThrowableOfType(HttpServerErrorException.class, () -> filterClient.duplicateFilters(sourceUuids)); // check result - assertThat(exception.getType()) - .isEqualTo(DUPLICATE_FILTER_ERROR); - assertThat(exception.getMessage()) - .isEqualTo(ERROR_MESSAGE); + assertThat(exception.getMessage()).contains(ERROR_MESSAGE); } @Test @@ -269,15 +251,9 @@ public void testDeleteFiltersGivenException() throws JsonProcessingException { .willReturn(WireMock.serverError() .withBody(ERROR_MESSAGE_JSON))); - DynamicMappingException exception = catchThrowableOfType( - () -> filterClient.deleteFilters(sourceUuids), - DynamicMappingException.class); - + HttpServerErrorException exception = catchThrowableOfType(HttpServerErrorException.class, () -> filterClient.deleteFilters(sourceUuids)); // check result - assertThat(exception.getType()) - .isEqualTo(DELETE_FILTER_ERROR); - assertThat(exception.getMessage()) - .isEqualTo(ERROR_MESSAGE); + assertThat(exception.getMessage()).contains(ERROR_MESSAGE); } @Test @@ -318,14 +294,8 @@ public void testGetFiltersGivenException() { .withBody(ERROR_MESSAGE_JSON) )); - DynamicMappingException exception = catchThrowableOfType( - () -> filterClient.getFilters(uuids), - DynamicMappingException.class); - + HttpServerErrorException exception = catchThrowableOfType(HttpServerErrorException.class, () -> filterClient.getFilters(uuids)); // check result - assertThat(exception.getType()) - .isEqualTo(GET_FILTER_ERROR); - assertThat(exception.getMessage()) - .isEqualTo(ERROR_MESSAGE); + assertThat(exception.getMessage()).contains(ERROR_MESSAGE); } }