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.equalTo;
17 import static org.hamcrest.Matchers.is;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
22 import java.math.BigDecimal;
23 import java.time.Instant;
24 import java.time.ZoneId;
25 import java.time.ZonedDateTime;
27 import javax.measure.Unit;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.params.ParameterizedTest;
32 import org.junit.jupiter.params.provider.ValueSource;
33 import org.openhab.core.i18n.UnitProvider;
34 import org.openhab.core.library.items.ContactItem;
35 import org.openhab.core.library.items.DateTimeItem;
36 import org.openhab.core.library.items.NumberItem;
37 import org.openhab.core.library.items.SwitchItem;
38 import org.openhab.core.library.types.DateTimeType;
39 import org.openhab.core.library.types.DecimalType;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.library.types.OpenClosedType;
42 import org.openhab.core.library.types.QuantityType;
43 import org.openhab.core.library.unit.SIUnits;
46 * @author Joan Pujol Espinar - Initial contribution
49 public class InfluxDBStateConvertUtilsTest {
52 public void convertDecimalState() {
53 DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
54 assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12")));
58 public void convertIntegerDecimalState() {
59 DecimalType decimalType = new DecimalType(12L);
60 assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12")));
64 public void convertOnOffState() {
65 assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
66 assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
70 public void convertDateTimeState() {
71 ZonedDateTime now = ZonedDateTime.now();
72 long nowInMillis = now.toInstant().toEpochMilli();
73 DateTimeType type = new DateTimeType(now);
74 assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis));
78 @ValueSource(strings = { "1.12", "25" })
79 public void convertDecimalToState(String number) {
80 UnitProvider unitProviderMock = mock(UnitProvider.class);
81 when(unitProviderMock.getUnit(any())).thenReturn((Unit) SIUnits.CELSIUS);
82 BigDecimal val = new BigDecimal(number);
83 NumberItem plainItem = new NumberItem("plain");
84 NumberItem dimensionItem = new NumberItem("Number:Temperature", "dimension", unitProviderMock);
85 assertThat(InfluxDBStateConvertUtils.objectToState(val, plainItem), equalTo(new DecimalType(val)));
86 assertThat(InfluxDBStateConvertUtils.objectToState(val, dimensionItem),
87 equalTo(new QuantityType<>(new BigDecimal(number), SIUnits.CELSIUS)));
91 public void convertOnOffToState() {
95 SwitchItem onOffItem = new SwitchItem("name");
96 ContactItem contactItem = new ContactItem("name");
97 assertThat(InfluxDBStateConvertUtils.objectToState(val1, onOffItem), equalTo(OnOffType.ON));
98 assertThat(InfluxDBStateConvertUtils.objectToState(val2, onOffItem), equalTo(OnOffType.ON));
99 assertThat(InfluxDBStateConvertUtils.objectToState(val3, onOffItem), equalTo(OnOffType.ON));
100 assertThat(InfluxDBStateConvertUtils.objectToState(val1, contactItem), equalTo(OpenClosedType.OPEN));
101 assertThat(InfluxDBStateConvertUtils.objectToState(val2, contactItem), equalTo(OpenClosedType.OPEN));
102 assertThat(InfluxDBStateConvertUtils.objectToState(val3, contactItem), equalTo(OpenClosedType.OPEN));
106 public void convertDateTimeToState() {
107 long val = System.currentTimeMillis();
108 DateTimeItem item = new DateTimeItem("name");
110 DateTimeType expected = new DateTimeType(
111 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
112 assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));