Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build parameters wrong condition #154

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
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,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<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);
}
}
}
Loading