]> git.basschouten.com Git - openhab-addons.git/blob
05eba6a18aa23f17746de769290f9cf529e91fdc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.dynamodb.internal;
14
15 import java.time.ZoneId;
16 import java.time.ZoneOffset;
17 import java.time.ZonedDateTime;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.junit.jupiter.api.BeforeAll;
22 import org.openhab.core.library.items.DateTimeItem;
23 import org.openhab.core.library.types.DateTimeType;
24 import org.openhab.core.types.State;
25
26 /**
27  *
28  * @author Sami Salonen - Initial contribution
29  *
30  */
31 @NonNullByDefault
32 public class DateTimeItemIntegrationTest extends AbstractTwoItemIntegrationTest {
33     public static final boolean LEGACY_MODE = false;
34
35     private static final String NAME = "datetime";
36     private static final ZonedDateTime ZDT1 = ZonedDateTime.parse("2016-06-15T10:00:00Z");
37     private static final ZonedDateTime ZDT2 = ZonedDateTime.parse("2016-06-15T16:00:00.123Z");
38     private static final ZonedDateTime ZDT_BETWEEN = ZonedDateTime.parse("2016-06-15T14:00:00Z");
39
40     // State1 stored as DateTimeType wrapping ZonedDateTime specified in UTC
41     private static final DateTimeType STATE1 = new DateTimeType(ZDT1);
42     // State2 stored as DateTimeType wrapping ZonedDateTime specified in UTC+5
43     private static final DateTimeType STATE2 = new DateTimeType(ZDT2.withZoneSameInstant(ZoneOffset.ofHours(5)));
44     private static final DateTimeType STATE_BETWEEN = new DateTimeType(ZDT_BETWEEN);
45
46     @SuppressWarnings("null")
47     @BeforeAll
48     public static void storeData() throws InterruptedException {
49         DateTimeItem item = (DateTimeItem) ITEMS.get(NAME);
50
51         item.setState(STATE1);
52
53         beforeStore = ZonedDateTime.now();
54         Thread.sleep(10);
55         service.store(item);
56         afterStore1 = ZonedDateTime.now();
57         Thread.sleep(10);
58         item.setState(STATE2);
59         service.store(item);
60         Thread.sleep(10);
61         afterStore2 = ZonedDateTime.now();
62         LOGGER.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore),
63                 AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
64     }
65
66     @Override
67     protected String getItemName() {
68         return NAME;
69     }
70
71     @Override
72     protected State getFirstItemState() {
73         // The persistence converts to system default timezone
74         // Thus we need to convert here as well for comparison
75         // In the logs:
76         // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
77         // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
78         // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
79         return STATE1.toZone(ZoneId.systemDefault());
80     }
81
82     @Override
83     protected State getSecondItemState() {
84         // The persistence converts to system default timezone
85         // Thus we need to convert here as well for comparison
86         // In the logs:
87         // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
88         // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
89         // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
90         return STATE2.toZone(ZoneId.systemDefault());
91     }
92
93     @Override
94     protected @Nullable State getQueryItemStateBetween() {
95         return STATE_BETWEEN;
96     }
97 }