]> git.basschouten.com Git - openhab-addons.git/blob
2b9ad2512fb86c594a5ca496f34c3fc3533138e6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Assert.assertEquals;
16
17 import org.junit.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.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 DecimalTypeWSIntegerValueConverterTest {
30
31     @Test
32     public void testConversion() throws ConversionException {
33         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
34
35         val = convertFromOHType(val, new DecimalType(2), new ConverterAdditionalInfo(null, false, null));
36         assertEquals(12345, val.resourceID);
37         assertEquals(2, val.value);
38         assertEquals(-100, val.minimumValue);
39         assertEquals(100, val.maximumValue);
40
41         DecimalType type = convertFromResourceValue(val, new ConverterAdditionalInfo(null, false, null));
42         assertEquals(new DecimalType(2), type);
43     }
44
45     @Test(expected = ConversionException.class)
46     public void testMinExceed() throws ConversionException {
47         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
48         val = convertFromOHType(val, new DecimalType(-101.5), new ConverterAdditionalInfo(null, false, null));
49     }
50
51     @Test(expected = ConversionException.class)
52     public void testMaxExceed() throws ConversionException {
53         WSIntegerValue val = new WSIntegerValue(12345, 0, -100, 100);
54         val = convertFromOHType(val, new DecimalType(101.5), new ConverterAdditionalInfo(null, false, null));
55     }
56
57     private WSIntegerValue convertFromOHType(WSIntegerValue IHCvalue, Type OHval,
58             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
59         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
60                 DecimalType.class);
61         return (WSIntegerValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
62     }
63
64     private DecimalType convertFromResourceValue(WSIntegerValue IHCvalue,
65             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
66         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
67                 DecimalType.class);
68         return (DecimalType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
69     }
70 }