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.fsinternetradio.test;
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;
21 import java.util.Objects;
22 import java.util.concurrent.ConcurrentHashMap;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants;
35 * @author Markus Rathgeb - Initial contribution
36 * @author Velin Yordanov - Small adjustments
38 public class RadioServiceDummy extends HttpServlet {
39 private static Map<Integer, String> requestParameters = new ConcurrentHashMap<>();
41 private static final long serialVersionUID = 1L;
43 private static final String MOCK_RADIO_PIN = "1234";
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";
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)
65 private final String playInfoNameValue = "random_station";
66 private final String playInfoNameTag = makeC8_arrayTag(playInfoNameValue);
68 private final String playInfoTextValue = "additional_info";
69 private final String playInfoTextTag = makeC8_arrayTag(playInfoTextValue);
71 private final int httpStatus;
73 private String tagToReturn = "";
74 private String responseToReturn = "";
76 private boolean isInvalidResponseExpected;
77 private boolean isInvalidValueExpected;
78 private boolean isOKAnswerExpected = true;
80 private String powerValue;
81 private String powerTag = "";
83 private String muteValue;
84 private String muteTag = "";
86 private String absoluteVolumeValue;
87 private String absoluteVolumeTag = "";
89 private String modeValue;
90 private String modeTag = "";
92 private String radioStation = "";
94 public RadioServiceDummy() {
95 this.httpStatus = HttpStatus.OK_200;
98 public String getRadioStation() {
102 public void setRadioStation(final String radioStation) {
103 this.radioStation = radioStation;
106 public void setInvalidResponseExpected(boolean isInvalidResponseExpected) {
107 this.isInvalidResponseExpected = isInvalidResponseExpected;
110 public void setOKAnswerExpected(boolean isOKAnswerExpected) {
111 this.isOKAnswerExpected = isOKAnswerExpected;
114 public boolean containsRequestParameter(int value, String parameter) {
115 String url = requestParameters.get(value);
120 return url.contains(parameter);
123 public void clearRequestParameters() {
124 requestParameters.clear();
127 public boolean areRequestParametersEmpty() {
128 return requestParameters.isEmpty();
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());
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);
148 response.setStatus(HttpStatus.OK_200);
149 response.setContentType("text/xml");
150 String commandString = request.getPathInfo();
152 switch (commandString) {
153 case (REQUEST_SET_POWER):
154 if (isInvalidValueExpected) {
157 powerValue = request.getParameter(VALUE);
160 case (REQUEST_GET_POWER):
161 powerTag = makeU8Tag(powerValue);
162 tagToReturn = powerTag;
165 case (REQUEST_SET_MUTE):
166 if (isInvalidValueExpected) {
169 muteValue = request.getParameter(VALUE);
172 case (REQUEST_GET_MUTE):
173 muteTag = makeU8Tag(muteValue);
174 tagToReturn = muteTag;
177 case (REQUEST_SET_MODE):
178 if (isInvalidValueExpected) {
181 modeValue = request.getParameter(VALUE);
184 case (REQUEST_GET_MODE):
185 modeTag = makeU32Tag(modeValue);
186 tagToReturn = modeTag;
189 case (REQUEST_SET_VOLUME):
190 if (isInvalidValueExpected) {
191 absoluteVolumeValue = null;
193 absoluteVolumeValue = request.getParameter(VALUE);
196 case (REQUEST_GET_VOLUME):
197 absoluteVolumeTag = makeU8Tag(absoluteVolumeValue);
198 tagToReturn = absoluteVolumeTag;
201 case (REQUEST_SET_PRESET_ACTION):
202 final String station = request.getParameter(VALUE);
203 setRadioStation(station);
206 case (REQUEST_GET_PLAY_INFO_NAME):
207 tagToReturn = playInfoNameTag;
210 case (REQUEST_GET_PLAY_INFO_TEXT):
211 tagToReturn = playInfoTextTag;
219 if (isInvalidResponseExpected) {
220 responseToReturn = makeInvalidXMLResponse();
222 responseToReturn = makeValidXMLResponse();
224 PrintWriter out = response.getWriter();
225 out.print(responseToReturn);
229 protected String makeU8Tag(final String value) {
230 return String.format("<value><u8>%s</u8></value>", value);
233 protected String makeU32Tag(final String value) {
234 return String.format("<value><u32>%s</u32></value>", value);
237 protected String makeC8_arrayTag(final String value) {
238 return String.format("<value><c8_array>%s</c8_array></value>", value);
241 private String makeValidXMLResponse() throws IOException {
242 return new String(getClass().getResourceAsStream("/validXml.xml").readAllBytes(), StandardCharsets.UTF_8);
245 private String makeInvalidXMLResponse() throws IOException {
246 return new String(getClass().getResourceAsStream("/invalidXml.xml").readAllBytes(), StandardCharsets.UTF_8);
249 public void setInvalidResponse(boolean value) {
250 isInvalidResponseExpected = value;