From dfd1c5c231fc43620dcd3acbdf24ec98456e6637 Mon Sep 17 00:00:00 2001 From: Anne Tilloy <48123713+annetill@users.noreply.github.com> Date: Thu, 1 Apr 2021 16:39:52 +0200 Subject: [PATCH] BusVoltagePerTargetV sensitivity factor (#1640) Signed-off-by: Anne Tilloy Co-authored-by: yichen88 Co-authored-by: Sylvain Leclerc --- .../powsybl/iidm/network/AbstractBusRef.java | 36 +++++ .../java/com/powsybl/iidm/network/BusRef.java | 26 ++++ .../powsybl/iidm/network/IdBasedBusRef.java | 107 +++++++++++++++ .../com/powsybl/iidm/network/BusRefTest.java | 125 ++++++++++++++++++ .../factors/BusVoltagePerTargetV.java | 35 +++++ .../factors/functions/BusVoltage.java | 70 ++++++++++ .../factors/variables/TargetVoltage.java | 50 +++++++ .../factors/BusVoltagePerTargetVTest.java | 54 ++++++++ .../factors/functions/BusVoltageTest.java | 65 +++++++++ .../factors/variables/TargetVoltageTest.java | 51 +++++++ 10 files changed, 619 insertions(+) create mode 100644 iidm/iidm-api/src/main/java/com/powsybl/iidm/network/AbstractBusRef.java create mode 100644 iidm/iidm-api/src/main/java/com/powsybl/iidm/network/BusRef.java create mode 100644 iidm/iidm-api/src/main/java/com/powsybl/iidm/network/IdBasedBusRef.java create mode 100644 iidm/iidm-api/src/test/java/com/powsybl/iidm/network/BusRefTest.java create mode 100644 sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetV.java create mode 100644 sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/functions/BusVoltage.java create mode 100644 sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/variables/TargetVoltage.java create mode 100644 sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetVTest.java create mode 100644 sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/functions/BusVoltageTest.java create mode 100644 sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/variables/TargetVoltageTest.java diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/AbstractBusRef.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/AbstractBusRef.java new file mode 100644 index 00000000000..9b272eb2ad1 --- /dev/null +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/AbstractBusRef.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2021, 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.iidm.network; + +import java.util.Objects; +import java.util.Optional; + +/** + * @author Yichen TANG + */ +public abstract class AbstractBusRef implements BusRef { + + @Override + public Optional resolve(Network network, TopologyLevel level) { + Objects.requireNonNull(network); + Objects.requireNonNull(level); + if (TopologyLevel.NODE_BREAKER == level) { + throw new IllegalArgumentException(level + " is not supported in resolve a BusRef."); + } + return resolveByLevel(network, level); + } + + protected static Optional chooseBusByLevel(Terminal t, TopologyLevel level) { + if (level == TopologyLevel.BUS_BRANCH) { + return Optional.ofNullable(t.getBusView().getBus()); + } else { + return Optional.ofNullable(t.getBusBreakerView().getBus()); + } + } + + protected abstract Optional resolveByLevel(Network network, TopologyLevel level); +} diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/BusRef.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/BusRef.java new file mode 100644 index 00000000000..5dae96977a9 --- /dev/null +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/BusRef.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2021, 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.iidm.network; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; + +import java.util.Optional; + +@JsonSubTypes({ + @JsonSubTypes.Type(value = IdBasedBusRef.class) +}) +@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS) +public interface BusRef { + + /** + * @return an empty if not found + * @throws com.powsybl.commons.PowsyblException if underlying implementation not supported + * @throws IllegalArgumentException if try to resolve by {@link com.powsybl.iidm.network.TopologyLevel#NODE_BREAKER} + */ + Optional resolve(Network network, TopologyLevel level); +} diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/IdBasedBusRef.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/IdBasedBusRef.java new file mode 100644 index 00000000000..0a299427828 --- /dev/null +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/IdBasedBusRef.java @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2021, 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.iidm.network; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.powsybl.commons.PowsyblException; + +import java.util.Objects; +import java.util.Optional; + +/** + * There would be three types of id: + * 1. id of equipment: + * 2. id of a configured bus itself: + * 3. id of branch, in this case, side is required + * @author Yichen TANG + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class IdBasedBusRef extends AbstractBusRef { + + private final String id; + private final Branch.Side side; + + public IdBasedBusRef(String id) { + this.id = Objects.requireNonNull(id); + this.side = null; + } + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public IdBasedBusRef(@JsonProperty("id") String id, @JsonProperty("side") Branch.Side side) { + this.id = Objects.requireNonNull(id); + this.side = side; + } + + @Override + protected Optional resolveByLevel(Network network, TopologyLevel level) { + if (side == null) { + final Identifiable identifiable = network.getIdentifiable(id); + if (identifiable == null) { + return Optional.empty(); + } + if (identifiable instanceof Bus) { + Bus bus = (Bus) identifiable; + if (level == TopologyLevel.BUS_BRANCH) { + return bus.getConnectedTerminalStream().map(t -> t.getBusView().getBus()).filter(Objects::nonNull).findFirst(); + } else { + return Optional.of(bus); + } + } else if (identifiable instanceof Injection) { + final Injection injection = (Injection) identifiable; + return chooseBusByLevel(injection.getTerminal(), level); + } else { + throw new PowsyblException(id + " is not a bus or injection."); + } + } else { + Branch branch = network.getBranch(id); + Terminal terminal; + switch (side) { + case ONE: + terminal = branch.getTerminal1(); + break; + case TWO: + terminal = branch.getTerminal2(); + break; + default: + throw new AssertionError("Unexpected side: " + side); + } + return chooseBusByLevel(terminal, level); + } + } + + public String getId() { + return id; + } + + public Branch.Side getSide() { + return side; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof IdBasedBusRef)) { + return false; + } + + IdBasedBusRef that = (IdBasedBusRef) o; + + if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) { + return false; + } + return getSide() != null ? getSide().equals(that.getSide()) : that.getSide() == null; + } + + @Override + public int hashCode() { + return Objects.hash(id, side); + } +} diff --git a/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/BusRefTest.java b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/BusRefTest.java new file mode 100644 index 00000000000..2e573c126b3 --- /dev/null +++ b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/BusRefTest.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2021, 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.iidm.network; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.Collections; +import java.util.Set; +import java.util.stream.Stream; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Yichen TANG + */ +public class BusRefTest { + + private final Bus bvBus = mock(Bus.class); + private final Bus bbvBus = mock(Bus.class); + + @Test + public void testIdBasedBusRef() throws JsonProcessingException { + Network network = mock(Network.class); + Network.BusView bv = mock(Network.BusView.class); + + VoltageLevel vl = mock(VoltageLevel.class); + when(bbvBus.getVoltageLevel()).thenReturn(vl); + when(network.getBusView()).thenReturn(bv); + when(bv.getBus(eq("busId"))).thenReturn(bvBus); + + final BusRef busRef = new IdBasedBusRef("busId"); + when(network.getIdentifiable(eq("busId"))).thenReturn((Identifiable) bvBus); + when(bvBus.getId()).thenReturn("busId"); + Terminal busTerminal = mock(Terminal.class); + Terminal.BusView terBusView = mock(Terminal.BusView.class); + when(busTerminal.getBusView()).thenReturn(terBusView); + when(terBusView.getBus()).thenReturn(bvBus); + final Set singleton = Collections.singleton(busTerminal); + final Stream mock = singleton.stream(); + when(bvBus.getConnectedTerminalStream()).thenReturn(mock); + assertEquals(bvBus, busRef.resolve(network, TopologyLevel.BUS_BRANCH).orElseThrow(AssertionError::new)); + + when(network.getIdentifiable(eq("busId"))).thenReturn((Identifiable) bbvBus); + assertEquals(bbvBus, busRef.resolve(network, TopologyLevel.BUS_BREAKER).orElseThrow(AssertionError::new)); + + assertFalse(new IdBasedBusRef("another").resolve(network, TopologyLevel.BUS_BRANCH).isPresent()); + + ObjectMapper objectMapper = new ObjectMapper(); + final String json = objectMapper.writeValueAsString(busRef); + assertEquals("{\"@c\":\".IdBasedBusRef\",\"id\":\"busId\"}", json); + final BusRef deserialized = objectMapper.readValue(json, BusRef.class); + assertEquals(busRef, deserialized); + + Identifiable busbarSection = mock(BusbarSection.class); + when(network.getIdentifiable(eq("busbarId"))).thenReturn(busbarSection); + final BusbarSection bbs = (BusbarSection) busbarSection; + Terminal terminal = mock(Terminal.class); + when(bbs.getTerminal()).thenReturn(terminal); + Terminal.BusView tbv = mock(Terminal.BusView.class); + when(terminal.getBusView()).thenReturn(tbv); + when(tbv.getBus()).thenReturn(bvBus); + final IdBasedBusRef busbarRef = new IdBasedBusRef("busbarId"); + assertEquals(bvBus, busbarRef.resolve(network, TopologyLevel.BUS_BRANCH).orElseThrow(AssertionError::new)); + + Terminal.BusBreakerView bbv = mock(Terminal.BusBreakerView.class); + when(terminal.getBusBreakerView()).thenReturn(bbv); + when(bbv.getBus()).thenReturn(bbvBus); + assertEquals(bbvBus, busbarRef.resolve(network, TopologyLevel.BUS_BREAKER).orElseThrow(AssertionError::new)); + + Identifiable branch = mock(Branch.class); + when(network.getIdentifiable(eq("branchId"))).thenReturn(branch); + try { + new IdBasedBusRef("branchId").resolve(network, TopologyLevel.BUS_BRANCH); + fail(); + } catch (Exception e) { + assertEquals("branchId is not a bus or injection.", e.getMessage()); + } + + try { + new IdBasedBusRef("branchId").resolve(network, TopologyLevel.NODE_BREAKER); + fail(); + } catch (Exception e) { + assertEquals("NODE_BREAKER is not supported in resolve a BusRef.", e.getMessage()); + } + } + + @Test + public void testBranch() throws JsonProcessingException { + Network network = mock(Network.class); + Branch branch = mock(Branch.class); + when(network.getBranch(eq("branchId"))).thenReturn(branch); + Terminal terminal = mock(Terminal.class); + Terminal.BusView bv = mock(Terminal.BusView.class); + when(branch.getTerminal1()).thenReturn(terminal); + when(terminal.getBusView()).thenReturn(bv); + when(bv.getBus()).thenReturn(bvBus); + final BusRef busRef = new IdBasedBusRef("branchId", Branch.Side.ONE); + assertEquals(bvBus, busRef.resolve(network, TopologyLevel.BUS_BRANCH).orElseThrow(AssertionError::new)); + Terminal terminal2 = mock(Terminal.class); + Terminal.BusView bv2 = mock(Terminal.BusView.class); + when(branch.getTerminal2()).thenReturn(terminal2); + when(terminal2.getBusView()).thenReturn(bv2); + Bus bus2 = mock(Bus.class); + when(bv2.getBus()).thenReturn(bus2); + final BusRef busRef2 = new IdBasedBusRef("branchId", Branch.Side.TWO); + assertEquals(bus2, busRef2.resolve(network, TopologyLevel.BUS_BRANCH).orElseThrow(AssertionError::new)); + + ObjectMapper objectMapper = new ObjectMapper(); + final String json = objectMapper.writeValueAsString(busRef); + assertEquals("{\"@c\":\".IdBasedBusRef\",\"id\":\"branchId\",\"side\":\"ONE\"}", json); + final BusRef deserialized = objectMapper.readValue(json, BusRef.class); + assertEquals(busRef, deserialized); + + assertEquals(busRef, new IdBasedBusRef("branchId", Branch.Side.ONE)); + } +} diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetV.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetV.java new file mode 100644 index 00000000000..9d3f202a60b --- /dev/null +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetV.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2021, 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.sensitivity.factors; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.powsybl.sensitivity.SensitivityFactor; +import com.powsybl.sensitivity.factors.functions.BusVoltage; +import com.powsybl.sensitivity.factors.variables.TargetVoltage; + +/** + * Sensitivity factor for an impact of a targetV increase from a regulating equipment on a bus ref + * + * @author Anne Tilloy {@literal } + * @see com.powsybl.sensitivity.factors.functions.BusVoltage + * @see com.powsybl.sensitivity.factors.variables.TargetVoltage + */ +public class BusVoltagePerTargetV extends SensitivityFactor { + + /** + * Sensitivity factor standard implementation constructor. + * + * @param sensitivityFunction sensitivity function + * @param sensitivityVariable sensitivity variable + */ + @JsonCreator + public BusVoltagePerTargetV(@JsonProperty("function") BusVoltage sensitivityFunction, + @JsonProperty("variable") TargetVoltage sensitivityVariable) { + super(sensitivityFunction, sensitivityVariable); + } +} diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/functions/BusVoltage.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/functions/BusVoltage.java new file mode 100644 index 00000000000..50e00d85880 --- /dev/null +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/functions/BusVoltage.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2021, 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.sensitivity.factors.functions; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.powsybl.iidm.network.BusRef; +import com.powsybl.sensitivity.SensitivityFunction; + +import java.util.Objects; + +/** + * Sensitivity function on a network bus voltage + * Only available in AC computation mode + * + * @author Anne Tilloy {@literal } + */ +public class BusVoltage extends SensitivityFunction { + + @JsonProperty("busRef") + private final BusRef busRef; + + /** + * Constructor + * + * @param id unique identifier of the function + * @param name readable name of the function + * @param busRef reference to the voltage bus is used as sensitivity function + * @throws NullPointerException if busRef is null + */ + @JsonCreator + public BusVoltage(@JsonProperty("id") String id, + @JsonProperty("name") String name, + @JsonProperty("busRef") BusRef busRef) { + super(id, name); + this.busRef = Objects.requireNonNull(busRef); + } + + /** + * Get the reference to the bus composing the sensitivity function + * + * @return the bus ref + */ + public BusRef getBusRef() { + return busRef; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof BusVoltage)) { + return false; + } + + BusVoltage that = (BusVoltage) o; + + return getBusRef().equals(that.getBusRef()); + } + + @Override + public int hashCode() { + return getBusRef().hashCode(); + } +} diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/variables/TargetVoltage.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/variables/TargetVoltage.java new file mode 100644 index 00000000000..9a1b8851d86 --- /dev/null +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/factors/variables/TargetVoltage.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2021, 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.sensitivity.factors.variables; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.powsybl.sensitivity.SensitivityVariable; + +import java.util.Objects; + +/** + * Sensitivity variable on a voltage target increase + * + * @author Anne Tilloy {@literal } + */ +public class TargetVoltage extends SensitivityVariable { + + @JsonProperty("equipmentId") + private final String equipmentId; + + /** + * Constructor + * + * @param id unique identifier of the variable + * @param name readable name of the variable + * @param equipmentId id of the network equipment (generator, static var compensator, two windings transformer, + * three windings transformer, shunt compensator, VSC, etc.) which targetV increase is used as sensitivity variable + * @throws NullPointerException if equipmentId is null + */ + @JsonCreator + public TargetVoltage(@JsonProperty("id") String id, + @JsonProperty("name") String name, + @JsonProperty("equipmentId") String equipmentId) { + super(id, name); + this.equipmentId = Objects.requireNonNull(equipmentId); + } + + /** + * Get the id of the equipment that is regulating voltage + * + * @return the id of the equipment + */ + public String getEquipmentId() { + return equipmentId; + } +} diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetVTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetVTest.java new file mode 100644 index 00000000000..c180ded37d5 --- /dev/null +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/BusVoltagePerTargetVTest.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2021, 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.sensitivity.factors; + +import com.powsybl.sensitivity.factors.functions.BusVoltage; +import com.powsybl.sensitivity.factors.variables.TargetVoltage; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +/** + * @author Anne Tilloy {@literal } + */ +public class BusVoltagePerTargetVTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void checkFailsWhenNullFunction() { + TargetVoltage targetV = Mockito.mock(TargetVoltage.class); + exception.expect(NullPointerException.class); + new BusVoltagePerTargetV(null, targetV); + } + + @Test + public void checkFailsWhenNullVariable() { + BusVoltage busVoltage = Mockito.mock(BusVoltage.class); + exception.expect(NullPointerException.class); + new BusVoltagePerTargetV(busVoltage, null); + } + + @Test + public void testGetFunction() { + BusVoltage busVoltage = Mockito.mock(BusVoltage.class); + TargetVoltage targetV = Mockito.mock(TargetVoltage.class); + BusVoltagePerTargetV factor = new BusVoltagePerTargetV(busVoltage, targetV); + Assert.assertEquals(busVoltage, factor.getFunction()); + } + + @Test + public void testGetVariable() { + BusVoltage busVoltage = Mockito.mock(BusVoltage.class); + TargetVoltage targetV = Mockito.mock(TargetVoltage.class); + BusVoltagePerTargetV factor = new BusVoltagePerTargetV(busVoltage, targetV); + Assert.assertEquals(targetV, factor.getVariable()); + } +} diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/functions/BusVoltageTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/functions/BusVoltageTest.java new file mode 100644 index 00000000000..45734744faf --- /dev/null +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/functions/BusVoltageTest.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2018, 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.sensitivity.factors.functions; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.powsybl.iidm.network.Branch; +import com.powsybl.iidm.network.BusRef; +import com.powsybl.iidm.network.IdBasedBusRef; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +/** + * @author Anne Tilloy {@literal } + */ +public class BusVoltageTest { + + private static final String FUNCTION_ID = "Function ID"; + private static final String FUNCTION_NAME = "Function name"; + private static final BusRef BUS_REF = mock(BusRef.class); + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void testNullTerminalId() { + exception.expect(NullPointerException.class); + new BusVoltage(FUNCTION_ID, FUNCTION_NAME, null); + } + + @Test + public void getName() { + BusVoltage busVoltage = new BusVoltage(FUNCTION_ID, FUNCTION_NAME, BUS_REF); + Assert.assertEquals(FUNCTION_NAME, busVoltage.getName()); + } + + @Test + public void getId() { + BusVoltage busVoltage = new BusVoltage(FUNCTION_ID, FUNCTION_NAME, BUS_REF); + Assert.assertEquals(FUNCTION_ID, busVoltage.getId()); + } + + @Test + public void getBranchId() throws JsonProcessingException { + IdBasedBusRef busRef = new IdBasedBusRef("branchId", Branch.Side.ONE); + BusVoltage busVoltage = new BusVoltage(FUNCTION_ID, FUNCTION_NAME, busRef); + assertEquals(busRef, busVoltage.getBusRef()); + ObjectMapper objectMapper = new ObjectMapper(); + final String json = objectMapper.writeValueAsString(busVoltage); + String expectedJson = "{\"@c\":\".BusVoltage\",\"id\":\"Function ID\",\"name\":\"Function name\",\"busRef\":{\"@c\":\".IdBasedBusRef\",\"id\":\"branchId\",\"side\":\"ONE\"}}"; + assertEquals(expectedJson, json); + final BusVoltage deserialized = objectMapper.readValue(expectedJson, BusVoltage.class); + assertEquals(busVoltage, deserialized); + } + +} diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/variables/TargetVoltageTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/variables/TargetVoltageTest.java new file mode 100644 index 00000000000..c94ec63d10e --- /dev/null +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/factors/variables/TargetVoltageTest.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2021, 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.sensitivity.factors.variables; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; + +/** + * @author Anne Tilloy {@literal } + */ +public class TargetVoltageTest { + + private static final String VARIABLE_ID = "Variable ID"; + private static final String VARIABLE_NAME = "Variable name"; + private static final String EQUIPMENT_ID = "Regulating terminal ID"; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void checkFailsWhenNullInjection() { + exception.expect(NullPointerException.class); + new TargetVoltage(VARIABLE_ID, VARIABLE_NAME, null); + } + + @Test + public void getName() { + TargetVoltage targetV = new TargetVoltage(VARIABLE_ID, VARIABLE_NAME, EQUIPMENT_ID); + Assert.assertEquals(VARIABLE_NAME, targetV.getName()); + } + + @Test + public void getId() { + TargetVoltage targetV = new TargetVoltage(VARIABLE_ID, VARIABLE_NAME, EQUIPMENT_ID); + Assert.assertEquals(VARIABLE_ID, targetV.getId()); + } + + @Test + public void getLine() { + TargetVoltage targetV = new TargetVoltage(VARIABLE_ID, VARIABLE_NAME, EQUIPMENT_ID); + assertEquals(EQUIPMENT_ID, targetV.getEquipmentId()); + } +}