From 4770a7a95c7e3ff355c1f208faa607a4621ad0ef Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Tue, 10 Dec 2024 16:04:10 -0500 Subject: [PATCH] Makes ApplicationPath a bean defining annotation. New functional test that shows support for this new feature. --- .../cdi/HelidonContainerImpl.java | 1 + tests/functional/jax-rs-bda/pom.xml | 52 +++++++++++++ .../tests/functional/bda/HelloWorld1.java | 28 +++++++ .../tests/functional/bda/HelloWorld2.java | 28 +++++++ .../tests/functional/bda/HelloWorld3.java | 32 ++++++++ .../tests/functional/bda/HelloWorldApp1.java | 34 +++++++++ .../tests/functional/bda/HelloWorldApp2.java | 36 +++++++++ .../src/main/resources/META-INF/beans.xml | 25 +++++++ .../META-INF/microprofile-config.properties | 17 +++++ .../tests/functional/bda/ResourcesTest.java | 74 +++++++++++++++++++ 10 files changed, 327 insertions(+) create mode 100644 tests/functional/jax-rs-bda/pom.xml create mode 100644 tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld1.java create mode 100644 tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld2.java create mode 100644 tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld3.java create mode 100644 tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp1.java create mode 100644 tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp2.java create mode 100644 tests/functional/jax-rs-bda/src/main/resources/META-INF/beans.xml create mode 100644 tests/functional/jax-rs-bda/src/main/resources/META-INF/microprofile-config.properties create mode 100644 tests/functional/jax-rs-bda/src/test/java/io/helidon/tests/functional/bda/ResourcesTest.java diff --git a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerImpl.java b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerImpl.java index 03670cd4dd4..363ccfae70c 100644 --- a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerImpl.java +++ b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerImpl.java @@ -151,6 +151,7 @@ private HelidonContainerImpl init() { LOGGER.fine(() -> "Initializing CDI container " + id); addHelidonBeanDefiningAnnotations("jakarta.ws.rs.Path", + "jakarta.ws.rs.ApplicationPath", "jakarta.ws.rs.ext.Provider", "jakarta.websocket.server.ServerEndpoint", "org.eclipse.microprofile.graphql.GraphQLApi", diff --git a/tests/functional/jax-rs-bda/pom.xml b/tests/functional/jax-rs-bda/pom.xml new file mode 100644 index 00000000000..4546af68ffd --- /dev/null +++ b/tests/functional/jax-rs-bda/pom.xml @@ -0,0 +1,52 @@ + + + + 4.0.0 + + io.helidon.tests + helidon-tests-project + 4.2.0-SNAPSHOT + ../../pom.xml + + + io.helidon.webserver.tests + helidon-tests-functional-jax-rs-bda + + + + io.helidon.microprofile.bundles + helidon-microprofile-core + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + \ No newline at end of file diff --git a/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld1.java b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld1.java new file mode 100644 index 00000000000..99703c594ef --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld1.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +@Path("/greet1") +public class HelloWorld1 { + + @GET + public String hello() { + return "hello1"; + } +} diff --git a/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld2.java b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld2.java new file mode 100644 index 00000000000..a878292dec6 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld2.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +@Path("/greet2") +public class HelloWorld2 { + + @GET + public String hello() { + return "hello2"; + } +} diff --git a/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld3.java b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld3.java new file mode 100644 index 00000000000..996730fa0e4 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorld3.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +/** + * This resource will not be available given that a synthetic app is not + * created when other apps exist. Note that {@link Path} is also a BDA. + */ +@Path("/greet3") +public class HelloWorld3 { + + @GET + public String hello() { + return "hello3"; + } +} diff --git a/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp1.java b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp1.java new file mode 100644 index 00000000000..41645ee9442 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp1.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import java.util.Set; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +/** + * Use {@link ApplicationPath} as a bean-defining annotation (BDA). Note that + * there is no CDI scope annotation on the class. + */ +@ApplicationPath("app1") +public class HelloWorldApp1 extends Application { + + @Override + public Set> getClasses() { + return Set.of(HelloWorld1.class); + } +} diff --git a/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp2.java b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp2.java new file mode 100644 index 00000000000..766835d87cf --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/java/io/helidon/tests/functional/bda/HelloWorldApp2.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import java.util.Set; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +/** + * Even though {@link ApplicationPath} is a bean-defining annotation, this + * class is also given an explicit application scope. + */ +@ApplicationScoped +@ApplicationPath("app2") +public class HelloWorldApp2 extends Application { + + @Override + public Set> getClasses() { + return Set.of(HelloWorld2.class); + } +} diff --git a/tests/functional/jax-rs-bda/src/main/resources/META-INF/beans.xml b/tests/functional/jax-rs-bda/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000..52f89a20d18 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + diff --git a/tests/functional/jax-rs-bda/src/main/resources/META-INF/microprofile-config.properties b/tests/functional/jax-rs-bda/src/main/resources/META-INF/microprofile-config.properties new file mode 100644 index 00000000000..b100c594e52 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/main/resources/META-INF/microprofile-config.properties @@ -0,0 +1,17 @@ +# +# Copyright (c) 2024 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +server.host=0.0.0.0 \ No newline at end of file diff --git a/tests/functional/jax-rs-bda/src/test/java/io/helidon/tests/functional/bda/ResourcesTest.java b/tests/functional/jax-rs-bda/src/test/java/io/helidon/tests/functional/bda/ResourcesTest.java new file mode 100644 index 00000000000..62fc33f9b22 --- /dev/null +++ b/tests/functional/jax-rs-bda/src/test/java/io/helidon/tests/functional/bda/ResourcesTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.tests.functional.bda; + +import io.helidon.microprofile.testing.junit5.HelidonTest; + +import jakarta.inject.Inject; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@HelidonTest +class ResourcesTest { + + @Inject + private WebTarget target; + + /** + * Test that resource {@code "app1/greet1"} exists as part of + * {@link io.helidon.tests.functional.bda.HelloWorld1}. + */ + @Test + void testHelloWorld1() { + Response response = target.path("app1") + .path("greet1") + .request() + .get(); + assertThat(response.getStatus(), is(200)); + assertThat(response.readEntity(String.class), is("hello1")); + } + + /** + * Test that resource {@code "app2/greet2"} exists as part of + * {@link io.helidon.tests.functional.bda.HelloWorld2}. + */ + @Test + void testHelloWorld2() { + Response response = target.path("app2") + .path("greet2") + .request() + .get(); + assertThat(response.getStatus(), is(200)); + assertThat(response.readEntity(String.class), is("hello2")); + } + + /** + * Test that resource {@code "app3/greet3"} does not exist since no + * synthetic app shall be created in this case. + */ + @Test + void testHelloWorld3() { + Response response = target.path("/") + .path("greet3") + .request() + .get(); + assertThat(response.getStatus(), is(404)); + } +}