Skip to content

Commit

Permalink
Fix build parameters wrong condition (#154)
Browse files Browse the repository at this point in the history
Signed-off-by: David BRAQUART <david.braquart@rte-france.com>
Co-authored-by: Ayoub LABIDI <ayoub.labidi@protonmail.com>
  • Loading branch information
dbraquart and ayolab authored Jan 8, 2025
1 parent b87b62f commit 3656ca2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LoadFlowRunContext extends AbstractComputationRunContext<LoadFlowPa
private final UUID parametersUuid;

public LoadFlowParameters buildParameters() {
LoadFlowParameters params = getParameters() == null || getParameters().getSpecificParameters() == null ?
LoadFlowParameters params = getParameters() == null || getParameters().getCommonParameters() == null ?
LoadFlowParameters.load() : getParameters().getCommonParameters();
if (getParameters() == null || getParameters().getSpecificParameters() == null || getParameters().getSpecificParameters().isEmpty()) {
return params; // no specific LF params
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* 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.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class LoadFlowRunContextTest {

@Test
void testBuildParametersWithNullParameters() {
LoadFlowParameters defaultParams = LoadFlowParameters.load();
LoadFlowRunContext context = LoadFlowRunContext.builder()
.parameters(null)
.build();

LoadFlowParameters params = context.buildParameters();
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
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<String, String> specificParams = Map.of("key", "value");
LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder()
.commonParameters(commonParams)
.specificParameters(specificParams)
.build();

LoadFlowProvider provider = mock(LoadFlowProvider.class);
Extension<LoadFlowParameters> extension = mock(Extension.class);

try (MockedStatic<LoadFlowProvider> 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<String, String> specificParams = Map.of("key", "value");
LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder()
.commonParameters(commonParams)
.specificParameters(specificParams)
.build();

try (MockedStatic<LoadFlowProvider> 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<String, String> specificParams = Map.of("key", "value");
LoadFlowParametersValues parametersValues = LoadFlowParametersValues.builder()
.commonParameters(commonParams)
.specificParameters(specificParams)
.build();

LoadFlowProvider provider = mock(LoadFlowProvider.class);

try (MockedStatic<LoadFlowProvider> 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);
}
}
}

0 comments on commit 3656ca2

Please sign in to comment.