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