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