]> git.basschouten.com Git - openhab-addons.git/blob
364489c5e33619f14e5e2b630787f6bcb0c2f486
[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.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;
39
40 /**
41  * @author Joan Pujol Espinar - Initial contribution
42  */
43 @ExtendWith(MockitoExtension.class)
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         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);
58
59         instance = new ItemToStorePointCreator(influxDBConfiguration, influxDBMetadataService);
60     }
61
62     @AfterEach
63     public void after() {
64         instance = null;
65         influxDBConfiguration = null;
66         metadataRegistry = null;
67     }
68
69     @ParameterizedTest
70     @MethodSource
71     public void convertBasicItem(Number number) {
72         NumberItem item = ItemTestHelper.createNumberItem("myitem", number);
73         InfluxPoint point = instance.convert(item, null);
74
75         if (point == null) {
76             Assertions.fail("'point' is null");
77             return;
78         }
79
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())));
83     }
84
85     @SuppressWarnings("unused")
86     private static Stream<Number> convertBasicItem() {
87         return Stream.of(5, 5.5, 5L);
88     }
89
90     @Test
91     public void shouldUseAliasAsMeasurementNameIfProvided() {
92         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
93         InfluxPoint point = instance.convert(item, "aliasName");
94
95         if (point == null) {
96             Assertions.fail("'point' is null");
97             return;
98         }
99
100         assertThat(point.getMeasurementName(), is("aliasName"));
101     }
102
103     @Test
104     public void shouldStoreCategoryTagIfProvidedAndConfigured() {
105         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
106         item.setCategory("categoryValue");
107
108         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
109         InfluxPoint point = instance.convert(item, null);
110
111         if (point == null) {
112             Assertions.fail("'point' is null");
113             return;
114         }
115
116         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
117
118         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
119         point = instance.convert(item, null);
120
121         if (point == null) {
122             Assertions.fail("'point' is null");
123             return;
124         }
125
126         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
127     }
128
129     @Test
130     public void shouldStoreTypeTagIfProvidedAndConfigured() {
131         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
132
133         when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
134         InfluxPoint point = instance.convert(item, null);
135
136         if (point == null) {
137             Assertions.fail("'point' is null");
138             return;
139         }
140
141         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
142
143         when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
144         point = instance.convert(item, null);
145
146         if (point == null) {
147             Assertions.fail("'point' is null");
148             return;
149         }
150
151         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
152     }
153
154     @Test
155     public void shouldStoreTypeLabelIfProvidedAndConfigured() {
156         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
157         item.setLabel("ItemLabel");
158
159         when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
160         InfluxPoint point = instance.convert(item, null);
161
162         if (point == null) {
163             Assertions.fail("'point' is null");
164             return;
165         }
166
167         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
168
169         when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
170         point = instance.convert(item, null);
171
172         if (point == null) {
173             Assertions.fail("'point' is null");
174             return;
175         }
176
177         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
178     }
179
180     @Test
181     public void shouldStoreMetadataAsTagsIfProvided() {
182         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
183         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
184
185         when(metadataRegistry.get(metadataKey))
186                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
187
188         InfluxPoint point = instance.convert(item, null);
189
190         if (point == null) {
191             Assertions.fail("'point' is null");
192             return;
193         }
194
195         assertThat(point.getTags(), hasEntry("key1", "val1"));
196         assertThat(point.getTags(), hasEntry("key2", "val2"));
197     }
198
199     @Test
200     public void shouldUseMeasurementNameFromMetadataIfProvided() {
201         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
202         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
203
204         InfluxPoint point = instance.convert(item, null);
205         if (point == null) {
206             Assertions.fail();
207             return;
208         }
209         assertThat(point.getMeasurementName(), equalTo(item.getName()));
210
211         point = instance.convert(item, null);
212         if (point == null) {
213             Assertions.fail();
214             return;
215         }
216         assertThat(point.getMeasurementName(), equalTo(item.getName()));
217         assertThat(point.getTags(), hasEntry("item", item.getName()));
218
219         when(metadataRegistry.get(metadataKey))
220                 .thenReturn(new Metadata(metadataKey, "measurementName", Map.of("key1", "val1", "key2", "val2")));
221
222         point = instance.convert(item, null);
223         if (point == null) {
224             Assertions.fail();
225             return;
226         }
227         assertThat(point.getMeasurementName(), equalTo("measurementName"));
228         assertThat(point.getTags(), hasEntry("item", item.getName()));
229
230         when(metadataRegistry.get(metadataKey))
231                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
232
233         point = instance.convert(item, null);
234         if (point == null) {
235             Assertions.fail();
236             return;
237         }
238         assertThat(point.getMeasurementName(), equalTo(item.getName()));
239         assertThat(point.getTags(), hasEntry("item", item.getName()));
240     }
241 }