Skip to content

Commit

Permalink
Refactor json tests to fix code smell.
Browse files Browse the repository at this point in the history
Signed-off-by: parvy <[email protected]>
  • Loading branch information
p-arvy committed Apr 17, 2024
1 parent 599e59d commit b49014d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class OpenReacJsonModuleTest {

@Test
void test() throws IOException {
void testOpenReacParametersLists() throws IOException {
ObjectMapper objectMapper = JsonUtil.createObjectMapper()
.registerModule(new OpenReactJsonModule());
OpenReacParameters parameters = new OpenReacParameters();
Expand All @@ -47,17 +47,41 @@ void test() throws IOException {
parameters.addConstantQGenerators(List.of("g1", "g2"));
parameters.addVariableTwoWindingsTransformers(List.of("tr1"));
parameters.addVariableShuntCompensators(List.of("sc1", "sc2"));
parameters.setReactiveSlackBusesMode(ReactiveSlackBusesMode.CONFIGURED);
parameters.addConfiguredReactiveSlackBuses(List.of("bus1", "bus2"));

String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parameters);
ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/parametersLists.json")), json);

OpenReacParameters parameters2 = objectMapper.readValue(json, OpenReacParameters.class);
// List of voltage limit overrides
List<VoltageLimitOverride> vloList2 = new ArrayList<>();
vloList2.add(new VoltageLimitOverride("foo", VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT, true, -1));
vloList2.add(new VoltageLimitOverride("foo", VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT, true, 2));
vloList2.add(new VoltageLimitOverride("bar", VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT, false, 20));
vloList2.add(new VoltageLimitOverride("bar", VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT, false, 26));

assertEquals(vloList2, parameters2.getSpecificVoltageLimits());
assertEquals(List.of("g1", "g2"), parameters2.getConstantQGenerators());
assertEquals(List.of("tr1"), parameters2.getVariableTwoWindingsTransformers());
assertEquals(List.of("sc1", "sc2"), parameters2.getVariableShuntCompensators());
assertEquals(ReactiveSlackBusesMode.CONFIGURED, parameters2.getReactiveSlackBusesMode());
assertEquals(List.of("bus1", "bus2"), parameters2.getConfiguredReactiveSlackBuses());
}

@Test
void testOpenReacParametersThresholds() throws IOException {
ObjectMapper objectMapper = JsonUtil.createObjectMapper()
.registerModule(new OpenReactJsonModule());
OpenReacParameters parameters = new OpenReacParameters();

// modify open reac parameters
parameters.setObjectiveDistance(5);
parameters.setLogLevelAmpl(OpenReacAmplLogLevel.WARNING);
parameters.setLogLevelSolver(OpenReacSolverLogLevel.NOTHING);
parameters.setMinPlausibleLowVoltageLimit(0.755);
parameters.setMaxPlausibleHighVoltageLimit(1.236);
parameters.setReactiveSlackBusesMode(ReactiveSlackBusesMode.CONFIGURED);
parameters.setDefaultVariableScalingFactor(0.756);
parameters.setDefaultConstraintScalingFactor(0.888);
parameters.setReactiveSlackVariableScalingFactor(1e-2);
parameters.setTwoWindingTransformerRatioVariableScalingFactor(0.005);
parameters.addConfiguredReactiveSlackBuses(List.of("bus1", "bus2"));
parameters.setReactiveSlackBusesMode(ReactiveSlackBusesMode.ALL);
parameters.setAlphaCoefficient(0.56);
parameters.setMinPlausibleActivePowerThreshold(0.5);
parameters.setLowImpedanceThreshold(1e-5);
Expand All @@ -68,29 +92,21 @@ void test() throws IOException {
parameters.setHighActivePowerDefaultLimit(1144.);
parameters.setDefaultQmaxPmaxRatio(0.4);
parameters.setDefaultMinimalQPRange(1.1);
parameters.setDefaultVariableScalingFactor(0.756);
parameters.setDefaultConstraintScalingFactor(0.888);
parameters.setReactiveSlackVariableScalingFactor(1e-2);
parameters.setTwoWindingTransformerRatioVariableScalingFactor(0.005);

String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parameters);
ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/parameters.json")), json);

ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/parametersThresholds.json")), json);
OpenReacParameters parameters2 = objectMapper.readValue(json, OpenReacParameters.class);
// List of voltage limit overrides
List<VoltageLimitOverride> vloList2 = new ArrayList<>();
vloList2.add(new VoltageLimitOverride("foo", VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT, true, -1));
vloList2.add(new VoltageLimitOverride("foo", VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT, true, 2));
vloList2.add(new VoltageLimitOverride("bar", VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT, false, 20));
vloList2.add(new VoltageLimitOverride("bar", VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT, false, 26));

assertEquals(vloList2, parameters2.getSpecificVoltageLimits());
assertEquals(List.of("g1", "g2"), parameters2.getConstantQGenerators());
assertEquals(List.of("tr1"), parameters2.getVariableTwoWindingsTransformers());
assertEquals(List.of("bus1", "bus2"), parameters2.getConfiguredReactiveSlackBuses());
assertEquals(List.of("sc1", "sc2"), parameters2.getVariableShuntCompensators());
assertEquals(5, parameters2.getObjectiveDistance());
assertEquals(OpenReacAmplLogLevel.WARNING, parameters2.getLogLevelAmpl());
assertEquals(OpenReacSolverLogLevel.NOTHING, parameters2.getLogLevelSolver());
assertEquals(0.755, parameters2.getMinPlausibleLowVoltageLimit());
assertEquals(1.236, parameters2.getMaxPlausibleHighVoltageLimit());
assertEquals(ReactiveSlackBusesMode.CONFIGURED, parameters2.getReactiveSlackBusesMode());
assertEquals(ReactiveSlackBusesMode.ALL, parameters2.getReactiveSlackBusesMode());
assertEquals(OpenReacOptimisationObjective.MIN_GENERATION, parameters2.getObjective());
assertEquals(0.56, parameters2.getAlphaCoefficient());
assertEquals(0.5, parameters2.getMinPlausibleActivePowerThreshold());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@
"variableShuntCompensators" : [ "sc1", "sc2" ],
"configuredReactiveSlackBuses" : [ "bus1", "bus2" ],
"objective" : "MIN_GENERATION",
"objectiveDistance" : 5.0,
"logLevelAmpl" : "WARNING",
"logLevelSolver" : "NOTHING",
"minPlausibleLowVoltageLimit" : 0.755,
"maxPlausibleHighVoltageLimit" : 1.236,
"logLevelAmpl" : "INFO",
"logLevelSolver" : "EVERYTHING",
"minPlausibleLowVoltageLimit" : 0.5,
"maxPlausibleHighVoltageLimit" : 1.5,
"reactiveSlackBusesMode" : "CONFIGURED",
"alphaCoefficient" : 0.56,
"minPlausibleActivePowerThreshold" : 0.5,
"lowImpedanceThreshold" : 1.0E-5,
"minNominalVoltageIgnoredBus" : 10.0,
"minNominalVoltageIgnoredVoltageBounds" : 5.0,
"maxPlausiblePowerLimit" : 8555.3,
"lowActivePowerDefaultLimit" : 99.2,
"highActivePowerDefaultLimit" : 1144.0,
"defaultQmaxPmaxRatio" : 0.4,
"defaultMinimalQPRange" : 1.1,
"defaultVariableScalingFactor" : 0.756,
"defaultConstraintScalingFactor" : 0.888,
"reactiveSlackVariableScalingFactor" : 0.01,
"twoWindingTransformerRatioVariableScalingFactor" : 0.005
"alphaCoefficient" : 1.0,
"minPlausibleActivePowerThreshold" : 0.01,
"lowImpedanceThreshold" : 1.0E-4,
"minNominalVoltageIgnoredBus" : 1.0,
"minNominalVoltageIgnoredVoltageBounds" : 0.0,
"maxPlausiblePowerLimit" : 9000.0,
"lowActivePowerDefaultLimit" : 0.0,
"highActivePowerDefaultLimit" : 1000.0,
"defaultQmaxPmaxRatio" : 0.3,
"defaultMinimalQPRange" : 1.0,
"defaultVariableScalingFactor" : 1.0,
"defaultConstraintScalingFactor" : 1.0,
"reactiveSlackVariableScalingFactor" : 0.1,
"twoWindingTransformerRatioVariableScalingFactor" : 0.001
}
28 changes: 28 additions & 0 deletions open-reac/src/test/resources/parametersThresholds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version" : "1.0",
"specificVoltageLimits" : [ ],
"constantQGenerators" : [ ],
"variableTwoWindingsTransformers" : [ ],
"variableShuntCompensators" : [ ],
"objective" : "MIN_GENERATION",
"objectiveDistance" : 5.0,
"logLevelAmpl" : "WARNING",
"logLevelSolver" : "NOTHING",
"minPlausibleLowVoltageLimit" : 0.755,
"maxPlausibleHighVoltageLimit" : 1.236,
"reactiveSlackBusesMode" : "ALL",
"alphaCoefficient" : 0.56,
"minPlausibleActivePowerThreshold" : 0.5,
"lowImpedanceThreshold" : 1.0E-5,
"minNominalVoltageIgnoredBus" : 10.0,
"minNominalVoltageIgnoredVoltageBounds" : 5.0,
"maxPlausiblePowerLimit" : 8555.3,
"lowActivePowerDefaultLimit" : 99.2,
"highActivePowerDefaultLimit" : 1144.0,
"defaultQmaxPmaxRatio" : 0.4,
"defaultMinimalQPRange" : 1.1,
"defaultVariableScalingFactor" : 0.756,
"defaultConstraintScalingFactor" : 0.888,
"reactiveSlackVariableScalingFactor" : 0.01,
"twoWindingTransformerRatioVariableScalingFactor" : 0.005
}

0 comments on commit b49014d

Please sign in to comment.