]> git.basschouten.com Git - openhab-addons.git/blob
249697475f8f24ea62902d552d72860721b1a935
[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 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21
22 import java.math.BigDecimal;
23 import java.time.Instant;
24 import java.time.ZoneId;
25 import java.time.ZonedDateTime;
26
27 import javax.measure.Unit;
28
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;
44
45 /**
46  * @author Joan Pujol Espinar - Initial contribution
47  */
48 @NonNullByDefault
49 public class InfluxDBStateConvertUtilsTest {
50
51     @Test
52     public void convertDecimalState() {
53         DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
54         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12")));
55     }
56
57     @Test
58     public void convertIntegerDecimalState() {
59         DecimalType decimalType = new DecimalType(12L);
60         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12")));
61     }
62
63     @Test
64     public void convertOnOffState() {
65         assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
66         assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
67     }
68
69     @Test
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));
75     }
76
77     @ParameterizedTest
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)));
88     }
89
90     @Test
91     public void convertOnOffToState() {
92         boolean val1 = true;
93         int val2 = 1;
94         double val3 = 1.0;
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));
103     }
104
105     @Test
106     public void convertDateTimeToState() {
107         long val = System.currentTimeMillis();
108         DateTimeItem item = new DateTimeItem("name");
109
110         DateTimeType expected = new DateTimeType(
111                 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
112         assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
113     }
114 }