From 1d01ed7d8d22fa3183ca8a7666a1573bf937d666 Mon Sep 17 00:00:00 2001 From: Franck LECUYER Date: Tue, 17 Dec 2024 16:15:33 +0100 Subject: [PATCH] Fix reactive threshold value when creating default parameters Fix pb when updating with null parameters Signed-off-by: Franck LECUYER --- .../VoltageInitParametersController.java | 2 +- .../VoltageInitParametersService.java | 20 +++++++++++++++---- .../server/VoltageInitParametersTest.java | 7 ++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/gridsuite/voltageinit/server/VoltageInitParametersController.java b/src/main/java/org/gridsuite/voltageinit/server/VoltageInitParametersController.java index 267a045..82a155e 100644 --- a/src/main/java/org/gridsuite/voltageinit/server/VoltageInitParametersController.java +++ b/src/main/java/org/gridsuite/voltageinit/server/VoltageInitParametersController.java @@ -82,7 +82,7 @@ public ResponseEntity> getAllParameters() { @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "parameters were updated")}) public ResponseEntity updateParameters( @Parameter(description = "parameters UUID") @PathVariable("uuid") UUID parametersUuid, - @RequestBody VoltageInitParametersInfos parametersInfos) { + @RequestBody(required = false) VoltageInitParametersInfos parametersInfos) { parametersService.updateParameters(parametersUuid, parametersInfos); return ResponseEntity.ok().build(); } diff --git a/src/main/java/org/gridsuite/voltageinit/server/service/parameters/VoltageInitParametersService.java b/src/main/java/org/gridsuite/voltageinit/server/service/parameters/VoltageInitParametersService.java index db06f8b..b9ca63f 100644 --- a/src/main/java/org/gridsuite/voltageinit/server/service/parameters/VoltageInitParametersService.java +++ b/src/main/java/org/gridsuite/voltageinit/server/service/parameters/VoltageInitParametersService.java @@ -59,9 +59,11 @@ public VoltageInitParametersService(VoltageInitParametersRepository voltageInitP } public UUID createParameters(VoltageInitParametersInfos parametersInfos) { - return voltageInitParametersRepository.save(parametersInfos != null - ? parametersInfos.toEntity() - : new VoltageInitParametersEntity()).getId(); + if (parametersInfos != null) { + return voltageInitParametersRepository.save(parametersInfos.toEntity()).getId(); + } else { + return voltageInitParametersRepository.save(new VoltageInitParametersEntity(getDefaultParametersInfos())).getId(); + } } public Optional duplicateParameters(UUID sourceParametersId) { @@ -94,13 +96,23 @@ public List getAllParameters() { @Transactional public void updateParameters(UUID parametersUuid, VoltageInitParametersInfos parametersInfos) { - voltageInitParametersRepository.findById(parametersUuid).orElseThrow().update(parametersInfos); + VoltageInitParametersEntity entity = voltageInitParametersRepository.findById(parametersUuid).orElseThrow(); + //if the parameters is null it means it's a reset to defaultValues + if (parametersInfos == null) { + entity.update(getDefaultParametersInfos()); + } else { + entity.update(parametersInfos); + } } public void deleteParameters(UUID parametersUuid) { voltageInitParametersRepository.deleteById(parametersUuid); } + private VoltageInitParametersInfos getDefaultParametersInfos() { + return VoltageInitParametersInfos.builder().reactiveSlacksThreshold(DEFAULT_REACTIVE_SLACKS_THRESHOLD).build(); + } + private Map resolveVoltageLevelLimits(VoltageInitRunContext context, List voltageLimits) { Map voltageLevelLimits = new HashMap<>(); //each voltage level is associated to a voltage limit setting diff --git a/src/test/java/org/gridsuite/voltageinit/server/VoltageInitParametersTest.java b/src/test/java/org/gridsuite/voltageinit/server/VoltageInitParametersTest.java index 280ee25..792c432 100644 --- a/src/test/java/org/gridsuite/voltageinit/server/VoltageInitParametersTest.java +++ b/src/test/java/org/gridsuite/voltageinit/server/VoltageInitParametersTest.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.UUID; +import static org.gridsuite.voltageinit.server.service.parameters.VoltageInitParametersService.DEFAULT_REACTIVE_SLACKS_THRESHOLD; import static org.gridsuite.voltageinit.utils.assertions.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -82,12 +83,12 @@ void testCreateDefault() throws Exception { mockMvc.perform(post(URI_PARAMETERS_BASE)).andExpect(status().isOk()).andReturn(); VoltageInitParametersInfos createdParameters = parametersRepository.findAll().get(0).toVoltageInitParametersInfos(); assertNotNull(createdParameters); - assertNull(createdParameters.getVoltageLimitsDefault()); + assertTrue(createdParameters.getVoltageLimitsDefault().isEmpty()); assertNull(createdParameters.getVariableShuntCompensators()); assertNull(createdParameters.getVariableTwoWindingsTransformers()); assertNull(createdParameters.getConstantQGenerators()); - assertNull(createdParameters.getVoltageLimitsModification()); - assertEquals(0., createdParameters.getReactiveSlacksThreshold()); + assertTrue(createdParameters.getVoltageLimitsModification().isEmpty()); + assertEquals(DEFAULT_REACTIVE_SLACKS_THRESHOLD, createdParameters.getReactiveSlacksThreshold()); assertEquals(0., createdParameters.getShuntCompensatorActivationThreshold()); assertFalse(createdParameters.isUpdateBusVoltage()); }