Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] - Add @RequestScoped support for testing #7916

Merged
merged 29 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ec3b0db
Add @RequestScoped support
dalexandrov Oct 30, 2023
3c28cdf
Add JaxRs support for Request-scoped beans.
dalexandrov Oct 31, 2023
a7f196d
Refactoring to separate module
dalexandrov Nov 13, 2023
836e6bf
Add Bean support
dalexandrov Nov 13, 2023
d8a7f18
Refactor. Reuse in TestNG.
dalexandrov Nov 14, 2023
38c50db
Add validation support. First iteration.
dalexandrov Nov 15, 2023
f6f00f5
extract validation
dalexandrov Nov 17, 2023
e319e70
Move validation back to common
dalexandrov Nov 20, 2023
c903648
Naming cleanup
dalexandrov Nov 20, 2023
c51dbc2
Should be only on class
dalexandrov Nov 20, 2023
ea9af2a
Minor clean up
dalexandrov Nov 21, 2023
7eed160
Dependency fix
dalexandrov Nov 21, 2023
bc91012
Update microprofile/testing/common/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
5704bae
Update microprofile/tests/testing/testng/src/test/java/io/helidon/mic…
dalexandrov Nov 29, 2023
efd0ebc
Update microprofile/testing/testng/pom.xml
dalexandrov Nov 29, 2023
fef4450
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
19074bf
Update microprofile/testing/testng/src/main/java/module-info.java
dalexandrov Nov 29, 2023
808fff8
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
20b25be
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
e62a48c
Move validator to modules
dalexandrov Nov 29, 2023
480757e
Rollback to simple design
dalexandrov Nov 30, 2023
22132ca
Rollback to simple design
dalexandrov Nov 30, 2023
67362f9
Mino clean up
dalexandrov Nov 30, 2023
8484dc3
Clean dependency tree
dalexandrov Dec 1, 2023
a1d4cc7
Copyright fix
dalexandrov Dec 1, 2023
c82f046
Apply comments
dalexandrov Dec 5, 2023
b56a6fb
Merge branch 'main' into 7564_add_JaxRs-annotation
dalexandrov Dec 5, 2023
722c95f
Fix copyright
dalexandrov Dec 5, 2023
d3ccf55
Fix dependencies
dalexandrov Dec 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,16 @@
<artifactId>helidon-microprofile-testing-testng</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-common</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-jaxrs</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.messaging.mock</groupId>
<artifactId>helidon-messaging-mock</artifactId>
Expand Down
70 changes: 62 additions & 8 deletions docs/mp/testing-ng.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 65 additions & 8 deletions docs/mp/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 43 additions & 0 deletions microprofile/testing/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<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>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>helidon-microprofile-testing-common</artifactId>
<name>Common testing support</name>

<description>
Common testing support
</description>

<dependencies>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Add a bean to other annotations.
* The class will be instantiated using CDI and will be available for injection into test classes and other beans.
* This annotation can be repeated.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Repeatable(CommonAddBeans.class)
public @interface CommonAddBean {

/**
* Class of the bean.
* @return the class of a bean
*/
Class<?> value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* A repeatable container for {@link CommonAddBean}.
* No need to use this annotation, just repeat {@link CommonAddBean} annotation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface CommonAddBeans {
/**
* Beans to be added.
* @return add bean annotations
*/
CommonAddBean[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.inject.spi.Extension;


/**
* Common CDI Extension.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Repeatable(CommonCdiExtensions.class)
@Inherited
public @interface CommonCdiExtension {

/**
* CDI Extension.
*
* @return The CDI Extension Class.
*/
Class<? extends Extension> value();
}
Loading
Loading