]> git.basschouten.com Git - openhab-addons.git/blob
5aca77ec1e6476bb0eba8ee17b36cc41af272877
[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.fsinternetradio.test;
14
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants;
31
32 /**
33  * Radio service mock.
34  *
35  * @author Markus Rathgeb - Initial contribution
36  * @author Velin Yordanov - Small adjustments
37  */
38 public class RadioServiceDummy extends HttpServlet {
39     private static Map<Integer, String> requestParameters = new ConcurrentHashMap<>();
40
41     private static final long serialVersionUID = 1L;
42
43     private static final String MOCK_RADIO_PIN = "1234";
44
45     private static final String REQUEST_SET_POWER = "/" + FrontierSiliconRadioConstants.REQUEST_SET_POWER;
46     private static final String REQUEST_GET_POWER = "/" + FrontierSiliconRadioConstants.REQUEST_GET_POWER;
47     private static final String REQUEST_GET_MODE = "/" + FrontierSiliconRadioConstants.REQUEST_GET_MODE;
48     private static final String REQUEST_SET_MODE = "/" + FrontierSiliconRadioConstants.REQUEST_SET_MODE;
49     private static final String REQUEST_SET_VOLUME = "/" + FrontierSiliconRadioConstants.REQUEST_SET_VOLUME;
50     private static final String REQUEST_GET_VOLUME = "/" + FrontierSiliconRadioConstants.REQUEST_GET_VOLUME;
51     private static final String REQUEST_SET_MUTE = "/" + FrontierSiliconRadioConstants.REQUEST_SET_MUTE;
52     private static final String REQUEST_GET_MUTE = "/" + FrontierSiliconRadioConstants.REQUEST_GET_MUTE;
53     private static final String REQUEST_SET_PRESET_ACTION = "/"
54             + FrontierSiliconRadioConstants.REQUEST_SET_PRESET_ACTION;
55     private static final String REQUEST_GET_PLAY_INFO_TEXT = "/"
56             + FrontierSiliconRadioConstants.REQUEST_GET_PLAY_INFO_TEXT;
57     private static final String REQUEST_GET_PLAY_INFO_NAME = "/"
58             + FrontierSiliconRadioConstants.REQUEST_GET_PLAY_INFO_NAME;
59     private static final String VALUE = "value";
60
61     /*
62      * For the purposes of the tests it is assumed that the current station and the additional information
63      * are always the same (random_station and additional_info)
64      */
65     private final String playInfoNameValue = "random_station";
66     private final String playInfoNameTag = makeC8_arrayTag(playInfoNameValue);
67
68     private final String playInfoTextValue = "additional_info";
69     private final String playInfoTextTag = makeC8_arrayTag(playInfoTextValue);
70
71     private final int httpStatus;
72
73     private String tagToReturn = "";
74     private String responseToReturn = "";
75
76     private boolean isInvalidResponseExpected;
77     private boolean isInvalidValueExpected;
78     private boolean isOKAnswerExpected = true;
79
80     private String powerValue;
81     private String powerTag = "";
82
83     private String muteValue;
84     private String muteTag = "";
85
86     private String absoluteVolumeValue;
87     private String absoluteVolumeTag = "";
88
89     private String modeValue;
90     private String modeTag = "";
91
92     private String radioStation = "";
93
94     public RadioServiceDummy() {
95         this.httpStatus = HttpStatus.OK_200;
96     }
97
98     public String getRadioStation() {
99         return radioStation;
100     }
101
102     public void setRadioStation(final String radioStation) {
103         this.radioStation = radioStation;
104     }
105
106     public void setInvalidResponseExpected(boolean isInvalidResponseExpected) {
107         this.isInvalidResponseExpected = isInvalidResponseExpected;
108     }
109
110     public void setOKAnswerExpected(boolean isOKAnswerExpected) {
111         this.isOKAnswerExpected = isOKAnswerExpected;
112     }
113
114     public boolean containsRequestParameter(int value, String parameter) {
115         String url = requestParameters.get(value);
116         if (url == null) {
117             return false;
118         }
119
120         return url.contains(parameter);
121     }
122
123     public void clearRequestParameters() {
124         requestParameters.clear();
125     }
126
127     public boolean areRequestParametersEmpty() {
128         return requestParameters.isEmpty();
129     }
130
131     @Override
132     protected void doGet(HttpServletRequest request, HttpServletResponse response)
133             throws ServletException, IOException {
134         String queryString = request.getQueryString();
135         Collection<String> requestParameterNames = Collections.list(request.getParameterNames());
136         if (queryString != null && requestParameterNames.contains(VALUE)) {
137             StringBuffer fullUrl = request.getRequestURL().append("?").append(queryString);
138             int value = Integer.parseInt(Objects.requireNonNullElse(request.getParameter(VALUE), ""));
139             requestParameters.put(value, fullUrl.toString());
140         }
141
142         String pin = request.getParameter("pin");
143         if (!MOCK_RADIO_PIN.equals(pin)) {
144             response.setStatus(HttpStatus.FORBIDDEN_403);
145         } else if (!isOKAnswerExpected) {
146             response.setStatus(HttpStatus.NOT_FOUND_404);
147         } else {
148             response.setStatus(HttpStatus.OK_200);
149             response.setContentType("text/xml");
150             String commandString = request.getPathInfo();
151
152             switch (commandString) {
153                 case (REQUEST_SET_POWER):
154                     if (isInvalidValueExpected) {
155                         powerValue = null;
156                     } else {
157                         powerValue = request.getParameter(VALUE);
158                     }
159
160                 case (REQUEST_GET_POWER):
161                     powerTag = makeU8Tag(powerValue);
162                     tagToReturn = powerTag;
163                     break;
164
165                 case (REQUEST_SET_MUTE):
166                     if (isInvalidValueExpected) {
167                         muteValue = null;
168                     } else {
169                         muteValue = request.getParameter(VALUE);
170                     }
171
172                 case (REQUEST_GET_MUTE):
173                     muteTag = makeU8Tag(muteValue);
174                     tagToReturn = muteTag;
175                     break;
176
177                 case (REQUEST_SET_MODE):
178                     if (isInvalidValueExpected) {
179                         modeValue = null;
180                     } else {
181                         modeValue = request.getParameter(VALUE);
182                     }
183
184                 case (REQUEST_GET_MODE):
185                     modeTag = makeU32Tag(modeValue);
186                     tagToReturn = modeTag;
187                     break;
188
189                 case (REQUEST_SET_VOLUME):
190                     if (isInvalidValueExpected) {
191                         absoluteVolumeValue = null;
192                     } else {
193                         absoluteVolumeValue = request.getParameter(VALUE);
194                     }
195
196                 case (REQUEST_GET_VOLUME):
197                     absoluteVolumeTag = makeU8Tag(absoluteVolumeValue);
198                     tagToReturn = absoluteVolumeTag;
199                     break;
200
201                 case (REQUEST_SET_PRESET_ACTION):
202                     final String station = request.getParameter(VALUE);
203                     setRadioStation(station);
204                     break;
205
206                 case (REQUEST_GET_PLAY_INFO_NAME):
207                     tagToReturn = playInfoNameTag;
208                     break;
209
210                 case (REQUEST_GET_PLAY_INFO_TEXT):
211                     tagToReturn = playInfoTextTag;
212                     break;
213
214                 default:
215                     tagToReturn = "";
216                     break;
217             }
218
219             if (isInvalidResponseExpected) {
220                 responseToReturn = makeInvalidXMLResponse();
221             } else {
222                 responseToReturn = makeValidXMLResponse();
223             }
224             PrintWriter out = response.getWriter();
225             out.print(responseToReturn);
226         }
227     }
228
229     protected String makeU8Tag(final String value) {
230         return String.format("<value><u8>%s</u8></value>", value);
231     }
232
233     protected String makeU32Tag(final String value) {
234         return String.format("<value><u32>%s</u32></value>", value);
235     }
236
237     protected String makeC8_arrayTag(final String value) {
238         return String.format("<value><c8_array>%s</c8_array></value>", value);
239     }
240
241     private String makeValidXMLResponse() throws IOException {
242         return new String(getClass().getResourceAsStream("/validXml.xml").readAllBytes(), StandardCharsets.UTF_8);
243     }
244
245     private String makeInvalidXMLResponse() throws IOException {
246         return new String(getClass().getResourceAsStream("/invalidXml.xml").readAllBytes(), StandardCharsets.UTF_8);
247     }
248
249     public void setInvalidResponse(boolean value) {
250         isInvalidResponseExpected = value;
251     }
252 }