2 * Copyright (c) 2010-2020 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.BigInteger;
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;
37 * @author Joan Pujol Espinar - Initial contribution
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 {
44 private @Mock InfluxDBConfiguration influxDBConfiguration;
45 private @Mock MetadataRegistry metadataRegistry;
46 private ItemToStorePointCreator instance;
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);
55 instance = new ItemToStorePointCreator(influxDBConfiguration, metadataRegistry);
61 influxDBConfiguration = null;
62 metadataRegistry = null;
66 public void convertBasicItem() {
67 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
68 InfluxPoint point = instance.convert(item, null);
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")));
76 public void shouldUseAliasAsMeasurementNameIfProvided() {
77 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
78 InfluxPoint point = instance.convert(item, "aliasName");
79 assertThat(point.getMeasurementName(), is("aliasName"));
83 public void shouldStoreCategoryTagIfProvidedAndConfigured() {
84 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
85 item.setCategory("categoryValue");
87 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(true);
88 InfluxPoint point = instance.convert(item, null);
89 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_CATEGORY_NAME, "categoryValue"));
91 when(influxDBConfiguration.isAddCategoryTag()).thenReturn(false);
92 point = instance.convert(item, null);
93 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_CATEGORY_NAME)));
97 public void shouldStoreTypeTagIfProvidedAndConfigured() {
98 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
100 when(influxDBConfiguration.isAddTypeTag()).thenReturn(true);
101 InfluxPoint point = instance.convert(item, null);
102 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_TYPE_NAME, "Number"));
104 when(influxDBConfiguration.isAddTypeTag()).thenReturn(false);
105 point = instance.convert(item, null);
106 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_TYPE_NAME)));
110 public void shouldStoreTypeLabelIfProvidedAndConfigured() {
111 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
112 item.setLabel("ItemLabel");
114 when(influxDBConfiguration.isAddLabelTag()).thenReturn(true);
115 InfluxPoint point = instance.convert(item, null);
116 assertThat(point.getTags(), hasEntry(InfluxDBConstants.TAG_LABEL_NAME, "ItemLabel"));
118 when(influxDBConfiguration.isAddLabelTag()).thenReturn(false);
119 point = instance.convert(item, null);
120 assertThat(point.getTags(), not(hasKey(InfluxDBConstants.TAG_LABEL_NAME)));
124 public void shouldStoreMetadataAsTagsIfProvided() {
125 NumberItem item = ItemTestHelper.createNumberItem("myitem", 5);
126 MetadataKey metadataKey = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
128 when(metadataRegistry.get(metadataKey))
129 .thenReturn(new Metadata(metadataKey, "", Map.of("key1", "val1", "key2", "val2")));
131 InfluxPoint point = instance.convert(item, null);
132 assertThat(point.getTags(), hasEntry("key1", "val1"));
133 assertThat(point.getTags(), hasEntry("key2", "val2"));