]> git.basschouten.com Git - openhab-addons.git/blob
95201c822224c055b5477aee665dc52292e9452b
[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 static org.junit.jupiter.api.Assertions.assertTrue;
16
17 import java.math.BigDecimal;
18 import java.time.ZonedDateTime;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.BeforeAll;
23 import org.openhab.core.library.items.NumberItem;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.types.State;
26
27 /**
28  *
29  * @author Sami Salonen - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class NumberItemIntegrationTest extends AbstractTwoItemIntegrationTest {
34
35     public static final boolean LEGACY_MODE = false;
36     private static final String NAME = "number";
37     // On purpose we have super accurate number here (testing limits of aws)
38     private static final DecimalType STATE1 = new DecimalType(new BigDecimal(
39             "-32343243.193490838904389298049802398048923849032809483209483209482309840239840932840932849083094809483"));
40     private static final DecimalType STATE2 = new DecimalType(600.9123);
41     private static final DecimalType STATE_BETWEEN = new DecimalType(500);
42
43     @SuppressWarnings("null")
44     @BeforeAll
45     public static void storeData() throws InterruptedException {
46         NumberItem item = (NumberItem) ITEMS.get(NAME);
47
48         item.setState(STATE1);
49
50         beforeStore = ZonedDateTime.now();
51         Thread.sleep(10);
52         service.store(item);
53         afterStore1 = ZonedDateTime.now();
54         Thread.sleep(10);
55         item.setState(STATE2);
56         service.store(item);
57         Thread.sleep(10);
58         afterStore2 = ZonedDateTime.now();
59
60         LOGGER.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore),
61                 AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
62     }
63
64     @Override
65     protected String getItemName() {
66         return NAME;
67     }
68
69     @Override
70     protected State getFirstItemState() {
71         return STATE1;
72     }
73
74     @Override
75     protected State getSecondItemState() {
76         return STATE2;
77     }
78
79     @Override
80     protected @Nullable State getQueryItemStateBetween() {
81         return STATE_BETWEEN;
82     }
83
84     /**
85      * Use relaxed state comparison due to numerical rounding. See also DynamoDBBigDecimalItem.loseDigits
86      */
87     @Override
88     protected void assertStateEquals(State expected, State actual) {
89         BigDecimal expectedDecimal = ((DecimalType) expected).toBigDecimal();
90         BigDecimal actualDecimal = ((DecimalType) actual).toBigDecimal();
91         assertTrue(DynamoDBBigDecimalItem.loseDigits(expectedDecimal).compareTo(actualDecimal) == 0);
92     }
93 }