]> git.basschouten.com Git - openhab-addons.git/blob
443f9165e020a53c3ef3156fe4dcf123ca5b5bcd
[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 java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
23 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSIntegerValue;
24 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.Type;
28
29 /**
30  * Test for IHC / ELKO binding
31  *
32  * @author Pauli Anttila - Initial contribution
33  */
34 public class OnOffTypeWSIntegerValueConverterTest {
35
36     @Test
37     public void testOn() throws ConversionException {
38         final boolean inverted = false;
39         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
40
41         val = convertFromOHType(val, OnOffType.ON, new ConverterAdditionalInfo(null, inverted, null));
42         assertEquals(12345, val.resourceID);
43         assertEquals(100, val.value);
44         assertEquals(-100, val.minimumValue);
45         assertEquals(100, val.maximumValue);
46
47         OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
48         assertEquals(OnOffType.ON, type);
49     }
50
51     @Test
52     public void testOnLevelled() throws ConversionException {
53         final boolean inverted = false;
54         final int onLevel = 70;
55         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
56
57         Map<Command, Object> commandLevels = new HashMap<>();
58         commandLevels.put(OnOffType.ON, onLevel);
59
60         val = convertFromOHType(val, OnOffType.ON,
61                 new ConverterAdditionalInfo(null, inverted, Collections.unmodifiableMap(commandLevels)));
62
63         assertEquals(12345, val.resourceID);
64         assertEquals(onLevel, val.value);
65         assertEquals(-100, val.minimumValue);
66         assertEquals(100, val.maximumValue);
67
68         OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
69         assertEquals(OnOffType.ON, type);
70     }
71
72     @Test
73     public void testOnLevelledError() {
74         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
75
76         Map<Command, Object> commandLevels = new HashMap<>();
77         commandLevels.put(OnOffType.ON, "70");
78
79         assertThrows(ConversionException.class, () -> convertFromOHType(val, OnOffType.ON,
80                 new ConverterAdditionalInfo(null, false, Collections.unmodifiableMap(commandLevels))));
81     }
82
83     @Test
84     public void testOff() throws ConversionException {
85         final boolean inverted = false;
86
87         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
88         val = convertFromOHType(val, OnOffType.OFF, new ConverterAdditionalInfo(null, inverted, null));
89         assertEquals(12345, val.resourceID);
90         assertEquals(-100, val.value);
91         assertEquals(-100, val.minimumValue);
92         assertEquals(100, val.maximumValue);
93
94         OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
95         assertEquals(OnOffType.OFF, type);
96     }
97
98     @Test
99     public void testOnInverted() throws ConversionException {
100         final boolean inverted = true;
101
102         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
103         val = convertFromOHType(val, OnOffType.ON, new ConverterAdditionalInfo(null, inverted, null));
104         assertEquals(12345, val.resourceID);
105         assertEquals(-100, val.value);
106         assertEquals(-100, val.minimumValue);
107         assertEquals(100, val.maximumValue);
108
109         OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
110         assertEquals(OnOffType.ON, type);
111     }
112
113     @Test
114     public void testOffInverted() throws ConversionException {
115         final boolean inverted = true;
116
117         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
118         val = convertFromOHType(val, OnOffType.OFF, new ConverterAdditionalInfo(null, inverted, null));
119         assertEquals(12345, val.resourceID);
120         assertEquals(100, val.value);
121         assertEquals(-100, val.minimumValue);
122         assertEquals(100, val.maximumValue);
123
124         OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
125         assertEquals(OnOffType.OFF, type);
126     }
127
128     private WSIntegerValue convertFromOHType(WSIntegerValue IHCvalue, Type OHval,
129             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
130         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
131                 OnOffType.class);
132         return (WSIntegerValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
133     }
134
135     private OnOffType convertFromResourceValue(WSIntegerValue IHCvalue, ConverterAdditionalInfo converterAdditionalInfo)
136             throws ConversionException {
137         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
138                 OnOffType.class);
139         return (OnOffType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
140     }
141 }