]> git.basschouten.com Git - openhab-addons.git/blob
7525b31042fb758bc35492544fadf24b21a8a53e
[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.protocol.xml;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import org.junit.jupiter.api.Test;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Mock;
22 import org.openhab.binding.yamahareceiver.internal.TestModels;
23 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
24 import org.openhab.binding.yamahareceiver.internal.state.SystemControlState;
25 import org.openhab.binding.yamahareceiver.internal.state.SystemControlStateListener;
26
27 /**
28  * Unit test for {@link SystemControlXML}.
29  *
30  * @author Tomasz Maruszak - Initial contribution
31  */
32 public class SystemControlXMLTest extends AbstractXMLProtocolTest {
33
34     private SystemControlXML subject;
35
36     private DeviceInformationState deviceInformationState;
37
38     private @Mock SystemControlStateListener systemControlStateListener;
39
40     protected void setupFor(String model) throws Exception {
41         ctx.prepareForModel(model);
42         ctx.respondWith("<System><Power_Control><Power>GetParam</Power></Power_Control></System>",
43                 "System_Power_Control_Power.xml");
44         ctx.respondWith("<System><Party_Mode><Mode>GetParam</Mode></Party_Mode></System>",
45                 "System_Party_Mode_Mode.xml");
46
47         deviceInformationState = new DeviceInformationState();
48         DeviceInformationXML deviceInformation = new DeviceInformationXML(con, deviceInformationState);
49         deviceInformation.update();
50
51         subject = new SystemControlXML(con, systemControlStateListener, deviceInformationState);
52     }
53
54     @Test
55     public void given_RX_S601D_when_update_then_parsesState() throws Exception {
56         // given
57         setupFor(TestModels.RX_S601D);
58
59         // when
60         subject.update();
61
62         // then
63         ArgumentCaptor<SystemControlState> stateArg = ArgumentCaptor.forClass(SystemControlState.class);
64         verify(systemControlStateListener, only()).systemControlStateChanged(stateArg.capture());
65
66         SystemControlState state = stateArg.getValue();
67         assertTrue(state.power);
68         assertTrue(state.partyMode);
69     }
70
71     @Test
72     public void given_RX_S601D_when_power_then_sendsProperCommand() throws Exception {
73         // given
74         setupFor(TestModels.RX_S601D);
75
76         // when
77         subject.setPower(true);
78         subject.setPower(false);
79         subject.setPartyMode(true);
80         subject.setPartyModeMute(true);
81
82         // then
83         verify(con).send(eq("<System><Power_Control><Power>On</Power></Power_Control></System>"));
84         verify(con).send(eq("<System><Power_Control><Power>Standby</Power></Power_Control></System>"));
85         verify(con).send(eq("<System><Party_Mode><Mode>On</Mode></Party_Mode></System>"));
86         verify(con).send(eq("<System><Party_Mode><Volume><Mute>On</Mute></Volume></Party_Mode></System>"));
87     }
88
89     @Test
90     public void given_RX_V3900_when_partyMode_then_noCommandSend() throws Exception {
91         // given
92         setupFor(TestModels.RX_V3900);
93
94         // when
95         subject.setPartyMode(true);
96         subject.setPartyModeMute(true);
97         subject.setPartyModeVolume(true);
98
99         // then
100         verify(con, never()).send(anyString());
101     }
102
103     @Test
104     public void given_RX_V3900_when_update_then_parsesStateAndDoesNotUpdateStateForPartyMode() throws Exception {
105         // given
106         setupFor(TestModels.RX_V3900);
107
108         // when
109         subject.update();
110
111         // then
112         ArgumentCaptor<SystemControlState> stateArg = ArgumentCaptor.forClass(SystemControlState.class);
113         verify(systemControlStateListener, only()).systemControlStateChanged(stateArg.capture());
114         verify(con, never()).sendReceive(eq("<System><Party_Mode><Mode>GetParam</Mode></Party_Mode></System>"));
115
116         SystemControlState state = stateArg.getValue();
117         assertTrue(state.power);
118         assertFalse(state.partyMode);
119     }
120 }