Skip to content

Commit

Permalink
fix cucumber tests
Browse files Browse the repository at this point in the history
Signed-off-by: CHEN Roxane <[email protected]>
  • Loading branch information
CHEN Roxane committed Feb 12, 2025
1 parent 595c45e commit 95a99b1
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import com.powsybl.openrao.data.crac.api.CracCreationContext;
import com.powsybl.openrao.data.crac.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.crac.api.parameters.JsonCracCreationParameters;
import com.powsybl.openrao.data.crac.io.cim.parameters.CimCracCreationParameters;
import com.powsybl.openrao.data.crac.io.csaprofiles.parameters.CsaCracCreationParameters;
import com.powsybl.openrao.data.crac.io.fbconstraint.parameters.FbConstraintCracCreationParameters;
import com.powsybl.openrao.data.glsk.virtual.hubs.GlskVirtualHubs;
import com.powsybl.openrao.data.raoresult.api.RaoResult;
import com.powsybl.openrao.data.refprog.referenceprogram.ReferenceProgram;
Expand All @@ -35,6 +38,7 @@
import org.apache.commons.lang3.tuple.Pair;

import java.io.*;
import java.time.OffsetDateTime;

import static com.powsybl.openrao.tests.utils.Helpers.*;

Expand Down Expand Up @@ -276,12 +280,25 @@ public static void loadData(String timestamp) throws IOException {
try {
cracCreationParametersInputStream = new BufferedInputStream(new FileInputStream(getFile(ccpToImport)));
cracCreationParameters = JsonCracCreationParameters.read(cracCreationParametersInputStream);

} catch (FileNotFoundException e) {
e.printStackTrace();
}

// Add timestamp manually if no crac creation parameters file were given explicitly
if (timestamp != null && cracCreationParametersPath == null) {
addTimestampToCracCreationParameters(cracFormat, timestamp, cracCreationParameters);
}

OffsetDateTime offsetDateTime;
if (cracCreationParametersPath != null) {
offsetDateTime = importTimestampFromCracCreationParameters(cracFormat, cracCreationParameters);
} else {
offsetDateTime = getOffsetDateTimeFromBrusselsTimestamp(timestamp);
}

// Crac
Pair<Crac, CracCreationContext> cracImportResult = importCrac(getFile(cracPath), network, timestamp, cracCreationParameters);
Pair<Crac, CracCreationContext> cracImportResult = importCrac(getFile(cracPath), network, cracCreationParameters);
crac = cracImportResult.getLeft();
cracCreationContext = cracImportResult.getRight();

Expand All @@ -299,17 +316,17 @@ public static void loadData(String timestamp) throws IOException {
// Loopflow GLSK
// only work with UCTE GLSK files
if (loopflowGlskPath != null) {
loopflowGlsks = importUcteGlskFile(getFile(loopflowGlskPath), timestamp, network);
loopflowGlsks = importUcteGlskFile(getFile(loopflowGlskPath), offsetDateTime, network);
}

// Monitoring GLSK
if (monitoringGlskPath != null) {
monitoringGlsks = importMonitoringGlskFile(getFile(monitoringGlskPath), timestamp, network);
monitoringGlsks = importMonitoringGlskFile(getFile(monitoringGlskPath), offsetDateTime, network);
}

// Reference program
if (refProgPath != null) {
referenceProgram = importRefProg(getFile(refProgPath), timestamp);
referenceProgram = importRefProg(getFile(refProgPath), offsetDateTime);
}

// RaoResult
Expand All @@ -330,6 +347,32 @@ public static void loadData(String timestamp) throws IOException {

}

private static OffsetDateTime importTimestampFromCracCreationParameters(String cracFormat, CracCreationParameters cracCreationParameters) {
if (cracFormat.equals("CimCrac")) {
return cracCreationParameters.getExtension(CimCracCreationParameters.class).getTimestamp();
} else if (cracFormat.equals("FlowBasedConstraintDocument")) {
return cracCreationParameters.getExtension(FbConstraintCracCreationParameters.class).getTimestamp();
} else if (cracFormat.equals("CsaCrac")) {
return cracCreationParameters.getExtension(CsaCracCreationParameters.class).getTimestamp();
} else {
return null;
}
}

private static void addTimestampToCracCreationParameters(String cracFormat, String timestamp, CracCreationParameters cracCreationParameters) {

if (cracFormat.equals("CimCrac")) {
cracCreationParameters.addExtension(CimCracCreationParameters.class, new CimCracCreationParameters());
cracCreationParameters.getExtension(CimCracCreationParameters.class).setTimestamp(getOffsetDateTimeFromBrusselsTimestamp(timestamp));
} else if (cracFormat.equals("FlowBasedConstraintDocument")) {
cracCreationParameters.addExtension(FbConstraintCracCreationParameters.class, new FbConstraintCracCreationParameters());
cracCreationParameters.getExtension(FbConstraintCracCreationParameters.class).setTimestamp(getOffsetDateTimeFromBrusselsTimestamp(timestamp));
} else if (cracFormat.equals("CsaCrac")) {
cracCreationParameters.addExtension(CsaCracCreationParameters.class, new CsaCracCreationParameters());
cracCreationParameters.getExtension(CsaCracCreationParameters.class).setTimestamp(getOffsetDateTimeFromBrusselsTimestamp(timestamp));
}
}

private static RaoParameters buildDefaultConfig() {
try (InputStream configStream = new FileInputStream(getFile(getResourcesPath().concat(DEFAULT_RAO_PARAMETERS_PATH)))) {
return JsonRaoParameters.read(configStream);
Expand Down
22 changes: 10 additions & 12 deletions tests/src/test/java/com/powsybl/openrao/tests/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public static Network importNetwork(File networkFile, boolean useRdfId) {
return Network.read(Paths.get(networkFile.toString()), LocalComputationManager.getDefault(), Suppliers.memoize(ImportConfig::load).get(), importParams);
}

public static Pair<Crac, CracCreationContext> importCrac(File cracFile, Network network, String timestamp, CracCreationParameters cracCreationParameters) throws IOException {
public static Pair<Crac, CracCreationContext> importCrac(File cracFile, Network network, CracCreationParameters cracCreationParameters) throws IOException {
if (cracFile.getName().endsWith(".json")) {
// for now, the only JSON format is the farao internal format
return Pair.of(importCracFromInternalFormat(cracFile, network), null);
} else {
CracCreationContext ccc = importCracFromNativeCrac(cracFile, network, timestamp, cracCreationParameters);
CracCreationContext ccc = importCracFromNativeCrac(cracFile, network, cracCreationParameters);
return Pair.of(ccc.getCrac(), ccc);
}
}
Expand All @@ -74,16 +74,15 @@ public static Crac importCracFromInternalFormat(File cracFile, Network network)
}
}

public static CracCreationContext importCracFromNativeCrac(File cracFile, Network network, String timestamp, CracCreationParameters cracCreationParameters) throws IOException {
public static CracCreationContext importCracFromNativeCrac(File cracFile, Network network, CracCreationParameters cracCreationParameters) throws IOException {
byte[] cracBytes = null;
try (InputStream cracInputStream = new BufferedInputStream(new FileInputStream(cracFile))) {
cracBytes = getBytesFromInputStream(cracInputStream);
} catch (IOException e) {
e.printStackTrace();
throw new OpenRaoException("Could not load CRAC file", e);
}
OffsetDateTime offsetDateTime = getOffsetDateTimeFromBrusselsTimestamp(timestamp);
CracCreationContext cracCreationContext = Crac.readWithContext(cracFile.getName(), new ByteArrayInputStream(cracBytes), network, offsetDateTime, cracCreationParameters);
CracCreationContext cracCreationContext = Crac.readWithContext(cracFile.getName(), new ByteArrayInputStream(cracBytes), network, cracCreationParameters);
// round-trip CRAC json export/import to test it implicitly
return roundTripOnCracCreationContext(cracCreationContext, network);
}
Expand Down Expand Up @@ -127,29 +126,29 @@ private static Crac roundTripOnCrac(Crac crac, Network network) throws IOExcepti
return Crac.read("crac.json", inputStream, network);
}

public static ZonalData<SensitivityVariableSet> importUcteGlskFile(File glskFile, String timestamp, Network network) throws IOException {
public static ZonalData<SensitivityVariableSet> importUcteGlskFile(File glskFile, OffsetDateTime timestamp, Network network) throws IOException {
InputStream inputStream = new FileInputStream(glskFile);
UcteGlskDocument ucteGlskDocument = UcteGlskDocument.importGlsk(inputStream);

Instant instant;
if (timestamp == null) {
instant = getStartInstantOfUcteGlsk(ucteGlskDocument);
} else {
instant = getOffsetDateTimeFromBrusselsTimestamp(timestamp).toInstant();
instant = timestamp.toInstant();
}

return ucteGlskDocument.getZonalGlsks(network, instant);
}

public static ZonalData<Scalable> importMonitoringGlskFile(File monitoringGlskFile, String timestamp, Network network) throws IOException {
public static ZonalData<Scalable> importMonitoringGlskFile(File monitoringGlskFile, OffsetDateTime timestamp, Network network) throws IOException {
InputStream inputStream = new FileInputStream(monitoringGlskFile);
CimGlskDocument cimGlskDocument = CimGlskDocument.importGlsk(inputStream);

Instant instant;
if (timestamp == null) {
instant = cimGlskDocument.getInstantStart();
} else {
instant = getOffsetDateTimeFromBrusselsTimestamp(timestamp).toInstant();
instant = timestamp.toInstant();
}

return cimGlskDocument.getZonalScalable(network, instant);
Expand All @@ -167,13 +166,12 @@ public static OffsetDateTime getOffsetDateTimeFromBrusselsTimestamp(String times
.toOffsetDateTime();
}

public static ReferenceProgram importRefProg(File refProgFile, String timestamp) throws IOException {
if (timestamp == null) {
public static ReferenceProgram importRefProg(File refProgFile, OffsetDateTime offsetDateTime) throws IOException {
if (offsetDateTime == null) {
throw new OpenRaoException("A timestamp should be provided in order to import the refProg file.");
}

InputStream refProgInputStream = new FileInputStream(refProgFile);
OffsetDateTime offsetDateTime = getOffsetDateTimeFromBrusselsTimestamp(timestamp);
return RefProgImporter.importRefProg(refProgInputStream, offsetDateTime);
}

Expand Down
Loading

0 comments on commit 95a99b1

Please sign in to comment.