mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
Integration tests for org.openhab.core.io.net (#3460)
* [org.openhab.core.io.net] add itests Co-authored-by: Wouter Born <github@maindrain.net> Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
parent
8494bf1199
commit
4cbc0e613e
@ -183,6 +183,12 @@
|
||||
<version>${jetty.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty.http2</groupId>
|
||||
<artifactId>http2-server</artifactId>
|
||||
<version>${jetty.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- JmDNS -->
|
||||
<dependency>
|
||||
|
@ -757,6 +757,12 @@
|
||||
<version>${jetty.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty.http2</groupId>
|
||||
<artifactId>http2-server</artifactId>
|
||||
<version>${jetty.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-alpn-java-client</artifactId>
|
||||
|
@ -25,5 +25,10 @@
|
||||
<attribute name="annotationpath" value="target/dependency"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
29
itests/org.openhab.core.io.net.tests/.classpath
Normal file
29
itests/org.openhab.core.io.net.tests/.classpath
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="annotationpath" value="target/dependency"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="annotationpath" value="target/dependency"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
23
itests/org.openhab.core.io.net.tests/.project
Normal file
23
itests/org.openhab.core.io.net.tests/.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>org.openhab.core.io.net.tests</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
14
itests/org.openhab.core.io.net.tests/NOTICE
Normal file
14
itests/org.openhab.core.io.net.tests/NOTICE
Normal file
@ -0,0 +1,14 @@
|
||||
This content is produced and maintained by the openHAB project.
|
||||
|
||||
* Project home: https://www.openhab.org
|
||||
|
||||
== Declared Project Licenses
|
||||
|
||||
This program and the accompanying materials are made available under the terms
|
||||
of the Eclipse Public License 2.0 which is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/.
|
||||
|
||||
== Source Code
|
||||
|
||||
https://github.com/openhab/openhab-core
|
||||
|
76
itests/org.openhab.core.io.net.tests/itest.bndrun
Normal file
76
itests/org.openhab.core.io.net.tests/itest.bndrun
Normal file
@ -0,0 +1,76 @@
|
||||
-include: ../itest-common.bndrun
|
||||
|
||||
Bundle-SymbolicName: ${project.artifactId}
|
||||
Fragment-Host: org.openhab.core.io.net
|
||||
|
||||
-runrequires: \
|
||||
bnd.identity;id='org.openhab.core.io.net.tests',\
|
||||
bnd.identity;id='org.eclipse.jetty.http2.hpack',\
|
||||
bnd.identity;id='org.eclipse.jetty.websocket.server',\
|
||||
bnd.identity;id='org.apache.aries.spifly.dynamic.bundle'
|
||||
|
||||
-runbundles: \
|
||||
biz.aQute.tester.junit-platform;version='[6.4.0,6.4.1)',\
|
||||
ch.qos.logback.classic;version='[1.2.11,1.2.12)',\
|
||||
ch.qos.logback.core;version='[1.2.11,1.2.12)',\
|
||||
com.google.gson;version='[2.9.1,2.9.2)',\
|
||||
com.sun.jna;version='[5.12.1,5.12.2)',\
|
||||
com.sun.xml.bind.jaxb-osgi;version='[2.3.3,2.3.4)',\
|
||||
io.methvin.directory-watcher;version='[0.17.1,0.17.2)',\
|
||||
jakarta.annotation-api;version='[2.0.0,2.0.1)',\
|
||||
jakarta.inject.jakarta.inject-api;version='[2.0.0,2.0.1)',\
|
||||
jakarta.xml.bind-api;version='[2.3.3,2.3.4)',\
|
||||
javax.measure.unit-api;version='[2.1.2,2.1.3)',\
|
||||
junit-jupiter-api;version='[5.9.2,5.9.3)',\
|
||||
junit-jupiter-engine;version='[5.9.2,5.9.3)',\
|
||||
junit-platform-commons;version='[1.9.2,1.9.3)',\
|
||||
junit-platform-engine;version='[1.9.2,1.9.3)',\
|
||||
junit-platform-launcher;version='[1.9.2,1.9.3)',\
|
||||
org.apache.felix.configadmin;version='[1.9.26,1.9.27)',\
|
||||
org.apache.felix.http.servlet-api;version='[1.2.0,1.2.1)',\
|
||||
org.apache.felix.scr;version='[2.2.4,2.2.5)',\
|
||||
org.apache.servicemix.specs.activation-api-1.2.1;version='[1.2.1,1.2.2)',\
|
||||
org.apache.servicemix.specs.annotation-api-1.3;version='[1.3.0,1.3.1)',\
|
||||
org.eclipse.equinox.event;version='[1.4.300,1.4.301)',\
|
||||
org.eclipse.jetty.alpn.client;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.client;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.http;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.http2.client;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.http2.common;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.http2.hpack;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.io;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.security;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.server;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.servlet;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.util;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.util.ajax;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.websocket.api;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.websocket.client;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.websocket.common;version='[9.4.50,9.4.51)',\
|
||||
org.glassfish.hk2.external.javax.inject;version='[2.4.0,2.4.1)',\
|
||||
org.glassfish.hk2.osgi-resource-locator;version='[1.0.3,1.0.4)',\
|
||||
org.hamcrest;version='[2.2.0,2.2.1)',\
|
||||
org.openhab.core;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.net;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.net.tests;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.test;version='[4.0.0,4.0.1)',\
|
||||
org.opentest4j;version='[1.2.0,1.2.1)',\
|
||||
org.ops4j.pax.logging.pax-logging-api;version='[2.2.0,2.2.1)',\
|
||||
org.osgi.service.cm;version='[1.6.0,1.6.1)',\
|
||||
org.osgi.service.component;version='[1.5.0,1.5.1)',\
|
||||
org.osgi.service.event;version='[1.4.0,1.4.1)',\
|
||||
org.osgi.util.function;version='[1.2.0,1.2.1)',\
|
||||
org.osgi.util.promise;version='[1.2.0,1.2.1)',\
|
||||
si-units;version='[2.1.0,2.1.1)',\
|
||||
si.uom.si-quantity;version='[2.1.0,2.1.1)',\
|
||||
tech.units.indriya;version='[2.1.2,2.1.3)',\
|
||||
uom-lib-common;version='[2.1.0,2.1.1)',\
|
||||
org.eclipse.jetty.http2.server;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.websocket.servlet;version='[9.4.50,9.4.51)',\
|
||||
org.apache.aries.spifly.dynamic.bundle;version='[1.3.4,1.3.5)',\
|
||||
org.eclipse.jetty.websocket.server;version='[9.4.50,9.4.51)',\
|
||||
org.objectweb.asm;version='[9.4.0,9.4.1)',\
|
||||
org.objectweb.asm.commons;version='[9.2.0,9.2.1)',\
|
||||
org.objectweb.asm.tree;version='[9.2.0,9.2.1)',\
|
||||
org.objectweb.asm.tree.analysis;version='[9.2.0,9.2.1)',\
|
||||
org.objectweb.asm.util;version='[9.2.0,9.2.1)'
|
17
itests/org.openhab.core.io.net.tests/pom.xml
Normal file
17
itests/org.openhab.core.io.net.tests/pom.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.openhab.core.itests</groupId>
|
||||
<artifactId>org.openhab.core.reactor.itests</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>org.openhab.core.io.net.tests</artifactId>
|
||||
|
||||
<name>openHAB Core :: Integration Tests :: Network I/O Tests</name>
|
||||
|
||||
</project>
|
@ -0,0 +1,189 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.client.api.ContentResponse;
|
||||
import org.eclipse.jetty.http.HttpFields;
|
||||
import org.eclipse.jetty.http.HttpMethod;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.eclipse.jetty.http.HttpURI;
|
||||
import org.eclipse.jetty.http.HttpVersion;
|
||||
import org.eclipse.jetty.http.MetaData;
|
||||
import org.eclipse.jetty.http2.api.Session;
|
||||
import org.eclipse.jetty.http2.api.Stream;
|
||||
import org.eclipse.jetty.http2.client.HTTP2Client;
|
||||
import org.eclipse.jetty.http2.frames.HeadersFrame;
|
||||
import org.eclipse.jetty.util.Promise.Completable;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.core.io.net.http.HttpClientFactory;
|
||||
import org.openhab.core.io.net.http.WebSocketFactory;
|
||||
import org.openhab.core.io.net.tests.internal.TestServer;
|
||||
import org.openhab.core.io.net.tests.internal.TestStreamAdapter;
|
||||
import org.openhab.core.io.net.tests.internal.TestWebSocket;
|
||||
import org.openhab.core.test.TestPortUtil;
|
||||
import org.openhab.core.test.java.JavaOSGiTest;
|
||||
|
||||
/**
|
||||
* Tests for HttpClientFactory and WebSocketFactory implementations.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class ClientFactoryTest extends JavaOSGiTest {
|
||||
public static final String RESPONSE = "response";
|
||||
|
||||
private static final String CONSUMER = "consumer";
|
||||
private static final String HOST = "127.0.0.1";
|
||||
private static final int WAIT_SECONDS = 10;
|
||||
private static final long CONNECTION_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(WAIT_SECONDS);
|
||||
|
||||
private static @Nullable TestServer server;
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() throws Exception {
|
||||
TestServer testServer = server;
|
||||
if (testServer != null) {
|
||||
testServer.stopServer();
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeAll() throws Exception {
|
||||
TestServer testServer = new TestServer(HOST, TestPortUtil.findFreePort());
|
||||
testServer.startServer();
|
||||
server = testServer;
|
||||
}
|
||||
|
||||
private TestServer getServer() {
|
||||
return Objects.requireNonNull(server);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHttp1Client() throws Exception {
|
||||
HttpClientFactory httpClientFactory = getService(HttpClientFactory.class);
|
||||
assertNotNull(httpClientFactory);
|
||||
|
||||
HttpClient client = httpClientFactory.createHttpClient(CONSUMER);
|
||||
assertNotNull(client);
|
||||
|
||||
try {
|
||||
// start client
|
||||
client.setConnectTimeout(CONNECTION_TIMEOUT_MS);
|
||||
client.start();
|
||||
|
||||
// send request
|
||||
ContentResponse response = client.GET(getServer().getHttpUri());
|
||||
|
||||
// check response
|
||||
assertEquals(HttpStatus.OK_200, response.getStatus());
|
||||
assertEquals(RESPONSE, response.getContentAsString());
|
||||
} finally {
|
||||
try {
|
||||
// stop client
|
||||
client.stop();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHttp2Client() throws Exception {
|
||||
HttpClientFactory httpClientFactory = getService(HttpClientFactory.class);
|
||||
assertNotNull(httpClientFactory);
|
||||
|
||||
HTTP2Client client = httpClientFactory.createHttp2Client(CONSUMER);
|
||||
assertNotNull(client);
|
||||
|
||||
Completable<@Nullable Session> sessionCompletable = new Completable<>();
|
||||
Completable<@Nullable Stream> streamCompletable = new Completable<>();
|
||||
|
||||
TestStreamAdapter streamAdapter = new TestStreamAdapter();
|
||||
|
||||
MetaData.Request request = new MetaData.Request(HttpMethod.GET.toString(),
|
||||
new HttpURI(getServer().getHttpUri()), HttpVersion.HTTP_2, new HttpFields());
|
||||
|
||||
HeadersFrame headers = new HeadersFrame(request, null, true);
|
||||
|
||||
try {
|
||||
// start client
|
||||
client.setConnectTimeout(CONNECTION_TIMEOUT_MS);
|
||||
client.start();
|
||||
|
||||
// establish session
|
||||
client.connect(new InetSocketAddress(getServer().getHost(), getServer().getPort()),
|
||||
new Session.Listener.Adapter(), sessionCompletable);
|
||||
Session session = sessionCompletable.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
assertNotNull(session);
|
||||
assertFalse(session.isClosed());
|
||||
|
||||
// open stream
|
||||
session.newStream(headers, streamCompletable, streamAdapter);
|
||||
Stream stream = streamCompletable.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
assertNotNull(stream);
|
||||
|
||||
// check response
|
||||
String response = streamAdapter.completable.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
assertEquals(RESPONSE, response);
|
||||
} finally {
|
||||
try {
|
||||
// stop client
|
||||
client.stop();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWebSocketClient() throws Exception {
|
||||
WebSocketFactory webSocketFactory = getService(WebSocketFactory.class);
|
||||
assertNotNull(webSocketFactory);
|
||||
|
||||
WebSocketClient client = webSocketFactory.createWebSocketClient(CONSUMER);
|
||||
assertNotNull(client);
|
||||
|
||||
try {
|
||||
// start client
|
||||
client.setConnectTimeout(CONNECTION_TIMEOUT_MS);
|
||||
client.start();
|
||||
|
||||
// create session future
|
||||
TestWebSocket webSocket = new TestWebSocket();
|
||||
Future<org.eclipse.jetty.websocket.api.Session> sessionFuture = client.connect(webSocket,
|
||||
getServer().getWebSocketUri());
|
||||
assertNotNull(sessionFuture);
|
||||
|
||||
// check response
|
||||
String response = webSocket.completable.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
assertEquals(RESPONSE, response);
|
||||
} finally {
|
||||
try {
|
||||
// stop client
|
||||
client.stop();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.core.io.net.tests.ClientFactoryTest;
|
||||
|
||||
/**
|
||||
* Servlet for org.openhab.core.io.net tests.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class TestHttpServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.setContentType(MediaType.TEXT_PLAIN);
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
|
||||
response.getWriter().write(ClientFactoryTest.RESPONSE);
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
|
||||
import org.eclipse.jetty.server.HttpConfiguration;
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
/**
|
||||
* Embedded jetty server used in the tests.
|
||||
*
|
||||
* Based on {@code TestServer} of the FS Internet Radio Binding.
|
||||
*
|
||||
* @author Velin Yordanov - Initial contribution
|
||||
* @author Wouter Born - Increase test coverage
|
||||
* @author Andrew Fiddian-Green - Adapted for org.openhab.core.io.net.tests
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class TestServer {
|
||||
private static final String SERVLET_PATH = "/servlet";
|
||||
private static final String WEBSOCKET_PATH = "/ws";
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final Server server;
|
||||
|
||||
public TestServer(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.server = new Server();
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public URI getHttpUri() {
|
||||
return URI.create("http://" + host + ":" + port + SERVLET_PATH);
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public URI getWebSocketUri() {
|
||||
return URI.create("ws://" + host + ":" + port + WEBSOCKET_PATH);
|
||||
}
|
||||
|
||||
public void startServer() throws Exception {
|
||||
ServletHandler handler = new ServletHandler();
|
||||
handler.addServletWithMapping(new ServletHolder(new TestHttpServlet()), SERVLET_PATH);
|
||||
handler.addServletWithMapping(new ServletHolder(new TestWebSocketServlet()), WEBSOCKET_PATH);
|
||||
server.setHandler(handler);
|
||||
|
||||
HttpConfiguration httpConfig = new HttpConfiguration();
|
||||
HttpConnectionFactory h1 = new HttpConnectionFactory(httpConfig);
|
||||
HTTP2CServerConnectionFactory h2c = new HTTP2CServerConnectionFactory(httpConfig);
|
||||
|
||||
ServerConnector connector = new ServerConnector(server, h1, h2c);
|
||||
connector.setHost(host);
|
||||
connector.setPort(port);
|
||||
server.addConnector(connector);
|
||||
|
||||
server.start();
|
||||
}
|
||||
|
||||
public void stopServer() throws Exception {
|
||||
server.stop();
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.http2.api.Stream;
|
||||
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||
import org.eclipse.jetty.util.Callback;
|
||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
|
||||
/**
|
||||
* HTTP/2 stream adapter for org.openhab.core.io.net tests.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@WebSocket
|
||||
@NonNullByDefault
|
||||
public class TestStreamAdapter extends Stream.Listener.Adapter {
|
||||
public final CompletableFuture<String> completable = new CompletableFuture<>();
|
||||
|
||||
@Override
|
||||
public void onData(@Nullable Stream stream, @Nullable DataFrame frame, @Nullable Callback callback) {
|
||||
assertNotNull(stream);
|
||||
assertNotNull(frame);
|
||||
assertTrue(frame.isEndStream());
|
||||
completable.complete(StandardCharsets.UTF_8.decode(frame.getData()).toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
|
||||
/**
|
||||
* WebSocket implementer class for org.openhab.core.io.net tests.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@WebSocket
|
||||
@NonNullByDefault
|
||||
public class TestWebSocket {
|
||||
public final CompletableFuture<String> completable = new CompletableFuture<>();
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(String message) {
|
||||
completable.complete(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
|
||||
import org.openhab.core.io.net.tests.ClientFactoryTest;
|
||||
|
||||
/**
|
||||
* WebSocket for org.openhab.core.io.net tests.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class TestWebSocketAdapter extends WebSocketAdapter {
|
||||
|
||||
@Override
|
||||
public void onWebSocketConnect(@Nullable Session session) {
|
||||
super.onWebSocketConnect(session);
|
||||
try {
|
||||
getRemote().sendString(ClientFactoryTest.RESPONSE);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* 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.openhab.core.io.net.tests.internal;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
||||
|
||||
/**
|
||||
* Servlet for org.openhab.core.io.net tests.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class TestWebSocketServlet extends WebSocketServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public void configure(@Nullable WebSocketServletFactory factory) {
|
||||
if (factory != null) {
|
||||
factory.register(TestWebSocketAdapter.class);
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@
|
||||
<module>org.openhab.core.config.discovery.usbserial.tests</module>
|
||||
<module>org.openhab.core.config.dispatch.tests</module>
|
||||
<module>org.openhab.core.ephemeris.tests</module>
|
||||
<module>org.openhab.core.io.net.tests</module>
|
||||
<module>org.openhab.core.io.rest.core.tests</module>
|
||||
<module>org.openhab.core.model.item.tests</module>
|
||||
<module>org.openhab.core.model.rule.tests</module>
|
||||
|
Loading…
Reference in New Issue
Block a user