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

Refactor Dynawaltz DSL unitary tests #173

Merged
merged 5 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,21 +6,6 @@
*/
package com.powsybl.dynawaltz.dsl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.dsl.DslException;
Expand All @@ -35,15 +20,24 @@
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static org.junit.Assert.*;

/**
* @author Marcos de Miguel <demiguelm at aia.es>
*/
public class DynaWaltzGroovyCurvesSupplierTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private FileSystem fileSystem;
private Network network;

Expand All @@ -52,10 +46,10 @@ public void setup() throws IOException {
fileSystem = Jimfs.newFileSystem(Configuration.unix());
network = EurostagTutorialExample1Factory.create();

Files.copy(getClass().getResourceAsStream("/curves.groovy"), fileSystem.getPath("/curves.groovy"));
Files.copy(getClass().getResourceAsStream("/curves_dynamicModelId_staticId.groovy"), fileSystem.getPath("/curves_dynamicModelId_staticId.groovy"));
Files.copy(getClass().getResourceAsStream("/curves_variable.groovy"), fileSystem.getPath("/curves_variable.groovy"));
Files.copy(getClass().getResourceAsStream("/curves_variables.groovy"), fileSystem.getPath("/curves_variables.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves.groovy")), fileSystem.getPath("/curves.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_dynamicModelId_staticId.groovy")), fileSystem.getPath("/curves_dynamicModelId_staticId.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_variable.groovy")), fileSystem.getPath("/curves_variable.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_variables.groovy")), fileSystem.getPath("/curves_variables.groovy"));
}

@After
Expand All @@ -65,58 +59,42 @@ public void tearDown() throws IOException {

@Test
public void test() {

List<CurveGroovyExtension> extensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(1, extensions.size());
assertTrue(extensions.get(0) instanceof DynaWaltzCurveGroovyExtension);

List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves.groovy"), extensions);

List<Curve> curves = supplier.get(network);
assertEquals(11, curves.size());
curves.forEach(this::validateCurve);
}

@Test
public void testModelIdStaticIdDefined() {

exception.expect(DslException.class);
exception.expectMessage("Both staticId and dynamicModelId are defined");

List<CurveGroovyExtension> extensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(1, extensions.size());
assertTrue(extensions.get(0) instanceof DynaWaltzCurveGroovyExtension);

List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_dynamicModelId_staticId.groovy"), extensions);
supplier.get(network);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("Both staticId and dynamicModelId are defined", exception.getMessage());
}

@Test
public void testVariableNotDefined() {

exception.expect(DslException.class);
exception.expectMessage("'variables' field is not set");

List<CurveGroovyExtension> extensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(1, extensions.size());
assertTrue(extensions.get(0) instanceof DynaWaltzCurveGroovyExtension);

List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_variable.groovy"), extensions);
supplier.get(network);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("'variables' field is not set", exception.getMessage());
}

@Test
public void testVariablesNotDefined() {
List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_variables.groovy"), extensions);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals("'variables' field is not set", exception.getMessage());
}

exception.expect(DslException.class);
exception.expectMessage("'variables' field is not set");

private List<CurveGroovyExtension> validateGroovyExtension() {
List<CurveGroovyExtension> extensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(1, extensions.size());
assertTrue(extensions.get(0) instanceof DynaWaltzCurveGroovyExtension);

CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath("/curves_variables.groovy"), extensions);
supplier.get(network);
return extensions;
}

private void validateCurve(Curve curve) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2020, 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.powsybl.dynawaltz.dsl.ieee;

import com.powsybl.computation.local.LocalCommandExecutor;
import com.powsybl.dynawaltz.DynaWaltzParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;

import javax.xml.transform.Source;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.powsybl.dynawaltz.xml.DynaWaltzConstants.*;
import static org.junit.Assert.assertFalse;

/**
* @author Marcos de Miguel <demiguelm at aia.es>
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
public class DynaWaltzLocalCommandExecutor implements LocalCommandExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(DynaWaltzLocalCommandExecutor.class);
private final FileSystem fileSystem;
private final String networkId;
private final DynaWaltzParameters dynaWaltzParameters;
private final String baseDirName;

public DynaWaltzLocalCommandExecutor(FileSystem fileSystem, String networkId, DynaWaltzParameters dynaWaltzParameters, String baseDir) {
this.fileSystem = Objects.requireNonNull(fileSystem);
this.networkId = Objects.requireNonNull(networkId);
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);
this.baseDirName = baseDir;
}

protected void validateInputs(Path workingDir) throws IOException {
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/powsybl_dynawaltz.xiidm"), Files.newInputStream(workingDir.resolve(NETWORK_FILENAME)));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/powsybl_dynawaltz.jobs"), Files.newInputStream(workingDir.resolve(JOBS_FILENAME)));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/powsybl_dynawaltz.dyd"), Files.newInputStream(workingDir.resolve(DYD_FILENAME)));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/models.par"), Files.newInputStream(workingDir.resolve(fileSystem.getPath(dynaWaltzParameters.getParametersFile()).getFileName().toString())));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/network.par"), Files.newInputStream(workingDir.resolve(fileSystem.getPath(dynaWaltzParameters.getNetwork().getParametersFile()).getFileName().toString())));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/solvers.par"), Files.newInputStream(workingDir.resolve(fileSystem.getPath(dynaWaltzParameters.getSolver().getParametersFile()).getFileName().toString())));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/" + networkId + ".par"), Files.newInputStream(workingDir.resolve(networkId + ".par")));
compareXml(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-inputs/powsybl_dynawaltz.crv"), Files.newInputStream(workingDir.resolve(CRV_FILENAME)));
}

protected void copyOutputs(Path workingDir) throws IOException {
Path output = Files.createDirectories(workingDir.resolve("outputs/curves").toAbsolutePath());
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/" + baseDirName + "/dynawaltz-outputs/curves.csv")), output.resolve("curves.csv"));
}

@Override
public int execute(String program, List<String> args, Path outFile, Path errFile, Path workingDir, Map<String, String> env) {
return execute(program, -1, args, outFile, errFile, workingDir, env);
}

@Override
public int execute(String program, long timeoutSeconds, List<String> args, Path outFile, Path errFile, Path workingDir, Map<String, String> env) {
try {
validateInputs(workingDir);
copyOutputs(workingDir);
} catch (Throwable throwable) {
LOGGER.error(throwable.toString(), throwable);
return -1;
}
return 0;
}

@Override
public void stop(Path workingDir) {
}

@Override
public void stopForcibly(Path workingDir) {
}

protected static void compareXml(InputStream expected, InputStream actual) {
Source control = Input.fromStream(expected).build();
Source test = Input.fromStream(actual).build();
Diff myDiff = DiffBuilder.compare(control).withTest(test).ignoreWhitespace().ignoreComments().build();
boolean hasDiff = myDiff.hasDifferences();
if (hasDiff) {
System.err.println(myDiff);
}
assertFalse(hasDiff);
}
}

This file was deleted.

Loading