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.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.MethodSource;
31 import org.mockito.Mock;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.openhab.core.items.Metadata;
34 import org.openhab.core.items.MetadataKey;
35 import org.openhab.core.items.MetadataRegistry;
36 import org.openhab.core.library.items.NumberItem;
37 import org.openhab.persistence.influxdb.InfluxDBPersistenceService;
40 * @author Joan Pujol Espinar - Initial contribution
42 @ExtendWith(MockitoExtension.class)
43 @SuppressWarnings("null") // In case of any NPE it will cause test fail that it's the expected result
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 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
54 when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
55 when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
56 when(influxDBConfiguration.isReplaceUnderscore()).thenReturn(false);
58 instance = new ItemToStorePointCreator(influxDBConfiguration, metadataRegistry);
64 influxDBConfiguration = null;
65 metadataRegistry = null;
70 public void convertBasicItem(Number number) {
71 NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
72 InfluxPoint point = instance.convert(item, null);
74 assertThat(point.getMeasurementName(), equalTo(item.getName()));
75 assertThat("Must Store item name", point.getTags(), hasEntry("item", item.getName()));
76 assertThat(point.getValue(), equalTo(new BigDecimal(number.toString())));
79 private static Stream<Number> convertBasicItem() {
80 return Stream.of(5, 5.5, 5L);
84 public void shouldUseAliasAsMeasurementNameIfProvided() {
85 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
86 InfluxPoint point = instance.convert(item, "aliasName");
87 assertThat(point.getMeasurementName(), is("aliasName"));
91 public void shouldStoreCategoryTagIfProvidedAndConfigured() {
92 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
93 item.setCategory("categoryValue");
95 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
96 InfluxPoint point = instance.convert(item, null);
97 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
99 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
100 point = instance.convert(item, null);
101 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
105 public void shouldStoreTypeTagIfProvidedAndConfigured() {
106 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
108 when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
109 InfluxPoint point = instance.convert(item, null);
110 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
112 when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
113 point = instance.convert(item, null);
114 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
118 public void shouldStoreTypeLabelIfProvidedAndConfigured() {
119 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
120 item.setLabel("ItemLabel");
122 when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
123 InfluxPoint point = instance.convert(item, null);
124 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
126 when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
127 point = instance.convert(item, null);
128 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
132 public void shouldStoreMetadataAsTagsIfProvided() {
133 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
134 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
136 when(metadataRegistry.get(metadataKey))
137 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
139 InfluxPoint point = instance.convert(item, null);
140 assertThat(point.getTags(), hasEntry("key1", "val1"));
141 assertThat(point.getTags(), hasEntry("key2", "val2"));
145 public void shouldUseMeasurementNameFromMetadataIfProvided() {
146 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
147 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
149 InfluxPoint point = instance.convert(item, null);
150 assertThat(point.getMeasurementName(), equalTo(item.getName()));
152 point = instance.convert(item, null);
153 assertThat(point.getMeasurementName(), equalTo(item.getName()));
154 assertThat(point.getTags(), hasEntry("item", item.getName()));
156 when(metadataRegistry.get(metadataKey))
157 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
159 point = instance.convert(item, null);
160 assertThat(point.getMeasurementName(), equalTo("measurementName"));
161 assertThat(point.getTags(), hasEntry("item", item.getName()));
163 when(metadataRegistry.get(metadataKey))
164 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
166 point = instance.convert(item, null);
167 assertThat(point.getMeasurementName(), equalTo(item.getName()));
168 assertThat(point.getTags(), hasEntry("item", item.getName()));