From 0c11a900b0743d894ffb6ab7d384081b571f1591 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 19 Nov 2024 20:27:10 +0100 Subject: [PATCH] Simplify DateTimeType handling for MongoDB Signed-off-by: Jacob Laursen --- .../persistence/mongodb/internal/MongoDBTypeConversions.java | 4 ++-- .../persistence/mongodb/internal/DataCreationHelper.java | 5 +++-- .../mongodb/internal/MongoDBPersistenceServiceTest.java | 5 +++-- .../persistence/mongodb/internal/VerificationHelper.java | 4 +++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBTypeConversions.java b/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBTypeConversions.java index cc3bc3771e9bd..6dabc9af2097e 100644 --- a/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBTypeConversions.java +++ b/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBTypeConversions.java @@ -125,7 +125,7 @@ public static Object convertValue(State state) { HSBType.class, State::toString, // QuantityType.class, state -> ((QuantityType) state).toBigDecimal().doubleValue(), // PercentType.class, state -> ((PercentType) state).intValue(), // - DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime().toString(), // + DateTimeType.class, state -> ((DateTimeType) state).getInstant().atZone(ZoneId.systemDefault()).toString(), // StringListType.class, State::toString, // DecimalType.class, state -> ((DecimalType) state).toBigDecimal().doubleValue(), // RawType.class, MongoDBTypeConversions::handleRawType// @@ -237,7 +237,7 @@ private static State handleDateTimeItem(Item item, Document doc) { if (value instanceof String) { return new DateTimeType(ZonedDateTime.parse(doc.getString(MongoDBFields.FIELD_VALUE))); } else { - return new DateTimeType(ZonedDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault())); + return new DateTimeType(((Date) value).toInstant()); } } diff --git a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/DataCreationHelper.java b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/DataCreationHelper.java index ac4deaf77322c..0fee51e23f095 100644 --- a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/DataCreationHelper.java +++ b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/DataCreationHelper.java @@ -18,6 +18,7 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.time.Instant; import java.time.LocalDate; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -212,7 +213,7 @@ public static Stream provideOpenhabItemTypes() { Arguments.of(DataCreationHelper.createItem(RollershutterItem.class, "RollershutterItem", new PercentType(30))), Arguments.of(DataCreationHelper.createItem(DateTimeItem.class, "DateTimeItem", - new DateTimeType(ZonedDateTime.now()))), + new DateTimeType(Instant.now()))), Arguments.of(DataCreationHelper.createItem(ColorItem.class, "ColorItem", new HSBType("180,100,100"))), Arguments.of( DataCreationHelper.createItem(LocationItem.class, "LocationItem", new PointType("51.0,0.0"))), @@ -397,7 +398,7 @@ private static Object convertValue(State state) { value = type.toBigDecimal().doubleValue(); } else if (state instanceof DateTimeType) { DateTimeType type = (DateTimeType) state; - value = Date.from(type.getZonedDateTime().toInstant()); + value = Date.from(type.getInstant()); } else if (state instanceof DecimalType) { DecimalType type = (DecimalType) state; value = type.toBigDecimal().doubleValue(); diff --git a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceServiceTest.java b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceServiceTest.java index 1c80bc9544ef4..7da6bf3fdc001 100644 --- a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceServiceTest.java +++ b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceServiceTest.java @@ -699,8 +699,9 @@ public void testOldDataQueryAllOpenhabItemTypesSingleCollection(GenericItem item if (item instanceof DateTimeItem) { // verify just the date part - assertEquals(((DateTimeType) item.getState()).getZonedDateTime().toLocalDate(), - ((DateTimeType) result.iterator().next().getState()).getZonedDateTime().toLocalDate()); + assertEquals(((DateTimeType) item.getState()).getInstant().atZone(ZoneId.systemDefault()).toLocalDate(), + ((DateTimeType) result.iterator().next().getState()).getInstant().atZone(ZoneId.systemDefault()) + .toLocalDate()); } else { VerificationHelper.verifyQueryResult(result, item.getState()); } diff --git a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/VerificationHelper.java b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/VerificationHelper.java index 1e6093f4fe661..57411063b0905 100644 --- a/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/VerificationHelper.java +++ b/bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/VerificationHelper.java @@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -176,7 +177,8 @@ private static Pair handleDecimalType(Object ev, Document doc) { private static Pair handleDateTimeType(Object ev, Document doc) { String value = doc.getString(MongoDBFields.FIELD_VALUE); - return Pair.of(((DateTimeType) ev).getZonedDateTime().toString(), value != null ? value : new Object()); + return Pair.of(((DateTimeType) ev).getInstant().atZone(ZoneId.systemDefault()).toString(), + value != null ? value : new Object()); } private static Pair handlePercentType(Object ev, Document doc) {