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.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;
49 * @author Joan Pujol Espinar - Initial contribution
51 @ExtendWith(MockitoExtension.class)
52 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
53 public class ItemToStorePointCreatorTest {
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");
62 private @Mock ItemRegistry itemRegistryMock;
63 private @Mock MetadataRegistry metadataRegistry;
64 private InfluxDBPersistenceService instance;
68 instance = getService(false, false, false, false);
73 public void convertBasicItem(Number number) throws ExecutionException, InterruptedException {
74 NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
75 InfluxPoint point = instance.convert(item, null).get();
78 Assertions.fail("'point' is null");
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())));
87 @SuppressWarnings("unused")
88 private static Stream<Number> convertBasicItem() {
89 return Stream.of(5, 5.5, 5L);
93 public void shouldUseAliasAsMeasurementNameIfProvided() throws ExecutionException, InterruptedException {
94 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
95 InfluxPoint point = instance.convert(item, "aliasName").get();
98 Assertions.fail("'point' is null");
102 assertThat(point.getMeasurementName(), is("aliasName"));
106 public void shouldStoreCategoryTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
107 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
108 item.setCategory("categoryValue");
110 instance = getService(false, true, false, false);
111 InfluxPoint point = instance.convert(item, null).get();
114 Assertions.fail("'point' is null");
118 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
120 instance = getService(false, false, false, false);
121 point = instance.convert(item, null).get();
124 Assertions.fail("'point' is null");
128 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
132 public void shouldStoreTypeTagIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
133 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
135 instance = getService(false, false, false, true);
136 InfluxPoint point = instance.convert(item, null).get();
139 Assertions.fail("'point' is null");
143 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
145 instance = getService(false, false, false, false);
146 point = instance.convert(item, null).get();
149 Assertions.fail("'point' is null");
153 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
157 public void shouldStoreTypeLabelIfProvidedAndConfigured() throws ExecutionException, InterruptedException {
158 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
159 item.setLabel("ItemLabel");
161 instance = getService(false, false, true, false);
162 InfluxPoint point = instance.convert(item, null).get();
165 Assertions.fail("'point' is null");
169 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
171 instance = getService(false, false, false, false);
172 point = instance.convert(item, null).get();
175 Assertions.fail("'point' is null");
179 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
183 public void shouldStoreMetadataAsTagsIfProvided() throws ExecutionException, InterruptedException {
184 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
185 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
187 when(metadataRegistry.get(metadataKey))
188 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
190 InfluxPoint point = instance.convert(item, null).get();
193 Assertions.fail("'point' is null");
197 assertThat(point.getTags(), hasEntry("key1", "val1"));
198 assertThat(point.getTags(), hasEntry("key2", "val2"));
202 public void shouldUseMeasurementNameFromMetadataIfProvided() throws ExecutionException, InterruptedException {
203 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
204 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
206 InfluxPoint point = instance.convert(item, null).get();
211 assertThat(point.getMeasurementName(), equalTo(item.getName()));
213 point = instance.convert(item, null).get();
218 assertThat(point.getMeasurementName(), equalTo(item.getName()));
219 assertThat(point.getTags(), hasEntry("item", item.getName()));
221 when(metadataRegistry.get(metadataKey))
222 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
224 point = instance.convert(item, null).get();
229 assertThat(point.getMeasurementName(), equalTo("measurementName"));
230 assertThat(point.getTags(), hasEntry("item", item.getName()));
232 when(metadataRegistry.get(metadataKey))
233 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
235 point = instance.convert(item, null).get();
240 assertThat(point.getMeasurementName(), equalTo(item.getName()));
241 assertThat(point.getTags(), hasEntry("item", item.getName()));
244 private InfluxDBPersistenceService getService(boolean replaceUnderscore, boolean category, boolean label,
246 InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(metadataRegistry);
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);
255 InfluxDBPersistenceService instance = new InfluxDBPersistenceService(itemRegistryMock, influxDBMetadataService,
257 instance.setItemFactory(new CoreItemFactory());