]> git.basschouten.com Git - openhab-addons.git/blob
3ee43d4aa3495f70120cccba8d9d9e4f9a85c4b2
[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.http.internal.converter;
14
15 import java.util.function.Function;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.Test;
20 import org.openhab.binding.http.internal.config.HttpChannelConfig;
21 import org.openhab.binding.http.internal.transform.NoOpValueTransformation;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.PointType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27
28 /**
29  * The {@link ConverterTest} is a test class for state converters
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @NonNullByDefault
34 public class ConverterTest {
35
36     @Test
37     public void stringTypeConverter() {
38         GenericItemConverter converter = createConverter(StringType::new);
39         Assertions.assertEquals(new StringType("Test"), converter.toState("Test"));
40     }
41
42     @Test
43     public void decimalTypeConverter() {
44         GenericItemConverter converter = createConverter(DecimalType::new);
45         Assertions.assertEquals(new DecimalType(15.6), converter.toState("15.6"));
46     }
47
48     @Test
49     public void pointTypeConverter() {
50         GenericItemConverter converter = createConverter(PointType::new);
51         Assertions.assertEquals(new PointType(new DecimalType(51.1), new DecimalType(7.2), new DecimalType(100)),
52                 converter.toState("51.1, 7.2, 100"));
53     }
54
55     private void sendHttpValue(String value) {
56     }
57
58     private void updateState(State state) {
59     }
60
61     public void postCommand(Command command) {
62     }
63
64     public GenericItemConverter createConverter(Function<String, State> fcn) {
65         return new GenericItemConverter(fcn, this::updateState, this::postCommand, this::sendHttpValue,
66                 NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), new HttpChannelConfig());
67     }
68 }