2 * Copyright (c) 2010-2023 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.digitalstrom.internal.lib.structure.devices.impl;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.*;
17 import static org.junit.jupiter.api.Assertions.assertNull;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
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;
34 import com.google.gson.Gson;
35 import com.google.gson.JsonObject;
36 import com.google.gson.JsonParser;
39 * Test class for certain code in {@link DeviceImpl}
41 * @author Rouven Schürch - Initial contribution
44 @ExtendWith(MockitoExtension.class)
46 class DeviceImplTest {
48 private static final List<OutputChannel> EMPTY_CHANNEL = new ArrayList<>();
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));
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));
58 private static final List<OutputChannel> NON_SHADE_CHANNEL = Arrays
59 .asList(new OutputChannel(OutputChannelEnum.BRIGHTNESS));
61 private static final List<OutputChannel> MIXED_SHADE_CHANNEL = Arrays.asList(
62 new OutputChannel(OutputChannelEnum.BRIGHTNESS),
63 new OutputChannel(OutputChannelEnum.SHADE_OPENING_ANGLE_OUTSIDE));
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));
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));
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));
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));
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));
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));
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));
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));
122 void isBlindPositionConNoChannel() {
123 JsonObject jsonObject = createJsonObject(OutputModeEnum.POSITION_CON, EMPTY_CHANNEL);
124 DeviceImpl deviceImpl = new DeviceImpl(jsonObject);
125 assertThat(deviceImpl.isBlind(), is(false));
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));
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));
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));
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));
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);
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());
171 assertThat(deviceImpl.getFunctionalColorGroup(), is(ApplicationGroup.JOKER));
174 private static JsonObject createJsonObject(OutputModeEnum outputMode, List<OutputChannel> channels) {
175 JsonModel model = new JsonModel(outputMode.getMode(), channels);
177 Gson gson = new Gson();
178 String json = gson.toJson(model);
180 return JsonParser.parseString(json).getAsJsonObject();