]> git.basschouten.com Git - openhab-addons.git/blob
d93b9debfdd3762d875213d2d6afb389487d8290
[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.yamahareceiver.internal;
14
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.*;
20
21 import java.util.List;
22 import java.util.Optional;
23
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;
40
41 /**
42  * Test cases for {@link YamahaBridgeHandler}. The tests provide mocks for supporting entities using Mockito.
43  *
44  * @author Tomasz Maruszak - Initial contribution
45  */
46 public class YamahaReceiverHandlerTest extends AbstractXMLProtocolTest {
47
48     private YamahaBridgeHandler subject;
49
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;
57
58     @Override
59     protected void onSetUp() throws Exception {
60         super.onSetUp();
61
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");
65
66         when(bridgeConfig.getHostWithPort()).thenReturn(Optional.of("localhost:80"));
67         when(bridgeConfig.getInputMapping()).thenReturn("");
68         when(bridgeConfig.getRefreshInterval()).thenReturn(10);
69
70         when(configuration.as(YamahaBridgeConfig.class)).thenReturn(bridgeConfig);
71         when(bridge.getConfiguration()).thenReturn(configuration);
72
73         when(protocolFactory.DeviceInformation(any(), any())).thenReturn(deviceInformation);
74         when(protocolFactory.SystemControl(any(), any(), any())).thenReturn(systemControl);
75
76         subject = new YamahaBridgeHandler(bridge);
77         subject.setZoneDiscoveryService(mock(ZoneDiscoveryService.class));
78         subject.setProtocolFactory(protocolFactory);
79         subject.setCallback(callback);
80
81         doAnswer(a -> {
82             ((ConnectionStateListener) a.getArgument(1)).onConnectionCreated(ctx.getConnection());
83             return null;
84         }).when(protocolFactory).createConnection(anyString(), same(subject));
85     }
86
87     @Test
88     public void afterInitializeBridgeShouldBeOnline() throws InterruptedException {
89         // when
90         subject.initialize();
91         // internally there is a timer, let's allow it to execute
92         Thread.sleep(200);
93
94         // then
95         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
96         verify(callback, atLeastOnce()).statusUpdated(same(bridge), statusInfoCaptor.capture());
97
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)));
103     }
104 }