]> git.basschouten.com Git - openhab-addons.git/blob
eda0c3fddc8a17668ea462c03b53e7644236d747
[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.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Feature.*;
17 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone.*;
18
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants;
26
27 /**
28  * Unit test for {@link DeviceDescriptorXML}.
29  *
30  * @author Tomasz Maruszak - Initial contribution
31  */
32 public class DeviceDescriptorXMLTest extends AbstractXMLProtocolTest {
33
34     @Test
35     public void given_RXS601D_parsesDescriptor() throws IOException {
36         parsesDescriptor("RX-S601D", Arrays.asList(Main_Zone, Zone_2),
37                 Arrays.asList(AIRPLAY, SPOTIFY, USB, BLUETOOTH, DAB, NET_RADIO),
38                 new CommandsSpec(9,
39                         Arrays.asList("System,Power_Control,Power", "System,Party_Mode,Mode",
40                                 "System,Party_Mode,Volume,Lvl", "System,Party_Mode,Volume,Mute")),
41                 new CommandsSpec[] {
42                         // Main_Zone
43                         new CommandsSpec(29, Arrays.asList("Main_Zone,Power_Control,Power", "Main_Zone,Volume,Lvl",
44                                 "Main_Zone,Volume,Mute", "Main_Zone,Input,Input_Sel", "Main_Zone,Input,Input_Sel_Item",
45                                 "Main_Zone,Scene,Scene_Sel", "Main_Zone,Scene,Scene_Sel_Item",
46                                 "Main_Zone,Surround,Program_Sel,Current,Straight",
47                                 "Main_Zone,Surround,Program_Sel,Current,Enhancer",
48                                 "Main_Zone,Surround,Program_Sel,Current,Sound_Program")),
49                         // Zone_2
50                         new CommandsSpec(20,
51                                 Arrays.asList("Zone_2,Power_Control,Power", "Zone_2,Volume,Lvl", "Zone_2,Volume,Mute",
52                                         "Zone_2,Input,Input_Sel", "Zone_2,Input,Input_Sel_Item",
53                                         "Zone_2,Scene,Scene_Sel", "Zone_2,Scene,Scene_Sel_Item")) });
54     }
55
56     @Test
57     public void given_RXV3900_parsesDescriptor() throws IOException {
58         parsesDescriptor("RX-V3900", Arrays.asList(Main_Zone, Zone_2, Zone_3), Arrays.asList(BLUETOOTH, TUNER, NET_USB),
59                 new CommandsSpec(2, Arrays.asList("System,Power_Control,Power")), new CommandsSpec[] {
60                         // Main_Zone
61                         new CommandsSpec(9, Arrays.asList("Main_Zone,Power_Control,Power", "Main_Zone,Vol,Lvl",
62                                 "Main_Zone,Vol,Mute", "Main_Zone,Input,Input_Sel", "Main_Zone,Input,Input_Sel_Item")),
63                         // Zone_2
64                         new CommandsSpec(9,
65                                 Arrays.asList("Zone_2,Power_Control,Power", "Zone_2,Vol,Lvl", "Zone_2,Vol,Mute",
66                                         "Zone_2,Input,Input_Sel", "Zone_2,Input,Input_Sel_Item")),
67                         // Zone_3
68                         new CommandsSpec(9, Arrays.asList("Zone_3,Power_Control,Power", "Zone_3,Vol,Lvl",
69                                 "Zone_3,Vol,Mute", "Zone_3,Input,Input_Sel", "Zone_3,Input,Input_Sel_Item")) });
70     }
71
72     private void parsesDescriptor(String model, List<YamahaReceiverBindingConstants.Zone> zones,
73             Collection<YamahaReceiverBindingConstants.Feature> features, CommandsSpec systemCommandsSpec,
74             CommandsSpec[] zonesCommandsSpec) throws IOException {
75         // arrange
76         ctx.prepareForModel(model);
77
78         DeviceDescriptorXML subject = new DeviceDescriptorXML();
79
80         // act
81         subject.load(con);
82
83         // assert
84         assertEquals(model, subject.getUnitName());
85
86         assertNotNull(subject.system);
87         assertCommands(subject.system, systemCommandsSpec);
88
89         assertNotNull(subject.features);
90         assertTrue(subject.features.keySet().containsAll(features), "Desired features present");
91
92         assertNotNull(subject.zones);
93         assertEquals(zones.size(), subject.zones.size(), "Number of zones match");
94
95         for (int i = 0; i < zones.size(); i++) {
96             YamahaReceiverBindingConstants.Zone zone = zones.get(i);
97
98             assertTrue(subject.zones.containsKey(zone), "Desired zone is present");
99
100             DeviceDescriptorXML.ZoneDescriptor zoneDesc = subject.zones.get(zone);
101             CommandsSpec zoneSpec = zonesCommandsSpec[i];
102             assertCommands(zoneDesc, zoneSpec);
103         }
104     }
105
106     private void assertCommands(DeviceDescriptorXML.HasCommands descWithCommands, CommandsSpec spec) {
107         assertNotNull(descWithCommands, "Descriptor commands are present");
108         assertEquals(spec.expectedNumber, descWithCommands.commands.size(), "Expected number of commands");
109         assertTrue(descWithCommands.commands.containsAll(spec.expected), "Expected commands are present");
110     }
111
112     private static class CommandsSpec {
113
114         private final int expectedNumber;
115
116         private final Collection<String> expected;
117
118         private CommandsSpec(int expectedNumber, Collection<String> expected) {
119             this.expectedNumber = expectedNumber;
120             this.expected = expected;
121         }
122     }
123 }