2 * Copyright (c) 2010-2020 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.concurrent.ConcurrentHashMap;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
28 import org.eclipse.jetty.http.HttpStatus;
29 import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants;
34 * @author Markus Rathgeb - Initial contribution
35 * @author Velin Yordanov - Small adjustments
37 public class RadioServiceDummy extends HttpServlet {
38 private static Map<Integer, String> requestParameters = new ConcurrentHashMap<>();
40 private static final long serialVersionUID = 1L;
42 private static final String MOCK_RADIO_PIN = "1234";
44 private static final String REQUEST_SET_POWER = "/" + FrontierSiliconRadioConstants.REQUEST_SET_POWER;
45 private static final String REQUEST_GET_POWER = "/" + FrontierSiliconRadioConstants.REQUEST_GET_POWER;
46 private static final String REQUEST_GET_MODE = "/" + FrontierSiliconRadioConstants.REQUEST_GET_MODE;
47 private static final String REQUEST_SET_MODE = "/" + FrontierSiliconRadioConstants.REQUEST_SET_MODE;
48 private static final String REQUEST_SET_VOLUME = "/" + FrontierSiliconRadioConstants.REQUEST_SET_VOLUME;
49 private static final String REQUEST_GET_VOLUME = "/" + FrontierSiliconRadioConstants.REQUEST_GET_VOLUME;
50 private static final String REQUEST_SET_MUTE = "/" + FrontierSiliconRadioConstants.REQUEST_SET_MUTE;
51 private static final String REQUEST_GET_MUTE = "/" + FrontierSiliconRadioConstants.REQUEST_GET_MUTE;
52 private static final String REQUEST_SET_PRESET_ACTION = "/"
53 + FrontierSiliconRadioConstants.REQUEST_SET_PRESET_ACTION;
54 private static final String REQUEST_GET_PLAY_INFO_TEXT = "/"
55 + FrontierSiliconRadioConstants.REQUEST_GET_PLAY_INFO_TEXT;
56 private static final String REQUEST_GET_PLAY_INFO_NAME = "/"
57 + FrontierSiliconRadioConstants.REQUEST_GET_PLAY_INFO_NAME;
58 private static final String VALUE = "value";
61 * For the purposes of the tests it is assumed that the current station and the additional information
62 * are always the same (random_station and additional_info)
64 private final String playInfoNameValue = "random_station";
65 private final String playInfoNameTag = makeC8_arrayTag(playInfoNameValue);
67 private final String playInfoTextValue = "additional_info";
68 private final String playInfoTextTag = makeC8_arrayTag(playInfoTextValue);
70 private final int httpStatus;
72 private String tagToReturn = "";
73 private String responseToReturn = "";
75 private boolean isInvalidResponseExpected;
76 private boolean isInvalidValueExpected;
77 private boolean isOKAnswerExpected = true;
79 private String powerValue;
80 private String powerTag = "";
82 private String muteValue;
83 private String muteTag = "";
85 private String absoluteVolumeValue;
86 private String absoluteVolumeTag = "";
88 private String modeValue;
89 private String modeTag = "";
91 private String radioStation = "";
93 public RadioServiceDummy() {
94 this.httpStatus = HttpStatus.OK_200;
97 public String getRadioStation() {
101 public void setRadioStation(final String radioStation) {
102 this.radioStation = radioStation;
105 public void setInvalidResponseExpected(boolean isInvalidResponseExpected) {
106 this.isInvalidResponseExpected = isInvalidResponseExpected;
109 public void setOKAnswerExpected(boolean isOKAnswerExpected) {
110 this.isOKAnswerExpected = isOKAnswerExpected;
113 public boolean containsRequestParameter(int value, String parameter) {
114 String url = requestParameters.get(value);
119 return url.contains(parameter);
122 public void clearRequestParameters() {
123 requestParameters.clear();
126 public boolean areRequestParametersEmpty() {
127 return requestParameters.isEmpty();
131 protected void doGet(HttpServletRequest request, HttpServletResponse response)
132 throws ServletException, IOException {
133 String queryString = request.getQueryString();
134 Collection<String> requestParameterNames = Collections.list(request.getParameterNames());
135 if (queryString != null && requestParameterNames.contains(VALUE)) {
136 StringBuffer fullUrl = request.getRequestURL().append("?").append(queryString);
137 int value = Integer.parseInt(request.getParameter(VALUE));
138 requestParameters.put(value, fullUrl.toString());
141 String pin = request.getParameter("pin");
142 if (!MOCK_RADIO_PIN.equals(pin)) {
143 response.setStatus(HttpStatus.FORBIDDEN_403);
144 } else if (!isOKAnswerExpected) {
145 response.setStatus(HttpStatus.NOT_FOUND_404);
147 response.setStatus(HttpStatus.OK_200);
148 response.setContentType("text/xml");
149 String commandString = request.getPathInfo();
151 switch (commandString) {
152 case (REQUEST_SET_POWER):
153 if (isInvalidValueExpected) {
156 powerValue = request.getParameter(VALUE);
159 case (REQUEST_GET_POWER):
160 powerTag = makeU8Tag(powerValue);
161 tagToReturn = powerTag;
164 case (REQUEST_SET_MUTE):
165 if (isInvalidValueExpected) {
168 muteValue = request.getParameter(VALUE);
171 case (REQUEST_GET_MUTE):
172 muteTag = makeU8Tag(muteValue);
173 tagToReturn = muteTag;
176 case (REQUEST_SET_MODE):
177 if (isInvalidValueExpected) {
180 modeValue = request.getParameter(VALUE);
183 case (REQUEST_GET_MODE):
184 modeTag = makeU32Tag(modeValue);
185 tagToReturn = modeTag;
188 case (REQUEST_SET_VOLUME):
189 if (isInvalidValueExpected) {
190 absoluteVolumeValue = null;
192 absoluteVolumeValue = request.getParameter(VALUE);
195 case (REQUEST_GET_VOLUME):
196 absoluteVolumeTag = makeU8Tag(absoluteVolumeValue);
197 tagToReturn = absoluteVolumeTag;
200 case (REQUEST_SET_PRESET_ACTION):
201 final String station = request.getParameter(VALUE);
202 setRadioStation(station);
205 case (REQUEST_GET_PLAY_INFO_NAME):
206 tagToReturn = playInfoNameTag;
209 case (REQUEST_GET_PLAY_INFO_TEXT):
210 tagToReturn = playInfoTextTag;
218 if (isInvalidResponseExpected) {
219 responseToReturn = makeInvalidXMLResponse();
221 responseToReturn = makeValidXMLResponse();
223 PrintWriter out = response.getWriter();
224 out.print(responseToReturn);
228 protected String makeU8Tag(final String value) {
229 return String.format("<value><u8>%s</u8></value>", value);
232 protected String makeU32Tag(final String value) {
233 return String.format("<value><u32>%s</u32></value>", value);
236 protected String makeC8_arrayTag(final String value) {
237 return String.format("<value><c8_array>%s</c8_array></value>", value);
240 private String makeValidXMLResponse() throws IOException {
241 return new String(getClass().getResourceAsStream("/validXml.xml").readAllBytes(), StandardCharsets.UTF_8);
244 private String makeInvalidXMLResponse() throws IOException {
245 return new String(getClass().getResourceAsStream("/invalidXml.xml").readAllBytes(), StandardCharsets.UTF_8);
248 public void setInvalidResponse(boolean value) {
249 isInvalidResponseExpected = value;