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