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;
19 import java.math.BigDecimal;
20 import java.time.Instant;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.params.ParameterizedTest;
27 import org.junit.jupiter.params.provider.ValueSource;
28 import org.openhab.core.library.items.ContactItem;
29 import org.openhab.core.library.items.DateTimeItem;
30 import org.openhab.core.library.items.NumberItem;
31 import org.openhab.core.library.items.SwitchItem;
32 import org.openhab.core.library.types.DateTimeType;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.OpenClosedType;
38 * @author Joan Pujol Espinar - Initial contribution
41 public class InfluxDBStateConvertUtilsTest {
44 public void convertDecimalState() {
45 DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
46 assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12")));
50 public void convertIntegerDecimalState() {
51 DecimalType decimalType = new DecimalType(12L);
52 assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12")));
56 public void convertOnOffState() {
57 assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
58 assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
62 public void convertDateTimeState() {
63 ZonedDateTime now = ZonedDateTime.now();
64 long nowInMillis = now.toInstant().toEpochMilli();
65 DateTimeType type = new DateTimeType(now);
66 assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis));
70 @ValueSource(strings = { "1.12", "25" })
71 public void convertDecimalToState(String number) {
72 BigDecimal val = new BigDecimal(number);
73 NumberItem item = new NumberItem("name");
74 assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(new DecimalType(val)));
78 public void convertOnOffToState() {
82 SwitchItem onOffItem = new SwitchItem("name");
83 ContactItem contactItem = new ContactItem("name");
84 assertThat(InfluxDBStateConvertUtils.objectToState(val1, onOffItem), equalTo(OnOffType.ON));
85 assertThat(InfluxDBStateConvertUtils.objectToState(val2, onOffItem), equalTo(OnOffType.ON));
86 assertThat(InfluxDBStateConvertUtils.objectToState(val3, onOffItem), equalTo(OnOffType.ON));
87 assertThat(InfluxDBStateConvertUtils.objectToState(val1, contactItem), equalTo(OpenClosedType.OPEN));
88 assertThat(InfluxDBStateConvertUtils.objectToState(val2, contactItem), equalTo(OpenClosedType.OPEN));
89 assertThat(InfluxDBStateConvertUtils.objectToState(val3, contactItem), equalTo(OpenClosedType.OPEN));
93 public void convertDateTimeToState() {
94 long val = System.currentTimeMillis();
95 DateTimeItem item = new DateTimeItem("name");
97 DateTimeType expected = new DateTimeType(
98 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
99 assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));