diff --git a/docs/mp/testing-ng.adoc b/docs/mp/testing-ng.adoc index 280d6fb98e5..d1ffb80c488 100644 --- a/docs/mp/testing-ng.adoc +++ b/docs/mp/testing-ng.adoc @@ -110,32 +110,86 @@ In addition to this simplification, the following annotations are supported: |`@io.helidon.microprofile.testing.testng.DisableDiscovery` |Used to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` +a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class: + +* `ServerCdiExtension` +* `JaxRsCdiExtension` +* `CdiComponentProvider` +* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes` +* `org.glassfish.jersey.weld.se.WeldRequestScope` |=== == Examples -In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. +In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. [source,java] .Code sample ---- -@HelidonTest -@DisableDiscovery -@AddBean(MyBean.class) -@AddExtension(ConfigCdiExtension.class) -@AddConfig(key = "app.greeting", value = "TestHello") +@HelidonTest <1> +@DisableDiscovery <2> +@AddBean(MyBean.class) <3> +@AddExtension(ConfigCdiExtension.class) <4> +@AddConfig(key = "app.greeting", value = "TestHello") <5> class TestExample { @Inject - private MyBean myBean; + private MyBean myBean; <6> @Test - void testGreeting() { + void testGreeting() { <7> assertThat(myBean, notNullValue()); assertThat(myBean.greeting(), is("TestHello")); } } ---- +<1> Start the Helidon container. +<2> Set disabled Bean Discovery for the current test class. +<3> Add `MyBean` to current context. +<4> Add a configuration CDI extension to the current test. +<5> Add configuration properties. +<6> Inject `MyBean` as it is available in the CDI context. +<7> Run rests. + +To test `@RequestScoped` bean with JaxRs support: + +[source,java] +.Test `RequestScoped` bean +---- + +@HelidonTest <1> +@DisableDiscovery <2> + +@AddJaxRs <3> +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped <4> + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} +---- +<1> Start the Helidon container. +<2> Set disabled Bean discovery. +<3> Add JaxRs support to the current test class. +<4> Define a `RequestScoped` bean. == Reference diff --git a/docs/mp/testing.adoc b/docs/mp/testing.adoc index 5387490ec28..e3ac24975ab 100644 --- a/docs/mp/testing.adoc +++ b/docs/mp/testing.adoc @@ -115,32 +115,89 @@ In addition to this simplification, the following annotations are supported: |Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery` |to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery` +|to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` +a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class: + +* `ServerCdiExtension` +* `JaxRsCdiExtension` +* `CdiComponentProvider` +* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes` +* `org.glassfish.jersey.weld.se.WeldRequestScope` |=== == Examples -In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. +In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. [source,java] .Code sample ---- -@HelidonTest -@DisableDiscovery -@AddBean(MyBean.class) -@AddExtension(ConfigCdiExtension.class) -@AddConfig(key = "app.greeting", value = "TestHello") +@HelidonTest <1> +@DisableDiscovery <2> +@AddBean(MyBean.class) <3> +@AddExtension(ConfigCdiExtension.class) <4> +@AddConfig(key = "app.greeting", value = "TestHello") <5> class TestExample { @Inject - private MyBean myBean; + private MyBean myBean; <6> @Test - void testGreeting() { + void testGreeting() { <7> assertThat(myBean, notNullValue()); assertThat(myBean.greeting(), is("TestHello")); } } ---- +<1> Start the Helidon container. +<2> Set disabled Bean Discovery for the current test class. +<3> Add `MyBean` to current context. +<4> Add a configuration CDI extension to the current test. +<5> Add configuration properties. +<6> Inject `MyBean` as it is available in the CDI context. +<7> Run rests. + +To test `@RequestScoped` bean with JaxRs support: + +[source,java] +.Test `RequestScoped` bean +---- + +@HelidonTest <1> +@DisableDiscovery <2> +@AddJaxRs <3> +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped <4> + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} +---- +<1> Start the Helidon container. +<2> Set disabled Bean discovery. +<3> Add JaxRs support to the current test class. +<4> Define a `RequestScoped` bean. == Additional Information diff --git a/microprofile/config/pom.xml b/microprofile/config/pom.xml index ac030952e53..c11d4c4cd16 100644 --- a/microprofile/config/pom.xml +++ b/microprofile/config/pom.xml @@ -74,31 +74,17 @@ jakarta.inject-api provided - - io.helidon.microprofile.cdi - helidon-microprofile-cdi - test - org.junit.jupiter junit-jupiter-api test - - org.mockito - mockito-core - test - + org.hamcrest hamcrest-all test - - io.helidon.microprofile.testing - helidon-microprofile-testing-junit5 - test - diff --git a/microprofile/server/pom.xml b/microprofile/server/pom.xml index 408898c8df8..b053fed4313 100644 --- a/microprofile/server/pom.xml +++ b/microprofile/server/pom.xml @@ -156,11 +156,6 @@ - - io.helidon.microprofile.testing - helidon-microprofile-testing-junit5 - test - io.helidon.common helidon-common-reactive diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 36fdb1089dc..fe4d067c095 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -34,11 +34,21 @@ + + io.helidon.microprofile.server + helidon-microprofile-server + true + io.helidon.microprofile.cdi helidon-microprofile-cdi provided + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + true + org.junit.jupiter junit-jupiter-api @@ -57,6 +67,7 @@ hamcrest-core test + diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java new file mode 100644 index 00000000000..1f776358f0d --- /dev/null +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 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.microprofile.testing.junit5; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Add JaxRS support for Request-scoped beans. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface AddJaxRs { +} diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index 6f80bd7d461..3f2484feec0 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.junit5; +import java.io.Serial; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -33,6 +34,8 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -47,6 +50,7 @@ import jakarta.enterprise.inject.spi.InjectionPoint; import jakarta.enterprise.inject.spi.ProcessInjectionPoint; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; +import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.ws.rs.client.Client; @@ -56,6 +60,7 @@ import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.AfterAllCallback; @@ -80,7 +85,7 @@ class HelidonJunitExtension implements BeforeAllCallback, InvocationInterceptor, ParameterResolver { private static final Set> HELIDON_TEST_ANNOTATIONS = - Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class); + Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class, AddJaxRs.class); private static final Map, Annotation> BEAN_DEFINING = new HashMap<>(); private static final List YAML_SUFFIXES = List.of(".yml", ".yaml"); @@ -137,6 +142,16 @@ public void beforeAll(ExtensionContext context) { } validatePerClass(); + // add beans when using JaxRS + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + classLevelExtensions.add(ProcessAllAnnotatedTypesLiteral.INSTANCE); + classLevelExtensions.add(ServerCdiExtensionLiteral.INSTANCE); + classLevelExtensions.add(JaxRsCdiExtensionLiteral.INSTANCE); + classLevelExtensions.add(CdiComponentProviderLiteral.INSTANCE); + classLevelBeans.add(WeldRequestScopeLiteral.INSTANCE); + } + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -234,6 +249,13 @@ private void validatePerClass() { } } } + + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + if (testClass.getAnnotation(DisableDiscovery.class) == null){ + throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); + } + } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -621,4 +643,92 @@ ConfigMeta nextMethod() { return methodMeta; } } + + + /** + * Add WeldRequestScope. Used with {@code AddJaxRs}. + */ + private static final class WeldRequestScopeLiteral extends AnnotationLiteral implements AddBean { + + static final WeldRequestScopeLiteral INSTANCE = new WeldRequestScopeLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.weld.se.WeldRequestScope.class; + } + + @Override + public Class scope() { + return RequestScoped.class; + } + } + + + /** + * Add ProcessAllAnnotatedTypes. Used with {@code AddJaxRs}. + */ + private static final class ProcessAllAnnotatedTypesLiteral extends AnnotationLiteral implements AddExtension { + + static final ProcessAllAnnotatedTypesLiteral INSTANCE = new ProcessAllAnnotatedTypesLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; + } + } + + /** + * Add ServerCdiExtension. Used with {@code AddJaxRs}. + */ + private static final class ServerCdiExtensionLiteral extends AnnotationLiteral implements AddExtension { + + static final ServerCdiExtensionLiteral INSTANCE = new ServerCdiExtensionLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return ServerCdiExtension.class; + } + } + + /** + * Add WeldRequestScope. Used with {@code AddJaxRs}. + */ + private static final class JaxRsCdiExtensionLiteral extends AnnotationLiteral implements AddExtension { + + static final JaxRsCdiExtensionLiteral INSTANCE = new JaxRsCdiExtensionLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return JaxRsCdiExtension.class; + } + } + + /** + * Add CdiComponentProvider. Used with {@code AddJaxRs}. + */ + private static final class CdiComponentProviderLiteral extends AnnotationLiteral implements AddExtension { + + static final CdiComponentProviderLiteral INSTANCE = new CdiComponentProviderLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return CdiComponentProvider.class; + } + } + } diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 1e0a02603e0..9799b1b0e0b 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -19,14 +19,19 @@ */ module io.helidon.microprofile.testing.junit5 { - requires io.helidon.microprofile.cdi; requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires org.junit.jupiter.api; + requires io.helidon.microprofile.cdi; requires jakarta.inject; + requires org.junit.jupiter.api; + requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; + requires static io.helidon.microprofile.server; + requires static jersey.cdi1x; + requires static jersey.weld2.se; + exports io.helidon.microprofile.testing.junit5; } diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index b7827f9d1a6..da432b69862 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -34,11 +34,21 @@ + + io.helidon.microprofile.server + helidon-microprofile-server + true + io.helidon.microprofile.cdi helidon-microprofile-cdi provided + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + true + io.helidon.jersey helidon-jersey-client @@ -47,6 +57,7 @@ io.helidon.config helidon-config-yaml-mp + org.testng testng diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java new file mode 100644 index 00000000000..250d2fb192e --- /dev/null +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 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.microprofile.testing.testng; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Add JaxRS support for Request-scoped beans. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface AddJaxRs { +} diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index 7843643781d..e2ddb7f3fc8 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.testng; +import java.io.Serial; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -32,6 +33,8 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -49,6 +52,7 @@ import jakarta.enterprise.inject.spi.InjectionTarget; import jakarta.enterprise.inject.spi.InjectionTargetFactory; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; +import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.ws.rs.client.Client; @@ -57,6 +61,7 @@ import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.testng.IClassListener; import org.testng.ITestClass; import org.testng.ITestListener; @@ -69,7 +74,7 @@ public class HelidonTestNgListener implements IClassListener, ITestListener { private static final Set> HELIDON_TEST_ANNOTATIONS = - Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class); + Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class, AddJaxRs.class); private static final Map, Annotation> BEAN_DEFINING = new HashMap<>(); private static final List YAML_SUFFIXES = List.of(".yml", ".yaml"); @@ -124,6 +129,16 @@ public void onBeforeClass(ITestClass iTestClass) { } validatePerClass(); + // add beans when using JaxRS + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + classLevelExtensions.add(ProcessAllAnnotatedTypesLiteral.INSTANCE); + classLevelExtensions.add(ServerCdiExtensionLiteral.INSTANCE); + classLevelExtensions.add(JaxRsCdiExtensionLiteral.INSTANCE); + classLevelExtensions.add(CdiComponentProviderLiteral.INSTANCE); + classLevelBeans.add(WeldRequestScopeLiteral.INSTANCE); + } + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -246,6 +261,14 @@ private void validatePerClass() { } } } + + + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + if (testClass.getAnnotation(DisableDiscovery.class) == null){ + throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); + } + } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -505,4 +528,93 @@ ConfigMeta nextMethod() { return methodMeta; } } + + + + /** + * Add WeldRequestScope. Used with {@code AddJaxRs}. + */ + private static final class WeldRequestScopeLiteral extends AnnotationLiteral implements AddBean { + + static final WeldRequestScopeLiteral INSTANCE = new WeldRequestScopeLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.weld.se.WeldRequestScope.class; + } + + @Override + public Class scope() { + return RequestScoped.class; + } + } + + + /** + * Add ProcessAllAnnotatedTypes. Used with {@code AddJaxRs}. + */ + private static final class ProcessAllAnnotatedTypesLiteral extends AnnotationLiteral implements AddExtension { + + static final ProcessAllAnnotatedTypesLiteral INSTANCE = new ProcessAllAnnotatedTypesLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; + } + } + + /** + * Add ServerCdiExtension. Used with {@code AddJaxRs}. + */ + private static final class ServerCdiExtensionLiteral extends AnnotationLiteral implements AddExtension { + + static final ServerCdiExtensionLiteral INSTANCE = new ServerCdiExtensionLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return ServerCdiExtension.class; + } + } + + /** + * Add WeldRequestScope. Used with {@code AddJaxRs}. + */ + private static final class JaxRsCdiExtensionLiteral extends AnnotationLiteral implements AddExtension { + + static final JaxRsCdiExtensionLiteral INSTANCE = new JaxRsCdiExtensionLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return JaxRsCdiExtension.class; + } + } + + /** + * Add CdiComponentProvider. Used with {@code AddJaxRs}. + */ + private static final class CdiComponentProviderLiteral extends AnnotationLiteral implements AddExtension { + + static final CdiComponentProviderLiteral INSTANCE = new CdiComponentProviderLiteral(); + + @Serial + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return CdiComponentProvider.class; + } + } + } diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 271be2a6022..c2e60fd39aa 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -30,6 +30,10 @@ requires microprofile.config.api; requires org.testng; + requires static io.helidon.microprofile.server; + requires static jersey.cdi1x; + requires static jersey.weld2.se; + exports io.helidon.microprofile.testing.testng; provides org.testng.ITestNGListener with HelidonTestNgListener; diff --git a/microprofile/tests/config/pom.xml b/microprofile/tests/config/pom.xml new file mode 100644 index 00000000000..da8fedd2810 --- /dev/null +++ b/microprofile/tests/config/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + io.helidon.microprofile.tests + helidon-microprofile-tests-project + 4.0.0-SNAPSHOT + ../pom.xml + + helidon-microprofile-tests-config + Helidon Microprofile Config Tests + + + Microprofile config implementation. + + + + + io.helidon.microprofile.config + helidon-microprofile-config + test + + + io.helidon.microprofile.cdi + helidon-microprofile-cdi + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.mockito + mockito-core + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + + diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java similarity index 98% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java index f90f6440948..f670e0decb1 100644 --- a/microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 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. @@ -111,4 +111,4 @@ static Config createTestConfiguredBeanConfig() { .build(); } -} +} \ No newline at end of file diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/Converters.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/Converters.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/Converters.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/Converters.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java new file mode 100644 index 00000000000..1064d7f5c13 --- /dev/null +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2020, 2023 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.microprofile.config; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import io.helidon.config.mp.MpConfigSources; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; + +class ObjectMappingTest { + private static ClassLoader classLoader; + private static ConfigProviderResolver resolver; + + @BeforeAll + static void initClass() { + classLoader = Thread.currentThread().getContextClassLoader(); + resolver = ConfigProviderResolver.instance(); + } + + @Test + void testIt() { + // Removed use of system properties, as those stay around after test is finished + Map configMap = Map.of("built.it", "configured", + "list.0.it", "first", + "list.1.it", "second"); + + resolver.registerConfig(resolver.getBuilder() + .withSources(MpConfigSources.create(configMap)) + .build(), + classLoader); + + // need to go through resolver to wrap config in our SE/MP wrapper + Config config = resolver.getConfig(classLoader); + io.helidon.config.Config helidonConfig = (io.helidon.config.Config) config; + + try { + Built built = helidonConfig.get("built").as(Built.class).get(); + assertThat(built.it, is("configured")); + + List list = helidonConfig.get("list").asList(Built.class).get(); + assertThat(list, hasSize(2)); + assertThat(list, Matchers.contains(new Built("first"), new Built("second"))); + + } finally { + resolver.releaseConfig(config); + } + } + + public static class Built { + private final String it; + + private Built(Builder builder) { + this.it = builder.it; + } + + private Built(String it) { + this.it = it; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Built built = (Built) o; + return it.equals(built.it); + } + + @Override + public int hashCode() { + return Objects.hash(it); + } + + public static class Builder { + private String it; + + public Builder it(String it) { + this.it = it; + return this; + } + + public Built build() { + return new Built(this); + } + } + } +} diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java similarity index 94% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java index 85a15973bc0..d709e2a7fa5 100644 --- a/microprofile/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 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. diff --git a/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties new file mode 100644 index 00000000000..c6f40ebb087 --- /dev/null +++ b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties @@ -0,0 +1,22 @@ +# +# Copyright (c) 2020, 2023 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. +# + +# needed to run unit tests independently based on explicit beans using SeContainerInitializer +mp.initializer.allow=true +mp.initializer.warn=false + +camelCase=yes +yamlProperty=no diff --git a/microprofile/tests/config/src/test/resources/application.yaml b/microprofile/tests/config/src/test/resources/application.yaml new file mode 100644 index 00000000000..2de7ba50ffa --- /dev/null +++ b/microprofile/tests/config/src/test/resources/application.yaml @@ -0,0 +1,18 @@ +# +# Copyright (c) 2021, 2023 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. +# +yamlProperty : "yes" +nested: + yamlProperty : "yes" diff --git a/microprofile/tests/pom.xml b/microprofile/tests/pom.xml index 80cda97c470..a06d9654c1f 100644 --- a/microprofile/tests/pom.xml +++ b/microprofile/tests/pom.xml @@ -45,6 +45,7 @@ arquillian server testing + config telemetry diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java similarity index 88% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java index 4e590b47f80..850bc363646 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java @@ -16,6 +16,9 @@ package io.helidon.microprofile.server; import io.helidon.microprofile.config.ConfigCdiExtension; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.JaxRsService; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddConfig; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.DisableDiscovery; @@ -32,7 +35,7 @@ /** * Test that it is possible to override {@code IGNORE_EXCEPTION_RESPONSE} in - * Jersey using config. See {@link JaxRsService} + * Jersey using config. See {@link io.helidon.microprofile.server.JaxRsService} * for more information. */ @HelidonTest diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java similarity index 98% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java index b4278346bfa..36add13be9c 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java similarity index 95% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java index d2d734711c5..2df3d159d12 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -22,6 +22,10 @@ import io.helidon.http.HeaderName; import io.helidon.http.HeaderNames; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.RoutingName; +import io.helidon.microprofile.server.RoutingPath; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddConfig; import io.helidon.microprofile.testing.junit5.AddExtension; @@ -61,7 +65,7 @@ value = RoutingName.DEFAULT_NAME) public class ProducedRouteTest { - static final String TEST_BEAN_FQDN = "io.helidon.microprofile.server.ProducedRouteTest$TestBean"; + static final String TEST_BEAN_FQDN = "io.helidon.microprofile.tests.server.ProducedRouteTest$TestBean"; static final String FILTERED_PATH = "/filtered"; static final String UNFILTERED_PATH = "/unfiltered"; diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java similarity index 94% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java index 1355514fdb4..41cce14fe49 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.net.URI; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.DisableDiscovery; diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java new file mode 100644 index 00000000000..b8831785ba8 --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 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.microprofile.tests.testing.junit5; + +import io.helidon.microprofile.testing.junit5.*; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@HelidonTest +@DisableDiscovery + +// JAX-RS Request scope +@AddJaxRs +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +class TestReqScopeDisabledDiscovery { + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} \ No newline at end of file diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java new file mode 100644 index 00000000000..a80f587cd67 --- /dev/null +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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.microprofile.tests.testing.testng; + + +import io.helidon.microprofile.testing.testng.*; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import org.testng.annotations.Test; + +import static org.testng.AssertJUnit.assertEquals; + +@HelidonTest +@DisableDiscovery + +// JAX-RS Request scope +@AddJaxRs +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} \ No newline at end of file