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.ihc.internal.converters;
15 import static org.junit.jupiter.api.Assertions.*;
17 import org.junit.jupiter.api.Test;
18 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
19 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSIntegerValue;
20 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
21 import org.openhab.core.library.types.UpDownType;
22 import org.openhab.core.types.Type;
25 * Test for IHC / ELKO binding
27 * @author Pauli Anttila - Initial contribution
29 public class UpDownTypeWSIntegerValueConverterTest {
32 public void testOpen() throws ConversionException {
33 final boolean inverted = false;
34 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
36 val = convertFromOHType(val, UpDownType.UP, new ConverterAdditionalInfo(null, inverted, null));
37 assertEquals(12345, val.resourceID);
38 assertEquals(-100, val.minimumValue);
39 assertEquals(100, val.maximumValue);
40 assertEquals(100, val.value);
42 UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
43 assertEquals(UpDownType.UP, type);
47 public void testClosed() throws ConversionException {
48 final boolean inverted = false;
50 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
51 val = convertFromOHType(val, UpDownType.DOWN, new ConverterAdditionalInfo(null, inverted, null));
52 assertEquals(12345, val.resourceID);
53 assertEquals(-100, val.minimumValue);
54 assertEquals(100, val.maximumValue);
55 assertEquals(-100, val.value);
57 UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
58 assertEquals(UpDownType.DOWN, type);
62 public void testOpenInverted() throws ConversionException {
63 final boolean inverted = true;
65 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
66 val = convertFromOHType(val, UpDownType.UP, new ConverterAdditionalInfo(null, inverted, null));
67 assertEquals(12345, val.resourceID);
68 assertEquals(-100, val.minimumValue);
69 assertEquals(100, val.maximumValue);
70 assertEquals(-100, val.value);
72 UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
73 assertEquals(UpDownType.UP, type);
77 public void testClosedInverted() throws ConversionException {
78 final boolean inverted = true;
80 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
81 val = convertFromOHType(val, UpDownType.DOWN, new ConverterAdditionalInfo(null, inverted, null));
82 assertEquals(12345, val.resourceID);
83 assertEquals(-100, val.minimumValue);
84 assertEquals(100, val.maximumValue);
85 assertEquals(100, val.value);
87 UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
88 assertEquals(UpDownType.DOWN, type);
91 private WSIntegerValue convertFromOHType(WSIntegerValue IHCvalue, Type OHval,
92 ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
93 Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
95 return (WSIntegerValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
98 private UpDownType convertFromResourceValue(WSIntegerValue IHCvalue,
99 ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
100 Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
102 return (UpDownType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);