]> git.basschouten.com Git - openhab-addons.git/blob
c115bd2f6a927318226411c5669285b2901f78e4
[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.max.internal.message;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.max.internal.device.Device;
27 import org.openhab.binding.max.internal.device.DeviceConfiguration;
28 import org.openhab.binding.max.internal.device.DeviceInformation;
29 import org.openhab.binding.max.internal.device.DeviceType;
30 import org.openhab.binding.max.internal.device.HeatingThermostat;
31 import org.openhab.binding.max.internal.device.ShutterContact;
32
33 /**
34  * Tests cases for {@link LMessage}.
35  *
36  * @author Dominic Lerbs - Initial contribution
37  * @author Christoph Weitkamp - OH2 Version and updates
38  */
39 @NonNullByDefault
40 public class LMessageTest {
41
42     private static final String RAWDATA = "L:BgVPngkSEAsLhBkJEhkLJQDAAAsLhwwJEhkRJwDKAAYO8ZIJEhAGBU+kCRIQCwxuRPEaGQMmAMcACwxuQwkSGQgnAM8ACwQd5t0SGQ0oAMsA";
43
44     private final Map<String, Device> testDevices = new HashMap<>();
45
46     private final LMessage message = new LMessage(RAWDATA);
47     private final List<DeviceConfiguration> configurations = new ArrayList<>();
48
49     @BeforeEach
50     public void setUp() {
51         createTestDevices();
52     }
53
54     private void createTestDevices() {
55         addShutterContact("054f9e");
56         addShutterContact("0ef192");
57         addShutterContact("054fa4");
58         addHeatingThermostat("0b8419");
59         addHeatingThermostat("0b870c");
60         addHeatingThermostat("0c6e43");
61         addHeatingThermostat("041de6");
62         addHeatingThermostat("0c6e44").setError(true);
63     }
64
65     private ShutterContact addShutterContact(String rfAddress) {
66         ShutterContact device = new ShutterContact(createConfiguration(DeviceType.ShutterContact, rfAddress));
67         testDevices.put(rfAddress, device);
68         return device;
69     }
70
71     private HeatingThermostat addHeatingThermostat(String rfAddress) {
72         HeatingThermostat device = new HeatingThermostat(createConfiguration(DeviceType.HeatingThermostat, rfAddress));
73         testDevices.put(rfAddress, device);
74         return device;
75     }
76
77     private DeviceConfiguration createConfiguration(DeviceType type, String rfAddress) {
78         DeviceConfiguration configuration = DeviceConfiguration
79                 .create(new DeviceInformation(type, "", rfAddress, "", 1));
80         configurations.add(configuration);
81         return configuration;
82     }
83
84     @Test
85     public void isCorrectMessageType() {
86         MessageType messageType = ((Message) message).getType();
87         assertEquals(MessageType.L, messageType);
88     }
89
90     @Test
91     public void allDevicesCreatedFromMessage() {
92         Collection<? extends Device> devices = message.getDevices(configurations);
93         assertEquals(testDevices.size(), devices.size(), "Incorrect number of devices created");
94         for (Device device : devices) {
95             assertTrue(testDevices.containsKey(device.getRFAddress()),
96                     "Unexpected device created: " + device.getRFAddress());
97         }
98     }
99
100     @Test
101     public void isCorrectErrorState() {
102         for (Device device : message.getDevices(configurations)) {
103             Device testDevice = testDevices.get(device.getRFAddress());
104             assertEquals(testDevice.isError(), device.isError(), "Error set incorrectly in Device");
105         }
106     }
107 }