2 * Copyright (c) 2010-2020 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.*;
18 import java.math.BigDecimal;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
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;
35 * @author Joan Pujol Espinar - Initial contribution
38 public class InfluxDBStateConvertUtilsTest {
41 public void convertDecimalState() {
42 DecimalType decimalType = new DecimalType(new BigDecimal("1.12"));
43 assertThat((Double) InfluxDBStateConvertUtils.stateToObject(decimalType), closeTo(1.12, 0.01));
47 public void convertOnOffState() {
48 assertThat(InfluxDBStateConvertUtils.stateToObject(OpenClosedType.OPEN), equalTo(1));
49 assertThat(InfluxDBStateConvertUtils.stateToObject(OnOffType.ON), equalTo(1));
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));
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)));
68 public void convertOnOffToState() {
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));
80 public void convertDateTimeToState() {
81 long val = System.currentTimeMillis();
82 DateTimeItem item = new DateTimeItem("name");
84 DateTimeType expected = new DateTimeType(
85 ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
86 assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));