From 39a0bbe28e797be1b7cccf14a2115b5c086a50c8 Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Fri, 22 Nov 2024 18:07:32 +0100 Subject: [PATCH] test logging Signed-off-by: Mark Herwege --- .../extensions/PersistenceExtensionsTest.java | 47 ++++++++++++++----- .../extensions/TestPersistenceService.java | 24 +++++++--- 2 files changed, 53 insertions(+), 18 deletions(-) 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 b5159c2daca..02faeaee7a0 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 @@ -54,6 +54,7 @@ import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.persistence.extensions.PersistenceExtensions.RiemannType; import org.openhab.core.types.State; +import org.slf4j.LoggerFactory; /** * @author Kai Kreuzer - Initial contribution @@ -1495,8 +1496,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); DecimalType dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since left expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); start = ZonedDateTime.of(HISTORIC_INTERMEDIATE_VALUE_1, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()); expected = riemannSum(HISTORIC_INTERMEDIATE_VALUE_1, null, type); @@ -1504,8 +1507,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since left expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service @@ -1520,8 +1525,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since right expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); start = ZonedDateTime.of(HISTORIC_INTERMEDIATE_VALUE_1, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()); expected = riemannSum(HISTORIC_INTERMEDIATE_VALUE_1, null, type); @@ -1529,8 +1536,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since right expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service @@ -1545,6 +1554,8 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class) + .info("Test Riemann since trapezoidal expected {}, sum {}", expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve // now assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); @@ -1554,8 +1565,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class) + .info("Test Riemann since trapezoidal expected {}, sum {}", expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service @@ -1570,8 +1583,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since midpoint expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); start = ZonedDateTime.of(HISTORIC_INTERMEDIATE_VALUE_1, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()); expected = riemannSum(HISTORIC_INTERMEDIATE_VALUE_1, null, type); @@ -1579,8 +1594,10 @@ public void testRiemannSumSinceDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann since midpoint expected {}, sum {}", + expected, sum); // Allow max 1 min difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 60.0); // default persistence service @@ -1598,8 +1615,10 @@ public void testRiemannSumUntilDecimalType() { assertNotNull(sum); DecimalType dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann until left expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service sum = PersistenceExtensions.riemannSumUntil(numberItem, end, type); @@ -1613,8 +1632,10 @@ public void testRiemannSumUntilDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann until right expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service sum = PersistenceExtensions.riemannSumUntil(numberItem, end, type); @@ -1628,8 +1649,10 @@ public void testRiemannSumUntilDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class) + .info("Test Riemann until trapezoidal expected {}, sum {}", expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service sum = PersistenceExtensions.riemannSumUntil(numberItem, end, type); @@ -1643,8 +1666,10 @@ public void testRiemannSumUntilDecimalType() { assertNotNull(sum); dt = sum.as(DecimalType.class); assertNotNull(dt); + LoggerFactory.getLogger(PersistenceExtensionsTest.class).info("Test Riemann until midpoint expected {}, sum {}", + expected, sum); // Allow max 5s difference between method and test, required as both expected and method tested retrieve - // now + // now from system assertEquals(expected, dt.doubleValue(), HISTORIC_END * 5.0); // default persistence service sum = PersistenceExtensions.riemannSumUntil(numberItem, end, type); diff --git a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/TestPersistenceService.java b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/TestPersistenceService.java index 188b0dd5d66..e47dcb7484a 100644 --- a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/TestPersistenceService.java +++ b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/TestPersistenceService.java @@ -44,6 +44,7 @@ import org.openhab.core.persistence.extensions.PersistenceExtensions.RiemannType; import org.openhab.core.persistence.strategy.PersistenceStrategy; import org.openhab.core.types.State; +import org.slf4j.LoggerFactory; /** * A simple persistence service used for unit tests @@ -258,6 +259,7 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear, double sum = 0; int index = begin; long duration = 0; + long totalDuration = 0; switch (type) { case left: if (beginYear == null) { @@ -282,6 +284,7 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear, .toSeconds(); } sum += value * duration; + totalDuration += duration; duration = 0; } break; @@ -308,6 +311,7 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear, .toSeconds(); } sum += value * duration; + totalDuration += duration; duration = 0; } break; @@ -335,12 +339,15 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear, .toSeconds(); } sum += value * duration; + totalDuration += duration; duration = 0; } + LoggerFactory.getLogger(TestPersistenceService.class).info("Test Riemann now {}", now); + LoggerFactory.getLogger(TestPersistenceService.class).info("Test Riemann trapezoidal total duration {}", + totalDuration); break; case midpoint: int nextIndex = begin; - long nextDuration = 0; if (beginYear == null) { duration = Duration .between(now, ZonedDateTime.of(now.getYear() + 1, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())) @@ -364,19 +371,22 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear, nextIndex++; } nextIndex++; + long nextDuration = Duration + .between(ZonedDateTime.of(bucketStart, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), + ZonedDateTime.of(nextIndex, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())) + .toSeconds(); if (endYear == null && nextIndex == end) { - nextDuration = Duration + nextDuration += Duration .between(ZonedDateTime.of(now.getYear(), 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), now) .toSeconds(); } - nextDuration += Duration - .between(ZonedDateTime.of(bucketStart, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), - ZonedDateTime.of(nextIndex, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())) - .toSeconds(); sum += value * (duration + nextDuration) / 2.0; + totalDuration += (duration + nextDuration) / 2; duration = 0; - nextDuration = 0; } + LoggerFactory.getLogger(TestPersistenceService.class).info("Test Riemann now {}", now); + LoggerFactory.getLogger(TestPersistenceService.class).info("Test Riemann midpoint total duration {}", + totalDuration); break; } return sum;