diff --git a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala index 5d15eac4b3..faf40980db 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -851,7 +851,7 @@ object ThermalGrid { def hasRequiredDemand: Boolean = required > zeroMWh - def hasAdditionalDemand: Boolean = possible > zeroMWh + def hasAdditionalDemand: Boolean = possible > required } object ThermalEnergyDemand { @@ -872,10 +872,15 @@ object ThermalGrid { math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) ) throw new InvalidParameterException( - s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." + s"The possible amount of energy $possible is smaller than the required amount of energy $required. This is not supported." ) - else - new ThermalEnergyDemand(required, possible) + + if (possible.toKilowattHours < 0 || required.toKilowattHours < 0) + throw new InvalidParameterException( + s"The possible $possible or required $required amount of energy cannot be negative. This is not supported." + ) + + new ThermalEnergyDemand(required, possible) } def noDemand: ThermalEnergyDemand = ThermalEnergyDemand( diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index cb09b2bb80..089f1630e5 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -6,6 +6,7 @@ package edu.ie3.simona.model.thermal +import edu.ie3.simona.exceptions.InvalidParameterException import edu.ie3.datamodel.models.input.thermal.ThermalStorageInput import edu.ie3.simona.exceptions.InvalidParameterException import edu.ie3.simona.model.thermal.ThermalGrid.ThermalEnergyDemand @@ -27,7 +28,7 @@ class ThermalGridSpec "Testing the thermal energy demand" when { "instantiating it from given values" should { - "throw exception for non-plausible input (positive)" in { + "throw exception for non-sensible input (positive)" in { val possible = MegawattHours(40d) val required = MegawattHours(42d) @@ -36,15 +37,6 @@ class ThermalGridSpec }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } - "throw exception for non-sensible input (negative)" in { - val possible = MegawattHours(-40d) - val required = MegawattHours(-42d) - - intercept[InvalidParameterException] { - ThermalEnergyDemand(required, possible) - }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." - } - "set the correct values, if they are sensible" in { val possible = MegawattHours(45d) val required = MegawattHours(42d) @@ -92,6 +84,30 @@ class ThermalGridSpec }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } + "throw exception, if required demand is smaller than zero" in { + val required = MegawattHours(-2d) + val possible = MegawattHours(5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + + "throw exception, if possible demand is smaller than zero" in { + val required = MegawattHours(2d) + val possible = MegawattHours(-5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + + "throw exception, if possible and required demand are smaller than zero" in { + val required = MegawattHours(-2d) + val possible = MegawattHours(-5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + "return proper information, if required and additional demand is apparent" in { val required = MegawattHours(45d) val possible = MegawattHours(47d)