Skip to content

Commit

Permalink
calculateSavedCarbonDelta and calculateSavedCarbonPercentage return n…
Browse files Browse the repository at this point in the history
…ull if no calculation possible
  • Loading branch information
lwluc authored and maxbehr801 committed Jan 10, 2025
1 parent 3598f1a commit 9a220c0
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Map<String, Object> mapFromDomain(CarbonReduction output, Map<String, Obj
variables.put("executionDelayed", output.getDelay().isExecutionDelayed());
variables.put("carbonWithoutOptimization", carbonWithoutOptimization);
variables.put("optimalForecastedCarbon", output.getOptimalForecastedCarbon().getValue());
variables.put("savedCarbonPercentage", output.getSavedCarbonPercentage().getValue());
variables.put("savedCarbonPercentage", Optional.ofNullable(output.getSavedCarbonPercentage()).map(ValueObject::getValue).orElse(null));
variables.put("reducedCarbon", Optional.ofNullable(output.calculateReduction()).map(ValueObject::getValue).orElse(null));
variables.put("delayedBy", output.getDelay().getDelayedBy());
// Override milestone variable because joda time is not a primitive object ..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void should_map_mandatory_fields_to_map() {
new Delay(true, 3),
null,
new Carbon(2.0),
new Percentage(3.0)
null
);
Map<String, Object> variables = Map.of("milestone", mileStoneDateString);

