This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
AbstractAudioServeltTest.java
124 lines (98 loc) · 3.95 KB
/
AbstractAudioServeltTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.audio.internal;
import static org.junit.Assert.fail;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.smarthome.core.audio.AudioFormat;
import org.eclipse.smarthome.core.audio.AudioStream;
import org.eclipse.smarthome.core.audio.ByteArrayAudioStream;
import org.eclipse.smarthome.core.audio.FixedLengthAudioStream;
import org.eclipse.smarthome.test.TestPortUtil;
import org.eclipse.smarthome.test.TestServer;
import org.eclipse.smarthome.test.java.JavaTest;
import org.junit.After;
import org.junit.Before;
/**
* Base class for tests using the {@link AudioServlet}.
*
* @author Henning Treu - initial contribution
*
*/
public abstract class AbstractAudioServeltTest extends JavaTest {
protected AudioServlet audioServlet;
private int port;
private TestServer server;
private final static String AUDIO_SERVLET_PROTOCOL = "http";
private final static String AUDIO_SERVLET_HOSTNAME = "localhost";
private CompletableFuture<Boolean> serverStarted;
private HttpClient httpClient;
@Before
public void setupServerAndClient() {
audioServlet = new AudioServlet();
ServletHolder servletHolder = new ServletHolder(audioServlet);
port = TestPortUtil.findFreePort();
server = new TestServer(AUDIO_SERVLET_HOSTNAME, port, 10000, servletHolder);
serverStarted = server.startServer();
httpClient = new HttpClient();
}
@After
public void tearDownServerAndClient() throws Exception {
server.stopServer();
httpClient.stop();
}
protected ByteArrayAudioStream getByteArrayAudioStream(byte[] byteArray, String container, String codec) {
int bitDepth = 16;
int bitRate = 1000;
long frequency = 16384;
AudioFormat audioFormat = new AudioFormat(container, codec, true, bitDepth, bitRate, frequency);
return new ByteArrayAudioStream(byteArray, audioFormat);
}
protected ContentResponse getHttpResponse(AudioStream audioStream) throws Exception {
String url = serveStream(audioStream);
return getHttpRequest(url).send();
}
protected String serveStream(AudioStream stream) throws Exception {
return serveStream(stream, null);
}
protected void startHttpClient(HttpClient client) {
if (!client.isStarted()) {
try {
client.start();
} catch (Exception e) {
fail("An exception " + e + " was thrown, while starting the HTTP client");
}
}
}
protected Request getHttpRequest(String url) {
startHttpClient(httpClient);
return httpClient.newRequest(url).method(HttpMethod.GET);
}
protected String serveStream(AudioStream stream, Integer timeInterval) throws Exception {
serverStarted.get(); // wait for the server thread to be started
String path;
if (timeInterval != null) {
path = audioServlet.serve((FixedLengthAudioStream) stream, timeInterval);
} else {
path = audioServlet.serve(stream);
}
return generateURL(AUDIO_SERVLET_PROTOCOL, AUDIO_SERVLET_HOSTNAME, port, path);
}
private String generateURL(String protocol, String hostname, int port, String path) {
return String.format("%s://%s:%s%s", protocol, hostname, port, path);
}
}