]> git.basschouten.com Git - openhab-addons.git/blob
13a0e10ac02d083b92dc9c66840bc46742e020fa
[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.*;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.core.library.items.NumberItem;
27 import org.openhab.core.library.items.PlayerItem;
28 import org.openhab.core.library.items.StringItem;
29 import org.openhab.core.library.items.SwitchItem;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.PlayPauseType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.persistence.FilterCriteria;
35 import org.openhab.core.persistence.FilterCriteria.Ordering;
36 import org.openhab.core.persistence.HistoricItem;
37 import org.openhab.core.types.State;
38
39 /**
40  *
41  * @author Sami Salonen - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 public class TestStoreMixedTypesTest extends BaseIntegrationTest {
46
47     public static final boolean LEGACY_MODE = false;
48
49     private static final AtomicInteger testCounter = new AtomicInteger();
50     private int uniqueId;
51
52     private String getItemName() {
53         return "localItem" + uniqueId;
54     }
55
56     @BeforeEach
57     public void generateUniqueItemId() {
58         uniqueId = testCounter.getAndIncrement();
59     }
60
61     @AfterEach
62     public void tearDownLocalItems() {
63         ITEMS.remove(getItemName());
64     }
65
66     @SuppressWarnings("null")
67     public void storeItemWithDifferentTypes() {
68         try {
69             // First writing two values with string item
70             {
71                 StringItem item = new StringItem(getItemName());
72                 ITEMS.put(getItemName(), item);
73                 item.setState(StringType.valueOf("a1"));
74                 service.store(item);
75                 Thread.sleep(10);
76
77                 item.setState(StringType.valueOf("b1"));
78                 service.store(item);
79                 Thread.sleep(10);
80             }
81             // then writing with same item but numbers
82             {
83                 NumberItem item = new NumberItem(getItemName());
84                 assert item != null;
85                 ITEMS.put(getItemName(), item);
86                 item.setState(DecimalType.valueOf("33.14"));
87                 service.store(item);
88                 Thread.sleep(10);
89                 item.setState(DecimalType.valueOf("66.28"));
90                 service.store(item);
91                 Thread.sleep(10);
92             }
93             // finally some switch values
94             {
95                 SwitchItem item = new SwitchItem(getItemName());
96                 assert item != null;
97                 ITEMS.put(getItemName(), item);
98                 item.setState(OnOffType.ON);
99                 service.store(item);
100                 Thread.sleep(10);
101                 item.setState(OnOffType.OFF);
102                 service.store(item);
103                 Thread.sleep(10);
104             }
105             // Player
106             {
107                 PlayerItem item = new PlayerItem(getItemName());
108                 assert item != null;
109                 ITEMS.put(getItemName(), item);
110                 item.setState(PlayPauseType.PAUSE);
111                 service.store(item);
112                 Thread.sleep(10);
113             }
114         } catch (InterruptedException e) {
115             fail("interrupted");
116         }
117     }
118
119     /**
120      * Test where first data is stored with various item types, some serialized as numbers and some as strings.
121      *
122      * - Querying with NumberItem returns data that have been persisted using DynamoDBBigDecimalItem DTO, that is,
123      * NumberItem and SwitchItem data
124      * - Querying with StringItem returns data that have been persisted using DynamoDBStringItem DTO (StringItem and
125      * PlayerItem)
126      * - Querying with SwitchItem returns data that have been persisted using DynamoDBBigDecimalItem DTO. All numbers
127      * are converted to OnOff (ON if nonzero)
128      * - Querying with PlayerItem returns data that have been persisted using DynamoDBStringItem DTO. However, some
129      * values are not convertible to PlayPauseType are ignored (warning logged).
130      *
131      */
132     @Test
133     public void testQueryAllItemTypeChanged() {
134         storeItemWithDifferentTypes();
135         {
136             NumberItem item = new NumberItem(getItemName());
137             ITEMS.put(getItemName(), item);
138             waitForAssert(() -> {
139                 assertQueryAll(getItemName(), expectedNumberItem());
140             });
141         }
142         {
143             SwitchItem item = new SwitchItem(getItemName());
144             ITEMS.put(getItemName(), item);
145             waitForAssert(() -> {
146                 assertQueryAll(getItemName(), expectedSwitchItem());
147             });
148         }
149         {
150             StringItem item = new StringItem(getItemName());
151             ITEMS.put(getItemName(), item);
152             waitForAssert(() -> {
153                 assertQueryAll(getItemName(), expectedStringItem());
154             });
155         }
156         {
157             PlayerItem item = new PlayerItem(getItemName());
158             assert item != null;
159             ITEMS.put(getItemName(), item);
160             waitForAssert(() -> {
161                 assertQueryAll(getItemName(), expectedPlayerItem());
162             });
163         }
164     }
165
166     protected PlayPauseType[] expectedPlayerItem() {
167         return new PlayPauseType[] { /* ON=1=PLAY */PlayPauseType.PLAY, /* OFF=0=PAUSE */PlayPauseType.PAUSE,
168                 PlayPauseType.PAUSE };
169     }
170
171     protected StringType[] expectedStringItem() {
172         return new StringType[] { StringType.valueOf("a1"), StringType.valueOf("b1") };
173     }
174
175     protected OnOffType[] expectedSwitchItem() {
176         return new OnOffType[] { /* 33.14 */OnOffType.ON, /* 66.28 */ OnOffType.ON, OnOffType.ON, OnOffType.OFF,
177                 /* pause */ OnOffType.OFF };
178     }
179
180     protected DecimalType[] expectedNumberItem() {
181         return new DecimalType[] { DecimalType.valueOf("33.14"), DecimalType.valueOf("66.28"),
182                 /* on */DecimalType.valueOf("1"), /* off */DecimalType.valueOf("0"),
183                 /* pause */DecimalType.valueOf("0") };
184     }
185
186     private void assertQueryAll(String item, State[] expectedStates) {
187         FilterCriteria criteria = new FilterCriteria();
188         criteria.setOrdering(Ordering.ASCENDING);
189         criteria.setItemName(item);
190         @SuppressWarnings("null")
191         Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
192         List<State> actualStatesList = new ArrayList<>();
193         iterable.forEach(i -> actualStatesList.add(i.getState()));
194         State[] actualStates = actualStatesList.toArray(new State[0]);
195         assertArrayEquals(expectedStates, actualStates, Arrays.toString(actualStates));
196     }
197 }