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.protocol.xml;
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.*;
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants;
28 * Unit test for {@link DeviceDescriptorXML}.
30 * @author Tomasz Maruszak - Initial contribution
32 public class DeviceDescriptorXMLTest extends AbstractXMLProtocolTest {
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),
39 Arrays.asList("System,Power_Control,Power", "System,Party_Mode,Mode",
40 "System,Party_Mode,Volume,Lvl", "System,Party_Mode,Volume,Mute")),
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")),
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")) });
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[] {
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")),
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")),
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")) });
72 private void parsesDescriptor(String model, List<YamahaReceiverBindingConstants.Zone> zones,
73 Collection<YamahaReceiverBindingConstants.Feature> features, CommandsSpec systemCommandsSpec,
74 CommandsSpec[] zonesCommandsSpec) throws IOException {
76 ctx.prepareForModel(model);
78 DeviceDescriptorXML subject = new DeviceDescriptorXML();
84 assertEquals(model, subject.getUnitName());
86 assertNotNull(subject.system);
87 assertCommands(subject.system, systemCommandsSpec);
89 assertNotNull(subject.features);
90 assertTrue(subject.features.keySet().containsAll(features), "Desired features present");
92 assertNotNull(subject.zones);
93 assertEquals(zones.size(), subject.zones.size(), "Number of zones match");
95 for (int i = 0; i < zones.size(); i++) {
96 YamahaReceiverBindingConstants.Zone zone = zones.get(i);
98 assertTrue(subject.zones.containsKey(zone), "Desired zone is present");
100 DeviceDescriptorXML.ZoneDescriptor zoneDesc = subject.zones.get(zone);
101 CommandsSpec zoneSpec = zonesCommandsSpec[i];
102 assertCommands(zoneDesc, zoneSpec);
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");
112 private static class CommandsSpec {
114 private final int expectedNumber;
116 private final Collection<String> expected;
118 private CommandsSpec(int expectedNumber, Collection<String> expected) {
119 this.expectedNumber = expectedNumber;
120 this.expected = expected;