From ebebcbcf57b19847c13fff664bf9b37e6b2a4015 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Thu, 13 Jun 2024 13:13:20 +0200 Subject: [PATCH] Test Framework container provider (Helidon 4.x) Signed-off-by: Maxim Nesen --- containers/helidon/pom.xml | 9 +- .../jersey/helidon/HelidonHttpContainer.java | 23 +++-- .../helidon/HelidonHttpContainerBuilder.java | 80 +++++++++++++++++ .../helidon/HelidonHttpContainerFactory.java | 2 +- .../helidon/HelidonHttpContainerProvider.java | 2 +- pom.xml | 1 + test-framework/providers/helidon-http/pom.xml | 48 ++++++++++ .../helidon/HelidonTestContainerFactory.java | 88 +++++++++++++++++++ .../jersey/test/helidon/package-info.java | 22 +++++ ...sfish.jersey.test.spi.TestContainerFactory | 1 + .../helidon/AvailablePortHelidonTest.java | 66 ++++++++++++++ .../jersey/test/helidon/BaseUriTest.java | 73 +++++++++++++++ test-framework/providers/pom.xml | 1 + 13 files changed, 405 insertions(+), 11 deletions(-) create mode 100644 containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerBuilder.java create mode 100644 test-framework/providers/helidon-http/pom.xml create mode 100644 test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/HelidonTestContainerFactory.java create mode 100644 test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/package-info.java create mode 100644 test-framework/providers/helidon-http/src/main/resources/META-INF/services/org.glassfish.jersey.test.spi.TestContainerFactory create mode 100644 test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/AvailablePortHelidonTest.java create mode 100644 test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/BaseUriTest.java diff --git a/containers/helidon/pom.xml b/containers/helidon/pom.xml index de3fb36aa0..fd6db25f42 100644 --- a/containers/helidon/pom.xml +++ b/containers/helidon/pom.xml @@ -26,7 +26,7 @@ 3.1.99-SNAPSHOT - jersey-container-helidon + jersey-container-helidon-http jar jersey-container-helidon @@ -47,21 +47,22 @@ jakarta.activation jakarta.activation-api + test io.helidon.webserver helidon-webserver - 4.0.9 + ${helidon.container.version} io.helidon.tracing helidon-tracing - 4.0.9 + ${helidon.container.version} io.helidon.http helidon-http - 4.0.9 + ${helidon.container.version} org.slf4j diff --git a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainer.java b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainer.java index c99dd1370c..c0a032372f 100644 --- a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainer.java +++ b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainer.java @@ -26,18 +26,31 @@ import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spi.Container; +import java.net.URI; + public class HelidonHttpContainer implements Container, WebServer { + private static final int DEFAULT_PORT = 8080; + private static final String DEFAULT_HOST = "0.0.0.0"; + private final WebServer webServer; private ApplicationHandler applicationHandler; - HelidonHttpContainer(Application application) { + HelidonHttpContainer(URI baseUri, Application application) { this.applicationHandler = new ApplicationHandler(application, new WebServerBinder()); - this.webServer = WebServer.builder().port(8080).routing( - HttpRouting.builder().register( - JerseySupport.create(this) - )).build(); + int port = baseUri == null ? DEFAULT_PORT : baseUri.getPort(); + final String host = baseUri == null ? DEFAULT_HOST : baseUri.getHost(); + final String path = baseUri == null ? null : baseUri.getPath(); + final HttpRouting.Builder routingBuilder = path == null ? HttpRouting.builder().register( + JerseySupport.create(this) + ) : HttpRouting.builder().register(path, + JerseySupport.create(this) + ); + this.webServer = WebServer.builder() + .port(port) + .host(host) + .routing(routingBuilder).build(); } @Override diff --git a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerBuilder.java b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerBuilder.java new file mode 100644 index 0000000000..2afe5b0dc4 --- /dev/null +++ b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerBuilder.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + * + */ + +package org.glassfish.jersey.helidon; + +import jakarta.ws.rs.core.Application; +import jakarta.ws.rs.core.UriBuilder; + +import java.net.URI; + +/** + * Helidon container builder + */ +class HelidonHttpContainerBuilder { + + private URI baseUri; + + private Application application; + + private String host; + + private String path; + + private int port; + + private HelidonHttpContainerBuilder() { + } + + + + public static HelidonHttpContainerBuilder builder() { + return new HelidonHttpContainerBuilder(); + } + + public HelidonHttpContainerBuilder withUri(URI baseUri) { + this.baseUri = baseUri; + return this; + } + + public HelidonHttpContainerBuilder withApplication(Application application) { + this.application = application; + return this; + } + + public HelidonHttpContainerBuilder withPort(int port) { + this.port = port; + return this; + } + + public HelidonHttpContainerBuilder withHost(String host) { + this.host = host; + return this; + } + + public HelidonHttpContainerBuilder withPath(String path) { + this.path = path; + return this; + } + + public HelidonHttpContainer build() { + if (this.baseUri == null) { + baseUri = UriBuilder.fromPath(path).port(port).host(host).build(); + } + return new HelidonHttpContainer(baseUri, application); + } +} diff --git a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerFactory.java b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerFactory.java index 6f2e70790b..e767649e0f 100644 --- a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerFactory.java +++ b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerFactory.java @@ -27,6 +27,6 @@ private HelidonHttpContainerFactory() { } public static WebServer createServer(URI baseUri, ResourceConfig config) { - return new HelidonHttpContainer(config); + return new HelidonHttpContainer(baseUri, config); } } diff --git a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerProvider.java b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerProvider.java index 657b5896bc..999c8000fe 100644 --- a/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerProvider.java +++ b/containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerProvider.java @@ -27,6 +27,6 @@ public T createContainer(Class type, Application application) throws Proc if (type != WebServer.class && type != HelidonHttpContainer.class) { return null; } - return type.cast(new HelidonHttpContainer(application)); + return type.cast(new HelidonHttpContainer(null, application)); } } diff --git a/pom.xml b/pom.xml index 66f4d9f2d1..9c0e59dc06 100644 --- a/pom.xml +++ b/pom.xml @@ -2152,6 +2152,7 @@ 3.0.3 3.0.1 3.2.6 + 4.0.10 3.2.6 1.4.14 3.7.1 diff --git a/test-framework/providers/helidon-http/pom.xml b/test-framework/providers/helidon-http/pom.xml new file mode 100644 index 0000000000..f2492eb545 --- /dev/null +++ b/test-framework/providers/helidon-http/pom.xml @@ -0,0 +1,48 @@ + + + + + 4.0.0 + + + org.glassfish.jersey.test-framework.providers + project + 3.1.99-SNAPSHOT + + + jersey-test-framework-provider-helidon + jar + jersey-test-framework-provider-helidon + + Jersey Test Framework - Helidon (4.x) container + + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + ${project.version} + + + org.glassfish.jersey.containers + jersey-container-helidon-http + ${project.version} + + + + diff --git a/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/HelidonTestContainerFactory.java b/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/HelidonTestContainerFactory.java new file mode 100644 index 0000000000..1527802ec9 --- /dev/null +++ b/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/HelidonTestContainerFactory.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.test.helidon; + +import io.helidon.webserver.WebServer; +import jakarta.ws.rs.core.UriBuilder; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.helidon.HelidonHttpContainerFactory; +import org.glassfish.jersey.test.DeploymentContext; +import org.glassfish.jersey.test.spi.TestContainer; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.glassfish.jersey.test.spi.TestHelper; + +import javax.net.ssl.SSLParameters; +import java.net.URI; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class HelidonTestContainerFactory implements TestContainerFactory { + + private static class HelidonTestContainer implements TestContainer { + + private static final Logger LOGGER = Logger.getLogger(HelidonTestContainer.class.getName()); + + private URI baseUri; + + private final WebServer server; + + private HelidonTestContainer(final URI baseUri, final DeploymentContext context) { + this.baseUri = UriBuilder.fromUri(baseUri).path(context.getContextPath()).build(); + + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("Creating HelidonTestContainer configured at the base URI " + + TestHelper.zeroPortToAvailablePort(baseUri)); + } + + if (context.getSslContext().isPresent() && context.getSslParameters().isPresent()) { + final SSLParameters sslParameters = context.getSslParameters().get(); + this.server = HelidonHttpContainerFactory.createServer( + this.baseUri, context.getResourceConfig()); + } else { + this.server = HelidonHttpContainerFactory.createServer(this.baseUri, context.getResourceConfig()); + } + } + + @Override + public ClientConfig getClientConfig() { + return null; + } + + @Override + public URI getBaseUri() { + return baseUri; + } + + @Override + public void start() { + if (!server.isRunning()) { + server.start(); + } + } + + @Override + public void stop() { + if (server.isRunning()) { + server.stop(); + } + } + } + @Override + public TestContainer create(URI baseUri, DeploymentContext deploymentContext) { + return new HelidonTestContainer(baseUri, deploymentContext); + } +} diff --git a/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/package-info.java b/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/package-info.java new file mode 100644 index 0000000000..651a5f4190 --- /dev/null +++ b/test-framework/providers/helidon-http/src/main/java/org/glassfish/jersey/test/helidon/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + * + */ + +/** + * Test Framework Jersey container provider based on Helidon 4.x. + */ + +package org.glassfish.jersey.test.helidon; \ No newline at end of file diff --git a/test-framework/providers/helidon-http/src/main/resources/META-INF/services/org.glassfish.jersey.test.spi.TestContainerFactory b/test-framework/providers/helidon-http/src/main/resources/META-INF/services/org.glassfish.jersey.test.spi.TestContainerFactory new file mode 100644 index 0000000000..375a254ed1 --- /dev/null +++ b/test-framework/providers/helidon-http/src/main/resources/META-INF/services/org.glassfish.jersey.test.spi.TestContainerFactory @@ -0,0 +1 @@ +org.glassfish.jersey.test.helidon.HelidonTestContainerFactory diff --git a/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/AvailablePortHelidonTest.java b/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/AvailablePortHelidonTest.java new file mode 100644 index 0000000000..e7bdb0eebc --- /dev/null +++ b/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/AvailablePortHelidonTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.test.helidon; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.DeploymentContext; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Tests finding an available port for container. + * + * @author Michal Gajdos + */ +public class AvailablePortHelidonTest extends JerseyTest { + + @Override + protected TestContainerFactory getTestContainerFactory() { + return new HelidonTestContainerFactory(); + } + + @Path("AvailablePortHelidonTest") + public static class TestResource { + @GET + public String get() { + return "GET"; + } + } + + @Override + protected DeploymentContext configureDeployment() { + forceSet(TestProperties.CONTAINER_PORT, "8080"); + + return DeploymentContext.builder(new ResourceConfig(TestResource.class)).build(); + } + + @Test + public void testGet() { + assertThat(target().getUri().getPort(), not(0)); + assertThat(getBaseUri().getPort(), not(0)); + + assertThat(target("AvailablePortHelidonTest").request().get(String.class), equalTo("GET")); + } +} diff --git a/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/BaseUriTest.java b/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/BaseUriTest.java new file mode 100644 index 0000000000..820554284d --- /dev/null +++ b/test-framework/providers/helidon-http/src/test/java/org/glassfish/jersey/test/helidon/BaseUriTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.test.helidon; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.DeploymentContext; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BaseUriTest extends JerseyTest { + + @Override + protected TestContainerFactory getTestContainerFactory() { + return new HelidonTestContainerFactory(); + } + + @Path("root") + public static class TestResource { + @GET + public String get() { + return "GET"; + } + + @Path("sub") + @GET + public String getSub() { + return "sub"; + } + } + + @Override + protected DeploymentContext configureDeployment() { + return DeploymentContext.builder(new ResourceConfig(TestResource.class)) + .contextPath("context1/context2") + .build(); + } + + @Test + public void testGet() { + final WebTarget target = target("root"); + + final String s = target.request().get(String.class); + Assertions.assertEquals("GET", s); + } + + @Test + public void testGetSub() { + final WebTarget target = target("root/sub"); + + final String s = target.request().get(String.class); + Assertions.assertEquals("sub", s); + } + +} diff --git a/test-framework/providers/pom.xml b/test-framework/providers/pom.xml index 1bb01c3d56..ecbdd42d1a 100644 --- a/test-framework/providers/pom.xml +++ b/test-framework/providers/pom.xml @@ -41,6 +41,7 @@ jdk-http jetty jetty-http2 + helidon-http netty simple