Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
Signed-off-by: Leclerc Clement <[email protected]>
  • Loading branch information
clementleclercRTE committed Dec 9, 2024
1 parent ed79865 commit 71046bb
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.powsybl.ucte.converter;

import com.powsybl.iidm.network.*;
import com.powsybl.ucte.converter.util.UcteConstants;
import com.powsybl.ucte.converter.util.UcteConverterConstants;
import com.powsybl.ucte.network.UcteElementId;
import com.powsybl.ucte.network.UcteNodeCode;

Expand All @@ -24,14 +24,14 @@ public abstract class AbstractNamingStrategy implements NamingStrategy {
protected final Map<String, UcteElementId> ucteElementIds = new HashMap<>();

@Override
public void initialiseNetwork(Network network) {
public void initializeNetwork(Network network) {
//Empty implementation by default
}

@Override
public UcteNodeCode getUcteNodeCode(String id) {
return ucteNodeIds.computeIfAbsent(id, k -> UcteNodeCode.parseUcteNodeCode(k)
.orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k)));
.orElseThrow(() -> new UcteException(UcteConverterConstants.NO_UCTE_CODE_ERROR + k)));
}

@Override
Expand All @@ -50,7 +50,7 @@ public UcteNodeCode getUcteNodeCode(DanglingLine danglingLine) {
@Override
public UcteElementId getUcteElementId(String id) {
return ucteElementIds.computeIfAbsent(id, k -> UcteElementId.parseUcteElementId(k)
.orElseThrow(() -> new UcteException(UcteConstants.NO_UCTE_CODE_ERROR + k)));
.orElseThrow(() -> new UcteException(UcteConverterConstants.NO_UCTE_CODE_ERROR + k)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import com.google.auto.service.AutoService;
import com.powsybl.iidm.network.*;
import com.powsybl.ucte.converter.util.UcteConstants;
import com.powsybl.ucte.converter.util.UcteConverterHelper;
import com.powsybl.ucte.network.UcteCountryCode;
import com.powsybl.ucte.network.UcteElementId;
import com.powsybl.ucte.network.UcteNodeCode;
import com.powsybl.ucte.network.UcteVoltageLevelCode;
import com.powsybl.ucte.network.util.UcteNetworkUtil;

import java.util.*;

Expand All @@ -32,7 +31,7 @@ public String getName() {
}

@Override
public void initialiseNetwork(Network network) {
public void initializeNetwork(Network network) {
voltageLevelCounter = 0;
network.getVoltageLevelStream()
.forEach(this::processVoltageLevel);
Expand All @@ -45,7 +44,7 @@ private void processVoltageLevel(VoltageLevel voltageLevel) {
Iterator<Bus> buslist = voltageLevel.getBusBreakerView().getBuses().iterator();
for (int i = 0; buslist.hasNext(); i++) {
Bus bus = buslist.next();
char orderCode = UcteConverterHelper.getOrderCode(i);
char orderCode = UcteNetworkUtil.getOrderCode(i);
generateUcteNodeId(bus.getId(), voltageLevel, orderCode);
}

Expand Down Expand Up @@ -88,7 +87,7 @@ private UcteElementId generateUcteElementId(String id, UcteNodeCode node1, UcteN
return ucteElementIds.get(id);
}

UcteElementId uniqueElementId = UcteConstants.ORDER_CODES.stream()
UcteElementId uniqueElementId = UcteNetworkUtil.ORDER_CODES.stream()
.map(orderCode -> new UcteElementId(node1, node2, orderCode))
.filter(elementId -> !ucteElementIds.containsValue(elementId))
.findFirst()
Expand Down Expand Up @@ -122,7 +121,7 @@ private UcteElementId generateUcteElementId(DanglingLine danglingLine) {
code2 = UcteNodeCode.parseUcteNodeCode(danglingLine.getPairingKey()).orElseThrow();
ucteNodeIds.put(danglingLine.getPairingKey(), code2);
} else {
code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteConverterHelper.getOrderCode(0));
code2 = generateUcteNodeId(danglingLine.getId(), danglingLine.getTerminal().getVoltageLevel(), UcteNetworkUtil.getOrderCode(0));
}
return generateUcteElementId(danglingLine.getId(), code1, code2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public interface NamingStrategy {

void initialiseNetwork(Network network);
void initializeNetwork(Network network);

String getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.*;
import java.util.function.Supplier;

import static com.powsybl.ucte.converter.util.UcteConstants.*;
import static com.powsybl.ucte.converter.util.UcteConverterConstants.*;
import static com.powsybl.ucte.converter.util.UcteConverterHelper.*;

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public void export(Network network, Properties parameters, DataSource dataSource

String namingStrategyName = Parameter.readString(getFormat(), parameters, NAMING_STRATEGY_PARAMETER, defaultValueConfig);
NamingStrategy namingStrategy = findNamingStrategy(namingStrategyName, NAMING_STRATEGY_SUPPLIERS.get());
namingStrategy.initialiseNetwork(network);
namingStrategy.initializeNetwork(network);
boolean combinePhaseAngleRegulation = Parameter.readBoolean(getFormat(), parameters, COMBINE_PHASE_ANGLE_REGULATION_PARAMETER, defaultValueConfig);

UcteNetwork ucteNetwork = createUcteNetwork(network, namingStrategy, combinePhaseAngleRegulation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.powsybl.ucte.converter.util.UcteConstants.*;
import static com.powsybl.ucte.converter.util.UcteConverterConstants.*;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
*/
package com.powsybl.ucte.converter.util;

import java.util.List;

/**
* @author Sebastien Murgey {@literal <sebastien.murgey at rte-france.com>}
*/
public final class UcteConstants {
public final class UcteConverterConstants {

private UcteConstants() {
private UcteConverterConstants() {
throw new IllegalStateException("Should not be constructed");
}

Expand All @@ -29,6 +27,4 @@ private UcteConstants() {
public static final String POWER_PLANT_TYPE_PROPERTY_KEY = "powerPlantType";
public static final int DEFAULT_POWER_LIMIT = 9999;
public static final String NO_UCTE_CODE_ERROR = "No UCTE code found for id: ";
public static final List<Character> ORDER_CODES = List.of('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.powsybl.iidm.network.PhaseTapChanger;
import com.powsybl.iidm.network.RatioTapChanger;
import com.powsybl.iidm.network.TwoWindingsTransformer;
import com.powsybl.ucte.converter.UcteException;
import com.powsybl.ucte.network.UcteAngleRegulation;
import com.powsybl.ucte.network.UctePhaseRegulation;
import org.apache.commons.math3.complex.Complex;
Expand Down Expand Up @@ -121,14 +120,6 @@ public static Complex calculateAsymmAngleDuAndAngle(TwoWindingsTransformer twoWi
}

return ComplexUtils.polar2Complex(BigDecimal.valueOf(absDu).setScale(4, RoundingMode.HALF_UP).doubleValue(),
theta);
theta);
}

public static char getOrderCode(int index) {
if (index > UcteConstants.ORDER_CODES.size() || index < 0) {
throw new UcteException("Order code index out of bounds");
}
return UcteConstants.ORDER_CODES.get(index);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ void testInitialNetwork() {
assertThrows(UcteException.class, () -> strategy.getUcteNodeCode("NGEN"));
assertThrows(UcteException.class, () -> strategy.getUcteElementId("NHV1_NHV2_1"));

strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

assertDoesNotThrow(() -> strategy.getUcteNodeCode("NGEN"));
assertDoesNotThrow(() -> strategy.getUcteElementId("NHV1_NHV2_1"));
}

@Test
void testVoltageLevelCounterNaming() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

UcteNodeCode firstBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN"));
UcteNodeCode secondBusCode = strategy.getUcteNodeCode(network.getBusBreakerView().getBus("NGEN2"));
Expand All @@ -69,7 +69,7 @@ void testVoltageLevelCounterNaming() {
@Test
void testBasicNodeCodeGeneration() {

strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);
Bus genBus = network.getBusBreakerView().getBus("NGEN");
Bus genbus2 = network.getBusBreakerView().getBus("NGEN2");
Bus ucteBus = network.getBusBreakerView().getBus("F0000079");
Expand All @@ -93,17 +93,17 @@ void testBasicNodeCodeGeneration() {
() -> assertNotEquals(genCode2, ucteCode),
() -> assertNotEquals(ucteCode, loadCode),

() -> assertEquals("F0000071", genCode.toString()),
() -> assertEquals("F0000072", genCode2.toString()),
() -> assertEquals("F0000074", ucteCode.toString()),
() -> assertEquals("F0000331", loadCode.toString())
() -> assertEquals("F0000070", genCode.toString()),
() -> assertEquals("F0000071", genCode2.toString()),
() -> assertEquals("F0000073", ucteCode.toString()),
() -> assertEquals("F0000330", loadCode.toString())

);
}

@Test
void testBranchElementIds() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

Branch<?> transformer1 = network.getBranch("NGEN_NHV1");
Branch<?> transformer2 = network.getBranch("NHV2_NLOAD");
Expand All @@ -127,17 +127,17 @@ void testBranchElementIds() {
() -> assertEquals(transformerId1, strategy.getUcteElementId(transformer1)),
() -> assertEquals(transformerId2, strategy.getUcteElementId(transformer2)),

() -> assertEquals("F0000071 F0000111 1", transformerId1.toString()),
() -> assertEquals("F0000211 F0000331 1", transformerId2.toString()),
() -> assertEquals("F0000111 F0000211 1", lineId1.toString()),
() -> assertEquals("F0000111 F0000211 2", lineId2.toString())
() -> assertEquals("F0000070 F0000110 0", transformerId1.toString()),
() -> assertEquals("F0000210 F0000330 0", transformerId2.toString()),
() -> assertEquals("F0000110 F0000210 0", lineId1.toString()),
() -> assertEquals("F0000110 F0000210 1", lineId2.toString())

);
}

@Test
void testSwitchElementIds() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);
Switch sw = network.getSwitch("NGEN-NGEN2");
Switch sw2 = network.getSwitch("NGEN-NGEN3");
Switch sw3 = network.getSwitch("NGEN-NGEN3");
Expand All @@ -159,16 +159,16 @@ void testSwitchElementIds() {
() -> assertNotEquals(swId2, swId4),
() -> assertEquals(swId3, swId2),

() -> assertEquals("F0000071 F0000072 1", swId.toString()),
() -> assertEquals("F0000071 F0000073 1", swId2.toString()),
() -> assertEquals("F0000071 F0000073 1", swId3.toString()),
() -> assertEquals("F0000071 F0000073 2", swId4.toString())
() -> assertEquals("F0000070 F0000071 0", swId.toString()),
() -> assertEquals("F0000070 F0000072 0", swId2.toString()),
() -> assertEquals("F0000070 F0000072 0", swId3.toString()),
() -> assertEquals("F0000070 F0000072 1", swId4.toString())
);
}

@Test
void testDanglingLineElementIds() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);
DanglingLine dl1 = network.getDanglingLine("DL1");
DanglingLine dl2 = network.getDanglingLine("DL2");
DanglingLine dl3 = network.getDanglingLine("DL3");
Expand All @@ -183,15 +183,15 @@ void testDanglingLineElementIds() {
() -> assertNotEquals(dlId1, dlId3),
() -> assertNotEquals(dlId2, dlId3),

() -> assertEquals("F0000072 F0000671 1", dlId1.toString()),
() -> assertEquals("F0000072 X0000011 1", dlId2.toString()),
() -> assertEquals("F0000072 F0000671 2", dlId3.toString())
() -> assertEquals("F0000071 F0000670 0", dlId1.toString()),
() -> assertEquals("F0000071 X0000011 0", dlId2.toString()),
() -> assertEquals("F0000071 F0000670 1", dlId3.toString())
);
}

@Test
void testParallelLines() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

Branch<?> line1 = network.getBranch("NHV1_NHV2_1");
Branch<?> line2 = network.getBranch("NHV1_NHV2_2");
Expand All @@ -204,14 +204,14 @@ void testParallelLines() {
() -> assertTrue(UcteElementId.isUcteElementId(id2.toString())),
() -> assertNotEquals(id1, id2),

() -> assertEquals("F0000111 F0000211 1", id1.toString()),
() -> assertEquals("F0000111 F0000211 2", id2.toString())
() -> assertEquals("F0000110 F0000210 0", id1.toString()),
() -> assertEquals("F0000110 F0000210 1", id2.toString())
);
}

@Test
void testExistingUcteNodeCodes() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

Bus bus = network.getBusBreakerView().getBus("NGEN");
UcteNodeCode firstCode = strategy.getUcteNodeCode(bus);
Expand All @@ -229,7 +229,7 @@ void testExistingUcteNodeCodes() {

@Test
void testNullAndInvalidIds() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

assertAll(
() -> assertThrows(PowsyblException.class, () -> strategy.getUcteNodeCode((String) null)),
Expand All @@ -241,10 +241,10 @@ void testNullAndInvalidIds() {

@Test
void testCountryCode() {
strategy.initialiseNetwork(network);
strategy.initializeNetwork(network);

Bus genBus = network.getBusBreakerView().getBus("NGEN");
UcteNodeCode code = strategy.getUcteNodeCode(genBus);
assertEquals('F', code.toString().charAt(0)); // France
assertEquals('F', code.toString().charAt(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.powsybl.entsoe.util.EntsoeGeographicalCode;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.impl.NetworkFactoryImpl;
import com.powsybl.ucte.converter.util.UcteConstants;
import com.powsybl.ucte.converter.util.UcteConverterConstants;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand Down Expand Up @@ -271,7 +271,7 @@ void emptyElementName() {
Network network = new UcteImporter().importData(dataSource, new NetworkFactoryImpl(), null);
Line l = network.getLine("F_SU1_12 F_SU1_11 1");
assertNotNull(l);
assertFalse(l.hasProperty(UcteConstants.ELEMENT_NAME_PROPERTY_KEY));
assertFalse(l.hasProperty(UcteConverterConstants.ELEMENT_NAME_PROPERTY_KEY));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.NetworkFactory;
import com.powsybl.ucte.converter.UcteException;
import com.powsybl.ucte.converter.UcteImporter;
import org.apache.commons.math3.complex.Complex;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -91,15 +90,4 @@ void calculatePhaseDuTest2() {
assertEquals(90.00, Math.toDegrees(duRef6.getArgument()), 0.00001); // loss of one decimal with sign
}

@Test
void getOrderCodeTest() {
assertEquals('1', getOrderCode(0));
assertEquals('A', getOrderCode(9));
assertThrows(UcteException.class, () -> {
getOrderCode(-1);
});
assertThrows(UcteException.class, () -> {
getOrderCode(50);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static UcteCountryCode fromVoltagelevel(VoltageLevel voltageLevel) {
try {
return UcteCountryCode.valueOf(country.name());
} catch (IllegalArgumentException e) {
throw new UcteException(String.format("No UCTE country found for " + country.name()));
throw new UcteException(String.format("No UCTE country found for %s",country.name()));
}
}

Expand Down
Loading

0 comments on commit 71046bb

Please sign in to comment.