]> git.basschouten.com Git - openhab-addons.git/blob
b430d5b8b93e231942acb2c855a323ce1c0c9128
[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.equalTo;
17 import static org.hamcrest.Matchers.is;
18
19 import java.math.BigDecimal;
20 import java.time.Instant;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23
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;
36
37 /**
38  * @author Joan Pujol Espinar - Initial contribution
39  */
40 @NonNullByDefault
41 public class InfluxDBStateConvertUtilsTest {
42
43     @Test
44     public void convertDecimalState() {
45         DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
46         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12")));
47     }
48
49     @Test
50     public void convertIntegerDecimalState() {
51         DecimalType decimalType = new DecimalType(12L);
52         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12")));
53     }
54
55     @Test
56     public void convertOnOffState() {
57         assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
58         assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
59     }
60
61     @Test
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));
67     }
68
69     @ParameterizedTest
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)));
75     }
76
77     @Test
78     public void convertOnOffToState() {
79         boolean val1 = true;
80         int val2 = 1;
81         double val3 = 1.0;
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));
90     }
91
92     @Test
93     public void convertDateTimeToState() {
94         long val = System.currentTimeMillis();
95         DateTimeItem item = new DateTimeItem("name");
96
97         DateTimeType expected = new DateTimeType(
98                 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
99         assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
100     }
101 }