]> git.basschouten.com Git - openhab-addons.git/blob
5a1d8ed8ed5e6c1a0800f31a04c553bab76ba1ae
[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.ihc.internal.converters;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
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;
23
24 /**
25  * Test for IHC / ELKO binding
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 public class UpDownTypeWSIntegerValueConverterTest {
30
31     @Test
32     public void testOpen() throws ConversionException {
33         final boolean inverted = false;
34         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
35
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);
41
42         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
43         assertEquals(UpDownType.UP, type);
44     }
45
46     @Test
47     public void testClosed() throws ConversionException {
48         final boolean inverted = false;
49
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);
56
57         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
58         assertEquals(UpDownType.DOWN, type);
59     }
60
61     @Test
62     public void testOpenInverted() throws ConversionException {
63         final boolean inverted = true;
64
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);
71
72         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
73         assertEquals(UpDownType.UP, type);
74     }
75
76     @Test
77     public void testClosedInverted() throws ConversionException {
78         final boolean inverted = true;
79
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);
86
87         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
88         assertEquals(UpDownType.DOWN, type);
89     }
90
91     private WSIntegerValue convertFromOHType(WSIntegerValue IHCvalue, Type OHval,
92             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
93         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
94                 UpDownType.class);
95         return (WSIntegerValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
96     }
97
98     private UpDownType convertFromResourceValue(WSIntegerValue IHCvalue,
99             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
100         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
101                 UpDownType.class);
102         return (UpDownType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
103     }
104 }