2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.influxdb;
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.*;
20 import java.math.BigDecimal;
21 import java.util.HashMap;
23 import java.util.concurrent.ExecutionException;
24 import java.util.stream.Stream;
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;
50 * @author Joan Pujol Espinar - Initial contribution
52 @ExtendWith(MockitoExtension.class)
53 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
54 public class ItemToStorePointCreatorTest {
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");
63 private @Mock UnitProvider unitProviderMock;
64 private @Mock ItemRegistry itemRegistryMock;
65 private @Mock MetadataRegistry metadataRegistry;
66 private InfluxDBPersistenceService instance;
70 instance = getService(false, false, false, false);
75 public void convertBasicItem(Number number) throws ExecutionException, InterruptedException {
76 NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
77 InfluxPoint point = instance.convert(item, null).get();
80 Assertions.fail("'point' is null");
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())));
89 @SuppressWarnings("unused")
90 private static Stream<Number> convertBasicItem() {
91 return Stream.of(5, 5.5, 5L);
95 public void shouldUseAliasAsMeasurementNameIfProvided() throws ExecutionException, InterruptedException {
96 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
97 InfluxPoint point = instance.convert(item, "aliasName").get();
100 Assertions.fail("'point' is null");
104 assertThat(point.getMeasurementName(), is("aliasName"));
108 public void shouldStoreCategoryTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
109 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
110 item.setCategory("categoryValue");
112 instance = getService(false, true, false, false);
113 InfluxPoint point = instance.convert(item, null).get();
116 Assertions.fail("'point' is null");
120 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
122 instance = getService(false, false, false, false);
123 point = instance.convert(item, null).get();
126 Assertions.fail("'point' is null");
130 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
134 public void shouldStoreTypeTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
135 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
137 instance = getService(false, false, false, true);
138 InfluxPoint point = instance.convert(item, null).get();
141 Assertions.fail("'point' is null");
145 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
147 instance = getService(false, false, false, false);
148 point = instance.convert(item, null).get();
151 Assertions.fail("'point' is null");
155 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
159 public void shouldStoreTypeLabelIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
160 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
161 item.setLabel("ItemLabel");
163 instance = getService(false, false, true, false);
164 InfluxPoint point = instance.convert(item, null).get();
167 Assertions.fail("'point' is null");
171 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
173 instance = getService(false, false, false, false);
174 point = instance.convert(item, null).get();
177 Assertions.fail("'point' is null");
181 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
185 public void shouldStoreMetadataAsTagsIfProvided() throws ExecutionException, InterruptedException {
186 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
187 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
189 when(metadataRegistry.get(metadataKey))
190 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
192 InfluxPoint point = instance.convert(item, null).get();
195 Assertions.fail("'point' is null");
199 assertThat(point.getTags(), hasEntry("key1", "val1"));
200 assertThat(point.getTags(), hasEntry("key2", "val2"));
204 public void shouldUseMeasurementNameFromMetadataIfProvided() throws ExecutionException, InterruptedException {
205 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
206 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
208 InfluxPoint point = instance.convert(item, null).get();
213 assertThat(point.getMeasurementName(), equalTo(item.getName()));
215 point = instance.convert(item, null).get();
220 assertThat(point.getMeasurementName(), equalTo(item.getName()));
221 assertThat(point.getTags(), hasEntry("item", item.getName()));
223 when(metadataRegistry.get(metadataKey))
224 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
226 point = instance.convert(item, null).get();
231 assertThat(point.getMeasurementName(), equalTo("measurementName"));
232 assertThat(point.getTags(), hasEntry("item", item.getName()));
234 when(metadataRegistry.get(metadataKey))
235 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
237 point = instance.convert(item, null).get();
242 assertThat(point.getMeasurementName(), equalTo(item.getName()));
243 assertThat(point.getTags(), hasEntry("item", item.getName()));
246 private InfluxDBPersistenceService getService(boolean replaceUnderscore, boolean category, boolean label,
248 InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(metadataRegistry);
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);
257 InfluxDBPersistenceService instance = new InfluxDBPersistenceService(itemRegistryMock, influxDBMetadataService,
259 instance.setItemFactory(new CoreItemFactory(unitProviderMock));