]> git.basschouten.com Git - openhab-addons.git/blob
1c4bd3b8eb0256737de32b9ed0484e56de087243
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.BeforeAll;
24 import org.openhab.core.common.registry.RegistryChangeListener;
25 import org.openhab.core.items.Item;
26 import org.openhab.core.items.ItemNotFoundException;
27 import org.openhab.core.items.ItemNotUniqueException;
28 import org.openhab.core.items.ItemRegistry;
29 import org.openhab.core.items.RegistryHook;
30 import org.openhab.core.library.items.CallItem;
31 import org.openhab.core.library.items.ColorItem;
32 import org.openhab.core.library.items.ContactItem;
33 import org.openhab.core.library.items.DateTimeItem;
34 import org.openhab.core.library.items.DimmerItem;
35 import org.openhab.core.library.items.LocationItem;
36 import org.openhab.core.library.items.NumberItem;
37 import org.openhab.core.library.items.PlayerItem;
38 import org.openhab.core.library.items.RollershutterItem;
39 import org.openhab.core.library.items.StringItem;
40 import org.openhab.core.library.items.SwitchItem;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException;
45
46 /**
47  *
48  * @author Sami Salonen - Initial contribution
49  *
50  */
51 @NonNullByDefault
52 public class BaseIntegrationTest {
53     protected static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBPersistenceService.class);
54     protected static @Nullable DynamoDBPersistenceService service;
55     protected static final Map<String, Item> ITEMS = new HashMap<>();
56
57     static {
58         System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
59     }
60
61     @BeforeAll
62     public static void initService() throws InterruptedException {
63         ITEMS.put("dimmer", new DimmerItem("dimmer"));
64         ITEMS.put("number", new NumberItem("number"));
65         ITEMS.put("string", new StringItem("string"));
66         ITEMS.put("switch", new SwitchItem("switch"));
67         ITEMS.put("contact", new ContactItem("contact"));
68         ITEMS.put("color", new ColorItem("color"));
69         ITEMS.put("rollershutter", new RollershutterItem("rollershutter"));
70         ITEMS.put("datetime", new DateTimeItem("datetime"));
71         ITEMS.put("call", new CallItem("call"));
72         ITEMS.put("location", new LocationItem("location"));
73         ITEMS.put("player_playpause", new PlayerItem("player_playpause"));
74         ITEMS.put("player_rewindfastforward", new PlayerItem("player_rewindfastforward"));
75
76         service = new DynamoDBPersistenceService(new ItemRegistry() {
77             @Override
78             public Collection<Item> getItems(String pattern) {
79                 throw new UnsupportedOperationException();
80             }
81
82             @Override
83             public Collection<Item> getItems() {
84                 throw new UnsupportedOperationException();
85             }
86
87             @Override
88             public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
89                 throw new UnsupportedOperationException();
90             }
91
92             @Override
93             public Item getItem(String name) throws ItemNotFoundException {
94                 Item item = ITEMS.get(name);
95                 if (item == null) {
96                     throw new ItemNotFoundException(name);
97                 }
98                 return item;
99             }
100
101             @Override
102             public void addRegistryChangeListener(RegistryChangeListener<Item> listener) {
103                 throw new UnsupportedOperationException();
104             }
105
106             @Override
107             public Collection<Item> getAll() {
108                 throw new UnsupportedOperationException();
109             }
110
111             @Override
112             public Stream<Item> stream() {
113                 throw new UnsupportedOperationException();
114             }
115
116             @Override
117             public @Nullable Item get(String key) {
118                 throw new UnsupportedOperationException();
119             }
120
121             @Override
122             public void removeRegistryChangeListener(RegistryChangeListener<Item> listener) {
123                 throw new UnsupportedOperationException();
124             }
125
126             @Override
127             public Item add(Item element) {
128                 throw new UnsupportedOperationException();
129             }
130
131             @Override
132             public @Nullable Item update(Item element) {
133                 throw new UnsupportedOperationException();
134             }
135
136             @Override
137             public @Nullable Item remove(String key) {
138                 throw new UnsupportedOperationException();
139             }
140
141             @Override
142             public Collection<Item> getItemsOfType(String type) {
143                 throw new UnsupportedOperationException();
144             }
145
146             @Override
147             public Collection<Item> getItemsByTag(String... tags) {
148                 throw new UnsupportedOperationException();
149             }
150
151             @Override
152             public Collection<Item> getItemsByTagAndType(String type, String... tags) {
153                 throw new UnsupportedOperationException();
154             }
155
156             @Override
157             public <T extends Item> Collection<T> getItemsByTag(Class<T> typeFilter, String... tags) {
158                 throw new UnsupportedOperationException();
159             }
160
161             @Override
162             public @Nullable Item remove(String itemName, boolean recursive) {
163                 throw new UnsupportedOperationException();
164             }
165
166             @Override
167             public void addRegistryHook(RegistryHook<Item> hook) {
168                 throw new UnsupportedOperationException();
169             }
170
171             @Override
172             public void removeRegistryHook(RegistryHook<Item> hook) {
173                 throw new UnsupportedOperationException();
174             }
175         });
176
177         Map<String, Object> config = new HashMap<>();
178         String value = System.getProperty("DYNAMODBTEST_REGION");
179         config.put("region", value != null ? value : "");
180         value = System.getProperty("DYNAMODBTEST_ACCESS");
181         config.put("accessKey", value != null ? value : "");
182         value = System.getProperty("DYNAMODBTEST_SECRET");
183         config.put("secretKey", value != null ? value : "");
184         config.put("tablePrefix", "dynamodb-integration-tests-");
185
186         // Disable buffering
187         config.put("bufferSize", "0");
188
189         for (Entry<String, Object> entry : config.entrySet()) {
190             if (((String) entry.getValue()).isEmpty()) {
191                 LOGGER.warn(String.format(
192                         "Expecting %s to have value for integration tests. Integration tests will be skipped",
193                         entry.getKey()));
194                 service = null;
195                 return;
196             }
197         }
198
199         service.activate(null, config);
200         clearData();
201     }
202
203     protected static void clearData() {
204         // Clear data
205         for (String table : new String[] { "dynamodb-integration-tests-bigdecimal",
206                 "dynamodb-integration-tests-string" }) {
207             try {
208                 service.getDb().getDynamoClient().deleteTable(table);
209                 service.getDb().getDynamoDB().getTable(table).waitForDelete();
210             } catch (ResourceNotFoundException e) {
211             } catch (InterruptedException e) {
212                 LOGGER.warn("Interrupted! Table might not have been deleted");
213             }
214         }
215     }
216 }