]> git.basschouten.com Git - openhab-addons.git/blob
2f03389ea0d31e4891cd12bdc83f7761acf4bb92
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.influxdb.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.mockito.Mockito.when;
18
19 import java.math.BigDecimal;
20 import java.util.Map;
21 import java.util.stream.Stream;
22
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;
38
39 /**
40  * @author Joan Pujol Espinar - Initial contribution
41  */
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 {
46
47     private @Mock InfluxDBConfiguration influxDBConfiguration;
48     private @Mock MetadataRegistry metadataRegistry;
49     private ItemToStorePointCreator instance;
50
51     @BeforeEach
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);
57
58         instance = new ItemToStorePointCreator(influxDBConfiguration, metadataRegistry);
59     }
60
61     @AfterEach
62     public void after() {
63         instance = null;
64         influxDBConfiguration = null;
65         metadataRegistry = null;
66     }
67
68     @ParameterizedTest
69     @MethodSource
70     public void convertBasicItem(Number number) {
71         NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
72         InfluxPoint point = instance.convert(item, null);
73
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())));
77     }
78
79     private static Stream<Number> convertBasicItem() {
80         return Stream.of(5, 5.5, 5L);
81     }
82
83     @Test
84     public void shouldUseAliasAsMeasurementNameIfProvided() {
85         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
86         InfluxPoint point = instance.convert(item, "aliasName");
87         assertThat(point.getMeasurementName(), is("aliasName"));
88     }
89
90     @Test
91     public void shouldStoreCategoryTagIfProvidedAndConfigured() {
92         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
93         item.setCategory("categoryValue");
94
95         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
96         InfluxPoint point = instance.convert(item, null);
97         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
98
99         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
100         point = instance.convert(item, null);
101         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
102     }
103
104     @Test
105     public void shouldStoreTypeTagIfProvidedAndConfigured() {
106         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
107
108         when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
109         InfluxPoint point = instance.convert(item, null);
110         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
111
112         when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
113         point = instance.convert(item, null);
114         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
115     }
116
117     @Test
118     public void shouldStoreTypeLabelIfProvidedAndConfigured() {
119         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
120         item.setLabel("ItemLabel");
121
122         when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
123         InfluxPoint point = instance.convert(item, null);
124         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
125
126         when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
127         point = instance.convert(item, null);
128         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
129     }
130
131     @Test
132     public void shouldStoreMetadataAsTagsIfProvided() {
133         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
134         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
135
136         when(metadataRegistry.get(metadataKey))
137                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
138
139         InfluxPoint point = instance.convert(item, null);
140         assertThat(point.getTags(), hasEntry("key1", "val1"));
141         assertThat(point.getTags(), hasEntry("key2", "val2"));
142     }
143
144     @Test
145     public void shouldUseMeasurementNameFromMetadataIfProvided() {
146         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
147         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
148
149         InfluxPoint point = instance.convert(item, null);
150         assertThat(point.getMeasurementName(), equalTo(item.getName()));
151
152         point = instance.convert(item, null);
153         assertThat(point.getMeasurementName(), equalTo(item.getName()));
154         assertThat(point.getTags(), hasEntry("item", item.getName()));
155
156         when(metadataRegistry.get(metadataKey))
157                 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
158
159         point = instance.convert(item, null);
160         assertThat(point.getMeasurementName(), equalTo("measurementName"));
161         assertThat(point.getTags(), hasEntry("item", item.getName()));
162
163         when(metadataRegistry.get(metadataKey))
164                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
165
166         point = instance.convert(item, null);
167         assertThat(point.getMeasurementName(), equalTo(item.getName()));
168         assertThat(point.getTags(), hasEntry("item", item.getName()));
169     }
170 }