]> git.basschouten.com Git - openhab-addons.git/blob
2dc2f8cb391856369071a30405b2e4a90d96d675
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.binding.homematic.internal.converter;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import org.junit.jupiter.api.Test;
19 import org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter;
20 import org.openhab.binding.homematic.internal.model.HmDatapoint;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.ImperialUnits;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.State;
26
27 import tech.units.indriya.unit.UnitDimension;
28
29 /**
30  * Tests for {@link AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
31  *
32  * @author Michael Reitler - Initial Contribution
33  *
34  */
35 public class ConvertFromBindingTest extends BaseConverterTest {
36
37     @Test
38     public void testDecimalTypeConverter() throws ConverterException {
39         State convertedState;
40         TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
41
42         // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
43         floatDp.setUnit("%");
44
45         floatDp.setValue(99.9);
46         convertedState = decimalConverter.convertFromBinding(floatDp);
47         assertThat(convertedState, instanceOf(DecimalType.class));
48         assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
49
50         floatDp.setValue(77.77777778);
51         convertedState = decimalConverter.convertFromBinding(floatDp);
52         assertThat(convertedState, instanceOf(DecimalType.class));
53         assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
54
55         integerDp.setValue(99.0);
56         convertedState = decimalConverter.convertFromBinding(integerDp);
57         assertThat(convertedState, instanceOf(DecimalType.class));
58         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
59
60         integerDp.setValue(99.9);
61         convertedState = decimalConverter.convertFromBinding(integerDp);
62         assertThat(convertedState, instanceOf(DecimalType.class));
63         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
64     }
65
66     @SuppressWarnings("null")
67     @Test
68     public void testQuantityTypeConverter() throws ConverterException {
69         State convertedState;
70         TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
71         TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
72         TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
73
74         floatQuantityDp.setValue(10.5);
75         floatQuantityDp.setUnit("°C");
76         convertedState = temperatureConverter.convertFromBinding(floatQuantityDp);
77         assertThat(convertedState, instanceOf(QuantityType.class));
78         assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
79         assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
80         assertThat(((QuantityType<?>) convertedState).toUnit(ImperialUnits.FAHRENHEIT).doubleValue(), is(50.9));
81
82         floatQuantityDp.setUnit("°C");
83         assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
84         assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
85
86         integerQuantityDp.setValue(50000);
87         integerQuantityDp.setUnit("mHz");
88         convertedState = frequencyConverter.convertFromBinding(integerQuantityDp);
89         assertThat(convertedState, instanceOf(QuantityType.class));
90         assertThat(((QuantityType<?>) convertedState).getDimension(),
91                 is(UnitDimension.NONE.divide(UnitDimension.TIME)));
92         assertThat(((QuantityType<?>) convertedState).intValue(), is(50000));
93         assertThat(((QuantityType<?>) convertedState).toUnit(Units.HERTZ).intValue(), is(50));
94
95         floatQuantityDp.setValue(0.7);
96         floatQuantityDp.setUnit("100%");
97         convertedState = timeConverter.convertFromBinding(floatQuantityDp);
98         assertThat(convertedState, instanceOf(QuantityType.class));
99         assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.NONE));
100         assertThat(((QuantityType<?>) convertedState).doubleValue(), is(70.0));
101         assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
102         assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));
103     }
104 }