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