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.internal;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.mockito.Mockito.when;
19 import java.math.BigDecimal;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.DefaultLocation;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.MethodSource;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.openhab.core.items.Metadata;
35 import org.openhab.core.items.MetadataKey;
36 import org.openhab.core.items.MetadataRegistry;
37 import org.openhab.core.library.items.NumberItem;
38 import org.openhab.persistence.influxdb.InfluxDBPersistenceService;
41 * @author Joan Pujol Espinar - Initial contribution
43 @ExtendWith(MockitoExtension.class)
44 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
45 public class ItemToStorePointCreatorTest {
47 private @Mock InfluxDBConfiguration influxDBConfiguration;
48 private @Mock MetadataRegistry metadataRegistry;
49 private ItemToStorePointCreator instance;
52 public void before() {
53 InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(metadataRegistry);
54 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
55 when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
56 when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
57 when(influxDBConfiguration.isReplaceUnderscore()).thenReturn(false);
59 instance = new ItemToStorePointCreator(influxDBConfiguration, influxDBMetadataService);
65 influxDBConfiguration = null;
66 metadataRegistry = null;
71 public void convertBasicItem(Number number) {
72 NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
73 InfluxPoint point = instance.convert(item, null);
76 Assertions.fail("'point' is null");
80 assertThat(point.getMeasurementName(), equalTo(item.getName()));
81 assertThat("Must Store item name", point.getTags(), hasEntry("item", item.getName()));
82 assertThat(point.getValue(), equalTo(new BigDecimal(number.toString())));
85 @SuppressWarnings("unused")
86 private static Stream<Number> convertBasicItem() {
87 return Stream.of(5, 5.5, 5L);
91 public void shouldUseAliasAsMeasurementNameIfProvided() {
92 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
93 InfluxPoint point = instance.convert(item, "aliasName");
96 Assertions.fail("'point' is null");
100 assertThat(point.getMeasurementName(), is("aliasName"));
104 public void shouldStoreCategoryTagIfProvidedAndConfigured() {
105 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
106 item.setCategory("categoryValue");
108 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
109 InfluxPoint point = instance.convert(item, null);
112 Assertions.fail("'point' is null");
116 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
118 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
119 point = instance.convert(item, null);
122 Assertions.fail("'point' is null");
126 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
130 public void shouldStoreTypeTagIfProvidedAndConfigured() {
131 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
133 when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
134 InfluxPoint point = instance.convert(item, null);
137 Assertions.fail("'point' is null");
141 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
143 when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
144 point = instance.convert(item, null);
147 Assertions.fail("'point' is null");
151 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
155 public void shouldStoreTypeLabelIfProvidedAndConfigured() {
156 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
157 item.setLabel("ItemLabel");
159 when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
160 InfluxPoint point = instance.convert(item, null);
163 Assertions.fail("'point' is null");
167 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
169 when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
170 point = instance.convert(item, null);
173 Assertions.fail("'point' is null");
177 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
181 public void shouldStoreMetadataAsTagsIfProvided() {
182 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
183 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
185 when(metadataRegistry.get(metadataKey))
186 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
188 InfluxPoint point = instance.convert(item, null);
191 Assertions.fail("'point' is null");
195 assertThat(point.getTags(), hasEntry("key1", "val1"));
196 assertThat(point.getTags(), hasEntry("key2", "val2"));
200 public void shouldUseMeasurementNameFromMetadataIfProvided() {
201 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
202 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
204 InfluxPoint point = instance.convert(item, null);
209 assertThat(point.getMeasurementName(), equalTo(item.getName()));
211 point = instance.convert(item, null);
216 assertThat(point.getMeasurementName(), equalTo(item.getName()));
217 assertThat(point.getTags(), hasEntry("item", item.getName()));
219 when(metadataRegistry.get(metadataKey))
220 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
222 point = instance.convert(item, null);
227 assertThat(point.getMeasurementName(), equalTo("measurementName"));
228 assertThat(point.getTags(), hasEntry("item", item.getName()));
230 when(metadataRegistry.get(metadataKey))
231 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
233 point = instance.convert(item, null);
238 assertThat(point.getMeasurementName(), equalTo(item.getName()));
239 assertThat(point.getTags(), hasEntry("item", item.getName()));