]> git.basschouten.com Git - openhab-addons.git/blob
a7ac2058d423ffe28b50e515be9d42a55b9d80f9
[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
18 import java.math.BigDecimal;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.library.items.ContactItem;
26 import org.openhab.core.library.items.DateTimeItem;
27 import org.openhab.core.library.items.NumberItem;
28 import org.openhab.core.library.items.SwitchItem;
29 import org.openhab.core.library.types.DateTimeType;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.OpenClosedType;
33
34 /**
35  * @author Joan Pujol Espinar - Initial contribution
36  */
37 @NonNullByDefault
38 public class InfluxDBStateConvertUtilsTest {
39
40     @Test
41     public void convertDecimalState() {
42         DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
43         assertThat((Double) InfluxDBStateConvertUtils.stateToObject(decimalType), closeTo(1.12, 0.01));
44     }
45
46     @Test
47     public void convertOnOffState() {
48         assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
49         assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
50     }
51
52     @Test
53     public void convertDateTimeState() {
54         ZonedDateTime now = ZonedDateTime.now();
55         long nowInMillis = now.toInstant().toEpochMilli();
56         DateTimeType type = new DateTimeType(now);
57         assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis));
58     }
59
60     @Test
61     public void convertDecimalToState() {
62         BigDecimal val = new BigDecimal("1.12");
63         NumberItem item = new NumberItem("name");
64         assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(new DecimalType(val)));
65     }
66
67     @Test
68     public void convertOnOffToState() {
69         boolean val1 = true;
70         int val2 = 1;
71         SwitchItem onOffItem = new SwitchItem("name");
72         ContactItem contactItem = new ContactItem("name");
73         assertThat(InfluxDBStateConvertUtils.objectToState(val1, onOffItem), equalTo(OnOffType.ON));
74         assertThat(InfluxDBStateConvertUtils.objectToState(val2, onOffItem), equalTo(OnOffType.ON));
75         assertThat(InfluxDBStateConvertUtils.objectToState(val1, contactItem), equalTo(OpenClosedType.OPEN));
76         assertThat(InfluxDBStateConvertUtils.objectToState(val2, contactItem), equalTo(OpenClosedType.OPEN));
77     }
78
79     @Test
80     public void convertDateTimeToState() {
81         long val = System.currentTimeMillis();
82         DateTimeItem item = new DateTimeItem("name");
83
84         DateTimeType expected = new DateTimeType(
85                 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
86         assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
87     }
88 }