]> git.basschouten.com Git - openhab-addons.git/blob
8b9265a42bc056e22b4ae26e8f46968cbd98e5e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
34     private static final String NAME = "datetime";
35     private static final ZonedDateTime ZDT1 = ZonedDateTime.parse("2016-06-15T10:00:00Z");
36     private static final ZonedDateTime ZDT2 = ZonedDateTime.parse("2016-06-15T16:00:00.123Z");
37     private static final ZonedDateTime ZDT_BETWEEN = ZonedDateTime.parse("2016-06-15T14:00:00Z");
38
39     // State1 stored as DateTimeType wrapping ZonedDateTime specified in UTC
40     private static final DateTimeType STATE1 = new DateTimeType(ZDT1);
41     // State2 stored as DateTimeType wrapping ZonedDateTime specified in UTC+5
42     private static final DateTimeType STATE2 = new DateTimeType(ZDT2.withZoneSameInstant(ZoneOffset.ofHours(5)));
43     private static final DateTimeType STATE_BETWEEN = new DateTimeType(ZDT_BETWEEN);
44
45     @BeforeAll
46     public static void storeData() throws InterruptedException {
47         DateTimeItem item = (DateTimeItem) ITEMS.get(NAME);
48
49         item.setState(STATE1);
50
51         beforeStore = ZonedDateTime.now();
52         Thread.sleep(10);
53         service.store(item);
54         afterStore1 = ZonedDateTime.now();
55         Thread.sleep(10);
56         item.setState(STATE2);
57         service.store(item);
58         Thread.sleep(10);
59         afterStore2 = ZonedDateTime.now();
60
61         LOGGER.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore),
62                 AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
63     }
64
65     @Override
66     protected String getItemName() {
67         return NAME;
68     }
69
70     @Override
71     protected State getFirstItemState() {
72         // The persistence converts to system default timezone
73         // Thus we need to convert here as well for comparison
74         // In the logs:
75         // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
76         // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
77         // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
78         return STATE1.toZone(ZoneId.systemDefault());
79     }
80
81     @Override
82     protected State getSecondItemState() {
83         // The persistence converts to system default timezone
84         // Thus we need to convert here as well for comparison
85         // In the logs:
86         // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
87         // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
88         // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
89         return STATE2.toZone(ZoneId.systemDefault());
90     }
91
92     @Override
93     protected @Nullable State getQueryItemStateBetween() {
94         return STATE_BETWEEN;
95     }
96 }