]> git.basschouten.com Git - openhab-addons.git/blob
405256a32345cf5586724427c8188a1cd8db72ed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.digitalstrom.internal.lib.structure.devices.impl;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.*;
17 import static org.junit.jupiter.api.Assertions.assertNull;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.DisplayName;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.ApplicationGroup;
29 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.OutputChannelEnum;
30 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.OutputModeEnum;
31 import org.openhab.binding.digitalstrom.internal.lib.util.JsonModel;
32 import org.openhab.binding.digitalstrom.internal.lib.util.OutputChannel;
33
34 import com.google.gson.Gson;
35 import com.google.gson.JsonObject;
36 import com.google.gson.JsonParser;
37
38 /**
39  * Test class for certain code in {@link DeviceImpl}
40  *
41  * @author Rouven Schürch - Initial contribution
42  *
43  */
44 @ExtendWith(MockitoExtension.class)
45 @NonNullByDefault
46 class DeviceImplTest {
47
48     private static final List<OutputChannel> EMPTY_CHANNEL = new ArrayList<>();
49
50     private static final List<OutputChannel> SHADE_ANGLE_CHANNELS = Arrays.asList(
51             new OutputChannel(OutputChannelEnum.SHADE_OPENING_ANGLE_OUTSIDE),
52             new OutputChannel(OutputChannelEnum.SHADE_OPENING_ANGLE_INDOOR));
53
54     private static final List<OutputChannel> SHADE_POSITION_CHANNELS = Arrays.asList(
55             new OutputChannel(OutputChannelEnum.SHADE_POSITION_INDOOR),
56             new OutputChannel(OutputChannelEnum.SHADE_POSITION_OUTSIDE));
57
58     private static final List<OutputChannel> NON_SHADE_CHANNEL = Arrays
59             .asList(new OutputChannel(OutputChannelEnum.BRIGHTNESS));
60
61     private static final List<OutputChannel> MIXED_SHADE_CHANNEL = Arrays.asList(
62             new OutputChannel(OutputChannelEnum.BRIGHTNESS),
63             new OutputChannel(OutputChannelEnum.SHADE_OPENING_ANGLE_OUTSIDE));
64
65     @Test
66     void isBlindSwitchShadeChannel() {
67         JsonObject jsonObject = createJsonObject(OutputModeEnum.SINGLE_SWITCH, SHADE_ANGLE_CHANNELS);
68         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
69         assertThat(deviceImpl.isBlind(), is(false));
70     }
71
72     @Test
73     void isBlindSwitchNoShadeChannel() {
74         JsonObject jsonObject = createJsonObject(OutputModeEnum.SINGLE_SWITCH, NON_SHADE_CHANNEL);
75         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
76         assertThat(deviceImpl.isBlind(), is(false));
77     }
78
79     @Test
80     void isBlindSwitchMixedShadeChannel() {
81         JsonObject jsonObject = createJsonObject(OutputModeEnum.SINGLE_SWITCH, MIXED_SHADE_CHANNEL);
82         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
83         assertThat(deviceImpl.isBlind(), is(false));
84     }
85
86     @Test
87     void isBlindPositionConUsNoChannel() {
88         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON_US, EMPTY_CHANNEL);
89         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
90         assertThat(deviceImpl.isBlind(), is(false));
91     }
92
93     @Test
94     void isBlindPositionConUsNonShadeChannel() {
95         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON_US, NON_SHADE_CHANNEL);
96         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
97         assertThat(deviceImpl.isBlind(), is(false));
98     }
99
100     @Test
101     void isBlindPositionConUsShadePositionChannels() {
102         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON_US, SHADE_POSITION_CHANNELS);
103         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
104         assertThat(deviceImpl.isBlind(), is(false));
105     }
106
107     @Test
108     void isBlindPositionConUsShadeAngleChannels() {
109         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON_US, SHADE_ANGLE_CHANNELS);
110         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
111         assertThat(deviceImpl.isBlind(), is(true));
112     }
113
114     @Test
115     void isBlindPositionConUsMixedChannels() {
116         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON_US, MIXED_SHADE_CHANNEL);
117         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
118         assertThat(deviceImpl.isBlind(), is(true));
119     }
120
121     @Test
122     void isBlindPositionConNoChannel() {
123         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, EMPTY_CHANNEL);
124         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
125         assertThat(deviceImpl.isBlind(), is(false));
126     }
127
128     @Test
129     void isBlindPositionConNonShadeChannel() {
130         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, NON_SHADE_CHANNEL);
131         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
132         assertThat(deviceImpl.isBlind(), is(false));
133     }
134
135     @Test
136     void isBlindPositionConShadePositionChannels() {
137         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, SHADE_POSITION_CHANNELS);
138         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
139         assertThat(deviceImpl.isBlind(), is(false));
140     }
141
142     @Test
143     void isBlindPositionConShadeAngleChannels() {
144         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, SHADE_ANGLE_CHANNELS);
145         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
146         assertThat(deviceImpl.isBlind(), is(true));
147     }
148
149     @Test
150     void isBlindPositionConMixedChannels() {
151         JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, MIXED_SHADE_CHANNEL);
152         DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
153         assertThat(deviceImpl.isBlind(), is(true));
154     }
155
156     @Test
157     @DisplayName("No functional color group set, expect a null value returned")
158     void noFunctionalColorGroupSet() {
159         DeviceImpl deviceImpl = new DeviceImpl(new JsonObject());
160         ApplicationGroup functionalColorGroup = deviceImpl.getFunctionalColorGroup();
161         assertNull(functionalColorGroup);
162     }
163
164     @Test
165     @DisplayName("Multiple functional color groups set, expect the first group returned which had previously been added")
166     void multipleFunctionalColorGroupSet() {
167         DeviceImpl deviceImpl = new DeviceImpl(new JsonObject());
168         deviceImpl.addGroup(ApplicationGroup.JOKER.getId());
169         deviceImpl.addGroup(ApplicationGroup.ACCESS.getId());
170
171         assertThat(deviceImpl.getFunctionalColorGroup(), is(ApplicationGroup.JOKER));
172     }
173
174     private static JsonObject createJsonObject(OutputModeEnum outputMode, List<OutputChannel> channels) {
175         JsonModel model = new JsonModel(outputMode.getMode(), channels);
176
177         Gson gson = new Gson();
178         String json = gson.toJson(model);
179
180         return JsonParser.parseString(json).getAsJsonObject();
181     }
182 }