Simplify DateTimeType handling for MongoDB

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-19 20:27:10 +01:00 committed by lsiepel
parent 2ded1d14c9
commit 254f2f5886
4 changed files with 12 additions and 8 deletions

View File

@ -125,7 +125,7 @@ public class MongoDBTypeConversions {
HSBType.class, State::toString, // HSBType.class, State::toString, //
QuantityType.class, state -> ((QuantityType<?>) state).toBigDecimal().doubleValue(), // QuantityType.class, state -> ((QuantityType<?>) state).toBigDecimal().doubleValue(), //
PercentType.class, state -> ((PercentType) state).intValue(), // PercentType.class, state -> ((PercentType) state).intValue(), //
DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime().toString(), // DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime(ZoneId.systemDefault()).toString(), //
StringListType.class, State::toString, // StringListType.class, State::toString, //
DecimalType.class, state -> ((DecimalType) state).toBigDecimal().doubleValue(), // DecimalType.class, state -> ((DecimalType) state).toBigDecimal().doubleValue(), //
RawType.class, MongoDBTypeConversions::handleRawType// RawType.class, MongoDBTypeConversions::handleRawType//
@ -237,7 +237,7 @@ public class MongoDBTypeConversions {
if (value instanceof String) { if (value instanceof String) {
return new DateTimeType(ZonedDateTime.parse(doc.getString(MongoDBFields.FIELD_VALUE))); return new DateTimeType(ZonedDateTime.parse(doc.getString(MongoDBFields.FIELD_VALUE)));
} else { } else {
return new DateTimeType(ZonedDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault())); return new DateTimeType(((Date) value).toInstant());
} }
} }

View File

@ -18,6 +18,7 @@ import static org.mockito.Mockito.when;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -212,7 +213,7 @@ public class DataCreationHelper {
Arguments.of(DataCreationHelper.createItem(RollershutterItem.class, "RollershutterItem", Arguments.of(DataCreationHelper.createItem(RollershutterItem.class, "RollershutterItem",
new PercentType(30))), new PercentType(30))),
Arguments.of(DataCreationHelper.createItem(DateTimeItem.class, "DateTimeItem", 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(ColorItem.class, "ColorItem", new HSBType("180,100,100"))),
Arguments.of( Arguments.of(
DataCreationHelper.createItem(LocationItem.class, "LocationItem", new PointType("51.0,0.0"))), DataCreationHelper.createItem(LocationItem.class, "LocationItem", new PointType("51.0,0.0"))),
@ -397,7 +398,7 @@ public class DataCreationHelper {
value = type.toBigDecimal().doubleValue(); value = type.toBigDecimal().doubleValue();
} else if (state instanceof DateTimeType) { } else if (state instanceof DateTimeType) {
DateTimeType type = (DateTimeType) state; DateTimeType type = (DateTimeType) state;
value = Date.from(type.getZonedDateTime().toInstant()); value = Date.from(type.getInstant());
} else if (state instanceof DecimalType) { } else if (state instanceof DecimalType) {
DecimalType type = (DecimalType) state; DecimalType type = (DecimalType) state;
value = type.toBigDecimal().doubleValue(); value = type.toBigDecimal().doubleValue();

View File

@ -59,7 +59,7 @@ import ch.qos.logback.core.read.ListAppender;
import de.bwaldvogel.mongo.backend.memory.MemoryBackend; import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
/** /**
* This is the implementation of the test for MongoDB {@link PersistenceService}. * This is the implementation of the test for MongoDB {@link org.openhab.core.persistence.PersistenceService}.
* *
* @author René Ulbricht - Initial contribution * @author René Ulbricht - Initial contribution
*/ */
@ -699,8 +699,9 @@ public class MongoDBPersistenceServiceTest {
if (item instanceof DateTimeItem) { if (item instanceof DateTimeItem) {
// verify just the date part // verify just the date part
assertEquals(((DateTimeType) item.getState()).getZonedDateTime().toLocalDate(), assertEquals(((DateTimeType) item.getState()).getZonedDateTime(ZoneId.systemDefault()).toLocalDate(),
((DateTimeType) result.iterator().next().getState()).getZonedDateTime().toLocalDate()); ((DateTimeType) result.iterator().next().getState()).getZonedDateTime(ZoneId.systemDefault())
.toLocalDate());
} else { } else {
VerificationHelper.verifyQueryResult(result, item.getState()); VerificationHelper.verifyQueryResult(result, item.getState());
} }

View File

@ -15,6 +15,7 @@ package org.openhab.persistence.mongodb.internal;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -176,7 +177,8 @@ public class VerificationHelper {
private static Pair<Object, Object> handleDateTimeType(Object ev, Document doc) { private static Pair<Object, Object> handleDateTimeType(Object ev, Document doc) {
String value = doc.getString(MongoDBFields.FIELD_VALUE); String value = doc.getString(MongoDBFields.FIELD_VALUE);
return Pair.of(((DateTimeType) ev).getZonedDateTime().toString(), value != null ? value : new Object()); return Pair.of(((DateTimeType) ev).getZonedDateTime(ZoneId.systemDefault()).toString(),
value != null ? value : new Object());
} }
private static Pair<Object, Object> handlePercentType(Object ev, Document doc) { private static Pair<Object, Object> handlePercentType(Object ev, Document doc) {