]> git.basschouten.com Git - openhab-addons.git/blob
4cf3f5525ef5b69004de21e93dfe9541a72c969b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.enigma2.internal;
14
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.*;
20
21 import java.io.IOException;
22 import java.time.LocalDateTime;
23
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;
28
29 /**
30  * The {@link Enigma2ClientTest} class is responsible for testing {@link Enigma2Client}.
31  *
32  * @author Guido Dolfen - Initial contribution
33  */
34 @SuppressWarnings({ "null" })
35 @NonNullByDefault
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";
40     @Nullable
41     private Enigma2Client enigma2Client;
42     @Nullable
43     private Enigma2HttpClient enigma2HttpClient;
44
45     @BeforeEach
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/>");
51     }
52
53     @Test
54     public void testSetPowerFalse() throws IOException {
55         whenStandby("true");
56         enigma2Client.setPower(false);
57         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
58         verifyNoMoreInteractions(enigma2HttpClient);
59     }
60
61     @Test
62     public void testSetPower() throws IOException {
63         whenStandby("true");
64         enigma2Client.setPower(true);
65         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_POWER);
66     }
67
68     @Test
69     public void testSetVolume() throws IOException {
70         enigma2Client.setVolume(20);
71         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_SET_VOLUME + 20);
72     }
73
74     @Test
75     public void testSetChannel() throws IOException {
76         whenStandby("false");
77         whenAllServices();
78         enigma2Client.refreshPower();
79         enigma2Client.refreshAllServices();
80         enigma2Client.setChannel("Channel 3");
81         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_ZAP + 3);
82     }
83
84     @Test
85     public void testSetChannelUnknown() throws IOException {
86         enigma2Client.setChannel("Channel 3");
87         verifyNoInteractions(enigma2HttpClient);
88     }
89
90     @Test
91     public void testSetMuteFalse() throws IOException {
92         whenStandby("false");
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);
99     }
100
101     @Test
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);
108     }
109
110     @Test
111     public void testSendRcCommand() throws IOException {
112         enigma2Client.sendRcCommand(2);
113         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_REMOTE_CONTROL + 2);
114     }
115
116     @Test
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);
120     }
121
122     @Test
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);
126     }
127
128     @Test
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);
132     }
133
134     @Test
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);
138     }
139
140     @Test
141     public void testRefreshPowerTrue() throws IOException {
142         whenStandby(" FALSE ");
143         enigma2Client.refreshPower();
144         assertThat(enigma2Client.isPower(), is(true));
145     }
146
147     @Test
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));
155     }
156
157     @Test
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));
165     }
166
167     @Test
168     public void testRefreshVolumePowerOff() throws IOException {
169         enigma2Client.refreshVolume();
170         assertThat(enigma2Client.isMute(), is(false));
171         assertThat(enigma2Client.getVolume(), is(0));
172     }
173
174     @Test
175     public void testRefreshPowerFalse() throws IOException {
176         whenStandby(" TRUE ");
177         enigma2Client.refreshPower();
178         assertThat(enigma2Client.isPower(), is(false));
179     }
180
181     @Test
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));
187     }
188
189     @Test
190     public void testRefreshAllServices() throws IOException {
191         whenStandby("false");
192         whenAllServices();
193         enigma2Client.refreshAllServices();
194         assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
195     }
196
197     @Test
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"));
204     }
205
206     @Test
207     public void testRefreshEpg() throws IOException {
208         whenStandby("false");
209         whenAllServices();
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"));
218     }
219
220     @Test
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(""));
228     }
229
230     @Test
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(""));
238     }
239
240     @Test
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"));
249     }
250
251     @Test
252     public void testRefresh() throws IOException {
253         whenStandby("false");
254         whenAllServices();
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"));
266     }
267
268     @Test
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));
280     }
281
282     @Test
283     public void testGetEnigma2HttpClient() {
284         enigma2Client = new Enigma2Client("http://localhost:8080", null, null, 5);
285         assertThat(enigma2Client.getEnigma2HttpClient(), is(notNullValue()));
286     }
287
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>");
291     }
292
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>");
296     }
297
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>");
301     }
302
303     private void whenStandby(String standby) throws IOException {
304         when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER))
305                 .thenReturn("<e2powerstate><e2instandby>" + standby + "</e2instandby></e2powerstate>");
306     }
307
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>");
312     }
313
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>");
323     }
324 }