]> git.basschouten.com Git - openhab-addons.git/blob
da78cf198dcafe3dd98d1a54eb2a4f6bcaf8e033
[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.homematic.test.util;
14
15 import java.io.IOException;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.openhab.binding.homematic.internal.common.HomematicConfig;
21 import org.openhab.binding.homematic.internal.communicator.client.RpcClient;
22 import org.openhab.binding.homematic.internal.communicator.message.RpcRequest;
23
24 /**
25  * @author Florian Stolte - Initial contribution
26  */
27 public class RpcClientMockImpl extends RpcClient<String> {
28
29     public static final String GET_PARAMSET_DESCRIPTION_NAME = "getParamsetDescription";
30     public static final String GET_PARAMSET_NAME = "getParamset";
31
32     public Map<String, Integer> numberOfCalls = new HashMap<>();
33
34     public RpcClientMockImpl() throws IOException {
35         this(new HomematicConfig());
36     }
37
38     public RpcClientMockImpl(HomematicConfig config) throws IOException {
39         super(config);
40
41         Arrays.asList(GET_PARAMSET_DESCRIPTION_NAME, GET_PARAMSET_NAME).forEach(method -> numberOfCalls.put(method, 0));
42     }
43
44     @Override
45     protected Object[] sendMessage(int port, RpcRequest<String> request) throws IOException {
46         String methodName = request.getMethodName();
47
48         increaseNumberOfCalls(methodName);
49
50         return mockResponse();
51     }
52
53     private void increaseNumberOfCalls(String methodName) {
54         Integer currentNumber = numberOfCalls.get(methodName);
55         if (currentNumber == null) {
56             numberOfCalls.put(methodName, 1);
57         } else {
58             numberOfCalls.put(methodName, currentNumber + 1);
59         }
60     }
61
62     private Object[] mockResponse() {
63         Object[] response = new Object[1];
64         response[0] = new HashMap<>();
65         return response;
66     }
67
68     @Override
69     protected RpcRequest<String> createRpcRequest(String methodName) {
70         return new RpcRequest<String>() {
71
72             @Override
73             public void addArg(Object arg) {
74             }
75
76             @Override
77             public String createMessage() {
78                 return null;
79             }
80
81             @Override
82             public String getMethodName() {
83                 return methodName;
84             }
85         };
86     }
87
88     @Override
89     public void dispose() {
90     }
91
92     @Override
93     protected String getRpcCallbackUrl() {
94         return null;
95     }
96 }