]> git.basschouten.com Git - openhab-addons.git/blob
931ee5d5d95a5d25a51667531e22e2036e2de7bd
[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.WSBooleanValue;
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 UpDownTypeWSBooleanValueConverterTest {
30
31     @Test
32     public void testOpen() throws ConversionException {
33         final boolean inverted = false;
34         WSBooleanValue val = new WSBooleanValue(12345, false);
35
36         val = convertFromOHType(val, UpDownType.UP, new ConverterAdditionalInfo(null, inverted, null));
37         assertEquals(12345, val.resourceID);
38         assertEquals(true, val.value);
39
40         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
41         assertEquals(UpDownType.UP, type);
42     }
43
44     @Test
45     public void testClosed() throws ConversionException {
46         final boolean inverted = false;
47
48         WSBooleanValue val = new WSBooleanValue(12345, true);
49         val = convertFromOHType(val, UpDownType.DOWN, new ConverterAdditionalInfo(null, inverted, null));
50         assertEquals(12345, val.resourceID);
51         assertEquals(false, val.value);
52
53         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
54         assertEquals(UpDownType.DOWN, type);
55     }
56
57     @Test
58     public void testOpenInverted() throws ConversionException {
59         final boolean inverted = true;
60
61         WSBooleanValue val = new WSBooleanValue(12345, true);
62         val = convertFromOHType(val, UpDownType.UP, new ConverterAdditionalInfo(null, inverted, null));
63         assertEquals(12345, val.resourceID);
64         assertEquals(false, val.value);
65
66         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
67         assertEquals(UpDownType.UP, type);
68     }
69
70     @Test
71     public void testClosedInverted() throws ConversionException {
72         final boolean inverted = true;
73
74         WSBooleanValue val = new WSBooleanValue(12345, false);
75         val = convertFromOHType(val, UpDownType.DOWN, new ConverterAdditionalInfo(null, inverted, null));
76         assertEquals(12345, val.resourceID);
77         assertEquals(true, val.value);
78
79         UpDownType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
80         assertEquals(UpDownType.DOWN, type);
81     }
82
83     private WSBooleanValue convertFromOHType(WSBooleanValue IHCvalue, Type OHval,
84             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
85         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
86                 UpDownType.class);
87         return (WSBooleanValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
88     }
89
90     private UpDownType convertFromResourceValue(WSBooleanValue IHCvalue,
91             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
92         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
93                 UpDownType.class);
94         return (UpDownType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
95     }
96 }