Expand All @@ -113,7 +113,7 @@ void should_map_mandatory_fields_to_map() {
softAssertions.assertThat(result.get("executionDelayed")).isEqualTo(carbonReduction.getDelay().isExecutionDelayed());
softAssertions.assertThat(result.get("carbonWithoutOptimization")).isNull();
softAssertions.assertThat(result.get("optimalForecastedCarbon")).isEqualTo(carbonReduction.getOptimalForecastedCarbon().getValue());
softAssertions.assertThat(result.get("savedCarbonPercentage")).isEqualTo(carbonReduction.getSavedCarbonPercentage().getValue());
softAssertions.assertThat(result.get("savedCarbonPercentage")).isNull();
softAssertions.assertThat(result.get("reducedCarbon")).isNull();
softAssertions.assertThat(result.get("delayedBy")).isEqualTo(carbonReduction.getDelay().getDelayedBy());
softAssertions.assertThat(result.get("milestone")).isEqualTo(mileStoneDateString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CarbonReductorOutputVariable {
private boolean executionDelayed;
private Double carbonWithoutOptimization;
private double optimalForecastedCarbon;
private double savedCarbonPercentage;
private Double savedCarbonPercentage;
private Double carbonReduction;
private long delayedBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public CarbonReductorOutputVariable mapFromDomain(CarbonReduction output) {
outputVariable.setDelayedBy(output.getDelay().getDelayedBy());
outputVariable.setOptimalForecastedCarbon(output.getOptimalForecastedCarbon().getValue());
outputVariable.setCarbonWithoutOptimization(carbonWithoutOptimization);
outputVariable.setSavedCarbonPercentage(output.getSavedCarbonPercentage().getValue());
outputVariable.setSavedCarbonPercentage(Optional.ofNullable(output.getSavedCarbonPercentage()).map(ValueObject::getValue).orElse(null));
outputVariable.setCarbonReduction(Optional.ofNullable(output.calculateReduction()).map(ValueObject::getValue).orElse(null));
return outputVariable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static CarbonReduction createCarbonReductorOutputWithoutCurrentValue() {
new Delay(true, 3),
null,
new Carbon(2.0),
new Percentage(3.0)
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void should_map_mandatory_fields_from_domain() {
softAssertions.assertThat(result.getDelayedBy()).isEqualTo(outputDDD.getDelay().getDelayedBy());
softAssertions.assertThat(result.getOptimalForecastedCarbon()).isEqualTo(outputDDD.getOptimalForecastedCarbon().getValue());
softAssertions.assertThat(result.getCarbonWithoutOptimization()).isNull();
softAssertions.assertThat(result.getSavedCarbonPercentage()).isEqualTo(outputDDD.getSavedCarbonPercentage().getValue());
softAssertions.assertThat(result.getSavedCarbonPercentage()).isNull();
softAssertions.assertThat(result.getCarbonReduction()).isNull();
softAssertions.assertAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public CarbonReduction(Delay delay, Carbon carbonWithoutOptimization, Carbon opt
protected void validate() {
validateNotNull(delay, "Delay");
validateNotNull(optimalForecastedCarbon, "actual Carbon");
validateNotNull(savedCarbonPercentage, "saved Carbon");
evaluateValidations();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public boolean isCleanerEnergyInFuture() {
return emissionReduction && optimalTime.isInFuture();
}

public double calculateSavedCarbonDelta() {
public Double calculateSavedCarbonDelta() {
if (earliestForecastedValue == null) {
return 0.0;
return null;
}
return earliestForecastedValue.getValue() - optimalValue.getValue();
}

public double calculateSavedCarbonPercentage() {
public Double calculateSavedCarbonPercentage() {
if (earliestForecastedValue == null) {
return 100;
return null;
}
double difference = earliestForecastedValue.getValue() - optimalValue.getValue();
return (difference / earliestForecastedValue.getValue()) * 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public CarbonReduction calculateDelay(CarbonReductorConfiguration input) throws
}

boolean isDelayNecessary = input.isDelayStillRelevant() && emissionTimeframe.isCleanerEnergyInFuture();
boolean isGreaterThanMinimumThreshold = input.getThreshold().isGreaterThanMinimumThreshold(
emissionTimeframe.calculateSavedCarbonDelta()
);
boolean isGreaterThanMinimumThreshold = Optional.ofNullable(emissionTimeframe.calculateSavedCarbonDelta())
.map(savedCarbonDelta -> input.getThreshold().isGreaterThanMinimumThreshold(savedCarbonDelta))
.orElse(false);

final Carbon carbonWithoutOptimization = Optional.ofNullable(emissionTimeframe.getEarliestForecastedValue())
.map(ValueObject::getValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ void should_throw_if_no_actual_carbon() {
assertThatThrownBy(() -> new CarbonReduction(delay, carbon, null, percentage))
.isInstanceOf(InvariantException.class);
}

@Test
void should_throw_if_no_saved_carbon() {
assertThatThrownBy(() -> new CarbonReduction(delay, carbon, carbon, null))
.isInstanceOf(InvariantException.class);
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ void should_calculate_saved_carbon_delta() {
}

@Test
void should_return_zero_on_calculate_saved_carbon_delta_if_earliest_is_null() {
void should_return_null_on_calculate_saved_carbon_delta_if_earliest_is_null() {
EmissionTimeframe emissionTimeframe = new EmissionTimeframe(optimalTime, null, forecastedValue);

assertThat(emissionTimeframe.calculateSavedCarbonDelta()).isZero();
assertThat(emissionTimeframe.calculateSavedCarbonDelta()).isNull();
}
}

Expand All @@ -106,10 +106,10 @@ void should_calculate_saved_carbon_in_percentage() {
}

@Test
void should_return_optimal_on_calculate_saved_carbon_in_percentage_if_earliest_is_null() {
void should_return_null_on_calculate_saved_carbon_in_percentage_if_earliest_is_null() {
EmissionTimeframe emissionTimeframe = new EmissionTimeframe(optimalTime, null, forecastedValue);

assertThat(emissionTimeframe.calculateSavedCarbonPercentage()).isEqualTo(100);
assertThat(emissionTimeframe.calculateSavedCarbonPercentage()).isNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ void shouldNotCalculateDelayBecauseBelowThreshold() throws CarbonReductorExcepti
Assertions.assertThat(result.getSavedCarbonPercentage().getValue()).isCloseTo(0, offset(0.1));
}

@Test
void shouldNotCalculateDelayBecauseNoThresholdPossible() throws CarbonReductorException, CarbonEmissionQueryException {
EmissionTimeframe emissionTimeframeIn3HoursWithoutActualCarbon = new EmissionTimeframe(
new OptimalTime(java.time.OffsetDateTime.now().plusHours(3)),
null,
new ForecastedValue(15.0)
);
CarbonReductorConfiguration inputConfig = new CarbonReductorConfiguration(
Locations.NORWAY_EAST.asLocation(),
new Milestone(createTimestamp(1)),
new ProcessDuration("PT5H"),
new ProcessDuration("PT10H"),
null,
false,
new Threshold(true, 200.0f));
when(carbonEmissionQueryMock.getEmissionTimeframe(eq(inputConfig.getLocation()),
any(ProcessDuration.class), eq(inputConfig.getRemainingProcessDuration()))).thenReturn(emissionTimeframeIn3HoursWithoutActualCarbon);

CarbonReduction result = classUnderTest.calculateDelay(inputConfig);

Assertions.assertThat(result.getDelay().isExecutionDelayed()).isFalse();
Assertions.assertThat(result.getDelay().getDelayedBy()).isZero();
Assertions.assertThat(result.getOptimalForecastedCarbon().getValue()).isEqualTo(emissionTimeframeIn3HoursWithoutActualCarbon.getOptimalValue().getValue());
Assertions.assertThat(result.getCarbonWithoutOptimization()).isNull();
Assertions.assertThat(result.getSavedCarbonPercentage().getValue()).isCloseTo(0, offset(0.1));
}

@Test
@DisplayName("should calculate no delay because maximumDuration would be breached")
void shouldCalculateNoDelayBecauseProcessIsRunningVeryLong() throws CarbonReductorException, CarbonEmissionQueryException {
Expand Down Expand Up @@ -181,6 +208,7 @@ void shouldCalculateNoDelayBecauseEnergyIsOptimal() throws CarbonReductorExcepti
Assertions.assertThat(result.getSavedCarbonPercentage().getValue()).isZero();
}


@ParameterizedTest(name = "{0}")
@DisplayName("should calculate delay duration")
@MethodSource("provideDelayDurationInput")
Expand Down

0 comments on commit 9a220c0

Please sign in to comment.