]> git.basschouten.com Git - openhab-addons.git/blob
b821a8e2bc28e769f977cac0b2ee5a1e238310d2
[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.yamahareceiver.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20 import static org.mockito.MockitoAnnotations.initMocks;
21
22 import java.util.List;
23 import java.util.Optional;
24
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
29 import org.openhab.binding.yamahareceiver.internal.discovery.ZoneDiscoveryService;
30 import org.openhab.binding.yamahareceiver.internal.handler.YamahaBridgeHandler;
31 import org.openhab.binding.yamahareceiver.internal.protocol.ConnectionStateListener;
32 import org.openhab.binding.yamahareceiver.internal.protocol.DeviceInformation;
33 import org.openhab.binding.yamahareceiver.internal.protocol.ProtocolFactory;
34 import org.openhab.binding.yamahareceiver.internal.protocol.SystemControl;
35 import org.openhab.binding.yamahareceiver.internal.protocol.xml.AbstractXMLProtocolTest;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusInfo;
40 import org.openhab.core.thing.binding.ThingHandlerCallback;
41
42 /**
43  * Test cases for {@link YamahaBridgeHandler}. The tests provide mocks for supporting entities using Mockito.
44  *
45  * @author Tomasz Maruszak - Initial contribution
46  */
47 public class YamahaReceiverHandlerTest extends AbstractXMLProtocolTest {
48
49     private YamahaBridgeHandler subject;
50
51     @Mock
52     private YamahaBridgeConfig bridgeConfig;
53
54     @Mock
55     private Configuration configuration;
56
57     @Mock
58     private ProtocolFactory protocolFactory;
59
60     @Mock
61     private DeviceInformation deviceInformation;
62
63     @Mock
64     private SystemControl systemControl;
65
66     @Mock
67     private ThingHandlerCallback callback;
68
69     @Mock
70     private Bridge bridge;
71
72     @Override
73     protected void onSetUp() throws Exception {
74         super.onSetUp();
75
76         initMocks(this);
77
78         ctx.prepareForModel(TestModels.RX_S601D);
79         ctx.respondWith("<Main_Zone><Input><Input_Sel_Item>GetParam</Input_Sel_Item></Input></Main_Zone>",
80                 "Main_Zone_Input_Input_Sel.xml");
81
82         when(bridgeConfig.getHostWithPort()).thenReturn(Optional.of("localhost:80"));
83         when(bridgeConfig.getInputMapping()).thenReturn("");
84         when(bridgeConfig.getRefreshInterval()).thenReturn(10);
85
86         when(configuration.as(YamahaBridgeConfig.class)).thenReturn(bridgeConfig);
87         when(bridge.getConfiguration()).thenReturn(configuration);
88
89         when(protocolFactory.DeviceInformation(any(), any())).thenReturn(deviceInformation);
90         when(protocolFactory.SystemControl(any(), any(), any())).thenReturn(systemControl);
91
92         subject = new YamahaBridgeHandler(bridge);
93         subject.setZoneDiscoveryService(mock(ZoneDiscoveryService.class));
94         subject.setProtocolFactory(protocolFactory);
95         subject.setCallback(callback);
96
97         doAnswer(a -> {
98             ((ConnectionStateListener) a.getArgument(1)).onConnectionCreated(ctx.getConnection());
99             return null;
100         }).when(protocolFactory).createConnection(anyString(), same(subject));
101     }
102
103     @Test
104     public void afterInitializeBridgeShouldBeOnline() throws InterruptedException {
105         // when
106         subject.initialize();
107         // internally there is an timer, let's allow it to execute
108         Thread.sleep(200);
109
110         // then
111         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
112         verify(callback, atLeastOnce()).statusUpdated(same(bridge), statusInfoCaptor.capture());
113
114         List<ThingStatusInfo> thingStatusInfo = statusInfoCaptor.getAllValues();
115         // the first one will be OFFLINE
116         assertThat(thingStatusInfo.get(0).getStatus(), is(equalTo(ThingStatus.OFFLINE)));
117         // depending on the internal timer, several status updates and especially the last one will be ONLINE
118         assertThat(thingStatusInfo.get(thingStatusInfo.size() - 1).getStatus(), is(equalTo(ThingStatus.ONLINE)));
119     }
120 }