]> git.basschouten.com Git - openhab-addons.git/blob
32239144738fe94671d6d48c73bf5a4c8845edc1
[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.ImageItem;
37 import org.openhab.core.library.items.NumberItem;
38 import org.openhab.core.library.items.PlayerItem;
39 import org.openhab.core.library.items.SwitchItem;
40 import org.openhab.core.library.types.DateTimeType;
41 import org.openhab.core.library.types.DecimalType;
42 import org.openhab.core.library.types.OnOffType;
43 import org.openhab.core.library.types.OpenClosedType;
44 import org.openhab.core.library.types.PlayPauseType;
45 import org.openhab.core.library.types.QuantityType;
46 import org.openhab.core.library.types.RawType;
47 import org.openhab.core.library.types.RewindFastforwardType;
48 import org.openhab.core.library.unit.SIUnits;
49
50 /**
51  * @author Joan Pujol Espinar - Initial contribution
52  */
53 @NonNullByDefault
54 public class InfluxDBStateConvertUtilsTest {
55
56     @Test
57     public void convertDecimalState() {
58         DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
59         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12")));
60     }
61
62     @Test
63     public void convertIntegerDecimalState() {
64         DecimalType decimalType = new DecimalType(12L);
65         assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12")));
66     }
67
68     @Test
69     public void convertOnOffState() {
70         assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
71         assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
72     }
73
74     @Test
75     public void convertDateTimeState() {
76         ZonedDateTime now = ZonedDateTime.now();
77         long nowInMillis = now.toInstant().toEpochMilli();
78         DateTimeType type = new DateTimeType(now);
79         assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis));
80     }
81
82     @Test
83     public void convertImageState() {
84         RawType type = new RawType(new byte[] { 0x64, 0x66, 0x55, 0x00, 0x34 }, RawType.DEFAULT_MIME_TYPE);
85         assertThat(InfluxDBStateConvertUtils.stateToObject(type), is("data:application/octet-stream;base64,ZGZVADQ="));
86     }
87
88     @Test
89     public void convertPlayPauseState() {
90         assertThat(InfluxDBStateConvertUtils.stateToObject(PlayPauseType.PAUSE), is("PAUSE"));
91         assertThat(InfluxDBStateConvertUtils.stateToObject(PlayPauseType.PLAY), is("PLAY"));
92     }
93
94     @Test
95     public void convertRewindFastForwardState() {
96         assertThat(InfluxDBStateConvertUtils.stateToObject(RewindFastforwardType.REWIND), is("REWIND"));
97         assertThat(InfluxDBStateConvertUtils.stateToObject(RewindFastforwardType.FASTFORWARD), is("FASTFORWARD"));
98     }
99
100     @ParameterizedTest
101     @ValueSource(strings = { "1.12", "25" })
102     public void convertDecimalToState(String number) {
103         UnitProvider unitProviderMock = mock(UnitProvider.class);
104         when(unitProviderMock.getUnit(any())).thenReturn((Unit) SIUnits.CELSIUS);
105         BigDecimal val = new BigDecimal(number);
106         NumberItem plainItem = new NumberItem("plain");
107         NumberItem dimensionItem = new NumberItem("Number:Temperature", "dimension", unitProviderMock);
108         assertThat(InfluxDBStateConvertUtils.objectToState(val, plainItem), equalTo(new DecimalType(val)));
109         assertThat(InfluxDBStateConvertUtils.objectToState(val, dimensionItem),
110                 equalTo(new QuantityType<>(new BigDecimal(number), SIUnits.CELSIUS)));
111     }
112
113     @Test
114     public void convertOnOffToState() {
115         boolean val1 = true;
116         int val2 = 1;
117         double val3 = 1.0;
118         SwitchItem onOffItem = new SwitchItem("name");
119         ContactItem contactItem = new ContactItem("name");
120         assertThat(InfluxDBStateConvertUtils.objectToState(val1, onOffItem), equalTo(OnOffType.ON));
121         assertThat(InfluxDBStateConvertUtils.objectToState(val2, onOffItem), equalTo(OnOffType.ON));
122         assertThat(InfluxDBStateConvertUtils.objectToState(val3, onOffItem), equalTo(OnOffType.ON));
123         assertThat(InfluxDBStateConvertUtils.objectToState(val1, contactItem), equalTo(OpenClosedType.OPEN));
124         assertThat(InfluxDBStateConvertUtils.objectToState(val2, contactItem), equalTo(OpenClosedType.OPEN));
125         assertThat(InfluxDBStateConvertUtils.objectToState(val3, contactItem), equalTo(OpenClosedType.OPEN));
126     }
127
128     @Test
129     public void convertDateTimeToState() {
130         long val = System.currentTimeMillis();
131         DateTimeItem item = new DateTimeItem("name");
132
133         DateTimeType expected = new DateTimeType(
134                 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
135         assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
136     }
137
138     @Test
139     public void convertImageToState() {
140         ImageItem item = new ImageItem("name");
141         RawType type = new RawType(new byte[] { 0x64, 0x66, 0x55, 0x00, 0x34 }, RawType.DEFAULT_MIME_TYPE);
142         assertThat(InfluxDBStateConvertUtils.objectToState("data:application/octet-stream;base64,ZGZVADQ=", item),
143                 is(type));
144     }
145
146     @Test
147     public void convertPlayerToState() {
148         PlayerItem item = new PlayerItem("name");
149         assertThat(InfluxDBStateConvertUtils.objectToState("PLAY", item), is(PlayPauseType.PLAY));
150         assertThat(InfluxDBStateConvertUtils.objectToState("PAUSE", item), is(PlayPauseType.PAUSE));
151         assertThat(InfluxDBStateConvertUtils.objectToState("REWIND", item), is(RewindFastforwardType.REWIND));
152         assertThat(InfluxDBStateConvertUtils.objectToState("FASTFORWARD", item), is(RewindFastforwardType.FASTFORWARD));
153     }
154 }