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 java.util.Collections;
18 import java.util.HashMap;
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;
30 * Test for IHC / ELKO binding
32 * @author Pauli Anttila - Initial contribution
34 public class OnOffTypeWSIntegerValueConverterTest {
37 public void testOn() throws ConversionException {
38 final boolean inverted = false;
39 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
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);
47 OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
48 assertEquals(OnOffType.ON, type);
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);
57 Map<Command, Object> commandLevels = new HashMap<>();
58 commandLevels.put(OnOffType.ON, onLevel);
60 val = convertFromOHType(val, OnOffType.ON,
61 new ConverterAdditionalInfo(null, inverted, Collections.unmodifiableMap(commandLevels)));
63 assertEquals(12345, val.resourceID);
64 assertEquals(onLevel, val.value);
65 assertEquals(-100, val.minimumValue);
66 assertEquals(100, val.maximumValue);
68 OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
69 assertEquals(OnOffType.ON, type);
73 public void testOnLevelledError() {
74 WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
76 Map<Command, Object> commandLevels = new HashMap<>();
77 commandLevels.put(OnOffType.ON, "70");
79 assertThrows(ConversionException.class, () -> convertFromOHType(val, OnOffType.ON,
80 new ConverterAdditionalInfo(null, false, Collections.unmodifiableMap(commandLevels))));
84 public void testOff() throws ConversionException {
85 final boolean inverted = false;
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);
94 OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
95 assertEquals(OnOffType.OFF, type);
99 public void testOnInverted() throws ConversionException {
100 final boolean inverted = true;
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);
109 OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
110 assertEquals(OnOffType.ON, type);
114 public void testOffInverted() throws ConversionException {
115 final boolean inverted = true;
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);
124 OnOffType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, inverted, null));
125 assertEquals(OnOffType.OFF, type);
128 private WSIntegerValue convertFromOHType(WSIntegerValue IHCvalue, Type OHval,
129 ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
130 Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
132 return (WSIntegerValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
135 private OnOffType convertFromResourceValue(WSIntegerValue IHCvalue, ConverterAdditionalInfo converterAdditionalInfo)
136 throws ConversionException {
137 Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
139 return (OnOffType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);