]> git.basschouten.com Git - openhab-addons.git/blob
8f7387f15b602134014e334b74738b33948a2118
[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.influxdb;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.mockito.Mockito.when;
18 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.*;
19
20 import java.math.BigDecimal;
21 import java.time.Instant;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.concurrent.ExecutionException;
25 import java.util.stream.Stream;
26
27 import org.eclipse.jdt.annotation.DefaultLocation;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.MethodSource;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.openhab.core.i18n.UnitProvider;
38 import org.openhab.core.items.ItemRegistry;
39 import org.openhab.core.items.Metadata;
40 import org.openhab.core.items.MetadataKey;
41 import org.openhab.core.items.MetadataRegistry;
42 import org.openhab.core.library.CoreItemFactory;
43 import org.openhab.core.library.items.NumberItem;
44 import org.openhab.persistence.influxdb.internal.InfluxDBConstants;
45 import org.openhab.persistence.influxdb.internal.InfluxDBMetadataService;
46 import org.openhab.persistence.influxdb.internal.InfluxDBVersion;
47 import org.openhab.persistence.influxdb.internal.InfluxPoint;
48 import org.openhab.persistence.influxdb.internal.ItemTestHelper;
49
50 /**
51  * @author Joan Pujol Espinar - Initial contribution
52  */
53 @ExtendWith(MockitoExtension.class)
54 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
55 public class ItemToStorePointCreatorTest {
56
57     private static final Map<String, Object> BASE_CONFIGURATION = Map.of( //
58             URL_PARAM, "http://localhost:8086", //
59             VERSION_PARAM, InfluxDBVersion.V1.name(), //
60             USER_PARAM, "user", PASSWORD_PARAM, "password", //
61             DATABASE_PARAM, "openhab", //
62             RETENTION_POLICY_PARAM, "default");
63
64     private @Mock UnitProvider unitProviderMock;
65     private @Mock ItemRegistry itemRegistryMock;
66     private @Mock MetadataRegistry metadataRegistry;
67     private InfluxDBPersistenceService instance;
68
69     @BeforeEach
70     public void setup() {
71         instance = getService(false, false, false, false);
72     }
73
74     @ParameterizedTest
75     @MethodSource
76     public void convertBasicItem(Number number) throws ExecutionException, InterruptedException {
77         NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
78         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
79
80         if (point == null) {
81             Assertions.fail("'point' is null");
82             return;
83         }
84
85         assertThat(point.getMeasurementName(), equalTo(item.getName()));
86         assertThat("Must Store item name", point.getTags(), hasEntry("item", item.getName()));
87         assertThat(point.getValue(), equalTo(new BigDecimal(number.toString())));
88     }
89
90     @SuppressWarnings("unused")
91     private static Stream<Number> convertBasicItem() {
92         return Stream.of(5, 5.5, 5L);
93     }
94
95     @Test
96     public void shouldUseAliasAsMeasurementNameIfProvided() throws ExecutionException, InterruptedException {
97         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
98         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), "aliasName").get();
99
100         if (point == null) {
101             Assertions.fail("'point' is null");
102             return;
103         }
104
105         assertThat(point.getMeasurementName(), is("aliasName"));
106     }
107
108     @Test
109     public void shouldStoreCategoryTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
110         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
111         item.setCategory("categoryValue");
112
113         instance = getService(false, true, false, false);
114         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
115
116         if (point == null) {
117             Assertions.fail("'point' is null");
118             return;
119         }
120
121         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
122
123         instance = getService(false, false, false, false);
124         point = instance.convert(item, item.getState(), Instant.now(), null).get();
125
126         if (point == null) {
127             Assertions.fail("'point' is null");
128             return;
129         }
130
131         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
132     }
133
134     @Test
135     public void shouldStoreTypeTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
136         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
137
138         instance = getService(false, false, false, true);
139         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
140
141         if (point == null) {
142             Assertions.fail("'point' is null");
143             return;
144         }
145
146         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
147
148         instance = getService(false, false, false, false);
149         point = instance.convert(item, item.getState(), Instant.now(), null).get();
150
151         if (point == null) {
152             Assertions.fail("'point' is null");
153             return;
154         }
155
156         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
157     }
158
159     @Test
160     public void shouldStoreTypeLabelIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
161         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
162         item.setLabel("ItemLabel");
163
164         instance = getService(false, false, true, false);
165         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
166
167         if (point == null) {
168             Assertions.fail("'point' is null");
169             return;
170         }
171
172         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
173
174         instance = getService(false, false, false, false);
175         point = instance.convert(item, item.getState(), Instant.now(), null).get();
176
177         if (point == null) {
178             Assertions.fail("'point' is null");
179             return;
180         }
181
182         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
183     }
184
185     @Test
186     public void shouldStoreMetadataAsTagsIfProvided() throws ExecutionException, InterruptedException {
187         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
188         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
189
190         when(metadataRegistry.get(metadataKey))
191                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
192
193         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
194
195         if (point == null) {
196             Assertions.fail("'point' is null");
197             return;
198         }
199
200         assertThat(point.getTags(), hasEntry("key1", "val1"));
201         assertThat(point.getTags(), hasEntry("key2", "val2"));
202     }
203
204     @Test
205     public void shouldUseMeasurementNameFromMetadataIfProvided() throws ExecutionException, InterruptedException {
206         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
207         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
208
209         InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
210         if (point == null) {
211             Assertions.fail();
212             return;
213         }
214         assertThat(point.getMeasurementName(), equalTo(item.getName()));
215
216         point = instance.convert(item, item.getState(), Instant.now(), null).get();
217         if (point == null) {
218             Assertions.fail();
219             return;
220         }
221         assertThat(point.getMeasurementName(), equalTo(item.getName()));
222         assertThat(point.getTags(), hasEntry("item", item.getName()));
223
224         when(metadataRegistry.get(metadataKey))
225                 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
226
227         point = instance.convert(item, item.getState(), Instant.now(), null).get();
228         if (point == null) {
229             Assertions.fail();
230             return;
231         }
232         assertThat(point.getMeasurementName(), equalTo("measurementName"));
233         assertThat(point.getTags(), hasEntry("item", item.getName()));
234
235         when(metadataRegistry.get(metadataKey))
236                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
237
238         point = instance.convert(item, item.getState(), Instant.now(), null).get();
239         if (point == null) {
240             Assertions.fail();
241             return;
242         }
243         assertThat(point.getMeasurementName(), equalTo(item.getName()));
244         assertThat(point.getTags(), hasEntry("item", item.getName()));
245     }
246
247     private InfluxDBPersistenceService getService(boolean replaceUnderscore, boolean category, boolean label,
248             boolean typeTag) {
249         InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(metadataRegistry);
250
251         Map<String, Object> configuration = new HashMap<>();
252         configuration.putAll(BASE_CONFIGURATION);
253         configuration.put(REPLACE_UNDERSCORE_PARAM, replaceUnderscore);
254         configuration.put(ADD_CATEGORY_TAG_PARAM, category);
255         configuration.put(ADD_LABEL_TAG_PARAM, label);
256         configuration.put(ADD_TYPE_TAG_PARAM, typeTag);
257
258         InfluxDBPersistenceService instance = new InfluxDBPersistenceService(itemRegistryMock, influxDBMetadataService,
259                 configuration);
260         instance.setItemFactory(new CoreItemFactory(unitProviderMock));
261
262         return instance;
263     }
264 }