]> git.basschouten.com Git - openhab-addons.git/blob
7198a58aafdfa454def2c377403d986bd84e34cd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.BigInteger;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.DefaultLocation;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.openhab.core.items.Metadata;
31 import org.openhab.core.items.MetadataKey;
32 import org.openhab.core.items.MetadataRegistry;
33 import org.openhab.core.library.items.NumberItem;
34 import org.openhab.persistence.influxdb.InfluxDBPersistenceService;
35
36 /**
37  * @author Joan Pujol Espinar - Initial contribution
38  */
39 @ExtendWith(MockitoExtension.class)
40 @SuppressWarnings("null") // In case of any NPE it will cause test fail that it's the expected result
41 @NonNullByDefault(value = { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE })
42 public class ItemToStorePointCreatorTest {
43
44     private @Mock InfluxDBConfiguration influxDBConfiguration;
45     private @Mock MetadataRegistry metadataRegistry;
46     private ItemToStorePointCreator instance;
47
48     @BeforeEach
49     public void before() {
50         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
51         when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
52         when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
53         when(influxDBConfiguration.isReplaceUnderscore()).thenReturn(false);
54
55         instance = new ItemToStorePointCreator(influxDBConfiguration, metadataRegistry);
56     }
57
58     @AfterEach
59     public void after() {
60         instance = null;
61         influxDBConfiguration = null;
62         metadataRegistry = null;
63     }
64
65     @Test
66     public void convertBasicItem() {
67         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
68         InfluxPoint point = instance.convert(item, null);
69
70         assertThat(point.getMeasurementName(), equalTo(item.getName()));
71         assertThat("Must Store item name", point.getTags(), hasEntry("item", item.getName()));
72         assertThat(point.getValue(), equalTo(new BigInteger("5")));
73     }
74
75     @Test
76     public void shouldUseAliasAsMeasurementNameIfProvided() {
77         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
78         InfluxPoint point = instance.convert(item, "aliasName");
79         assertThat(point.getMeasurementName(), is("aliasName"));
80     }
81
82     @Test
83     public void shouldStoreCategoryTagIfProvidedAndConfigured() {
84         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
85         item.setCategory("categoryValue");
86
87         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
88         InfluxPoint point = instance.convert(item, null);
89         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
90
91         when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
92         point = instance.convert(item, null);
93         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
94     }
95
96     @Test
97     public void shouldStoreTypeTagIfProvidedAndConfigured() {
98         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
99
100         when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
101         InfluxPoint point = instance.convert(item, null);
102         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
103
104         when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
105         point = instance.convert(item, null);
106         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
107     }
108
109     @Test
110     public void shouldStoreTypeLabelIfProvidedAndConfigured() {
111         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
112         item.setLabel("ItemLabel");
113
114         when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
115         InfluxPoint point = instance.convert(item, null);
116         assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
117
118         when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
119         point = instance.convert(item, null);
120         assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
121     }
122
123     @Test
124     public void shouldStoreMetadataAsTagsIfProvided() {
125         NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
126         MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
127
128         when(metadataRegistry.get(metadataKey))
129                 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
130
131         InfluxPoint point = instance.convert(item, null);
132         assertThat(point.getTags(), hasEntry("key1", "val1"));
133         assertThat(point.getTags(), hasEntry("key2", "val2"));
134     }
135 }