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.time.Instant;
22 import java.util.HashMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.stream.Stream;
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;
51 * @author Joan Pujol Espinar - Initial contribution
53 @ExtendWith(MockitoExtension.class)
54 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
55 public class ItemToStorePointCreatorTest {
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");
64 private @Mock UnitProvider unitProviderMock;
65 private @Mock ItemRegistry itemRegistryMock;
66 private @Mock MetadataRegistry metadataRegistry;
67 private InfluxDBPersistenceService instance;
71 instance = getService(false, false, false, false);
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();
81 Assertions.fail("'point' is null");
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())));
90 @SuppressWarnings("unused")
91 private static Stream<Number> convertBasicItem() {
92 return Stream.of(5, 5.5, 5L);
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();
101 Assertions.fail("'point' is null");
105 assertThat(point.getMeasurementName(), is("aliasName"));
109 public void shouldStoreCategoryTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
110 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
111 item.setCategory("categoryValue");
113 instance = getService(false, true, false, false);
114 InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
117 Assertions.fail("'point' is null");
121 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
123 instance = getService(false, false, false, false);
124 point = instance.convert(item, item.getState(), Instant.now(), null).get();
127 Assertions.fail("'point' is null");
131 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
135 public void shouldStoreTypeTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
136 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
138 instance = getService(false, false, false, true);
139 InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
142 Assertions.fail("'point' is null");
146 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
148 instance = getService(false, false, false, false);
149 point = instance.convert(item, item.getState(), Instant.now(), null).get();
152 Assertions.fail("'point' is null");
156 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
160 public void shouldStoreTypeLabelIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
161 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
162 item.setLabel("ItemLabel");
164 instance = getService(false, false, true, false);
165 InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
168 Assertions.fail("'point' is null");
172 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
174 instance = getService(false, false, false, false);
175 point = instance.convert(item, item.getState(), Instant.now(), null).get();
178 Assertions.fail("'point' is null");
182 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
186 public void shouldStoreMetadataAsTagsIfProvided() throws ExecutionException, InterruptedException {
187 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
188 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
190 when(metadataRegistry.get(metadataKey))
191 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
193 InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
196 Assertions.fail("'point' is null");
200 assertThat(point.getTags(), hasEntry("key1", "val1"));
201 assertThat(point.getTags(), hasEntry("key2", "val2"));
205 public void shouldUseMeasurementNameFromMetadataIfProvided() throws ExecutionException, InterruptedException {
206 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
207 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
209 InfluxPoint point = instance.convert(item, item.getState(), Instant.now(), null).get();
214 assertThat(point.getMeasurementName(), equalTo(item.getName()));
216 point = instance.convert(item, item.getState(), Instant.now(), null).get();
221 assertThat(point.getMeasurementName(), equalTo(item.getName()));
222 assertThat(point.getTags(), hasEntry("item", item.getName()));
224 when(metadataRegistry.get(metadataKey))
225 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
227 point = instance.convert(item, item.getState(), Instant.now(), null).get();
232 assertThat(point.getMeasurementName(), equalTo("measurementName"));
233 assertThat(point.getTags(), hasEntry("item", item.getName()));
235 when(metadataRegistry.get(metadataKey))
236 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
238 point = instance.convert(item, item.getState(), Instant.now(), null).get();
243 assertThat(point.getMeasurementName(), equalTo(item.getName()));
244 assertThat(point.getTags(), hasEntry("item", item.getName()));
247 private InfluxDBPersistenceService getService(boolean replaceUnderscore, boolean category, boolean label,
249 InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(metadataRegistry);
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);
258 InfluxDBPersistenceService instance = new InfluxDBPersistenceService(itemRegistryMock, influxDBMetadataService,
260 instance.setItemFactory(new CoreItemFactory(unitProviderMock));