Skip to content

Commit

Permalink
WCARestrictingThresholdLevel enum, revisited; adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Biasuzzi committed Feb 17, 2017
1 parent f663e35 commit 01c3d4f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 48 deletions.
25 changes: 15 additions & 10 deletions wca-integration/src/main/java/eu/itesla_project/wca/WCAConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import eu.itesla_project.commons.config.PlatformConfig;

import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -28,25 +30,28 @@ public class WCAConfig {

private final boolean exportStates;

private final WCARestrictingThresholdLevel restrictingThresholdLevel;
private final Set<WCARestrictingThresholdLevel> restrictingThresholdLevels;

public static WCAConfig load() {
ModuleConfig config = PlatformConfig.defaultConfig().getModuleConfig("wca");
return load(PlatformConfig.defaultConfig());
}

public static WCAConfig load(PlatformConfig platformConfig) {
ModuleConfig config = platformConfig.getModuleConfig("wca");
Path xpressHome = config.getPathProperty("xpressHome");
float reducedVariableRatio = config.getFloatProperty("reducedVariableRatio", DEFAULT_REDUCED_VARIABLE_RATIO);
boolean debug = config.getBooleanProperty("debug", false);
boolean exportStates = config.getBooleanProperty("exportStates", false);
int restrictingThresholdLevelInt = config.getIntProperty("restrictingThresholdLevel", 0);
WCARestrictingThresholdLevel restrictingThresholdLevel = WCARestrictingThresholdLevel.fromLevel(restrictingThresholdLevelInt);
return new WCAConfig(xpressHome, reducedVariableRatio, debug, exportStates, restrictingThresholdLevel);
Set<WCARestrictingThresholdLevel> restrictingThresholdLevels = config.getEnumSetProperty("restrictingThresholdLevels", WCARestrictingThresholdLevel.class, EnumSet.noneOf(WCARestrictingThresholdLevel.class));
return new WCAConfig(xpressHome, reducedVariableRatio, debug, exportStates, restrictingThresholdLevels);
}

public WCAConfig(Path xpressHome, float reducedVariableRatio, boolean debug, boolean exportStates, WCARestrictingThresholdLevel restrictingThresholdLevel) {
public WCAConfig(Path xpressHome, float reducedVariableRatio, boolean debug, boolean exportStates, Set<WCARestrictingThresholdLevel> restrictingThresholdLevels) {
this.xpressHome = Objects.requireNonNull(xpressHome);
this.reducedVariableRatio = reducedVariableRatio;
this.debug = debug;
this.exportStates = exportStates;
this.restrictingThresholdLevel = Objects.requireNonNull(restrictingThresholdLevel, "invalid restrictingThresholdLevel");
this.restrictingThresholdLevels = Objects.requireNonNull(restrictingThresholdLevels, "invalid restrictingThresholdLevels");
}

public Path getXpressHome() {
Expand All @@ -65,8 +70,8 @@ public boolean isExportStates() {
return exportStates;
}

public WCARestrictingThresholdLevel getRestrictingThresholdLevel() {
return restrictingThresholdLevel;
public Set<WCARestrictingThresholdLevel> getRestrictingThresholdLevels() {
return restrictingThresholdLevels;
}

@Override
Expand All @@ -75,7 +80,7 @@ public String toString() {
", reducedVariableRatio=" + reducedVariableRatio +
", debug=" + debug +
", exportStates=" + exportStates +
", restrictingThresholdLevel=" + restrictingThresholdLevel.getLevel() +
", restrictingThresholdLevels=" + restrictingThresholdLevels + " -> level=" + WCARestrictingThresholdLevel.getLevel(restrictingThresholdLevels) +
"]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public List<CommandExecution> before(Path workingDir) throws IOException {
Float.toString(config.getReducedVariableRatio()),
Float.toString(UNCERTAINTY_THRESHOLD),
Integer.toString(config.isDebug() ? DETAILS_LEVEL_DEBUG : DETAILS_LEVEL_NORMAL),
Integer.toString(config.getRestrictingThresholdLevel().getLevel()))
Integer.toString(WCARestrictingThresholdLevel.getLevel(config.getRestrictingThresholdLevels())))
.build();
return Arrays.asList(new CommandExecution(cmd, 1));
}
Expand Down Expand Up @@ -449,7 +449,7 @@ public List<CommandExecution> before(Path workingDir) throws IOException {
Float.toString(UNCERTAINTY_THRESHOLD),
Integer.toString(SecurityIndexType.TSO_OVERLOAD.ordinal()),
Integer.toString(config.isDebug() ? DETAILS_LEVEL_DEBUG : DETAILS_LEVEL_NORMAL),
Integer.toString(config.getRestrictingThresholdLevel().getLevel()))
Integer.toString(WCARestrictingThresholdLevel.getLevel(config.getRestrictingThresholdLevels())))
.build();
return Arrays.asList(new CommandExecution(cmd, 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,34 @@
*/
package eu.itesla_project.wca;

import java.util.Arrays;
import java.util.stream.Stream;
import java.util.Objects;
import java.util.Set;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public enum WCARestrictingThresholdLevel {
ZERO(0, "NO RESTRICTION"),
ONE(1, "No flow thresholds on the HV part"),
TWO(2, "No flow thresholds on the foreign part"),
THREE(3, "No flow thresholds on the HV and foreign parts");
NO_HV_THRESHOLDS("No flow thresholds on the HV part", 1),
NO_FOREIGN_THRESHOLDS("No flow thresholds on the foreign part", 2);

private final int level;
private final String description;

WCARestrictingThresholdLevel(int level, String description) {
this.level = level;
private final int level;

WCARestrictingThresholdLevel(String description, int level) {
this.description = description;
this.level = level;
}

public int getLevel() {
private int getLevel() {
return level;
}

@Override
public String toString() {
return "" + level + " (" + description + ")";
}

public static WCARestrictingThresholdLevel fromLevel(int level) {
return Arrays.stream(values())
.filter(restrLevel -> ( restrLevel.level == level))
.findFirst()
.orElse(null);
public static int getLevel(Set<WCARestrictingThresholdLevel> levels) {
Objects.requireNonNull(levels);
return levels.stream()
.map(WCARestrictingThresholdLevel::getLevel)
.reduce(0, (a, b) -> (a | b));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (c) 2017, 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 eu.itesla_project.wca;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import eu.itesla_project.commons.config.InMemoryPlatformConfig;
import eu.itesla_project.commons.config.MapModuleConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public class WCARestrictingThresholdLevelConfigTest {

private FileSystem fileSystem;
private InMemoryPlatformConfig platformConfig;
private MapModuleConfig moduleConfig;

@Before
public void setUp() throws Exception {
fileSystem = Jimfs.newFileSystem(Configuration.unix());
platformConfig = new InMemoryPlatformConfig(fileSystem);
Path xpressPath = fileSystem.getPath("/tmp/xpress");
moduleConfig = platformConfig.createModuleConfig("wca");
moduleConfig.setStringProperty("xpressHome", xpressPath.toString());
moduleConfig.setStringProperty("reducedVariableRatio", "1.5");
moduleConfig.setStringProperty("debug", "true");
moduleConfig.setStringProperty("exportStates", "true");
}

@After
public void tearDown() throws Exception {
fileSystem.close();
}


private void checkConfigValues(WCAConfig config, Path expectedXpressHome, float expectedReducedVariableRatio, boolean expectedDebug, boolean expectedExportStates, Set<WCARestrictingThresholdLevel> expectedRestrictingThresholdLevels) {
assertEquals(config.getXpressHome(), expectedXpressHome);
assertEquals(config.getReducedVariableRatio(), expectedReducedVariableRatio, 0.0f);
assertEquals(config.isDebug(), expectedDebug);
assertEquals(config.isExportStates(), expectedExportStates);
assertEquals(config.getRestrictingThresholdLevels(), expectedRestrictingThresholdLevels);
}

private void checkConfigValues(WCAConfig config, Set<WCARestrictingThresholdLevel> expectedRestrictingThresholdLevels) {
checkConfigValues(config, fileSystem.getPath("/tmp/xpress"), 1.5f, true, true, expectedRestrictingThresholdLevels);
}

@Test
public void testConfig() throws Exception {
moduleConfig.setStringProperty("restrictingThresholdLevels", "NO_HV_THRESHOLDS,NO_FOREIGN_THRESHOLDS");
WCAConfig config = WCAConfig.load(platformConfig);
checkConfigValues(config, EnumSet.of(WCARestrictingThresholdLevel.NO_HV_THRESHOLDS, WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS));
}

@Test
public void testConfigDefaultsNotDeclaredThresholds() throws Exception {
//no restrictingThresholdLevels parameter
WCAConfig config = WCAConfig.load(platformConfig);
checkConfigValues(config, EnumSet.noneOf(WCARestrictingThresholdLevel.class));
}

@Test
public void testConfigEmptyThresholds() throws Exception {
moduleConfig.setStringProperty("restrictingThresholdLevels", "");
WCAConfig config = WCAConfig.load(platformConfig);
checkConfigValues(config, EnumSet.noneOf(WCARestrictingThresholdLevel.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.junit.Test;

import java.util.EnumSet;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -16,22 +18,15 @@
public class WCARestrictingThresholdLevelTest {

@Test
public void testIntToEnum() {
assertEquals(WCARestrictingThresholdLevel.fromLevel(0), WCARestrictingThresholdLevel.ZERO);
assertEquals(WCARestrictingThresholdLevel.fromLevel(1), WCARestrictingThresholdLevel.ONE);
assertEquals(WCARestrictingThresholdLevel.fromLevel(2), WCARestrictingThresholdLevel.TWO);
assertEquals(WCARestrictingThresholdLevel.fromLevel(3), WCARestrictingThresholdLevel.THREE);
assertEquals(WCARestrictingThresholdLevel.fromLevel(4), null);
}

@Test
public void testEnumToInt() {
assertEquals(WCARestrictingThresholdLevel.ZERO.getLevel(), 0);
assertEquals(WCARestrictingThresholdLevel.ONE.getLevel(), 1);
assertEquals(WCARestrictingThresholdLevel.TWO.getLevel(), 2);
assertEquals(WCARestrictingThresholdLevel.THREE.getLevel(), 3);

public void testThresholdLevels() {
assertEquals(0, WCARestrictingThresholdLevel.getLevel(EnumSet.noneOf(WCARestrictingThresholdLevel.class)));
assertEquals(1, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_HV_THRESHOLDS)));
assertEquals(2, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS)));
assertEquals(3, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS, WCARestrictingThresholdLevel.NO_HV_THRESHOLDS)));
assertEquals(3, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_HV_THRESHOLDS, WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS)));
assertEquals(1, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_HV_THRESHOLDS, WCARestrictingThresholdLevel.NO_HV_THRESHOLDS)));
assertEquals(2, WCARestrictingThresholdLevel.getLevel(EnumSet.of(WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS, WCARestrictingThresholdLevel.NO_FOREIGN_THRESHOLDS)));
assertEquals(3, WCARestrictingThresholdLevel.getLevel(EnumSet.allOf(WCARestrictingThresholdLevel.class)));
}


}

0 comments on commit 01c3d4f

Please sign in to comment.