Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
<version>1.6.0-SNAPSHOT</version>
</dependency>

<!-- Runtime dependencies -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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.shortcircuit.server;

import com.powsybl.ws.commons.error.ServerNameProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@Component
public class PropertyServerNameProvider implements ServerNameProvider {
private final String name;

public PropertyServerNameProvider(@Value("${spring.application.name:shortcircuit-server}") String name) {
this.name = name;
}

@Override
public String serverName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,41 @@

package org.gridsuite.shortcircuit.server;

import com.powsybl.ws.commons.error.AbstractBaseRestExceptionHandler;
import com.powsybl.ws.commons.error.AbstractBusinessException;
import com.powsybl.ws.commons.error.BusinessErrorCode;
import com.powsybl.ws.commons.error.ServerNameProvider;
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;

import static org.gridsuite.computation.ComputationBusinessErrorCode.*;
import static org.gridsuite.shortcircuit.server.ShortcircuitBusinessErrorCode.*;

/**
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
*/
@ControllerAdvice
public class RestResponseEntityExceptionHandler {
@ExceptionHandler(ShortCircuitException.class)
protected ResponseEntity<Object> handleShortCircuitException(ShortCircuitException exception) {
return switch (exception.getType()) {
case RESULT_NOT_FOUND -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.getMessage());
case INVALID_EXPORT_PARAMS -> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getMessage());
case BUS_OUT_OF_VOLTAGE, FILE_EXPORT_ERROR, MISSING_EXTENSION_DATA, INCONSISTENT_VOLTAGE_LEVELS ->
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(exception.getMessage());
public class RestResponseEntityExceptionHandler extends AbstractBaseRestExceptionHandler<AbstractBusinessException, BusinessErrorCode> {

protected RestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider) {
super(serverNameProvider);
}

@Override
protected @NonNull BusinessErrorCode getBusinessCode(AbstractBusinessException e) {
return e.getBusinessErrorCode();
}

@Override
protected HttpStatus mapStatus(BusinessErrorCode businessErrorCode) {
return switch (businessErrorCode) {
case RESULT_NOT_FOUND, NETWORK_NOT_FOUND, PARAMETERS_NOT_FOUND -> HttpStatus.NOT_FOUND;
case INVALID_FILTER_FORMAT,
INVALID_SORT_FORMAT,
INVALID_FILTER,
INVALID_EXPORT_PARAMS -> HttpStatus.BAD_REQUEST;
default -> HttpStatus.INTERNAL_SERVER_ERROR;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.gridsuite.shortcircuit.server;

import com.powsybl.ws.commons.error.AbstractBusinessException;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

import java.util.Objects;
Expand All @@ -14,27 +16,18 @@
* @author David SARTORI <david.sartori_externe at rte-france.com>
*/
@Getter
public class ShortCircuitException extends RuntimeException {
public class ShortCircuitException extends AbstractBusinessException {

public enum Type {
BUS_OUT_OF_VOLTAGE,
RESULT_NOT_FOUND,
INVALID_EXPORT_PARAMS,
FILE_EXPORT_ERROR,
MISSING_EXTENSION_DATA,
INCONSISTENT_VOLTAGE_LEVELS
}

private final Type type;
private final ShortcircuitBusinessErrorCode errorCode;

public ShortCircuitException(Type type) {
super(Objects.requireNonNull(type.name()));
this.type = type;
public ShortCircuitException(ShortcircuitBusinessErrorCode errorCode, String message) {
super(Objects.requireNonNull(message, "message must not be null"));
this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null");
}

public ShortCircuitException(Type type, String message) {
super(message);
this.type = type;
@NotNull
@Override
public ShortcircuitBusinessErrorCode getBusinessErrorCode() {
return errorCode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.gridsuite.shortcircuit.server;

import com.powsybl.ws.commons.error.BusinessErrorCode;

public enum ShortcircuitBusinessErrorCode implements BusinessErrorCode {
BUS_OUT_OF_VOLTAGE("shortcircuit.busOutOfVoltage"),
INVALID_EXPORT_PARAMS("shortcircuit.invalidExportParams"),
MISSING_EXTENSION_DATA("shortcircuit.missingExtensionData"),
INCONSISTENT_VOLTAGE_LEVELS("shortcircuit.inconsistentVoltageLevels"),;

private final String code;

ShortcircuitBusinessErrorCode(String code) {
this.code = code;
}

public String value() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.computation.ComputationException;
import org.gridsuite.computation.dto.GlobalFilter;
import org.gridsuite.computation.dto.ResourceFilterDTO;
import org.gridsuite.computation.s3.ComputationS3Service;
Expand Down Expand Up @@ -47,8 +48,10 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.gridsuite.computation.ComputationBusinessErrorCode.FILE_EXPORT_ERROR;
import static org.gridsuite.computation.ComputationBusinessErrorCode.RESULT_NOT_FOUND;
import static org.gridsuite.computation.utils.FilterUtils.fromStringFiltersToDTO;
import static org.gridsuite.shortcircuit.server.ShortCircuitException.Type.*;
import static org.gridsuite.shortcircuit.server.ShortcircuitBusinessErrorCode.INVALID_EXPORT_PARAMS;

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
Expand Down Expand Up @@ -94,8 +97,8 @@ public UUID runAndSaveResult(UUID networkUuid, String variantId, String receiver
parameters.setWithFortescueResult(StringUtils.isNotBlank(busId));
parameters.setDetailedReport(false);
return runAndSaveResult(new ShortCircuitRunContext(networkUuid, variantId, receiver, parameters, reportUuid, reporterId, reportType, userId,
"default-provider", // TODO : replace with null when fix in powsybl-ws-commons will handle null provider
busId, debug));
"default-provider", // TODO : replace with null when fix in powsybl-ws-commons will handle null provider
busId, debug));
}

@Override
Expand Down Expand Up @@ -157,37 +160,37 @@ private static FeederResult fromEntity(FeederResultEntity feederResultEntity) {
private static ShortCircuitParametersEntity toEntity(ShortCircuitParametersInfos parametersInfos) {
final ShortCircuitParameters parameters = parametersInfos.parameters();
return new ShortCircuitParametersEntity(
parameters.isWithLimitViolations(),
parameters.isWithVoltageResult(),
parameters.isWithFeederResult(),
parameters.getStudyType(),
parameters.getMinVoltageDropProportionalThreshold(),
parametersInfos.predefinedParameters(),
parameters.isWithLoads(),
parameters.isWithShuntCompensators(),
parameters.isWithVSCConverterStations(),
parameters.isWithNeutralPosition(),
parameters.getInitialVoltageProfileMode()
parameters.isWithLimitViolations(),
parameters.isWithVoltageResult(),
parameters.isWithFeederResult(),
parameters.getStudyType(),
parameters.getMinVoltageDropProportionalThreshold(),
parametersInfos.predefinedParameters(),
parameters.isWithLoads(),
parameters.isWithShuntCompensators(),
parameters.isWithVSCConverterStations(),
parameters.isWithNeutralPosition(),
parameters.getInitialVoltageProfileMode()
);
}

private static ShortCircuitParametersInfos fromEntity(ShortCircuitParametersEntity entity) {
Objects.requireNonNull(entity);
return new ShortCircuitParametersInfos(
entity.getPredefinedParameters(),
new ShortCircuitParameters()
.setStudyType(entity.getStudyType())
.setMinVoltageDropProportionalThreshold(entity.getMinVoltageDropProportionalThreshold())
.setWithFeederResult(entity.isWithFeederResult())
.setWithLimitViolations(entity.isWithLimitViolations())
.setWithVoltageResult(entity.isWithVoltageResult())
.setWithLoads(entity.isWithLoads())
.setWithShuntCompensators(entity.isWithShuntCompensators())
.setWithVSCConverterStations(entity.isWithVscConverterStations())
.setWithNeutralPosition(entity.isWithNeutralPosition())
.setInitialVoltageProfileMode(entity.getInitialVoltageProfileMode())
// the voltageRanges is not taken into account when initialVoltageProfileMode=NOMINAL
.setVoltageRanges(InitialVoltageProfileMode.CONFIGURED.equals(entity.getInitialVoltageProfileMode()) ? CEI909_VOLTAGE_PROFILE : null)
entity.getPredefinedParameters(),
new ShortCircuitParameters()
.setStudyType(entity.getStudyType())
.setMinVoltageDropProportionalThreshold(entity.getMinVoltageDropProportionalThreshold())
.setWithFeederResult(entity.isWithFeederResult())
.setWithLimitViolations(entity.isWithLimitViolations())
.setWithVoltageResult(entity.isWithVoltageResult())
.setWithLoads(entity.isWithLoads())
.setWithShuntCompensators(entity.isWithShuntCompensators())
.setWithVSCConverterStations(entity.isWithVscConverterStations())
.setWithNeutralPosition(entity.isWithNeutralPosition())
.setInitialVoltageProfileMode(entity.getInitialVoltageProfileMode())
// the voltageRanges is not taken into account when initialVoltageProfileMode=NOMINAL
.setVoltageRanges(InitialVoltageProfileMode.CONFIGURED.equals(entity.getInitialVoltageProfileMode()) ? CEI909_VOLTAGE_PROFILE : null)
);
}

Expand Down Expand Up @@ -291,13 +294,13 @@ public static byte[] exportToCsv(ShortCircuitAnalysisResult result, List<String>
csvWriter.close();
return outputStream.toByteArray();
} catch (IOException e) {
throw new ShortCircuitException(FILE_EXPORT_ERROR, e.getMessage());
throw new ComputationException(FILE_EXPORT_ERROR, e.getMessage());
}
}

public byte[] getZippedCsvExportResult(UUID resultUuid, ShortCircuitAnalysisResult result, CsvTranslation csvTranslation) {
if (result == null) {
throw new ShortCircuitException(RESULT_NOT_FOUND, "The short circuit analysis result '" + resultUuid + "' does not exist");
throw new ComputationException(RESULT_NOT_FOUND, "The short circuit analysis result '" + resultUuid + "' does not exist");
}
if (Objects.isNull(csvTranslation) || Objects.isNull(csvTranslation.headersCsv()) || Objects.isNull(csvTranslation.enumValueTranslations())) {
throw new ShortCircuitException(INVALID_EXPORT_PARAMS, "Missing information to export short-circuit result as csv: file headers and enum translation must be provided");
Expand Down Expand Up @@ -433,9 +436,9 @@ public boolean deleteParameters(final UUID parametersUuid) {
@Transactional
public Optional<UUID> duplicateParameters(UUID sourceParametersUuid) {
return parametersRepository.findById(sourceParametersUuid)
.map(ShortCircuitParametersEntity::new)
.map(parametersRepository::save)
.map(ShortCircuitParametersEntity::getId);
.map(ShortCircuitParametersEntity::new)
.map(parametersRepository::save)
.map(ShortCircuitParametersEntity::getId);
}

public UUID createParameters(@Nullable final ShortCircuitParametersInfos parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.gridsuite.shortcircuit.server.ShortCircuitException.Type.*;
import static org.gridsuite.shortcircuit.server.ShortcircuitBusinessErrorCode.BUS_OUT_OF_VOLTAGE;
import static org.gridsuite.shortcircuit.server.ShortcircuitBusinessErrorCode.INCONSISTENT_VOLTAGE_LEVELS;
import static org.gridsuite.shortcircuit.server.ShortcircuitBusinessErrorCode.MISSING_EXTENSION_DATA;
import static org.gridsuite.shortcircuit.server.service.ShortCircuitResultContext.HEADER_BUS_ID;

/**
Expand Down Expand Up @@ -199,7 +201,7 @@ public void postRun(ShortCircuitRunContext runContext, AtomicReference<ReportNod

@Override
protected void handleNonCancellationException(AbstractResultContext<ShortCircuitRunContext> resultContext, Exception exception, AtomicReference<ReportNode> rootReporter) {
if (exception instanceof ShortCircuitException shortCircuitException && shortCircuitException.getType() == INCONSISTENT_VOLTAGE_LEVELS) {
if (exception instanceof ShortCircuitException shortCircuitException && shortCircuitException.getBusinessErrorCode() == INCONSISTENT_VOLTAGE_LEVELS) {
postRun(resultContext.getRunContext(), rootReporter, null);
sendResultMessage(resultContext, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.shortcircuit.server;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>}
*/
class PropertyServerNameProviderTest {

@Test
void returnsProvidedName() {
PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server");
assertThat(provider.serverName()).isEqualTo("custom-server");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@
import java.util.UUID;
import java.util.stream.Stream;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith({ MockitoExtension.class })
@WebMvcTest(controllers = { ShortCircuitParametersController.class })
@WebMvcTest(controllers = { ShortCircuitParametersController.class, PropertyServerNameProvider.class })
class ShortCircuitParametersControllerTest implements WithAssertions {
private final String defaultParametersJson;

Expand Down
Loading
Loading