]> git.basschouten.com Git - openhab-addons.git/blob
2dba2484f645ec0e0407229f7c2935724620577d
[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     private void generateUniqueItemId() {
58         uniqueId = testCounter.getAndIncrement();
59     }
60
61     @AfterEach
62     private void tearDownLocalItems() {
63         ITEMS.remove(getItemName());
64     }
65
66     @SuppressWarnings("null")
67     public void storeItemWithDifferentTypes() {
68
69         try {
70             // First writing two values with string item
71             {
72                 StringItem item = new StringItem(getItemName());
73                 ITEMS.put(getItemName(), item);
74                 item.setState(StringType.valueOf("a1"));
75                 service.store(item);
76                 Thread.sleep(10);
77
78                 item.setState(StringType.valueOf("b1"));
79                 service.store(item);
80                 Thread.sleep(10);
81             }
82             // then writing with same item but numbers
83             {
84                 NumberItem item = new NumberItem(getItemName());
85                 assert item != null;
86                 ITEMS.put(getItemName(), item);
87                 item.setState(DecimalType.valueOf("33.14"));
88                 service.store(item);
89                 Thread.sleep(10);
90                 item.setState(DecimalType.valueOf("66.28"));
91                 service.store(item);
92                 Thread.sleep(10);
93             }
94             // finally some switch values
95             {
96                 SwitchItem item = new SwitchItem(getItemName());
97                 assert item != null;
98                 ITEMS.put(getItemName(), item);
99                 item.setState(OnOffType.ON);
100                 service.store(item);
101                 Thread.sleep(10);
102                 item.setState(OnOffType.OFF);
103                 service.store(item);
104                 Thread.sleep(10);
105             }
106             // Player
107             {
108                 PlayerItem item = new PlayerItem(getItemName());
109                 assert item != null;
110                 ITEMS.put(getItemName(), item);
111                 item.setState(PlayPauseType.PAUSE);
112                 service.store(item);
113                 Thread.sleep(10);
114             }
115         } catch (InterruptedException e) {
116             fail("interrupted");
117         }
118     }
119
120     /**
121      * Test where first data is stored with various item types, some serialized as numbers and some as strings.
122      *
123      * - Querying with NumberItem returns data that have been persisted using DynamoDBBigDecimalItem DTO, that is,
124      * NumberItem and SwitchItem data
125      * - Querying with StringItem returns data that have been persisted using DynamoDBStringItem DTO (StringItem and
126      * PlayerItem)
127      * - Querying with SwitchItem returns data that have been persisted using DynamoDBBigDecimalItem DTO. All numbers
128      * are converted to OnOff (ON if nonzero)
129      * - Querying with PlayerItem returns data that have been persisted using DynamoDBStringItem DTO. However, some
130      * values are not convertible to PlayPauseType are ignored (warning logged).
131      *
132      */
133     @Test
134     public void testQueryAllItemTypeChanged() {
135         storeItemWithDifferentTypes();
136         {
137             NumberItem item = new NumberItem(getItemName());
138             ITEMS.put(getItemName(), item);
139             waitForAssert(() -> {
140                 assertQueryAll(getItemName(), expectedNumberItem());
141             });
142         }
143         {
144             SwitchItem item = new SwitchItem(getItemName());
145             ITEMS.put(getItemName(), item);
146             waitForAssert(() -> {
147                 assertQueryAll(getItemName(), expectedSwitchItem());
148             });
149         }
150         {
151             StringItem item = new StringItem(getItemName());
152             ITEMS.put(getItemName(), item);
153             waitForAssert(() -> {
154                 assertQueryAll(getItemName(), expectedStringItem());
155             });
156         }
157         {
158             PlayerItem item = new PlayerItem(getItemName());
159             assert item != null;
160             ITEMS.put(getItemName(), item);
161             waitForAssert(() -> {
162                 assertQueryAll(getItemName(), expectedPlayerItem());
163             });
164         }
165     }
166
167     protected PlayPauseType[] expectedPlayerItem() {
168         return new PlayPauseType[] { /* ON=1=PLAY */PlayPauseType.PLAY, /* OFF=0=PAUSE */PlayPauseType.PAUSE,
169                 PlayPauseType.PAUSE };
170     }
171
172     protected StringType[] expectedStringItem() {
173         return new StringType[] { StringType.valueOf("a1"), StringType.valueOf("b1") };
174     }
175
176     protected OnOffType[] expectedSwitchItem() {
177         return new OnOffType[] { /* 33.14 */OnOffType.ON, /* 66.28 */ OnOffType.ON, OnOffType.ON, OnOffType.OFF,
178                 /* pause */ OnOffType.OFF };
179     }
180
181     protected DecimalType[] expectedNumberItem() {
182         return new DecimalType[] { DecimalType.valueOf("33.14"), DecimalType.valueOf("66.28"),
183                 /* on */DecimalType.valueOf("1"), /* off */DecimalType.valueOf("0"),
184                 /* pause */DecimalType.valueOf("0") };
185     }
186
187     private void assertQueryAll(String item, State[] expectedStates) {
188         FilterCriteria criteria = new FilterCriteria();
189         criteria.setOrdering(Ordering.ASCENDING);
190         criteria.setItemName(item);
191         @SuppressWarnings("null")
192         Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
193         List<State> actualStatesList = new ArrayList<>();
194         iterable.forEach(i -> actualStatesList.add(i.getState()));
195         State[] actualStates = actualStatesList.toArray(new State[0]);
196         assertArrayEquals(expectedStates, actualStates, Arrays.toString(actualStates));
197     }
198 }