From 55b13f807c695e408d1945f5b81a96118d833029 Mon Sep 17 00:00:00 2001 From: David BRAQUART Date: Wed, 8 Jan 2025 10:34:38 +0100 Subject: [PATCH 1/3] fix condition Signed-off-by: David BRAQUART --- .../gridsuite/loadflow/server/service/LoadFlowRunContext.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 Date: Wed, 8 Jan 2025 11:54:13 +0100 Subject: [PATCH 2/3] Add tests Signed-off-by: Ayoub LABIDI --- .../server/LoadFlowRunContextTest.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java new file mode 100644 index 00000000..76b19e29 --- /dev/null +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java @@ -0,0 +1,125 @@ +/** + * 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.loadflow.server; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.commons.config.PlatformConfig; +import com.powsybl.commons.extensions.Extension; +import com.powsybl.loadflow.LoadFlowParameters; +import com.powsybl.loadflow.LoadFlowProvider; +import org.gridsuite.loadflow.server.dto.parameters.LoadFlowParametersValues; +import org.gridsuite.loadflow.server.service.LoadFlowRunContext; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class LoadFlowRunContextTest { + + @Test + void testBuildParametersWithNullParameters() { + LoadFlowRunContext context = LoadFlowRunContext.builder() + .parameters(null) + .build(); + + LoadFlowParameters params = context.buildParameters(); + assertNotNull(params); + } + + @Test + void testBuildParametersWithCommonParameters() { + LoadFlowParameters commonParams = LoadFlowParameters.load(); + LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder() + .commonParameters(commonParams) + .build(); + + LoadFlowRunContext context = LoadFlowRunContext.builder() + .parameters(parametersValues) + .build(); + + LoadFlowParameters params = context.buildParameters(); + assertEquals(commonParams, params); + } + + @Test + void testBuildParametersWithSpecificParameters() { + LoadFlowParameters commonParams = LoadFlowParameters.load(); + Map 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); + } + } +} From 94a1d6767f33e53fdc1d797702f216355917f134 Mon Sep 17 00:00:00 2001 From: David BRAQUART Date: Wed, 8 Jan 2025 12:18:14 +0100 Subject: [PATCH 3/3] add 1 test Signed-off-by: David BRAQUART --- .../server/LoadFlowRunContextTest.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java index 76b19e29..f6165b23 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowRunContextTest.java @@ -19,6 +19,8 @@ import java.util.Collections; import java.util.Map; import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -26,12 +28,32 @@ class LoadFlowRunContextTest { @Test void testBuildParametersWithNullParameters() { + LoadFlowParameters defaultParams = LoadFlowParameters.load(); LoadFlowRunContext context = LoadFlowRunContext.builder() .parameters(null) .build(); LoadFlowParameters params = context.buildParameters(); - assertNotNull(params); + assertThat(params) + .usingRecursiveComparison() + .isEqualTo(defaultParams); + } + + @Test + void testBuildParametersWithNullCommonParameters() { + LoadFlowParameters defaultParams = LoadFlowParameters.load(); + LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder() + .commonParameters(null) + .build(); + + LoadFlowRunContext context = LoadFlowRunContext.builder() + .parameters(parametersValues) + .build(); + + LoadFlowParameters params = context.buildParameters(); + assertThat(params) + .usingRecursiveComparison() + .isEqualTo(defaultParams); } @Test