From 3656ca27b6a96720b50c0db24ac9aa73b62a07ac Mon Sep 17 00:00:00 2001 From: dbraquart <107846716+dbraquart@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:25:20 +0100 Subject: [PATCH] Fix build parameters wrong condition (#154) Signed-off-by: David BRAQUART Co-authored-by: Ayoub LABIDI --- .../server/service/LoadFlowRunContext.java | 2 +- .../server/LoadFlowRunContextTest.java | 147 ++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowRunContext.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowRunContext.java index cb1139f3..190e0b3b 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowRunContext.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowRunContext.java @@ -28,7 +28,7 @@ public class LoadFlowRunContext extends AbstractComputationRunContext specificParams = Map.of("key", "value"); + LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder() + .commonParameters(commonParams) + .specificParameters(specificParams) + .build(); + + LoadFlowProvider provider = mock(LoadFlowProvider.class); + Extension extension = mock(Extension.class); + + try (MockedStatic mockedProvider = Mockito.mockStatic(LoadFlowProvider.class)) { + mockedProvider.when(LoadFlowProvider::findAll).thenReturn(Collections.singletonList(provider)); + when(provider.getName()).thenReturn("provider"); + when(provider.loadSpecificParameters(any(PlatformConfig.class))).thenReturn(Optional.of(extension)); + + LoadFlowRunContext context = LoadFlowRunContext.builder() + .provider("provider") + .parameters(parametersValues) + .build(); + + LoadFlowParameters params = context.buildParameters(); + assertNotNull(params); + verify(provider).updateSpecificParameters(extension, specificParams); + } + } + + @Test + void testBuildParametersWithProviderNotFound() { + LoadFlowParameters commonParams = LoadFlowParameters.load(); + Map specificParams = Map.of("key", "value"); + LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder() + .commonParameters(commonParams) + .specificParameters(specificParams) + .build(); + + try (MockedStatic mockedProvider = Mockito.mockStatic(LoadFlowProvider.class)) { + mockedProvider.when(LoadFlowProvider::findAll).thenReturn(Collections.emptyList()); + + LoadFlowRunContext context = LoadFlowRunContext.builder() + .provider("provider") + .parameters(parametersValues) + .build(); + + assertThrows(PowsyblException.class, context::buildParameters); + } + } + + @Test + void testBuildParametersWithExtensionNotFound() { + LoadFlowParameters commonParams = LoadFlowParameters.load(); + Map specificParams = Map.of("key", "value"); + LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder() + .commonParameters(commonParams) + .specificParameters(specificParams) + .build(); + + LoadFlowProvider provider = mock(LoadFlowProvider.class); + + try (MockedStatic mockedProvider = Mockito.mockStatic(LoadFlowProvider.class)) { + mockedProvider.when(LoadFlowProvider::findAll).thenReturn(Collections.singletonList(provider)); + when(provider.getName()).thenReturn("provider"); + when(provider.loadSpecificParameters(any(PlatformConfig.class))).thenReturn(Optional.empty()); + + LoadFlowRunContext context = LoadFlowRunContext.builder() + .provider("provider") + .parameters(parametersValues) + .build(); + + assertThrows(PowsyblException.class, context::buildParameters); + } + } +}