]> git.basschouten.com Git - openhab-addons.git/blob
38bde289a46e6bca3806ff54f9702d89c58155ec
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.loxone.internal.controls;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.junit.jupiter.api.Assertions.*;
18
19 import java.io.IOException;
20 import java.lang.reflect.Type;
21 import java.math.BigDecimal;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27
28 import org.openhab.binding.loxone.internal.types.LxCategory;
29 import org.openhab.binding.loxone.internal.types.LxContainer;
30 import org.openhab.binding.loxone.internal.types.LxState;
31 import org.openhab.binding.loxone.internal.types.LxUuid;
32 import org.openhab.core.thing.Channel;
33 import org.openhab.core.thing.type.ChannelKind;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36 import org.openhab.core.types.StateDescription;
37 import org.openhab.core.types.StateOption;
38
39 /**
40  * Common test framework class for all (@link LxControl} objects
41  *
42  * @author Pawel Pieczul - initial contribution
43  *
44  */
45 class LxControlTest {
46     LxServerHandlerDummy handler;
47     LxUuid controlUuid;
48     LxUuid roomUuid;
49     LxUuid categoryUuid;
50     String controlName;
51
52     void setupControl(String controlUuid, String roomUuid, String categoryUuid, String controlName) {
53         this.controlUuid = new LxUuid(controlUuid);
54         this.roomUuid = new LxUuid(roomUuid);
55         this.categoryUuid = new LxUuid(categoryUuid);
56         this.controlName = controlName;
57         handler = new LxServerHandlerDummy();
58         handler.loadConfiguration();
59     }
60
61     <T> void testControlCreation(Class<T> testClass, int numberOfControls, int numberOfSubcontrols,
62             int numberOfChannels, int numberOfChannelsWithSubs, int numberOfStates) {
63         assertEquals(numberOfControls, numberOfControls(testClass));
64         LxControl c = getControl(controlUuid);
65         assertNotNull(c);
66         Map<LxUuid, LxControl> subC = c.getSubControls();
67         assertNotNull(subC);
68         assertEquals(numberOfSubcontrols, subC.size());
69         assertEquals(controlUuid, c.getUuid());
70         assertEquals(controlName, c.getName());
71         assertEquals(controlName, c.getLabel());
72         LxContainer room = c.getRoom();
73         assertNotNull(room);
74         assertEquals(roomUuid, room.getUuid());
75         LxCategory cat = c.getCategory();
76         assertNotNull(cat);
77         assertEquals(categoryUuid, cat.getUuid());
78         assertEquals(numberOfChannels, c.getChannels().size());
79         assertEquals(numberOfChannelsWithSubs, c.getChannelsWithSubcontrols().size());
80         assertEquals(numberOfStates, c.getStates().size());
81     }
82
83     void testChannel(LxControl ctrl, String itemType, String namePostFix, BigDecimal min, BigDecimal max,
84             BigDecimal step, String format, Boolean readOnly, List<StateOption> options, Set<String> tags) {
85         assertNotNull(ctrl);
86         Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
87         assertNotNull(c);
88         assertNotNull(c.getUID());
89         assertNotNull(c.getDescription());
90         assertEquals(itemType, c.getAcceptedItemType());
91         assertEquals(ChannelKind.STATE, c.getKind());
92         StateDescription d = handler.stateDescriptions.get(c.getUID());
93         if (readOnly != null || min != null || max != null || step != null || format != null || options != null) {
94             assertNotNull(d);
95             assertEquals(min, d.getMinimum());
96             assertEquals(max, d.getMaximum());
97             assertEquals(step, d.getStep());
98             assertEquals(format, d.getPattern());
99             assertEquals(readOnly, d.isReadOnly());
100             List<StateOption> opts = d.getOptions();
101             if (options == null) {
102                 assertTrue(opts.isEmpty());
103             } else {
104                 assertNotNull(opts);
105                 assertEquals(options.size(), opts.size());
106                 options.forEach(o -> {
107                     String label = o.getLabel();
108                     long num = opts.stream().filter(
109                             f -> label != null && label.equals(f.getLabel()) && o.getValue().equals(f.getValue()))
110                             .collect(Collectors.counting());
111                     assertEquals(1, num);
112                 });
113             }
114         } else {
115             assertNull(d);
116         }
117         if (tags != null) {
118             assertThat(c.getDefaultTags(), hasItems(tags.toArray(new String[0])));
119         } else {
120             assertThat(c.getDefaultTags(), empty());
121         }
122     }
123
124     void testChannel(String itemType, String namePostFix, BigDecimal min, BigDecimal max, BigDecimal step,
125             String format, Boolean readOnly, List<StateOption> options, Set<String> tags) {
126         LxControl ctrl = getControl(controlUuid);
127         testChannel(ctrl, itemType, namePostFix, min, max, step, format, readOnly, options, tags);
128     }
129
130     void testChannel(String itemType) {
131         testChannel(itemType, null, null, null, null, null, null, null, null);
132     }
133
134     void testChannel(String itemType, Set<String> tags) {
135         testChannel(itemType, null, null, null, null, null, null, null, tags);
136     }
137
138     void testChannel(LxControl ctrl, String itemType) {
139         testChannel(ctrl, itemType, null, null, null, null, null, null, null, null);
140     }
141
142     void testChannel(LxControl ctrl, String itemType, Set<String> tags) {
143         testChannel(ctrl, itemType, null, null, null, null, null, null, null, tags);
144     }
145
146     void testChannel(String itemType, String namePostFix) {
147         testChannel(itemType, namePostFix, null, null, null, null, null, null, null);
148     }
149
150     void testChannel(String itemType, String namePostFix, Set<String> tags) {
151         testChannel(itemType, namePostFix, null, null, null, null, null, null, tags);
152     }
153
154     void testChannel(String itemType, String namePostFix, BigDecimal min, BigDecimal max, BigDecimal step,
155             String format, Boolean readOnly, List<StateOption> options) {
156         testChannel(itemType, namePostFix, min, max, step, format, readOnly, options, null);
157     }
158
159     State getChannelState(LxControl ctrl, String namePostFix) {
160         assertNotNull(ctrl);
161         Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
162         assertNotNull(c);
163         return ctrl.getChannelState(c.getUID());
164     }
165
166     State getChannelState(String namePostFix) {
167         LxControl ctrl = getControl(controlUuid);
168         return getChannelState(ctrl, namePostFix);
169     }
170
171     void testChannelState(LxControl ctrl, String namePostFix, State expectedValue) {
172         State current = getChannelState(ctrl, namePostFix);
173         if (expectedValue != null) {
174             assertNotNull(current);
175         }
176         assertEquals(expectedValue, current);
177     }
178
179     void testChannelState(String namePostFix, State expectedValue) {
180         LxControl ctrl = getControl(controlUuid);
181         testChannelState(ctrl, namePostFix, expectedValue);
182     }
183
184     void testChannelState(State expectedValue) {
185         testChannelState((String) null, expectedValue);
186     }
187
188     void testChannelState(LxControl ctrl, State expectedValue) {
189         testChannelState(ctrl, null, expectedValue);
190     }
191
192     void changeLoxoneState(String stateName, Object value) {
193         LxControl ctrl = getControl(controlUuid);
194         assertNotNull(ctrl);
195         LxState state = ctrl.getStates().get(stateName);
196         assertNotNull(state);
197         state.setStateValue(value);
198     }
199
200     void executeCommand(LxControl ctrl, String namePostFix, Command command) {
201         assertNotNull(ctrl);
202         Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
203         assertNotNull(c);
204         try {
205             ctrl.handleCommand(c.getUID(), command);
206         } catch (IOException e) {
207             fail("This exception should never happen in test environment.");
208         }
209     }
210
211     void executeCommand(String namePostFix, Command command) {
212         LxControl ctrl = getControl(controlUuid);
213         executeCommand(ctrl, namePostFix, command);
214     }
215
216     void executeCommand(LxControl ctrl, Command command) {
217         executeCommand(ctrl, null, command);
218     }
219
220     void executeCommand(Command command) {
221         executeCommand((String) null, command);
222     }
223
224     void testAction(String expectedAction, int numberOfActions) {
225         assertEquals(numberOfActions, handler.actionQueue.size());
226         if (numberOfActions > 0) {
227             String action = handler.actionQueue.poll();
228             assertNotNull(action);
229             assertEquals(controlUuid + "/" + expectedAction, action);
230         }
231     }
232
233     void testAction(String expectedAction) {
234         if (expectedAction == null) {
235             testAction(null, 0);
236         } else {
237             testAction(expectedAction, 1);
238         }
239     }
240
241     void testSubControl(Type type, String name) {
242         LxControl ctrl = getControl(controlUuid);
243         assertNotNull(ctrl);
244         long n = ctrl.getSubControls().values().stream().filter(c -> name.equals(c.getName()))
245                 .collect(Collectors.counting());
246         assertEquals(1L, n);
247     }
248
249     private Channel getChannel(String name, LxControl c) {
250         List<Channel> channels = c.getChannels();
251         List<Channel> filtered = channels.stream().filter(a -> name.equals(a.getLabel())).collect(Collectors.toList());
252         assertEquals(1, filtered.size());
253         return filtered.get(0);
254     }
255
256     private <T> long numberOfControls(Class<T> c) {
257         Collection<LxControl> v = handler.controls.values();
258         return v.stream().filter(o -> c.equals(o.getClass())).collect(Collectors.counting());
259     }
260
261     private LxControl getControl(LxUuid uuid) {
262         return handler.controls.get(uuid);
263     }
264
265     private String getExpectedName(String controlName, String roomName, String postFix) {
266         return roomName + " / " + controlName + (postFix != null ? postFix : "");
267     }
268 }