switched PersistenceExtensions to ZonedDateTime (#1588)

Signed-off-by: Kai Kreuzer <kai@openhab.org>
pull/1591/head
Kai Kreuzer 2020-08-12 21:59:55 +02:00 committed by GitHub
parent 7300734585
commit 3c83e8a8eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 101 additions and 180 deletions

View File

@ -332,12 +332,12 @@ public class PersistenceResource implements RESTResource {
// to avoid diagonal lines
if (state instanceof OnOffType || state instanceof OpenClosedType) {
if (lastItem != null) {
dto.addData(historicItem.getTimestamp().getTime(), lastItem.getState());
dto.addData(historicItem.getTimestamp().toInstant().toEpochMilli(), lastItem.getState());
quantity++;
}
}
dto.addData(historicItem.getTimestamp().getTime(), state);
dto.addData(historicItem.getTimestamp().toInstant().toEpochMilli(), state);
quantity++;
lastItem = historicItem;
}

View File

@ -28,6 +28,7 @@ Import-Package: \
org.openhab.core.library.types,\
org.openhab.core.library.unit,\
org.openhab.core.persistence,\
org.openhab.core.persistence.extensions,\
org.openhab.core.scheduler,\
org.openhab.core.thing,\
org.openhab.core.thing.binding,\
@ -41,8 +42,6 @@ Import-Package: \
org.openhab.core.io.net.exec,\
org.openhab.core.io.net.http,\
org.openhab.core.model.core,\
org.openhab.core.model.persistence.extensions,\
org.openhab.core.model.script.engine.action,\
com.google.common.*;version="14",\
javax.measure.*,\
org.apache.*,\

View File

@ -12,8 +12,8 @@
*/
package org.openhab.core.model.script.internal.engine.action;
import org.openhab.core.model.persistence.extensions.PersistenceExtensions;
import org.openhab.core.model.script.engine.action.ActionService;
import org.openhab.core.persistence.extensions.PersistenceExtensions;
import org.osgi.service.component.annotations.Component;
/**

View File

@ -25,16 +25,11 @@ import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.model.persistence.extensions.PersistenceExtensions;
import org.openhab.core.model.script.actions.Audio;
import org.openhab.core.model.script.actions.BusEvent;
import org.openhab.core.model.script.actions.Exec;
import org.openhab.core.model.script.actions.HTTP;
import org.openhab.core.model.script.actions.LogAction;
import org.openhab.core.model.script.actions.Ping;
import org.openhab.core.model.script.actions.ScriptExecution;
import org.openhab.core.model.script.actions.Things;
import org.openhab.core.model.script.actions.Voice;
import org.openhab.core.model.script.engine.IActionServiceProvider;
import org.openhab.core.model.script.engine.IThingActionsProvider;
import org.openhab.core.model.script.engine.action.ActionService;
@ -74,14 +69,11 @@ public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedFeatures {
result.remove(double.class);
result.add(NumberExtensions.class);
result.add(URLEncoder.class);
result.add(PersistenceExtensions.class);
result.add(ScriptExecution.class);
result.add(BusEvent.class);
result.add(Exec.class);
result.add(HTTP.class);
result.add(Ping.class);
result.add(Audio.class);
result.add(Voice.class);
result.add(Things.class);
result.addAll(getActionClasses());
return result;
@ -90,15 +82,11 @@ public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedFeatures {
@Override
protected List<Class<?>> getStaticImportClasses() {
List<Class<?>> result = super.getStaticImportClasses();
result.add(ScriptExecution.class);
result.add(BusEvent.class);
result.add(Exec.class);
result.add(HTTP.class);
result.add(Ping.class);
result.add(ScriptExecution.class);
result.add(LogAction.class);
result.add(Audio.class);
result.add(Voice.class);
result.add(Things.class);
result.add(ImperialUnits.class);
result.add(MetricPrefix.class);

View File

@ -12,7 +12,7 @@
*/
package org.openhab.core.persistence;
import java.util.Date;
import java.time.ZonedDateTime;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.items.Item;
@ -36,7 +36,7 @@ public interface HistoricItem {
*
* @return the timestamp of the item
*/
Date getTimestamp();
ZonedDateTime getTimestamp();
/**
* returns the current state of the item

View File

@ -10,17 +10,15 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.persistence.extensions;
package org.openhab.core.persistence.extensions;
import java.math.BigDecimal;
import java.math.MathContext;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.Item;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.persistence.FilterCriteria;
@ -30,6 +28,7 @@ import org.openhab.core.persistence.PersistenceService;
import org.openhab.core.persistence.PersistenceServiceRegistry;
import org.openhab.core.persistence.QueryablePersistenceService;
import org.openhab.core.types.State;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.LoggerFactory;
@ -51,30 +50,12 @@ public class PersistenceExtensions {
private static final BigDecimal BIG_DECIMAL_TWO = BigDecimal.valueOf(2);
private static PersistenceServiceRegistry registry;
private static TimeZoneProvider timeZoneProvider;
public PersistenceExtensions() {
// default constructor, necessary for osgi-ds
}
@Reference
protected void setPersistenceServiceRegistry(PersistenceServiceRegistry registry) {
@Activate
public PersistenceExtensions(@Reference PersistenceServiceRegistry registry) {
PersistenceExtensions.registry = registry;
}
protected void unsetPersistenceServiceRegistry(PersistenceServiceRegistry registry) {
PersistenceExtensions.registry = null;
}
@Reference
protected void setTimeZoneProvider(TimeZoneProvider timeZoneProvider) {
PersistenceExtensions.timeZoneProvider = timeZoneProvider;
}
protected void unsetTimeZoneProvider(TimeZoneProvider timeZoneProvider) {
PersistenceExtensions.timeZoneProvider = null;
}
private static PersistenceService getService(String serviceId) {
PersistenceService service = null;
if (registry != null) {
@ -139,7 +120,7 @@ public class PersistenceExtensions {
* the default persistence service is not available or does not refer to a
* {@link QueryablePersistenceService}
*/
public static HistoricItem historicState(Item item, Instant timestamp) {
public static HistoricItem historicState(Item item, ZonedDateTime timestamp) {
return historicState(item, timestamp, getDefaultServiceId());
}
@ -154,12 +135,12 @@ public class PersistenceExtensions {
* if the provided <code>serviceId</code> does not refer to an available
* {@link QueryablePersistenceService}
*/
public static HistoricItem historicState(Item item, Instant timestamp, String serviceId) {
public static HistoricItem historicState(Item item, ZonedDateTime timestamp, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setEndDate(ZonedDateTime.ofInstant(timestamp, timeZoneProvider.getTimeZone()));
filter.setEndDate(timestamp);
filter.setItemName(item.getName());
filter.setPageSize(1);
filter.setOrdering(Ordering.DESCENDING);
@ -185,7 +166,7 @@ public class PersistenceExtensions {
* @return <code>true</code> if item state has changed, <code>false</code> if it has not changed or if the default
* persistence service is not available or does not refer to a {@link QueryablePersistenceService}
*/
public static Boolean changedSince(Item item, Instant timestamp) {
public static Boolean changedSince(Item item, ZonedDateTime timestamp) {
return changedSince(item, timestamp, getDefaultServiceId());
}
@ -199,7 +180,7 @@ public class PersistenceExtensions {
* @return <code>true</code> if item state has changed, or <code>false</code> if it has not changed or if the
* provided <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
*/
public static Boolean changedSince(Item item, Instant timestamp, String serviceId) {
public static Boolean changedSince(Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
HistoricItem itemThen = historicState(item, timestamp);
@ -231,7 +212,7 @@ public class PersistenceExtensions {
* {@link QueryablePersistenceService}, or <code>null</code> if the default persistence service is not
* available
*/
public static Boolean updatedSince(Item item, Instant timestamp) {
public static Boolean updatedSince(Item item, ZonedDateTime timestamp) {
return updatedSince(item, timestamp, getDefaultServiceId());
}
@ -246,7 +227,7 @@ public class PersistenceExtensions {
* since <code>timestamp</code> or if the given <code>serviceId</code> does not refer to a
* {@link QueryablePersistenceService}
*/
public static Boolean updatedSince(Item item, Instant timestamp, String serviceId) {
public static Boolean updatedSince(Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
if (result.iterator().hasNext()) {
return true;
@ -265,7 +246,7 @@ public class PersistenceExtensions {
* constructed from the <code>item</code> if the default persistence service does not refer to a
* {@link QueryablePersistenceService}
*/
public static HistoricItem maximumSince(Item item, Instant timestamp) {
public static HistoricItem maximumSince(Item item, ZonedDateTime timestamp) {
return maximumSince(item, timestamp, getDefaultServiceId());
}
@ -281,7 +262,7 @@ public class PersistenceExtensions {
* maximum value or if the given <code>serviceId</code> does not refer to an available
* {@link QueryablePersistenceService}
*/
public static HistoricItem maximumSince(final Item item, Instant timestamp, String serviceId) {
public static HistoricItem maximumSince(final Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
HistoricItem maximumHistoricItem = null;
@ -303,8 +284,8 @@ public class PersistenceExtensions {
return new HistoricItem() {
@Override
public Date getTimestamp() {
return Date.from(ZonedDateTime.now().toInstant());
public ZonedDateTime getTimestamp() {
return ZonedDateTime.now();
}
@Override
@ -332,7 +313,7 @@ public class PersistenceExtensions {
* constructed from the <code>item</code>'s state if <code>item</code>'s state is the minimum value or if
* the default persistence service does not refer to an available {@link QueryablePersistenceService}
*/
public static HistoricItem minimumSince(Item item, Instant timestamp) {
public static HistoricItem minimumSince(Item item, ZonedDateTime timestamp) {
return minimumSince(item, timestamp, getDefaultServiceId());
}
@ -347,7 +328,7 @@ public class PersistenceExtensions {
* constructed from the <code>item</code>'s state if <code>item</code>'s state is the minimum value or if
* the given <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
*/
public static HistoricItem minimumSince(final Item item, Instant timestamp, String serviceId) {
public static HistoricItem minimumSince(final Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
HistoricItem minimumHistoricItem = null;
@ -369,8 +350,8 @@ public class PersistenceExtensions {
return new HistoricItem() {
@Override
public Date getTimestamp() {
return Date.from(ZonedDateTime.now().toInstant());
public ZonedDateTime getTimestamp() {
return ZonedDateTime.now();
}
@Override
@ -398,7 +379,7 @@ public class PersistenceExtensions {
* previous states could be found or if the default persistence service does not refer to an available
* {@link QueryablePersistenceService}
*/
public static DecimalType averageSince(Item item, Instant timestamp) {
public static DecimalType averageSince(Item item, ZonedDateTime timestamp) {
return averageSince(item, timestamp, getDefaultServiceId());
}
@ -413,7 +394,7 @@ public class PersistenceExtensions {
* previous states could be found or if the persistence service given by <code>serviceId</code> does not
* refer to an available {@link QueryablePersistenceService}
*/
public static DecimalType averageSince(Item item, Instant timestamp, String serviceId) {
public static DecimalType averageSince(Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
@ -430,7 +411,7 @@ public class PersistenceExtensions {
if (state instanceof DecimalType) {
thisState = (DecimalType) state;
thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().getTime());
thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().toInstant().toEpochMilli());
if (firstTimestamp == null || lastState == null) {
firstTimestamp = thisTimestamp;
} else {
@ -476,7 +457,7 @@ public class PersistenceExtensions {
* states could be found or if the default persistence service does not refer to a
* {@link QueryablePersistenceService}
*/
public static DecimalType sumSince(Item item, Instant timestamp) {
public static DecimalType sumSince(Item item, ZonedDateTime timestamp) {
return sumSince(item, timestamp, getDefaultServiceId());
}
@ -491,7 +472,7 @@ public class PersistenceExtensions {
* states could be found for the <code>item</code> or if <code>serviceId</code> does no refer to a
* {@link QueryablePersistenceService}
*/
public static DecimalType sumSince(Item item, Instant timestamp, String serviceId) {
public static DecimalType sumSince(Item item, ZonedDateTime timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
@ -506,12 +487,12 @@ public class PersistenceExtensions {
return new DecimalType(sum);
}
private static Iterable<HistoricItem> getAllStatesSince(Item item, Instant timestamp, String serviceId) {
private static Iterable<HistoricItem> getAllStatesSince(Item item, ZonedDateTime timestamp, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setBeginDate(ZonedDateTime.ofInstant(timestamp, timeZoneProvider.getTimeZone()));
filter.setBeginDate(timestamp);
filter.setItemName(item.getName());
filter.setOrdering(Ordering.ASCENDING);
return qService.query(filter);
@ -530,7 +511,7 @@ public class PersistenceExtensions {
* persisted updates or the default persistence service is not available or a
* {@link QueryablePersistenceService}
*/
public static Instant lastUpdate(Item item) {
public static ZonedDateTime lastUpdate(Item item) {
return lastUpdate(item, getDefaultServiceId());
}
@ -543,7 +524,7 @@ public class PersistenceExtensions {
* persisted updates or if persistence service given by <code>serviceId</code> does not refer to an
* available {@link QueryablePersistenceService}
*/
public static Instant lastUpdate(Item item, String serviceId) {
public static ZonedDateTime lastUpdate(Item item, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
@ -553,7 +534,7 @@ public class PersistenceExtensions {
filter.setPageSize(1);
Iterable<HistoricItem> result = qService.query(filter);
if (result.iterator().hasNext()) {
return result.iterator().next().getTimestamp().toInstant();
return result.iterator().next().getTimestamp();
} else {
return null;
}
@ -575,7 +556,7 @@ public class PersistenceExtensions {
* there is no persisted state for the given <code>item</code> at the given <code>timestamp</code> available
* in the default persistence service
*/
public static DecimalType deltaSince(Item item, Instant timestamp) {
public static DecimalType deltaSince(Item item, ZonedDateTime timestamp) {
return deltaSince(item, timestamp, getDefaultServiceId());
}
@ -591,7 +572,7 @@ public class PersistenceExtensions {
* <code>item</code> at the given <code>timestamp</code> using the persistence service named
* <code>serviceId</code>
*/
public static DecimalType deltaSince(Item item, Instant timestamp, String serviceId) {
public static DecimalType deltaSince(Item item, ZonedDateTime timestamp, String serviceId) {
HistoricItem itemThen = historicState(item, timestamp, serviceId);
if (itemThen != null) {
DecimalType valueThen = (DecimalType) itemThen.getState();
@ -617,7 +598,7 @@ public class PersistenceExtensions {
* the given <code>timestamp</code>, or if there is a state but it is zero (which would cause a
* divide-by-zero error)
*/
public static DecimalType evolutionRate(Item item, Instant timestamp) {
public static DecimalType evolutionRate(Item item, ZonedDateTime timestamp) {
return evolutionRate(item, timestamp, getDefaultServiceId());
}
@ -635,7 +616,7 @@ public class PersistenceExtensions {
* <code>serviceId</code>, or if there is a state but it is zero (which would cause a divide-by-zero
* error)
*/
public static DecimalType evolutionRate(Item item, Instant timestamp, String serviceId) {
public static DecimalType evolutionRate(Item item, ZonedDateTime timestamp, String serviceId) {
HistoricItem itemThen = historicState(item, timestamp, serviceId);
if (itemThen != null) {
DecimalType valueThen = (DecimalType) itemThen.getState();
@ -721,5 +702,4 @@ public class PersistenceExtensions {
return null;
}
}
}

View File

@ -333,7 +333,7 @@ public class DefaultChartProvider implements ChartProvider {
}
state = historicItem.getState();
xData.add(historicItem.getTimestamp());
xData.add(Date.from(historicItem.getTimestamp().toInstant()));
yData.add(convertData(state));
}

View File

@ -44,7 +44,7 @@ import org.slf4j.LoggerFactory;
public class HostFragmentSupportTest extends JavaOSGiTest {
private static final Locale BULGARIAN = new Locale("bg");
private static final Locale DEFAULT = Locale.getDefault();
private static final Locale DEFAULT = Locale.ENGLISH;
private static final Locale GERMAN = Locale.GERMANY;
private static final List<Locale> LOCALES = Arrays.asList(BULGARIAN, DEFAULT, GERMAN);

View File

@ -18,8 +18,8 @@ import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.openMocks;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
@ -69,8 +69,8 @@ public class PersistenceResourceTest {
final int year = i;
items.add(new HistoricItem() {
@Override
public Date getTimestamp() {
return new Date(year - 1900, 0, 1);
public ZonedDateTime getTimestamp() {
return ZonedDateTime.of(year, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
}
@Override

View File

@ -15,6 +15,7 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
@ -23,10 +24,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.core.model.persistence.tests</name>
<name>org.openhab.core.persistence.tests</name>
<comment></comment>
<projects>
</projects>

View File

@ -4,8 +4,9 @@ Bundle-SymbolicName: ${project.artifactId}
Fragment-Host: org.openhab.core.model.persistence
-runrequires: \
bnd.identity;id='org.openhab.core.model.persistence.tests',\
bnd.identity;id='org.openhab.core.model.persistence.runtime'
bnd.identity;id='org.openhab.core.persistence.tests',\
bnd.identity;id='org.openhab.core.model.persistence.runtime',\
bnd.identity;id='org.openhab.core.persistence'
#
# done
@ -96,7 +97,7 @@ Fragment-Host: org.openhab.core.model.persistence
org.openhab.core.model.item;version='[3.0.0,3.0.1)',\
org.openhab.core.model.persistence;version='[3.0.0,3.0.1)',\
org.openhab.core.model.persistence.runtime;version='[3.0.0,3.0.1)',\
org.openhab.core.model.persistence.tests;version='[3.0.0,3.0.1)',\
org.openhab.core.persistence.tests;version='[3.0.0,3.0.1)',\
org.openhab.core.model.rule;version='[3.0.0,3.0.1)',\
org.openhab.core.model.script;version='[3.0.0,3.0.1)',\
org.openhab.core.model.script.runtime;version='[3.0.0,3.0.1)',\

View File

@ -10,9 +10,9 @@
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.openhab.core.model.persistence.tests</artifactId>
<artifactId>org.openhab.core.persistence.tests</artifactId>
<name>openHAB Core :: Integration Tests :: Model Persistence Tests</name>
<name>openHAB Core :: Integration Tests :: Persistence Tests</name>
<dependencies>
<dependency>

View File

@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.persistence.extensions;
package org.openhab.core.persistence.tests;
import static org.junit.jupiter.api.Assertions.*;
@ -18,23 +18,20 @@ import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.model.persistence.tests.TestPersistenceService;
import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.persistence.PersistenceService;
import org.openhab.core.persistence.PersistenceServiceRegistry;
import org.openhab.core.persistence.extensions.PersistenceExtensions;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
@ -72,21 +69,11 @@ public class PersistenceExtensionsTest {
}
};
private final TimeZoneProvider timeZoneProvider = new TimeZoneProvider() {
@Override
public ZoneId getTimeZone() {
return ZoneId.systemDefault();
}
};
private PersistenceExtensions ext;
private GenericItem item;
@BeforeEach
public void setUp() {
ext = new PersistenceExtensions();
ext.setPersistenceServiceRegistry(registry);
ext.setTimeZoneProvider(timeZoneProvider);
new PersistenceExtensions(registry);
item = new GenericItem("Test", "Test") {
@Override
public List<Class<? extends State>> getAcceptedDataTypes() {
@ -100,40 +87,31 @@ public class PersistenceExtensionsTest {
};
}
@AfterEach
public void tearDown() {
ext.unsetPersistenceServiceRegistry(registry);
}
@Test
public void testHistoricState() {
HistoricItem historicItem = PersistenceExtensions.historicState(item,
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2012", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item,
ZonedDateTime.of(2011, 12, 31, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2011, 12, 31, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2011", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item,
ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2011", historicItem.getState().toString());
historicItem = PersistenceExtensions.historicState(item,
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2000", historicItem.getState().toString());
// default persistence service
historicItem = PersistenceExtensions.historicState(item,
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNull(historicItem);
}
@ -141,29 +119,25 @@ public class PersistenceExtensionsTest {
public void testMinimumSince() {
item.setState(new QuantityType<>(5000, SIUnits.CELSIUS));
HistoricItem historicItem = PersistenceExtensions.minimumSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("5000", historicItem.getState().toString());
item.setState(new DecimalType(5000));
historicItem = PersistenceExtensions.minimumSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("5000", historicItem.getState().toString());
historicItem = PersistenceExtensions.minimumSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2005", historicItem.getState().toString());
assertEquals(Date.from(ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant()),
historicItem.getTimestamp());
assertEquals(ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), historicItem.getTimestamp());
// default persistence service
historicItem = PersistenceExtensions.minimumSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNotNull(historicItem);
assertEquals("5000", historicItem.getState().toString());
}
@ -172,29 +146,25 @@ public class PersistenceExtensionsTest {
public void testMaximumSince() {
item.setState(new QuantityType<>(1, SIUnits.CELSIUS));
HistoricItem historicItem = PersistenceExtensions.maximumSince(item,
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("1", historicItem.getState().toString());
item.setState(new DecimalType(1));
historicItem = PersistenceExtensions.maximumSince(item,
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("1", historicItem.getState().toString());
historicItem = PersistenceExtensions.maximumSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(historicItem);
assertEquals("2012", historicItem.getState().toString());
assertEquals(Date.from(ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant()),
historicItem.getTimestamp());
assertEquals(ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), historicItem.getTimestamp());
// default persistence service
historicItem = PersistenceExtensions.maximumSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNotNull(historicItem);
assertEquals("1", historicItem.getState().toString());
}
@ -203,10 +173,10 @@ public class PersistenceExtensionsTest {
public void testAverageSince() {
item.setState(new DecimalType(3025));
Instant startStored = ZonedDateTime.of(2003, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant();
Instant endStored = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant();
long storedInterval = endStored.toEpochMilli() - startStored.toEpochMilli();
long recentInterval = Instant.now().toEpochMilli() - endStored.toEpochMilli();
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 expected = (2007.4994 * storedInterval + 2518.5 * recentInterval) / (storedInterval + recentInterval);
DecimalType average = PersistenceExtensions.averageSince(item, startStored, TestPersistenceService.ID);
assertNotNull(average);
@ -225,20 +195,17 @@ public class PersistenceExtensionsTest {
@Test
public void testSumSince() {
DecimalType sum = PersistenceExtensions.sumSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
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(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
sum = PersistenceExtensions.sumSince(item, 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(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
sum = PersistenceExtensions.sumSince(item, ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNotNull(sum);
assertEquals(0.0, sum.doubleValue(), 0.001);
}
@ -246,9 +213,9 @@ public class PersistenceExtensionsTest {
@Test
public void testLastUpdate() {
item.setState(new DecimalType(2005));
Instant lastUpdate = PersistenceExtensions.lastUpdate(item, TestPersistenceService.ID);
ZonedDateTime lastUpdate = PersistenceExtensions.lastUpdate(item, TestPersistenceService.ID);
assertNotNull(lastUpdate);
assertEquals(ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(), lastUpdate);
assertEquals(ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), lastUpdate);
// default persistence service
lastUpdate = PersistenceExtensions.lastUpdate(item);
@ -259,26 +226,23 @@ public class PersistenceExtensionsTest {
public void testDeltaSince() {
item.setState(new DecimalType(2012));
DecimalType delta = PersistenceExtensions.deltaSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNull(delta);
delta = PersistenceExtensions.deltaSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
delta = PersistenceExtensions.deltaSince(item, ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()),
TestPersistenceService.ID);
assertNotNull(delta);
assertEquals(7, delta.doubleValue(), 0.001);
item.setState(new QuantityType<>(2012, SIUnits.CELSIUS));
delta = PersistenceExtensions.deltaSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
delta = PersistenceExtensions.deltaSince(item, ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()),
TestPersistenceService.ID);
assertNotNull(delta);
assertEquals(7, delta.doubleValue(), 0.001);
// default persistence service
delta = PersistenceExtensions.deltaSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNull(delta);
}
@ -286,28 +250,25 @@ public class PersistenceExtensionsTest {
public void testEvolutionRate() {
item.setState(new DecimalType(2012));
DecimalType rate = PersistenceExtensions.evolutionRate(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNull(rate);
rate = PersistenceExtensions.evolutionRate(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(rate);
// ((now - then) / then) * 100
assertEquals(0.349127182, rate.doubleValue(), 0.001);
item.setState(new QuantityType<>(2012, SIUnits.CELSIUS));
rate = PersistenceExtensions.evolutionRate(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertNotNull(rate);
// ((now - then) / then) * 100
assertEquals(0.349127182, rate.doubleValue(), 0.001);
// default persistence service
rate = PersistenceExtensions.evolutionRate(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertNull(rate);
}
@ -343,36 +304,32 @@ public class PersistenceExtensionsTest {
@Test
public void testChangedSince() {
boolean changed = PersistenceExtensions.changedSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertFalse(changed);
changed = PersistenceExtensions.changedSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertTrue(changed);
// default persistence service
changed = PersistenceExtensions.changedSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertFalse(changed);
}
@Test
public void testUpdatedSince() {
boolean updated = PersistenceExtensions.updatedSince(item,
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertFalse(updated);
updated = PersistenceExtensions.updatedSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant(),
TestPersistenceService.ID);
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertTrue(updated);
// default persistence service
updated = PersistenceExtensions.updatedSince(item,
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant());
ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertFalse(updated);
}
}

View File

@ -10,11 +10,12 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.persistence.tests;
package org.openhab.core.persistence.tests;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@ -54,7 +55,6 @@ public class TestPersistenceService implements QueryablePersistenceService {
public void store(Item item, @Nullable String alias) {
}
@SuppressWarnings("deprecation")
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
int startValue = 1950;
@ -76,8 +76,8 @@ public class TestPersistenceService implements QueryablePersistenceService {
final int year = i;
results.add(new HistoricItem() {
@Override
public Date getTimestamp() {
return new Date(year - 1900, 0, 1);
public ZonedDateTime getTimestamp() {
return ZonedDateTime.of(year, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
}
@Override

View File

@ -35,11 +35,11 @@
<module>org.openhab.core.io.rest.core.tests</module>
<module>org.openhab.core.model.core.tests</module>
<module>org.openhab.core.model.item.tests</module>
<module>org.openhab.core.model.persistence.tests</module>
<module>org.openhab.core.model.rule.tests</module>
<module>org.openhab.core.model.script.tests</module>
<module>org.openhab.core.model.thing.testsupport</module>
<module>org.openhab.core.model.thing.tests</module>
<module>org.openhab.core.persistence.tests</module>
<module>org.openhab.core.storage.json.tests</module>
<module>org.openhab.core.thing.tests</module>
<module>org.openhab.core.thing.xml.tests</module>