2 * Copyright (c) 2010-2023 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.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
21 import java.util.List;
22 import java.util.Optional;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
28 import org.openhab.binding.yamahareceiver.internal.discovery.ZoneDiscoveryService;
29 import org.openhab.binding.yamahareceiver.internal.handler.YamahaBridgeHandler;
30 import org.openhab.binding.yamahareceiver.internal.protocol.ConnectionStateListener;
31 import org.openhab.binding.yamahareceiver.internal.protocol.DeviceInformation;
32 import org.openhab.binding.yamahareceiver.internal.protocol.ProtocolFactory;
33 import org.openhab.binding.yamahareceiver.internal.protocol.SystemControl;
34 import org.openhab.binding.yamahareceiver.internal.protocol.xml.AbstractXMLProtocolTest;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusInfo;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 * Test cases for {@link YamahaBridgeHandler}. The tests provide mocks for supporting entities using Mockito.
44 * @author Tomasz Maruszak - Initial contribution
46 public class YamahaReceiverHandlerTest extends AbstractXMLProtocolTest {
48 private YamahaBridgeHandler subject;
50 private @Mock YamahaBridgeConfig bridgeConfig;
51 private @Mock Configuration configuration;
52 private @Mock ProtocolFactory protocolFactory;
53 private @Mock DeviceInformation deviceInformation;
54 private @Mock SystemControl systemControl;
55 private @Mock ThingHandlerCallback callback;
56 private @Mock Bridge bridge;
59 protected void onSetUp() throws Exception {
62 ctx.prepareForModel(TestModels.RX_S601D);
63 ctx.respondWith("<Main_Zone><Input><Input_Sel_Item>GetParam</Input_Sel_Item></Input></Main_Zone>",
64 "Main_Zone_Input_Input_Sel.xml");
66 when(bridgeConfig.getHostWithPort()).thenReturn(Optional.of("localhost:80"));
67 when(bridgeConfig.getInputMapping()).thenReturn("");
68 when(bridgeConfig.getRefreshInterval()).thenReturn(10);
70 when(configuration.as(YamahaBridgeConfig.class)).thenReturn(bridgeConfig);
71 when(bridge.getConfiguration()).thenReturn(configuration);
73 when(protocolFactory.DeviceInformation(any(), any())).thenReturn(deviceInformation);
74 when(protocolFactory.SystemControl(any(), any(), any())).thenReturn(systemControl);
76 subject = new YamahaBridgeHandler(bridge);
77 subject.setZoneDiscoveryService(mock(ZoneDiscoveryService.class));
78 subject.setProtocolFactory(protocolFactory);
79 subject.setCallback(callback);
82 ((ConnectionStateListener) a.getArgument(1)).onConnectionCreated(ctx.getConnection());
84 }).when(protocolFactory).createConnection(anyString(), same(subject));
88 public void afterInitializeBridgeShouldBeOnline() throws InterruptedException {
91 // internally there is a timer, let's allow it to execute
95 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
96 verify(callback, atLeastOnce()).statusUpdated(same(bridge), statusInfoCaptor.capture());
98 List<ThingStatusInfo> thingStatusInfo = statusInfoCaptor.getAllValues();
99 // the first one will be OFFLINE
100 assertThat(thingStatusInfo.get(0).getStatus(), is(equalTo(ThingStatus.OFFLINE)));
101 // depending on the internal timer, several status updates and especially the last one will be ONLINE
102 assertThat(thingStatusInfo.get(thingStatusInfo.size() - 1).getStatus(), is(equalTo(ThingStatus.ONLINE)));