]> git.basschouten.com Git - openhab-addons.git/blob
702b5e9563bbcf6ea57e6b25790252e8fed8150b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Matchers.*;
17 import static org.junit.Assert.*;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.time.LocalDateTime;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27
28 /**
29  * The {@link Enigma2ClientTest} class is responsible for testing {@link Enigma2Client}.
30  *
31  * @author Guido Dolfen - Initial contribution
32  */
33 @SuppressWarnings({ "null" })
34 @NonNullByDefault
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";
39     @Nullable
40     private Enigma2Client enigma2Client;
41     @Nullable
42     private Enigma2HttpClient enigma2HttpClient;
43
44     @Before
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/>");
50     }
51
52     @Test
53     public void testSetPowerFalse() throws IOException {
54         whenStandby("true");
55         enigma2Client.setPower(false);
56         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_POWER);
57         verifyNoMoreInteractions(enigma2HttpClient);
58     }
59
60     @Test
61     public void testSetPower() throws IOException {
62         whenStandby("true");
63         enigma2Client.setPower(true);
64         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_TOGGLE_POWER);
65     }
66
67     @Test
68     public void testSetVolume() throws IOException {
69         enigma2Client.setVolume(20);
70         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_SET_VOLUME + 20);
71     }
72
73     @Test
74     public void testSetChannel() throws IOException {
75         whenStandby("false");
76         whenAllServices();
77         enigma2Client.refreshPower();
78         enigma2Client.refreshAllServices();
79         enigma2Client.setChannel("Channel 3");
80         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_ZAP + 3);
81     }
82
83     @Test
84     public void testSetChannelUnknown() throws IOException {
85         enigma2Client.setChannel("Channel 3");
86         verifyNoInteractions(enigma2HttpClient);
87     }
88
89     @Test
90     public void testSetMuteFalse() throws IOException {
91         whenStandby("false");
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);
98     }
99
100     @Test
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);
107     }
108
109     @Test
110     public void testSendRcCommand() throws IOException {
111         enigma2Client.sendRcCommand(2);
112         verify(enigma2HttpClient).get(HOST + Enigma2Client.PATH_REMOTE_CONTROL + 2);
113     }
114
115     @Test
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);
119     }
120
121     @Test
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);
125     }
126
127     @Test
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);
131     }
132
133     @Test
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);
137     }
138
139     @Test
140     public void testRefreshPowerTrue() throws IOException {
141         whenStandby(" FALSE ");
142         enigma2Client.refreshPower();
143         assertThat(enigma2Client.isPower(), is(true));
144     }
145
146     @Test
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));
154     }
155
156     @Test
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));
164     }
165
166     @Test
167     public void testRefreshVolumePowerOff() throws IOException {
168         enigma2Client.refreshVolume();
169         assertThat(enigma2Client.isMute(), is(false));
170         assertThat(enigma2Client.getVolume(), is(0));
171     }
172
173     @Test
174     public void testRefreshPowerFalse() throws IOException {
175         whenStandby(" TRUE ");
176         enigma2Client.refreshPower();
177         assertThat(enigma2Client.isPower(), is(false));
178     }
179
180     @Test
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));
186     }
187
188     @Test
189     public void testRefreshAllServices() throws IOException {
190         whenStandby("false");
191         whenAllServices();
192         enigma2Client.refreshAllServices();
193         assertThat(enigma2Client.getChannels(), containsInAnyOrder("Channel 1", "Channel 2", "Channel 3"));
194     }
195
196     @Test
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"));
203     }
204
205     @Test
206     public void testRefreshEpg() throws IOException {
207         whenStandby("false");
208         whenAllServices();
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"));
217     }
218
219     @Test
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(""));
227     }
228
229     @Test
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(""));
237     }
238
239     @Test
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"));
248     }
249
250     @Test
251     public void testRefresh() throws IOException {
252         whenStandby("false");
253         whenAllServices();
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"));
265     }
266
267     @Test
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));
279     }
280
281     @Test
282     public void testGetEnigma2HttpClient() {
283         enigma2Client = new Enigma2Client("http://localhost:8080", null, null, 5);
284         assertThat(enigma2Client.getEnigma2HttpClient(), is(notNullValue()));
285     }
286
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>");
290     }
291
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>");
295     }
296
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>");
300     }
301
302     private void whenStandby(String standby) throws IOException {
303         when(enigma2HttpClient.get(HOST + Enigma2Client.PATH_POWER))
304                 .thenReturn("<e2powerstate><e2instandby>" + standby + "</e2instandby></e2powerstate>");
305     }
306
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>");
311     }
312
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>");
322     }
323 }