Skip to content

Commit

Permalink
test logging
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <[email protected]>
  • Loading branch information
mherwege committed Nov 22, 2024
1 parent 06edb0a commit 39a0bbe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1495,17 +1496,21 @@ 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);
sum = PersistenceExtensions.riemannSumSince(numberItem, start, type, SERVICE_ID);
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
Expand All @@ -1520,17 +1525,21 @@ 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);
sum = PersistenceExtensions.riemannSumSince(numberItem, start, type, SERVICE_ID);
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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -1570,17 +1583,21 @@ 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);
sum = PersistenceExtensions.riemannSumSince(numberItem, start, type, SERVICE_ID);
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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -282,6 +284,7 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear,
.toSeconds();
}
sum += value * duration;
totalDuration += duration;
duration = 0;
}
break;
Expand All @@ -308,6 +311,7 @@ static double riemannSum(@Nullable Integer beginYear, @Nullable Integer endYear,
.toSeconds();
}
sum += value * duration;
totalDuration += duration;
duration = 0;
}
break;
Expand Down Expand Up @@ -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()))
Expand All @@ -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;
Expand Down

0 comments on commit 39a0bbe

Please sign in to comment.