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.enigma2.internal;
15 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
16 import static org.hamcrest.Matchers.*;
17 import static org.junit.Assert.*;
18 import static org.mockito.Mockito.*;
20 import java.io.IOException;
21 import java.time.LocalDateTime;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
29 * The {@link Enigma2ClientTest} class is responsible for testing {@link Enigma2Client}.
31 * @author Guido Dolfen - Initial contribution
33 @SuppressWarnings({ "null" })
35 public class Enigma2ClientTest {
36 public static final String HOST = "http://user:password@localhost:8080";
37 public static final String SOME_TEXT = "some Text";
38 public static final String SOME_TEXT_ENCODED = "some+Text";
40 private Enigma2Client enigma2Client;
42 private Enigma2HttpClient enigma2HttpClient;
45 public void setUp() throws IOException {
46 enigma2HttpClient = mock(Enigma2HttpClient.class);
47 enigma2Client = spy(new Enigma2Client("localhost:8080", "user", "password", 5));
48 when(enigma2Client.getEnigma2HttpClient()).thenReturn(requireNonNull(enigma2HttpClient));
49 when(enigma2HttpClient.get(anyString())).thenReturn("<emptyResult/>");
53 public void testSetPowerFalse() throws IOException {
55 enigma2Client.setPower(false);
56 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
57 verifyNoMoreInteractions(enigma2HttpClient);
61 public void testSetPower() throws IOException {
63 enigma2Client.setPower(true);
64 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_POWER);
68 public void testSetVolume() throws IOException {
69 enigma2Client.setVolume(20);
70 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_SET_VOLUME + 20);
74 public void testSetChannel() throws IOException {
77 enigma2Client.refreshPower();
78 enigma2Client.refreshAllServices();
79 enigma2Client.setChannel("Channel 3");
80 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_ZAP + 3);
84 public void testSetChannelUnknown() throws IOException {
85 enigma2Client.setChannel("Channel 3");
86 verifyNoInteractions(enigma2HttpClient);
90 public void testSetMuteFalse() throws IOException {
92 whenVolume("10", false);
93 enigma2Client.refreshPower();
94 enigma2Client.setMute(false);
95 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
96 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_VOLUME);
97 verifyNoMoreInteractions(enigma2HttpClient);
101 public void testSetMute() throws IOException {
102 whenStandby("false");
103 whenVolume("10", false);
104 enigma2Client.refreshPower();
105 enigma2Client.setMute(true);
106 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_MUTE);
110 public void testSendRcCommand() throws IOException {
111 enigma2Client.sendRcCommand(2);
112 verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_REMOTE_CONTROL + 2);
116 public void testSendError() throws IOException {
117 enigma2Client.sendError(20, SOME_TEXT);
118 verify(enigma2HttpClient).get(HOST + "/web/message?type=3&timeout=20&text=" + SOME_TEXT_ENCODED);
122 public void testSendWarning() throws IOException {
123 enigma2Client.sendWarning(35, SOME_TEXT);
124 verify(enigma2HttpClient).get(HOST + "/web/message?type=2&timeout=35&text=" + SOME_TEXT_ENCODED);
128 public void testSendInfo() throws IOException {
129 enigma2Client.sendInfo(40, SOME_TEXT);
130 verify(enigma2HttpClient).get(HOST + "/web/message?type=1&timeout=40&text=" + SOME_TEXT_ENCODED);
134 public void testSendQuestion() throws IOException {
135 enigma2Client.sendQuestion(50, SOME_TEXT);
136 verify(enigma2HttpClient).get(HOST + "/web/message?type=0&timeout=50&text=" + SOME_TEXT_ENCODED);
140 public void testRefreshPowerTrue() throws IOException {
141 whenStandby(" FALSE ");
142 enigma2Client.refreshPower();
143 assertThat(enigma2Client.isPower(), is(true));
147 public void testRefreshVolumeMuteTrue() throws IOException {
148 whenStandby("false");
149 whenVolume("30", true);
150 enigma2Client.refreshPower();
151 enigma2Client.refreshVolume();
152 assertThat(enigma2Client.isMute(), is(true));
153 assertThat(enigma2Client.getVolume(), is(30));
157 public void testRefreshVolumeMuteFalse() throws IOException {
158 whenStandby("false");
159 whenVolume("30", false);
160 enigma2Client.refreshPower();
161 enigma2Client.refreshVolume();
162 assertThat(enigma2Client.isMute(), is(false));
163 assertThat(enigma2Client.getVolume(), is(30));
167 public void testRefreshVolumePowerOff() throws IOException {
168 enigma2Client.refreshVolume();
169 assertThat(enigma2Client.isMute(), is(false));
170 assertThat(enigma2Client.getVolume(), is(0));
174 public void testRefreshPowerFalse() throws IOException {
175 whenStandby(" TRUE ");
176 enigma2Client.refreshPower();
177 assertThat(enigma2Client.isPower(), is(false));
181 public void testRefreshPowerOffline() throws IOException {
182 IOException ioException = new IOException();
183 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER)).thenThrow(ioException);
184 enigma2Client.refreshPower();
185 assertThat(enigma2Client.isPower(), is(false));
189 public void testRefreshAllServices() throws IOException {
190 whenStandby("false");
192 enigma2Client.refreshAllServices();
193 assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
197 public void testRefreshChannel() throws IOException {
198 whenStandby("false");
199 whenChannel("2", "Channel 2");
200 enigma2Client.refreshPower();
201 enigma2Client.refreshChannel();
202 assertThat(enigma2Client.getChannel(), is("Channel 2"));
206 public void testRefreshEpg() throws IOException {
207 whenStandby("false");
209 whenChannel("2", "Channel 2");
210 whenEpg("2", "Title", "Description");
211 enigma2Client.refreshPower();
212 enigma2Client.refreshAllServices();
213 enigma2Client.refreshChannel();
214 enigma2Client.refreshEpg();
215 assertThat(enigma2Client.getTitle(), is("Title"));
216 assertThat(enigma2Client.getDescription(), is("Description"));
220 public void testRefreshAnswerTimeout() throws IOException {
221 whenStandby("false");
222 whenAnswer("False", "Timeout");
223 enigma2Client.refreshPower();
224 enigma2Client.refreshAnswer();
225 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(false));
226 assertThat(enigma2Client.getAnswer(), is(""));
230 public void testRefreshAnswerNoQuestion() throws IOException {
231 whenStandby("false");
232 whenAnswer("True", "Antwort lautet NEIN!");
233 enigma2Client.refreshPower();
234 enigma2Client.refreshAnswer();
235 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(false));
236 assertThat(enigma2Client.getAnswer(), is(""));
240 public void testRefreshAnswer() throws IOException {
241 whenStandby("false");
242 whenAnswer("True", "Antwort lautet NEIN!");
243 enigma2Client.refreshPower();
244 enigma2Client.sendQuestion(50, SOME_TEXT);
245 enigma2Client.refreshAnswer();
246 assertThat(enigma2Client.getLastAnswerTime().isAfter(LocalDateTime.of(2020, 1, 1, 0, 0)), is(true));
247 assertThat(enigma2Client.getAnswer(), is("NEIN"));
251 public void testRefresh() throws IOException {
252 whenStandby("false");
254 whenVolume("A", false);
255 whenChannel("1", "Channel 1");
256 whenEpg("1", "Title", "Description");
257 assertThat(enigma2Client.refresh(), is(true));
258 assertThat(enigma2Client.isPower(), is(true));
259 assertThat(enigma2Client.isMute(), is(false));
260 assertThat(enigma2Client.getVolume(), is(0));
261 assertThat(enigma2Client.getChannel(), is("Channel 1"));
262 assertThat(enigma2Client.getTitle(), is("Title"));
263 assertThat(enigma2Client.getDescription(), is("Description"));
264 assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
268 public void testRefreshOffline() throws IOException {
269 IOException ioException = new IOException();
270 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER)).thenThrow(ioException);
271 assertThat(enigma2Client.refresh(), is(false));
272 assertThat(enigma2Client.isPower(), is(false));
273 assertThat(enigma2Client.isMute(), is(false));
274 assertThat(enigma2Client.getVolume(), is(0));
275 assertThat(enigma2Client.getChannel(), is(""));
276 assertThat(enigma2Client.getTitle(), is(""));
277 assertThat(enigma2Client.getDescription(), is(""));
278 assertThat(enigma2Client.getChannels().isEmpty(), is(true));
282 public void testGetEnigma2HttpClient() {
283 enigma2Client = new Enigma2Client("http://localhost:8080", null, null, 5);
284 assertThat(enigma2Client.getEnigma2HttpClient(), is(notNullValue()));
287 private void whenVolume(String volume, boolean mute) throws IOException {
288 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_VOLUME)).thenReturn(
289 "<e2volume><e2current>" + volume + "</e2current><e2ismuted>" + mute + "</e2ismuted></e2volume>");
292 private void whenEpg(String id, String title, String description) throws IOException {
293 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_EPG + id)).thenReturn("<e2event><e2eventtitle>" + title
294 + "</e2eventtitle><e2eventdescription>" + description + "</e2eventdescription></e2event>");
297 private void whenAnswer(String state, String answer) throws IOException {
298 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_ANSWER)).thenReturn("<e2simplexmlresult><e2state>" + state
299 + "</e2state><e2statetext>" + answer + "</e2statetext></e2simplexmlresult>");
302 private void whenStandby(String standby) throws IOException {
303 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER))
304 .thenReturn("<e2powerstate><e2instandby>" + standby + "</e2instandby></e2powerstate>");
307 private void whenChannel(String id, String name) throws IOException {
308 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_CHANNEL)).thenReturn(
309 "<e2servicelist><e2service><e2servicereference>" + id + "</e2servicereference><e2servicename>" + name
310 + "</e2servicename></e2service></e2servicelist>");
313 private void whenAllServices() throws IOException {
314 when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_ALL_SERVICES))
315 .thenReturn("<e2servicelistrecursive>" + "<e2bouquet>" + "<e2servicelist>" + "<e2service>"
316 + "<e2servicereference>1</e2servicereference>" + "<e2servicename>Channel 1</e2servicename>"
317 + "</e2service>" + "<e2service>" + "<e2servicereference>2</e2servicereference>"
318 + "<e2servicename>Channel 2</e2servicename>" + "</e2service>" + "</e2servicelist>"
319 + "</e2bouquet>" + "<e2bouquet>" + "<e2servicelist>" + "<e2service>"
320 + "<e2servicereference>3</e2servicereference>" + "<e2servicename>Channel 3</e2servicename>"
321 + "</e2service>" + "</e2servicelist>" + "</e2bouquet>" + "</e2servicelistrecursive>");