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.yamahareceiver.internal;
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;
22 import java.util.List;
23 import java.util.Optional;
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;
43 * Test cases for {@link YamahaBridgeHandler}. The tests provide mocks for supporting entities using Mockito.
45 * @author Tomasz Maruszak - Initial contribution
47 public class YamahaReceiverHandlerTest extends AbstractXMLProtocolTest {
49 private YamahaBridgeHandler subject;
52 private YamahaBridgeConfig bridgeConfig;
55 private Configuration configuration;
58 private ProtocolFactory protocolFactory;
61 private DeviceInformation deviceInformation;
64 private SystemControl systemControl;
67 private ThingHandlerCallback callback;
70 private Bridge bridge;
73 protected void onSetUp() throws Exception {
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");
82 when(bridgeConfig.getHostWithPort()).thenReturn(Optional.of("localhost:80"));
83 when(bridgeConfig.getInputMapping()).thenReturn("");
84 when(bridgeConfig.getRefreshInterval()).thenReturn(10);
86 when(configuration.as(YamahaBridgeConfig.class)).thenReturn(bridgeConfig);
87 when(bridge.getConfiguration()).thenReturn(configuration);
89 when(protocolFactory.DeviceInformation(any(), any())).thenReturn(deviceInformation);
90 when(protocolFactory.SystemControl(any(), any(), any())).thenReturn(systemControl);
92 subject = new YamahaBridgeHandler(bridge);
93 subject.setZoneDiscoveryService(mock(ZoneDiscoveryService.class));
94 subject.setProtocolFactory(protocolFactory);
95 subject.setCallback(callback);
98 ((ConnectionStateListener) a.getArgument(1)).onConnectionCreated(ctx.getConnection());
100 }).when(protocolFactory).createConnection(anyString(), same(subject));
104 public void afterInitializeBridgeShouldBeOnline() throws InterruptedException {
106 subject.initialize();
107 // internally there is an timer, let's allow it to execute
111 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
112 verify(callback, atLeastOnce()).statusUpdated(same(bridge), statusInfoCaptor.capture());
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)));