Skip to content

Commit

Permalink
adapt the RaoRequest to the new builder pattern (#166)
Browse files Browse the repository at this point in the history
* adapt the RaoRequest to the new builder pattern

* rao-runner : 1.18.0 -> 1.18.1

* add tests for the RaoRunnerService

* add tests for the RaoRunnerService

* add tests for the RaoRunnerService

* Fix test

---------

Co-authored-by: Theo Pascoli <theo.pascoli@rte-france.com>
Co-authored-by: vbochetRTE <vincent.bochet@rte-france.com>
  • Loading branch information
3 people authored Nov 24, 2023
1 parent 8ce32d1 commit ddd0577
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ public RaoResponse run(String id, String networkPresignedUrl, String cracInJsonF
}

private RaoRequest buildRaoRequest(String id, String networkPresignedUrl, String cracUrl, String raoParametersUrl, String artifactDestinationPath) {

return new RaoRequest(id, networkPresignedUrl, cracUrl, raoParametersUrl, artifactDestinationPath);
return new RaoRequest.RaoRequestBuilder()
.withId(id)
.withNetworkFileUrl(networkPresignedUrl)
.withCracFileUrl(cracUrl)
.withRaoParametersFileUrl(raoParametersUrl)
.withResultsDestination(artifactDestinationPath)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2022, 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 com.farao_community.farao.cse.export_runner.app.services;

import com.farao_community.farao.cse.runner.api.exception.CseInternalException;
import com.farao_community.farao.rao_runner.api.resource.RaoRequest;
import com.farao_community.farao.rao_runner.api.resource.RaoResponse;
import com.farao_community.farao.rao_runner.starter.RaoRunnerClient;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* @author Theo Pascoli {@literal <theo.pascoli at rte-france.com>}
*/
@SpringBootTest
class RaoRunnerServiceTest {

@Mock
private RaoRunnerClient raoRunnerClient;

@InjectMocks
private RaoRunnerService raoRunnerService;

private final String id = "testId";
private final String networkPresignedUrl = "http://network.url";
private final String cracInJsonFormatUrl = "http://crac.url";
private final String raoParametersUrl = "http://parameters.url";
private final String artifactDestinationPath = "/path/to/artifact";

@Test
void testRunSuccessful() throws CseInternalException {
RaoResponse expectedResponse = new RaoResponse.RaoResponseBuilder().withId("id").build(); // Assuming RaoResponse is a valid response type

when(raoRunnerClient.runRao(any(RaoRequest.class))).thenReturn(expectedResponse);

RaoResponse actualResponse = raoRunnerService.run(id, networkPresignedUrl, cracInJsonFormatUrl, raoParametersUrl, artifactDestinationPath);

assertEquals(expectedResponse, actualResponse);
}

@Test
void testRunThrowsCseInternalException() {
when(raoRunnerClient.runRao(any())).thenThrow(new RuntimeException("Test exception"));

Exception exception = assertThrows(CseInternalException.class, () -> {
raoRunnerService.run(id, networkPresignedUrl, cracInJsonFormatUrl, raoParametersUrl, artifactDestinationPath);
});

String expectedMessage = "RAO run failed";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
void testRaoRequestValues() {
RaoRequest raoRequest = new RaoRequest.RaoRequestBuilder()
.withId(id)
.withNetworkFileUrl(networkPresignedUrl)
.withCracFileUrl(cracInJsonFormatUrl)
.withRaoParametersFileUrl(raoParametersUrl)
.withResultsDestination(artifactDestinationPath)
.build();

assertEquals("testId", raoRequest.getId());
assertEquals("http://network.url", raoRequest.getNetworkFileUrl());
assertEquals("http://crac.url", raoRequest.getCracFileUrl());
assertEquals("http://parameters.url", raoRequest.getRaoParametersFileUrl());
assertEquals("/path/to/artifact", raoRequest.getResultsDestination().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
package com.farao_community.farao.cse.import_runner.app.dichotomy;

import com.farao_community.farao.commons.Unit;
import com.farao_community.farao.cse.import_runner.app.services.ForcedPrasHandler;
import com.farao_community.farao.cse.import_runner.app.util.FlowEvaluator;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.farao_community.farao.cse.import_runner.app.services.FileExporter;
import com.farao_community.farao.cse.import_runner.app.services.FileImporter;
import com.farao_community.farao.cse.import_runner.app.services.ForcedPrasHandler;
import com.farao_community.farao.cse.import_runner.app.util.FlowEvaluator;
import com.farao_community.farao.cse.import_runner.app.util.MinioStorageHelper;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.farao_community.farao.data.crac_api.Crac;
import com.farao_community.farao.data.crac_api.network_action.NetworkAction;
import com.farao_community.farao.data.rao_result_api.RaoResult;
Expand All @@ -32,7 +32,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Joris Mancini {@literal <joris.mancini at rte-france.com>}
Expand Down Expand Up @@ -87,7 +86,7 @@ public DichotomyStepResult<DichotomyRaoResponse> validateNetwork(Network network
try {
Crac crac = fileImporter.importCracFromJson(cracUrl);
List<String> appliedRemedialActionInPreviousStep = lastDichotomyStepResult != null && lastDichotomyStepResult.getRaoResult() != null ? lastDichotomyStepResult.getRaoResult().getActivatedNetworkActionsDuringState(crac.getPreventiveState())
.stream().map(NetworkAction::getId).collect(Collectors.toList()) : Collections.emptyList();
.stream().map(NetworkAction::getId).toList() : Collections.emptyList();
RaoRequest raoRequest = buildRaoRequest(networkPresignedUrl, baseDirPathForCurrentStep, appliedRemedialActionInPreviousStep);
// We don't stop computation even if there are no applied RAs, because we cannot be sure in the case where
// the RAs are applicable on constraint, that it won't be applicable later on in the dichotomy (higher index)
Expand Down Expand Up @@ -124,13 +123,25 @@ private Set<String> applyForcedPras(Crac crac, Network network) {
}
}

private RaoRequest buildRaoRequest(String networkPreSignedUrl, String baseDirPathForCurrentStep, List<String> appliedRemedialActionInPreviousStep) {
if (appliedRemedialActionInPreviousStep.isEmpty() || appliedRemedialActionInPreviousStep.size() == 1) {
return new RaoRequest(requestId, networkPreSignedUrl, cracUrl, raoParametersUrl, baseDirPathForCurrentStep);
public RaoRequest buildRaoRequest(String networkPreSignedUrl, String baseDirPathForCurrentStep, List<String> appliedRemedialActionInPreviousStep) {
RaoRequest.RaoRequestBuilder builder = new RaoRequest.RaoRequestBuilder()
.withId(requestId)
.withNetworkFileUrl(networkPreSignedUrl)
.withCracFileUrl(cracUrl)
.withResultsDestination(baseDirPathForCurrentStep);

if (appliedRemedialActionInPreviousStep.size() == 1) {
builder.withRaoParametersFileUrl(this.raoParametersUrl);
} else {
String raoParametersWithAppliedRemedialActionInPreviousStepUrl = fileExporter.saveRaoParameters(baseDirPathForCurrentStep, appliedRemedialActionInPreviousStep, processTargetDateTime, processType, isImportEcProcess);
return new RaoRequest(requestId, networkPreSignedUrl, cracUrl, raoParametersWithAppliedRemedialActionInPreviousStepUrl, baseDirPathForCurrentStep);
String raoParametersWithAppliedRemedialActionInPreviousStepUrl = saveRaoParametersIfNeeded(baseDirPathForCurrentStep, appliedRemedialActionInPreviousStep);
builder.withRaoParametersFileUrl(raoParametersWithAppliedRemedialActionInPreviousStepUrl);
}

return builder.build();
}

private String saveRaoParametersIfNeeded(String baseDirPathForCurrentStep, List<String> appliedRemedialActionInPreviousStep) {
return fileExporter.saveRaoParameters(baseDirPathForCurrentStep, appliedRemedialActionInPreviousStep, processTargetDateTime, processType, isImportEcProcess);
}

private String generateBaseDirPathFromScaledNetwork(Network network) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, 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 com.farao_community.farao.cse.import_runner.app.dichotomy;

import com.farao_community.farao.cse.import_runner.app.services.FileExporter;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.farao_community.farao.rao_runner.api.resource.RaoRequest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
* @author Theo Pascoli {@literal <theo.pascoli at rte-france.com>}
*/
@SpringBootTest
class RaoRunnerValidatorTest {

private static final String NETWORK_PRE_SIGNED_URL = "http://network.url";
private static final String BASE_DIR_PATH = "/base/dir/path";
private static final String RAO_PARAMETERS_URL = "http://parameters.url";
private static final String REQUEST_ID = "requestId";
private static final String CRAC_URL = "http://crac.url";

@MockBean
private FileExporter fileExporter;

@Test
void buildRaoRequestWithEmptyPreviousActionsShouldNotSaveParameters() {
List<String> previousActions = Collections.singletonList("Action1");

RaoRunnerValidator raoRunnerValidator = new RaoRunnerValidator(ProcessType.D2CC, REQUEST_ID, null, CRAC_URL, RAO_PARAMETERS_URL, null, null, null, null, null, false);
RaoRequest result = raoRunnerValidator.buildRaoRequest(NETWORK_PRE_SIGNED_URL, BASE_DIR_PATH, previousActions);

verify(fileExporter, never()).saveRaoParameters(anyString(), anyList(), any(), any(), anyBoolean());
assertNotNull(result);
assertEquals(RAO_PARAMETERS_URL, result.getRaoParametersFileUrl());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<!-- BUSINESS DEPENDENCIES -->
<farao.dependencies.version>1.22.0</farao.dependencies.version>
<farao.dichotomy.version>4.14.0</farao.dichotomy.version>
<gridcapa.rao.runner.version>1.17.2</gridcapa.rao.runner.version>
<gridcapa.rao.runner.version>1.18.1</gridcapa.rao.runner.version>
<gridcapa.task-manager.version>1.21.0</gridcapa.task-manager.version>
<gridcapa.starter.minio.adapter.version>1.1.0</gridcapa.starter.minio.adapter.version>
</properties>
Expand Down

0 comments on commit ddd0577

Please sign in to comment.