From 7a9b76dfdfedaa3b5bd3016961378bcc01ca654d Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Sun, 16 Apr 2023 00:03:45 +1000 Subject: [PATCH] Fix division-by-zero error in Persistence average (#3556) * Fix division-by-zero error in Persistence average * Add test Signed-off-by: Jimmy Tanagra --- .../persistence/extensions/PersistenceExtensions.java | 3 ++- .../extensions/PersistenceExtensionsTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java index b0807792a..1b8984c81 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java @@ -824,7 +824,8 @@ public class PersistenceExtensions { if (firstTimestamp != null) { BigDecimal totalDuration = BigDecimal.valueOf(Duration.between(firstTimestamp, endTime).toMillis()); - return new DecimalType(sum.divide(totalDuration, MathContext.DECIMAL64)); + return totalDuration.signum() == 0 ? null + : new DecimalType(sum.divide(totalDuration, MathContext.DECIMAL64)); } return null; diff --git a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java index 6d8ea45d5..973038006 100644 --- a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java +++ b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java @@ -18,6 +18,7 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.closeTo; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -680,6 +681,15 @@ public class PersistenceExtensionsTest { assertThat(average, is(nullValue())); } + @Test + public void testAverageBetweenZeroDuration() { + ZonedDateTime now = ZonedDateTime.now(); + assertDoesNotThrow( + () -> PersistenceExtensions.averageBetween(quantityItem, now, now, TestPersistenceService.ID)); + assertThat(PersistenceExtensions.averageBetween(quantityItem, now, now, TestPersistenceService.ID), + is(nullValue())); + } + @Test public void testSumSinceDecimalType() { DecimalType sum = PersistenceExtensions.sumSince(numberItem,