]> git.basschouten.com Git - openhab-addons.git/blob
5f1ade9d73bd37df688d02b24c19de5271557241
[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.ArrayList;
18
19 import org.junit.jupiter.api.Test;
20 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
21 import org.openhab.binding.ihc.internal.ws.projectfile.IhcEnumValue;
22 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSEnumValue;
23 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSResourceValue;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.Type;
26
27 /**
28  * Test for IHC / ELKO binding
29  *
30  * @author Pauli Anttila - Initial contribution
31  */
32 public class StringTypeWSEnumValueConverterTest {
33
34     @Test
35     public void test() throws ConversionException {
36         ArrayList<IhcEnumValue> enumValues = new ArrayList<>();
37         enumValues.add(new IhcEnumValue(101, "testA"));
38         enumValues.add(new IhcEnumValue(102, "testB"));
39         enumValues.add(new IhcEnumValue(103, "testC"));
40         enumValues.add(new IhcEnumValue(104, "testD"));
41
42         WSEnumValue val = new WSEnumValue(12345, 5555, 0, "");
43
44         val = convertFromOHType(val, new StringType("testC"), new ConverterAdditionalInfo(enumValues, false, null));
45         assertEquals(12345, val.resourceID);
46         assertEquals(5555, val.definitionTypeID);
47         assertEquals(103, val.enumValueID);
48         assertEquals("testC", val.enumName);
49
50         StringType type = convertFromResourceValue(val, new ConverterAdditionalInfo(enumValues, false, null));
51         assertEquals(new StringType("testC"), type);
52     }
53
54     private WSEnumValue convertFromOHType(WSEnumValue IHCvalue, Type OHval,
55             ConverterAdditionalInfo converterAdditionalInfo) throws ConversionException {
56         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
57                 StringType.class);
58         return (WSEnumValue) converter.convertFromOHType(OHval, IHCvalue, converterAdditionalInfo);
59     }
60
61     private StringType convertFromResourceValue(WSEnumValue IHCvalue, ConverterAdditionalInfo converterAdditionalInfo)
62             throws ConversionException {
63         Converter<WSResourceValue, Type> converter = ConverterFactory.getInstance().getConverter(IHCvalue.getClass(),
64                 StringType.class);
65         return (StringType) converter.convertFromResourceValue(IHCvalue, converterAdditionalInfo);
66     }
67 }