2 * Copyright (c) 2010-2023 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.binding.http.internal.converter;
15 import java.util.function.Function;
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;
33 * The {@link ConverterTest} is a test class for state converters
35 * @author Jan N. Klug - Initial contribution
38 public class ConverterTest {
41 public void numberItemConverter() {
42 NumberItemConverter converter = new NumberItemConverter(this::updateState, this::postCommand,
43 this::sendHttpValue, NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(),
44 new HttpChannelConfig());
47 Assertions.assertEquals(new DecimalType(1234), converter.toState("1234"));
49 // unit in transformation result
50 Assertions.assertEquals(new QuantityType<>(100, SIUnits.CELSIUS), converter.toState("100°C"));
53 Assertions.assertEquals(UnDefType.UNDEF, converter.toState("W"));
54 Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
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(),
66 Assertions.assertEquals(new QuantityType<>(500, Units.WATT), converter.toState("500"));
69 Assertions.assertEquals(UnDefType.UNDEF, converter.toState("100foo"));
70 Assertions.assertEquals(UnDefType.UNDEF, converter.toState("foo"));
71 Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
75 public void stringTypeConverter() {
76 GenericItemConverter converter = createConverter(StringType::new);
77 Assertions.assertEquals(new StringType("Test"), converter.toState("Test"));
81 public void decimalTypeConverter() {
82 GenericItemConverter converter = createConverter(DecimalType::new);
83 Assertions.assertEquals(new DecimalType(15.6), converter.toState("15.6"));
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"));
93 private void sendHttpValue(String value) {
96 private void updateState(State state) {
99 public void postCommand(Command command) {
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());