diff --git a/CHANGELOG.md b/CHANGELOG.md index 71570eba..2860ea0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added quantity for volumetric flow rate [#363](https://github.com/ie3-institute/PowerSystemUtils/issues/363) +- Register missing units for serialization/deserialization. Added test for labeling unit symbols correctly within PowerSystemUnits [#280](https://github.com/ie3-institute/PowerSystemUtils/issues/280) ### Fixed - `TimeUtil` changes time zone when parsing `ZonedDateTime` [#422](https://github.com/ie3-institute/PowerSystemUtils/issues/422) diff --git a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java index 01f07428..260dbb5c 100644 --- a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java +++ b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java @@ -76,18 +76,21 @@ public class PowerSystemUnits extends Units { public static final Unit WATTHOUR = new TransformedUnit<>("Wh", JOULE, DoubleConverterFactory.withFactor(3600)); + /** Varhour */ public static final Unit VARHOUR = new TransformedUnit<>("varh", JOULE, DoubleConverterFactory.withFactor(3600)); /** Kilowatthour */ public static final Unit KILOWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, KILO); - public static final Unit KILOVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, KILO); + /** Kilovarhour */ + public static final Unit KILOVARHOUR = KILO(VARHOUR); /** Megawatthour */ public static final Unit MEGAWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, MEGA); - public static final Unit MEGAVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, MEGA); + /** Megavarhour */ + public static final Unit MEGAVARHOUR = MEGA(VARHOUR); /** Watthour per metre */ public static final Unit WATTHOUR_PER_METRE = @@ -226,13 +229,17 @@ public class PowerSystemUnits extends Units { private static final HashSet REGISTERED_LABELS = new HashSet<>(); static { - // varh, kvarh, Mvarh are kept out of this because they register for the same units as Wh, kWh, - // MWh + addUnit(WATTHOUR, "Wh"); addUnit(WATTHOUR_PER_METRE, "Wh/m"); addUnit(KILOWATTHOUR_PER_KILOMETRE, "kWh/km"); + addUnit(KILOWATTHOUR, "kWh"); + addUnit(MEGAWATTHOUR, "MWh"); addUnit(OHM_PER_KILOMETRE, "Ω/km"); addUnit(SIEMENS_PER_KILOMETRE, "S/km"); addUnit(VOLTAMPERE, "VA"); + addUnit(WATT, "W"); + addUnit(KILOWATT, "kW"); + addUnit(MEGAWATT, "MW"); addUnit(KILOVOLTAMPERE, "kVA"); addUnit(MEGAVOLTAMPERE, "MVA"); addUnit(WATT_PER_SQUAREMETRE, "W/m²"); @@ -241,8 +248,12 @@ public class PowerSystemUnits extends Units { addUnit(VAR, "var"); addUnit(KILOVAR, "kvar"); addUnit(MEGAVAR, "Mvar"); + addUnit(VARHOUR, "varh"); + addUnit(KILOVARHOUR, "kvarh"); + addUnit(MEGAVARHOUR, "Mvarh"); addUnit(PU, "p.u."); addUnit(EURO, "EUR"); + addUnit(EURO, "€"); addUnit(EURO_PER_KILOMETRE, "EUR/km"); addUnit(EURO_PER_WATTHOUR, "EUR/Wh"); addUnit(EURO_PER_KILOWATTHOUR, "EUR/kWh"); diff --git a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy index 73cc708e..71eededc 100644 --- a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy +++ b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy @@ -9,6 +9,8 @@ import spock.lang.Shared import spock.lang.Specification import tech.units.indriya.quantity.Quantities +import javax.measure.format.MeasurementParseException + import static edu.ie3.util.quantities.PowerSystemUnits.* import static tech.units.indriya.unit.Units.JOULE import static tech.units.indriya.unit.Units.RADIAN @@ -73,4 +75,41 @@ class PowerSystemUnitsTest extends Specification { Math.PI || 180.0 Math.PI * 3 / 2 || 270.0 } + + def "Units are labeled with the correct label of the expected unit symbol"() { + when: + def dut = Quantities.getQuantity(input) + + then: + dut.unit == expectedUnit + + where: + expectedUnit || input + WATTHOUR || "1 Wh" + KILOWATTHOUR_PER_KILOMETRE || "1 kWh/km" + VOLTAMPERE || "1 VA" + PU_PER_HOUR || "1 p.u./h" + VAR || "1 var" + VARHOUR || "1 varh" + PU || "1 p.u." + EURO || "99 EUR" + EURO || "99 €" + MICROFARAD_PER_KILOMETRE || "5 µF/km" + FARAD_PER_KILOMETRE || "3.14 F/km" + DEGREE_GEOM || "42 °" + CUBIC_METRE_PER_SECOND || "430431 m³/s" + PERCENT_PER_HOUR || "4 %/h" + MEGAWATT || "87 MW" + KILOWATTHOUR_PER_KELVIN_TIMES_CUBICMETRE || "2.034 kWh/K*m³" + } + + def "when an unregistered Unit should be parsed an Exception should be thrown"() { + when: + def dut = Quantities.getQuantity("1 kWh") + dut + + then: + MeasurementParseException exception = thrown(MeasurementParseException) + exception.message == "Parse Error" + } }