]> git.basschouten.com Git - openhab-addons.git/blob
cf5d7ce0f839f22bd58affc22688f40c78d10a2f
[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.binding.http.internal.converter;
14
15 import java.util.function.Function;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.Test;
20 import org.openhab.binding.http.internal.config.HttpChannelConfig;
21 import org.openhab.binding.http.internal.transform.NoOpValueTransformation;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.PointType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * The {@link ConverterTest} is a test class for state converters
34  *
35  * @author Jan N. Klug - Initial contribution
36  */
37 @NonNullByDefault
38 public class ConverterTest {
39
40     @Test
41     public void numberItemConverter() {
42         NumberItemConverter converter = new NumberItemConverter(this::updateState, this::postCommand,
43                 this::sendHttpValue, NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(),
44                 new HttpChannelConfig());
45
46         // without unit
47         Assertions.assertEquals(new DecimalType(1234), converter.toState("1234"));
48
49         // unit in transformation result
50         Assertions.assertEquals(new QuantityType<>(100, SIUnits.CELSIUS), converter.toState("100°C"));
51
52         // no valid value
53         Assertions.assertEquals(UnDefType.UNDEF, converter.toState("W"));
54         Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
55     }
56
57     @Test
58     public void numberItemConverterWithUnit() {
59         HttpChannelConfig channelConfig = new HttpChannelConfig();
60         channelConfig.unit = "W";
61         NumberItemConverter converter = new NumberItemConverter(this::updateState, this::postCommand,
62                 this::sendHttpValue, NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(),
63                 channelConfig);
64
65         // without unit
66         Assertions.assertEquals(new QuantityType<>(500, Units.WATT), converter.toState("500"));
67
68         // no valid value
69         Assertions.assertEquals(UnDefType.UNDEF, converter.toState("100foo"));
70         Assertions.assertEquals(UnDefType.UNDEF, converter.toState("foo"));
71         Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
72     }
73
74     @Test
75     public void stringTypeConverter() {
76         GenericItemConverter converter = createConverter(StringType::new);
77         Assertions.assertEquals(new StringType("Test"), converter.toState("Test"));
78     }
79
80     @Test
81     public void decimalTypeConverter() {
82         GenericItemConverter converter = createConverter(DecimalType::new);
83         Assertions.assertEquals(new DecimalType(15.6), converter.toState("15.6"));
84     }
85
86     @Test
87     public void pointTypeConverter() {
88         GenericItemConverter converter = createConverter(PointType::new);
89         Assertions.assertEquals(new PointType(new DecimalType(51.1), new DecimalType(7.2), new DecimalType(100)),
90                 converter.toState("51.1, 7.2, 100"));
91     }
92
93     private void sendHttpValue(String value) {
94     }
95
96     private void updateState(State state) {
97     }
98
99     public void postCommand(Command command) {
100     }
101
102     public GenericItemConverter createConverter(Function<String, State> fcn) {
103         return new GenericItemConverter(fcn, this::updateState, this::postCommand, this::sendHttpValue,
104                 NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), new HttpChannelConfig());
105     }
106 }