2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.loxone.internal.controls;
15 import static org.hamcrest.Matchers.*;
16 import static org.junit.Assert.*;
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;
25 import java.util.stream.Collectors;
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;
39 * Common test framework class for all (@link LxControl} objects
41 * @author Pawel Pieczul - initial contribution
45 LxServerHandlerDummy handler;
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();
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);
65 Map<LxUuid, LxControl> subC = c.getSubControls();
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();
73 assertEquals(roomUuid, room.getUuid());
74 LxCategory cat = c.getCategory();
76 assertEquals(categoryUuid, cat.getUuid());
77 assertEquals(numberOfChannels, c.getChannels().size());
78 assertEquals(numberOfChannelsWithSubs, c.getChannelsWithSubcontrols().size());
79 assertEquals(numberOfStates, c.getStates().size());
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) {
85 Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
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) {
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());
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);
117 assertThat(c.getDefaultTags(), hasItems(tags.toArray(new String[0])));
119 assertThat(c.getDefaultTags(), empty());
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);
129 void testChannel(String itemType) {
130 testChannel(itemType, null, null, null, null, null, null, null, null);
133 void testChannel(String itemType, Set<String> tags) {
134 testChannel(itemType, null, null, null, null, null, null, null, tags);
137 void testChannel(LxControl ctrl, String itemType) {
138 testChannel(ctrl, itemType, null, null, null, null, null, null, null, null);
141 void testChannel(LxControl ctrl, String itemType, Set<String> tags) {
142 testChannel(ctrl, itemType, null, null, null, null, null, null, null, tags);
145 void testChannel(String itemType, String namePostFix) {
146 testChannel(itemType, namePostFix, null, null, null, null, null, null, null);
149 void testChannel(String itemType, String namePostFix, Set<String> tags) {
150 testChannel(itemType, namePostFix, null, null, null, null, null, null, tags);
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);
158 State getChannelState(LxControl ctrl, String namePostFix) {
160 Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
162 return ctrl.getChannelState(c.getUID());
165 State getChannelState(String namePostFix) {
166 LxControl ctrl = getControl(controlUuid);
167 return getChannelState(ctrl, namePostFix);
170 void testChannelState(LxControl ctrl, String namePostFix, State expectedValue) {
171 State current = getChannelState(ctrl, namePostFix);
172 if (expectedValue != null) {
173 assertNotNull(current);
175 assertEquals(expectedValue, current);
178 void testChannelState(String namePostFix, State expectedValue) {
179 LxControl ctrl = getControl(controlUuid);
180 testChannelState(ctrl, namePostFix, expectedValue);
183 void testChannelState(State expectedValue) {
184 testChannelState((String) null, expectedValue);
187 void testChannelState(LxControl ctrl, State expectedValue) {
188 testChannelState(ctrl, null, expectedValue);
191 void changeLoxoneState(String stateName, Object value) {
192 LxControl ctrl = getControl(controlUuid);
194 LxState state = ctrl.getStates().get(stateName);
195 assertNotNull(state);
196 state.setStateValue(value);
199 void executeCommand(LxControl ctrl, String namePostFix, Command command) {
201 Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
204 ctrl.handleCommand(c.getUID(), command);
205 } catch (IOException e) {
206 fail("This exception should never happen in test environment.");
210 void executeCommand(String namePostFix, Command command) {
211 LxControl ctrl = getControl(controlUuid);
212 executeCommand(ctrl, namePostFix, command);
215 void executeCommand(LxControl ctrl, Command command) {
216 executeCommand(ctrl, null, command);
219 void executeCommand(Command command) {
220 executeCommand((String) null, command);
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);
232 void testAction(String expectedAction) {
233 if (expectedAction == null) {
236 testAction(expectedAction, 1);
240 void testSubControl(Type type, String name) {
241 LxControl ctrl = getControl(controlUuid);
243 long n = ctrl.getSubControls().values().stream().filter(c -> name.equals(c.getName()))
244 .collect(Collectors.counting());
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);
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());
260 private LxControl getControl(LxUuid uuid) {
261 return handler.controls.get(uuid);
264 private String getExpectedName(String controlName, String roomName, String postFix) {
265 return roomName + " / " + controlName + (postFix != null ? postFix : "");