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.enigma2.internal;
15 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.Matchers.*;
18 import static org.mockito.ArgumentMatchers.anyString;
19 import static org.mockito.Mockito.*;
21 import java.io.IOException;
22 import java.time.LocalDateTime;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
30 * The {@link Enigma2ClientTest} class is responsible for testing {@link Enigma2Client}.
32 * @author Guido Dolfen - Initial contribution
34 @SuppressWarnings({ "null" })
36 public class Enigma2ClientTest {
37 public static final String HOST = "http://user:password@localhost:8080";
38 public static final String SOME_TEXT = "some Text";
39 public static final String SOME_TEXT_ENCODED = "some+Text";
41 private Enigma2Client enigma2Client;
43 private Enigma2HttpClient enigma2HttpClient;
46 public void setUp() throws IOException {
47 enigma2HttpClient = mock(Enigma2HttpClient.class);
48 enigma2Client = spy(new Enigma2Client("localhost:8080", "user", "password", 5));
49 when(enigma2Client.getEnigma2HttpClient()).thenReturn(requireNonNull(enigma2HttpClient));
50 when(enigma2HttpClient.get(anyString())).thenReturn("<emptyResult/>");
54 public void testSetPowerFalse() throws IOException {
56 enigma2Client.setPower(false);
57 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
58 verifyNoMoreInteractions(enigma2HttpClient);
62 public void testSetPower() throws IOException {
64 enigma2Client.setPower(true);
65 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_POWER);
69 public void testSetVolume() throws IOException {
70 enigma2Client.setVolume(20);
71 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_SET_VOLUME + 20);
75 public void testSetChannel() throws IOException {
78 enigma2Client.refreshPower();
79 enigma2Client.refreshAllServices();
80 enigma2Client.setChannel("Channel 3");
81 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_ZAP + 3);
85 public void testSetChannelUnknown() throws IOException {
86 enigma2Client.setChannel("Channel 3");
87 verifyNoInteractions(enigma2HttpClient);
91 public void testSetMuteFalse() throws IOException {
93 whenVolume("10", false);
94 enigma2Client.refreshPower();
95 enigma2Client.setMute(false);
96 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
97 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_VOLUME);
98 verifyNoMoreInteractions(enigma2HttpClient);
102 public void testSetMute() throws IOException {
103 whenStandby("false");
104 whenVolume("10", false);
105 enigma2Client.refreshPower();
106 enigma2Client.setMute(true);
107 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_MUTE);
111 public void testSendRcCommand() throws IOException {
112 enigma2Client.sendRcCommand(2);
113 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_REMOTE_CONTROL + 2);
117 public void testSendError() throws IOException {
118 enigma2Client.sendError(20, SOME_TEXT);
119 verify(enigma2HttpClient).get(HOST + "/web/message?type=3&timeout=20&text=" + SOME_TEXT_ENCODED);
123 public void testSendWarning() throws IOException {
124 enigma2Client.sendWarning(35, SOME_TEXT);
125 verify(enigma2HttpClient).get(HOST + "/web/message?type=2&timeout=35&text=" + SOME_TEXT_ENCODED);
129 public void testSendInfo() throws IOException {
130 enigma2Client.sendInfo(40, SOME_TEXT);
131 verify(enigma2HttpClient).get(HOST + "/web/message?type=1&timeout=40&text=" + SOME_TEXT_ENCODED);
135 public void testSendQuestion() throws IOException {
136 enigma2Client.sendQuestion(50, SOME_TEXT);
137 verify(enigma2HttpClient).get(HOST + "/web/message?type=0&timeout=50&text=" + SOME_TEXT_ENCODED);
141 public void testRefreshPowerTrue() throws IOException {
142 whenStandby(" FALSE ");
143 enigma2Client.refreshPower();
144 assertThat(enigma2Client.isPower(), is(true));
148 public void testRefreshVolumeMuteTrue() throws IOException {
149 whenStandby("false");
150 whenVolume("30", true);
151 enigma2Client.refreshPower();
152 enigma2Client.refreshVolume();
153 assertThat(enigma2Client.isMute(), is(true));
154 assertThat(enigma2Client.getVolume(), is(30));
158 public void testRefreshVolumeMuteFalse() throws IOException {
159 whenStandby("false");
160 whenVolume("30", false);
161 enigma2Client.refreshPower();
162 enigma2Client.refreshVolume();
163 assertThat(enigma2Client.isMute(), is(false));
164 assertThat(enigma2Client.getVolume(), is(30));
168 public void testRefreshVolumePowerOff() throws IOException {
169 enigma2Client.refreshVolume();
170 assertThat(enigma2Client.isMute(), is(false));
171 assertThat(enigma2Client.getVolume(), is(0));
175 public void testRefreshPowerFalse() throws IOException {
176 whenStandby(" TRUE ");
177 enigma2Client.refreshPower();
178 assertThat(enigma2Client.isPower(), is(false));
182 public void testRefreshPowerOffline() throws IOException {
183 IOException ioException = new IOException();
184 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER)).thenThrow(ioException);
185 enigma2Client.refreshPower();
186 assertThat(enigma2Client.isPower(), is(false));
190 public void testRefreshAllServices() throws IOException {
191 whenStandby("false");
193 enigma2Client.refreshAllServices();
194 assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
198 public void testRefreshChannel() throws IOException {
199 whenStandby("false");
200 whenChannel("2", "Channel 2");
201 enigma2Client.refreshPower();
202 enigma2Client.refreshChannel();
203 assertThat(enigma2Client.getChannel(), is("Channel 2"));
207 public void testRefreshEpg() throws IOException {
208 whenStandby("false");
210 whenChannel("2", "Channel 2");
211 whenEpg("2", "Title", "Description");
212 enigma2Client.refreshPower();
213 enigma2Client.refreshAllServices();
214 enigma2Client.refreshChannel();
215 enigma2Client.refreshEpg();
216 assertThat(enigma2Client.getTitle(), is("Title"));
217 assertThat(enigma2Client.getDescription(), is("Description"));
221 public void testRefreshAnswerTimeout() throws IOException {
222 whenStandby("false");
223 whenAnswer("False", "Timeout");
224 enigma2Client.refreshPower();
225 enigma2Client.refreshAnswer();
226 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(false));
227 assertThat(enigma2Client.getAnswer(), is(""));
231 public void testRefreshAnswerNoQuestion() throws IOException {
232 whenStandby("false");
233 whenAnswer("True", "Antwort lautet NEIN!");
234 enigma2Client.refreshPower();
235 enigma2Client.refreshAnswer();
236 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(false));
237 assertThat(enigma2Client.getAnswer(), is(""));
241 public void testRefreshAnswer() throws IOException {
242 whenStandby("false");
243 whenAnswer("True", "Antwort lautet NEIN!");
244 enigma2Client.refreshPower();
245 enigma2Client.sendQuestion(50, SOME_TEXT);
246 enigma2Client.refreshAnswer();
247 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(true));
248 assertThat(enigma2Client.getAnswer(), is("NEIN"));
252 public void testRefresh() throws IOException {
253 whenStandby("false");
255 whenVolume("A", false);
256 whenChannel("1", "Channel 1");
257 whenEpg("1", "Title", "Description");
258 assertThat(enigma2Client.refresh(), is(true));
259 assertThat(enigma2Client.isPower(), is(true));
260 assertThat(enigma2Client.isMute(), is(false));
261 assertThat(enigma2Client.getVolume(), is(0));
262 assertThat(enigma2Client.getChannel(), is("Channel 1"));
263 assertThat(enigma2Client.getTitle(), is("Title"));
264 assertThat(enigma2Client.getDescription(), is("Description"));
265 assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
269 public void testRefreshOffline() throws IOException {
270 IOException ioException = new IOException();
271 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER)).thenThrow(ioException);
272 assertThat(enigma2Client.refresh(), is(false));
273 assertThat(enigma2Client.isPower(), is(false));
274 assertThat(enigma2Client.isMute(), is(false));
275 assertThat(enigma2Client.getVolume(), is(0));
276 assertThat(enigma2Client.getChannel(), is(""));
277 assertThat(enigma2Client.getTitle(), is(""));
278 assertThat(enigma2Client.getDescription(), is(""));
279 assertThat(enigma2Client.getChannels().isEmpty(), is(true));
283 public void testGetEnigma2HttpClient() {
284 enigma2Client = new Enigma2Client("http://localhost:8080", null, null, 5);
285 assertThat(enigma2Client.getEnigma2HttpClient(), is(notNullValue()));
288 private void whenVolume(String volume, boolean mute) throws IOException {
289 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_VOLUME)).thenReturn(
290 "<e2volume><e2current>" + volume + "</e2current><e2ismuted>" + mute + "</e2ismuted></e2volume>");
293 private void whenEpg(String id, String title, String description) throws IOException {
294 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_EPG + id)).thenReturn("<e2event><e2eventtitle>" + title
295 + "</e2eventtitle><e2eventdescription>" + description + "</e2eventdescription></e2event>");
298 private void whenAnswer(String state, String answer) throws IOException {
299 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_ANSWER)).thenReturn("<e2simplexmlresult><e2state>" + state
300 + "</e2state><e2statetext>" + answer + "</e2statetext></e2simplexmlresult>");
303 private void whenStandby(String standby) throws IOException {
304 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER))
305 .thenReturn("<e2powerstate><e2instandby>" + standby + "</e2instandby></e2powerstate>");
308 private void whenChannel(String id, String name) throws IOException {
309 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_CHANNEL)).thenReturn(
310 "<e2servicelist><e2service><e2servicereference>" + id + "</e2servicereference><e2servicename>" + name
311 + "</e2servicename></e2service></e2servicelist>");
314 private void whenAllServices() throws IOException {
315 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_ALL_SERVICES))
316 .thenReturn("<e2servicelistrecursive>" + "<e2bouquet>" + "<e2servicelist>" + "<e2service>"
317 + "<e2servicereference>1</e2servicereference>" + "<e2servicename>Channel 1</e2servicename>"
318 + "</e2service>" + "<e2service>" + "<e2servicereference>2</e2servicereference>"
319 + "<e2servicename>Channel 2</e2servicename>" + "</e2service>" + "</e2servicelist>"
320 + "</e2bouquet>" + "<e2bouquet>" + "<e2servicelist>" + "<e2service>"
321 + "<e2servicereference>3</e2servicereference>" + "<e2servicename>Channel 3</e2servicename>"
322 + "</e2service>" + "</e2servicelist>" + "</e2bouquet>" + "</e2servicelistrecursive>");