mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 19:55:48 +01:00
Added unit tests for check if persistence extension returns QuantityType if persistence service returns QuantityType (#2029)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
eee30b0c57
commit
90a460dd03
@ -12,7 +12,10 @@
|
||||
*/
|
||||
package org.openhab.core.persistence.extensions;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
@ -21,11 +24,20 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import javax.measure.quantity.Temperature;
|
||||
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.openhab.core.i18n.UnitProvider;
|
||||
import org.openhab.core.items.GenericItem;
|
||||
import org.openhab.core.items.ItemRegistry;
|
||||
import org.openhab.core.items.ItemUtil;
|
||||
import org.openhab.core.library.CoreItemFactory;
|
||||
import org.openhab.core.library.types.DecimalType;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
@ -40,55 +52,67 @@ import org.openhab.core.persistence.PersistenceServiceRegistry;
|
||||
* @author Chris Jackson - Initial contribution
|
||||
* @author Jan N. Klug - Fix averageSince calculation
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.WARN)
|
||||
public class PersistenceExtensionsTest {
|
||||
|
||||
public static final String TEST_NUMBER = "Test Number";
|
||||
public static final String TEST_SWITCH = "Test Switch";
|
||||
public static final String TEST_NUMBER = "testNumber";
|
||||
public static final String TEST_QUANTITY_NUMBER = "testQuantityNumber";
|
||||
public static final String TEST_SWITCH = "testSwitch";
|
||||
|
||||
@NonNullByDefault
|
||||
private final PersistenceServiceRegistry registry = new PersistenceServiceRegistry() {
|
||||
private @Mock UnitProvider unitProvider;
|
||||
private @Mock ItemRegistry mockedItemRegistry;
|
||||
|
||||
private final PersistenceService testPersistenceService = new TestPersistenceService();
|
||||
|
||||
@Override
|
||||
public @Nullable String getDefaultId() {
|
||||
// not available
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PersistenceService getDefault() {
|
||||
// not available
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<PersistenceService> getAll() {
|
||||
return Set.of(testPersistenceService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PersistenceService get(@Nullable String serviceId) {
|
||||
return TestPersistenceService.ID.equals(serviceId) ? testPersistenceService : null;
|
||||
}
|
||||
};
|
||||
|
||||
private CoreItemFactory itemFactory;
|
||||
private GenericItem numberItem, switchItem;
|
||||
private GenericItem numberItem, quantityItem, switchItem;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
new PersistenceExtensions(registry);
|
||||
itemFactory = new CoreItemFactory();
|
||||
when(unitProvider.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);
|
||||
|
||||
CoreItemFactory itemFactory = new CoreItemFactory();
|
||||
numberItem = itemFactory.createItem(CoreItemFactory.NUMBER, TEST_NUMBER);
|
||||
quantityItem = itemFactory.createItem(CoreItemFactory.NUMBER + ItemUtil.EXTENSION_SEPARATOR + "Temperature",
|
||||
TEST_QUANTITY_NUMBER);
|
||||
quantityItem.setUnitProvider(unitProvider);
|
||||
switchItem = itemFactory.createItem(CoreItemFactory.SWITCH, TEST_SWITCH);
|
||||
|
||||
when(mockedItemRegistry.get(TEST_NUMBER)).thenReturn(numberItem);
|
||||
when(mockedItemRegistry.get(TEST_QUANTITY_NUMBER)).thenReturn(quantityItem);
|
||||
when(mockedItemRegistry.get(TEST_SWITCH)).thenReturn(switchItem);
|
||||
|
||||
new PersistenceExtensions(new PersistenceServiceRegistry() {
|
||||
private final PersistenceService testPersistenceService = new TestPersistenceService(mockedItemRegistry);
|
||||
|
||||
@Override
|
||||
public @Nullable String getDefaultId() {
|
||||
// not available
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PersistenceService getDefault() {
|
||||
// not available
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<PersistenceService> getAll() {
|
||||
return Set.of(testPersistenceService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PersistenceService get(@Nullable String serviceId) {
|
||||
return TestPersistenceService.ID.equals(serviceId) ? testPersistenceService : null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoricNumberState() {
|
||||
public void testHistoricStateDecimalType() {
|
||||
HistoricItem historicItem = PersistenceExtensions.historicState(numberItem,
|
||||
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(DecimalType.class)));
|
||||
assertEquals("2012", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.historicState(numberItem,
|
||||
@ -112,6 +136,35 @@ public class PersistenceExtensionsTest {
|
||||
assertNull(historicItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoricStateQuantityType() {
|
||||
HistoricItem historicItem = PersistenceExtensions.historicState(quantityItem,
|
||||
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(QuantityType.class)));
|
||||
assertEquals("2012.0 °C", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.historicState(quantityItem,
|
||||
ZonedDateTime.of(2011, 12, 31, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("2011.0 °C", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.historicState(quantityItem,
|
||||
ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("2011.0 °C", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.historicState(quantityItem,
|
||||
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("2000.0 °C", historicItem.getState().toString());
|
||||
|
||||
// default persistence service
|
||||
historicItem = PersistenceExtensions.historicState(quantityItem,
|
||||
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
|
||||
assertNull(historicItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoricSwitchState() {
|
||||
ZonedDateTime now = ZonedDateTime.now().truncatedTo(ChronoUnit.HOURS);
|
||||
@ -129,17 +182,12 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaximumNumberSince() {
|
||||
numberItem.setState(new QuantityType<>(1, SIUnits.CELSIUS));
|
||||
public void testMaximumSinceDecimalType() {
|
||||
numberItem.setState(new DecimalType(1));
|
||||
HistoricItem historicItem = PersistenceExtensions.maximumSince(numberItem,
|
||||
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("1 °C", historicItem.getState().toString());
|
||||
|
||||
numberItem.setState(new DecimalType(1));
|
||||
historicItem = PersistenceExtensions.maximumSince(numberItem,
|
||||
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(DecimalType.class)));
|
||||
assertEquals("1", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.maximumSince(numberItem,
|
||||
@ -156,7 +204,29 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaximumSwitchSince() {
|
||||
public void testMaximumSinceQuantityType() {
|
||||
quantityItem.setState(QuantityType.valueOf(1, SIUnits.CELSIUS));
|
||||
HistoricItem historicItem = PersistenceExtensions.maximumSince(quantityItem,
|
||||
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(QuantityType.class)));
|
||||
assertEquals("1.0 °C", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.maximumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("2012.0 °C", historicItem.getState().toString());
|
||||
assertEquals(ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), historicItem.getTimestamp());
|
||||
|
||||
// default persistence service
|
||||
historicItem = PersistenceExtensions.maximumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("1.0 °C", historicItem.getState().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaximumSinceSwitch() {
|
||||
switchItem.setState(OnOffType.OFF);
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
@ -177,23 +247,18 @@ public class PersistenceExtensionsTest {
|
||||
assertNotNull(historicItem);
|
||||
assertEquals(OnOffType.OFF, historicItem.getState());
|
||||
|
||||
historicItem = PersistenceExtensions.minimumSince(switchItem, now.plusHours(1), TestPersistenceService.ID);
|
||||
historicItem = PersistenceExtensions.maximumSince(switchItem, now.plusHours(1), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals(OnOffType.OFF, historicItem.getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinimumNumberSince() {
|
||||
numberItem.setState(new QuantityType<>(5000, SIUnits.CELSIUS));
|
||||
public void testMinimumSinceDecimalType() {
|
||||
numberItem.setState(new DecimalType(5000));
|
||||
HistoricItem historicItem = PersistenceExtensions.minimumSince(numberItem,
|
||||
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("5000 °C", historicItem.getState().toString());
|
||||
|
||||
numberItem.setState(new DecimalType(5000));
|
||||
historicItem = PersistenceExtensions.minimumSince(numberItem,
|
||||
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(DecimalType.class)));
|
||||
assertEquals("5000", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.minimumSince(numberItem,
|
||||
@ -210,7 +275,29 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinimumSwitchSince() {
|
||||
public void testMinimumSinceQuantityType() {
|
||||
quantityItem.setState(QuantityType.valueOf(5000, SIUnits.CELSIUS));
|
||||
HistoricItem historicItem = PersistenceExtensions.minimumSince(quantityItem,
|
||||
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertThat(historicItem.getState(), is(instanceOf(QuantityType.class)));
|
||||
assertEquals("5000.0 °C", historicItem.getState().toString());
|
||||
|
||||
historicItem = PersistenceExtensions.minimumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("2005.0 °C", historicItem.getState().toString());
|
||||
assertEquals(ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), historicItem.getTimestamp());
|
||||
|
||||
// default persistence service
|
||||
historicItem = PersistenceExtensions.minimumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
|
||||
assertNotNull(historicItem);
|
||||
assertEquals("5000.0 °C", historicItem.getState().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinimumSinceSwitch() {
|
||||
switchItem.setState(OnOffType.ON);
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
@ -253,18 +340,13 @@ public class PersistenceExtensionsTest {
|
||||
assertNotNull(variance);
|
||||
assertEquals(expected, variance.doubleValue(), 0.01);
|
||||
|
||||
numberItem.setState(new QuantityType<>(3025, SIUnits.CELSIUS));
|
||||
variance = PersistenceExtensions.varianceSince(numberItem, startStored, TestPersistenceService.ID);
|
||||
assertNotNull(variance);
|
||||
assertEquals(expected, variance.doubleValue(), 0.01);
|
||||
|
||||
// default persistence service
|
||||
variance = PersistenceExtensions.varianceSince(numberItem, startStored);
|
||||
assertNull(variance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviationSince() {
|
||||
public void testDeviationSinceDecimalType() {
|
||||
numberItem.setState(new DecimalType(3025));
|
||||
|
||||
ZonedDateTime startStored = ZonedDateTime.of(2003, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
@ -281,18 +363,36 @@ public class PersistenceExtensionsTest {
|
||||
assertNotNull(deviation);
|
||||
assertEquals(expected, deviation.doubleValue(), 0.01);
|
||||
|
||||
numberItem.setState(new QuantityType<>(3025, SIUnits.CELSIUS));
|
||||
deviation = PersistenceExtensions.deviationSince(numberItem, startStored, TestPersistenceService.ID);
|
||||
assertNotNull(deviation);
|
||||
assertEquals(expected, deviation.doubleValue(), 0.01);
|
||||
|
||||
// default persistence service
|
||||
deviation = PersistenceExtensions.deviationSince(numberItem, startStored);
|
||||
assertNull(deviation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAverageNumberSince() {
|
||||
public void testDeviationSinceQuantityType() {
|
||||
quantityItem.setState(QuantityType.valueOf(3025, SIUnits.CELSIUS));
|
||||
|
||||
ZonedDateTime startStored = ZonedDateTime.of(2003, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
ZonedDateTime endStored = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
long storedInterval = endStored.toInstant().toEpochMilli() - startStored.toInstant().toEpochMilli();
|
||||
long recentInterval = Instant.now().toEpochMilli() - endStored.toInstant().toEpochMilli();
|
||||
double expectedAverage = (2007.4994 * storedInterval + 2518.5 * recentInterval)
|
||||
/ (storedInterval + recentInterval);
|
||||
double expected = Math.sqrt(IntStream.of(2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012)
|
||||
.mapToDouble(i -> Double.parseDouble(Integer.toString(i))).map(d -> Math.pow(d - expectedAverage, 2))
|
||||
.sum() / 10d);
|
||||
DecimalType deviation = PersistenceExtensions.deviationSince(quantityItem, startStored,
|
||||
TestPersistenceService.ID);
|
||||
assertNotNull(deviation);
|
||||
assertEquals(expected, deviation.doubleValue(), 0.01);
|
||||
|
||||
// default persistence service
|
||||
deviation = PersistenceExtensions.deviationSince(quantityItem, startStored);
|
||||
assertNull(deviation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAverageSinceDecimalType() {
|
||||
numberItem.setState(new DecimalType(3025));
|
||||
|
||||
ZonedDateTime startStored = ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
@ -308,18 +408,35 @@ public class PersistenceExtensionsTest {
|
||||
assertNotNull(average);
|
||||
assertEquals(expected, average.doubleValue(), 0.01);
|
||||
|
||||
numberItem.setState(new QuantityType<>(3025, SIUnits.CELSIUS));
|
||||
average = PersistenceExtensions.averageSince(numberItem, startStored, TestPersistenceService.ID);
|
||||
assertNotNull(average);
|
||||
assertEquals(expected, average.doubleValue(), 0.01);
|
||||
|
||||
// default persistence service
|
||||
average = PersistenceExtensions.averageSince(numberItem, startStored);
|
||||
assertNull(average);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAverageSwitchSince() {
|
||||
public void testAverageSinceQuantityType() {
|
||||
quantityItem.setState(QuantityType.valueOf(3025, SIUnits.CELSIUS));
|
||||
|
||||
ZonedDateTime startStored = ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
DecimalType average = PersistenceExtensions.averageSince(quantityItem, startStored, TestPersistenceService.ID);
|
||||
assertNull(average);
|
||||
|
||||
startStored = ZonedDateTime.of(2003, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
|
||||
Instant endStored = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant();
|
||||
long storedInterval = endStored.toEpochMilli() - startStored.toInstant().toEpochMilli();
|
||||
long recentInterval = Instant.now().toEpochMilli() - endStored.toEpochMilli();
|
||||
double expected = (2007.4994 * storedInterval + 2518.5 * recentInterval) / (storedInterval + recentInterval);
|
||||
average = PersistenceExtensions.averageSince(quantityItem, startStored, TestPersistenceService.ID);
|
||||
assertNotNull(average);
|
||||
assertEquals(expected, average.doubleValue(), 0.01);
|
||||
|
||||
// default persistence service
|
||||
average = PersistenceExtensions.averageSince(quantityItem, startStored);
|
||||
assertNull(average);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAverageSinceSwitch() {
|
||||
switchItem.setState(OnOffType.ON);
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now().truncatedTo(ChronoUnit.MINUTES);
|
||||
@ -352,7 +469,7 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSumSince() {
|
||||
public void testSumSinceDecimalType() {
|
||||
DecimalType sum = PersistenceExtensions.sumSince(numberItem,
|
||||
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(sum);
|
||||
@ -370,6 +487,25 @@ public class PersistenceExtensionsTest {
|
||||
assertEquals(0.0, sum.doubleValue(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSumSinceQuantityType() {
|
||||
DecimalType sum = PersistenceExtensions.sumSince(quantityItem,
|
||||
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(sum);
|
||||
assertEquals(0.0, sum.doubleValue(), 0.001);
|
||||
|
||||
sum = PersistenceExtensions.sumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
|
||||
assertNotNull(sum);
|
||||
assertEquals(IntStream.of(2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012).sum(), sum.doubleValue(), 0.001);
|
||||
|
||||
// default persistence service
|
||||
sum = PersistenceExtensions.sumSince(quantityItem,
|
||||
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
|
||||
assertNotNull(sum);
|
||||
assertEquals(0.0, sum.doubleValue(), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastUpdate() {
|
||||
numberItem.setState(new DecimalType(2005));
|
||||
@ -433,10 +569,15 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousStateNoSkip() {
|
||||
numberItem.setState(new DecimalType(4321));
|
||||
public void testPreviousStateDecimalTypeNoSkip() {
|
||||
HistoricItem prevStateItem = PersistenceExtensions.previousState(numberItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertThat(prevStateItem.getState(), is(instanceOf(DecimalType.class)));
|
||||
assertEquals("2012", prevStateItem.getState().toString());
|
||||
|
||||
numberItem.setState(new DecimalType(4321));
|
||||
prevStateItem = PersistenceExtensions.previousState(numberItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012", prevStateItem.getState().toString());
|
||||
|
||||
numberItem.setState(new DecimalType(2012));
|
||||
@ -444,7 +585,7 @@ public class PersistenceExtensionsTest {
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012", prevStateItem.getState().toString());
|
||||
|
||||
numberItem.setState(new QuantityType<>(3025, SIUnits.CELSIUS));
|
||||
numberItem.setState(new DecimalType(3025));
|
||||
prevStateItem = PersistenceExtensions.previousState(numberItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012", prevStateItem.getState().toString());
|
||||
@ -455,22 +596,57 @@ public class PersistenceExtensionsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousStateSkip() {
|
||||
public void testPreviousStateQuantityTypeNoSkip() {
|
||||
HistoricItem prevStateItem = PersistenceExtensions.previousState(quantityItem, false,
|
||||
TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertThat(prevStateItem.getState(), is(instanceOf(QuantityType.class)));
|
||||
assertEquals("2012.0 °C", prevStateItem.getState().toString());
|
||||
|
||||
quantityItem.setState(QuantityType.valueOf(4321, SIUnits.CELSIUS));
|
||||
prevStateItem = PersistenceExtensions.previousState(quantityItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012.0 °C", prevStateItem.getState().toString());
|
||||
|
||||
quantityItem.setState(QuantityType.valueOf(2012, SIUnits.CELSIUS));
|
||||
prevStateItem = PersistenceExtensions.previousState(quantityItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012.0 °C", prevStateItem.getState().toString());
|
||||
|
||||
quantityItem.setState(QuantityType.valueOf(3025, SIUnits.CELSIUS));
|
||||
prevStateItem = PersistenceExtensions.previousState(quantityItem, false, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012.0 °C", prevStateItem.getState().toString());
|
||||
|
||||
// default persistence service
|
||||
prevStateItem = PersistenceExtensions.previousState(quantityItem, false);
|
||||
assertNull(prevStateItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousStateDecimalTypeSkip() {
|
||||
numberItem.setState(new DecimalType(2012));
|
||||
HistoricItem prevStateItem = PersistenceExtensions.previousState(numberItem, true, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2011", prevStateItem.getState().toString());
|
||||
|
||||
numberItem.setState(new QuantityType<>(2012, SIUnits.CELSIUS));
|
||||
prevStateItem = PersistenceExtensions.previousState(numberItem, true, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2012", prevStateItem.getState().toString());
|
||||
|
||||
// default persistence service
|
||||
prevStateItem = PersistenceExtensions.previousState(numberItem, true);
|
||||
assertNull(prevStateItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreviousStateQuantityTypeSkip() {
|
||||
quantityItem.setState(QuantityType.valueOf(2012, SIUnits.CELSIUS));
|
||||
HistoricItem prevStateItem = PersistenceExtensions.previousState(quantityItem, true, TestPersistenceService.ID);
|
||||
assertNotNull(prevStateItem);
|
||||
assertEquals("2011.0 °C", prevStateItem.getState().toString());
|
||||
|
||||
// default persistence service
|
||||
prevStateItem = PersistenceExtensions.previousState(quantityItem, true);
|
||||
assertNull(prevStateItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangedSince() {
|
||||
boolean changed = PersistenceExtensions.changedSince(numberItem,
|
||||
|
@ -21,11 +21,16 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.measure.Unit;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.items.Item;
|
||||
import org.openhab.core.items.ItemRegistry;
|
||||
import org.openhab.core.library.items.NumberItem;
|
||||
import org.openhab.core.library.types.DecimalType;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
import org.openhab.core.library.types.QuantityType;
|
||||
import org.openhab.core.persistence.FilterCriteria;
|
||||
import org.openhab.core.persistence.FilterCriteria.Ordering;
|
||||
import org.openhab.core.persistence.HistoricItem;
|
||||
@ -44,6 +49,12 @@ public class TestPersistenceService implements QueryablePersistenceService {
|
||||
|
||||
public static final String ID = "test";
|
||||
|
||||
private final ItemRegistry itemRegistry;
|
||||
|
||||
public TestPersistenceService(ItemRegistry itemRegistry) {
|
||||
this.itemRegistry = itemRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return ID;
|
||||
@ -96,9 +107,7 @@ public class TestPersistenceService implements QueryablePersistenceService {
|
||||
Collections.reverse(results);
|
||||
}
|
||||
return results;
|
||||
} else
|
||||
|
||||
{
|
||||
} else {
|
||||
int startValue = 1950;
|
||||
int endValue = 2012;
|
||||
|
||||
@ -124,7 +133,9 @@ public class TestPersistenceService implements QueryablePersistenceService {
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
return new DecimalType(year);
|
||||
Item item = itemRegistry.get(filter.getItemName());
|
||||
Unit<?> unit = item instanceof NumberItem ? ((NumberItem) item).getUnit() : null;
|
||||
return unit == null ? new DecimalType(year) : QuantityType.valueOf(year, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user