]> git.basschouten.com Git - openhab-addons.git/blob
1d3d0147d91dac9e3e95382140ece954d7ede633
[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.mockito.ArgumentMatchers.eq;
16 import static org.mockito.Mockito.when;
17 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLConstants.Commands.SYSTEM_STATUS_CONFIG_CMD;
18 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLConstants.Commands.ZONE_BASIC_STATUS_CMD;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22
23 import org.openhab.binding.yamahareceiver.internal.ResponseLoader;
24
25 /**
26  * Testing context for a selected Yamaha model.
27  *
28  * @author Tomasz Maruszak - Initial contribution
29  */
30 public class ModelContext {
31
32     private final ResponseLoader rl = new ResponseLoader();
33     private final XMLConnection connection;
34     private String model;
35
36     public XMLConnection getConnection() {
37         return connection;
38     }
39
40     public ModelContext(XMLConnection connection) {
41         this.connection = connection;
42     }
43
44     public void prepareForModel(String model) throws IOException {
45         this.model = model;
46
47         String descFile = String.format("/desc_%s.xml", model);
48         String desc = rl.load(descFile, model);
49         if (desc == null) {
50             throw new FileNotFoundException("Could not load " + descFile);
51         }
52
53         when(connection.getResponse(eq("/YamahaRemoteControl/desc.xml"))).thenReturn(desc);
54         when(connection.getHost()).thenReturn("localhost");
55
56         respondWith(SYSTEM_STATUS_CONFIG_CMD, "System_Config.xml");
57         respondWith(String.format("<Main_Zone>%s</Main_Zone>", ZONE_BASIC_STATUS_CMD), "Main_Zone_Basic_Status.xml");
58     }
59
60     public void respondWith(String command, String path) {
61         try {
62             String response = rl.load(path, model);
63             if (response != null) {
64                 when(connection.sendReceive(eq(command))).thenReturn(response);
65             }
66         } catch (IOException e) {
67             throw new RuntimeException(e);
68         }
69     }
70 }