]> git.basschouten.com Git - openhab-addons.git/blob
5046b72a09b9a822d17a7b4a530dc01f76f1110e
[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.WSFloatingPointValue;
20 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
21 import org.openhab.core.library.types.DecimalType;
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 DecimalTypeWSFloatingPointValueConverterTest {
30
31     @Test
32     public void testConversion() throws ConversionException {
33         WSFloatingPointValue val = new WSFloatingPointValue(12345, 0, -100, 100);
34
35         val = convertFromOHType(val, new DecimalType(2.54), new ConverterAdditionalInfo(null, false, null));
36         assertEquals(12345, val.resourceID);
37         assertEquals(-100, val.minimumValue, 0.001);
38         assertEquals(100, val.maximumValue, 0.001);
39         assertEquals(2.54, val.value, 0.001);
40
41         DecimalType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, false, null));
42         assertEquals(new DecimalType(2.54), type);
43     }
44
45     @Test
46     public void testMinExceed() {
47         WSFloatingPointValue val = new WSFloatingPointValue(12345, 0, -100, 100);
48         assertThrows(ConversionException.class,
49                 () -> convertFromOHType(val, new DecimalType(-101.5), new ConverterAdditionalInfo(null, false, null)));
50     }
51
52     @Test
53     public void testMaxExceed() {
54         WSFloatingPointValue val = new WSFloatingPointValue(12345, 0, -100, 100);
55         assertThrows(ConversionException.class,
56                 () -> convertFromOHType(val, new DecimalType(101.5), new ConverterAdditionalInfo(null, false, null)));
57     }
58
59     private WSFloatingPointValue convertFromOHType(WSFloatingPointValue IHCvalue, Type OHval,
60             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
61         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
62                 DecimalType.class);
63         return (WSFloatingPointValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
64     }
65
66     private DecimalType convertFromResourceValue(WSFloatingPointValue IHCvalue,
67             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
68         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
69                 DecimalType.class);
70         return (DecimalType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
71     }
72 }