Skip to content

Commit

Permalink
Add @RequestScoped support
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Aleksandrov <dmitry.aleksandrov@oracle.com>
  • Loading branch information
dalexandrov committed Oct 30, 2023
1 parent 77936c6 commit ec3b0db
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 3 deletions.
6 changes: 6 additions & 0 deletions microprofile/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-client</artifactId>
Expand All @@ -57,6 +62,7 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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, ElementType.METHOD})
public @interface AddJaxRs {
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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;
Expand Down Expand Up @@ -80,7 +81,7 @@ class HelidonJunitExtension implements BeforeAllCallback,
InvocationInterceptor,
ParameterResolver {
private static final Set<Class<? extends Annotation>> 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<Class<? extends Annotation>, Annotation> BEAN_DEFINING = new HashMap<>();

private static final List<String> YAML_SUFFIXES = List.of(".yml", ".yaml");
Expand Down Expand Up @@ -137,6 +138,13 @@ public void beforeAll(ExtensionContext context) {
}
validatePerClass();

// add beans when using JaxRS
AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class);
if (addJaxRsAnnotation != null){
classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE);
classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE);
}

configure(classLevelConfigMeta);

if (!classLevelConfigMeta.useExisting) {
Expand Down Expand Up @@ -234,6 +242,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) {
Expand Down Expand Up @@ -621,4 +636,42 @@ ConfigMeta nextMethod() {
return methodMeta;
}
}


/**
* Add JaxRs Bean when {@code AddJaxRs} annotation is used.
*/
private static final class AddBeanJaxRsLiteral extends AnnotationLiteral<AddBean> implements AddBean {

static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral();

private static final long serialVersionUID = 1L;

@Override
public Class<?> value() {
return org.glassfish.jersey.weld.se.WeldRequestScope.class;
}

@Override
public Class<? extends Annotation> scope() {
return RequestScoped.class;
}
}


/**
* Add JaxRs Extension when {@code AddJaxRs} annotation is used.
*/
private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral<AddExtension> implements AddExtension {

static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral();

private static final long serialVersionUID = 1L;

@Override
public Class<? extends Extension> value() {
return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class;
}
}

}
7 changes: 5 additions & 2 deletions microprofile/testing/junit5/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
*/
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 jersey.cdi1x;
requires jersey.weld2.se;
requires org.junit.jupiter.api;

requires transitive jakarta.cdi;
requires transitive jakarta.ws.rs;

Expand Down
5 changes: 5 additions & 0 deletions microprofile/testing/testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml-mp</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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, ElementType.METHOD})
public @interface AddJaxRs {
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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;
Expand Down Expand Up @@ -174,6 +175,14 @@ public void onTestStart(ITestResult result) {
methodLevelDisableDiscovery = discovery.value();
}


// add beans when using JaxRS
AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class);
if (addJaxRsAnnotation != null){
classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE);
classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE);
}

startContainer(methodLevelBeans, methodLevelExtensions, methodLevelDisableDiscovery);
}
}
Expand Down Expand Up @@ -246,6 +255,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) {
Expand Down Expand Up @@ -505,4 +522,42 @@ ConfigMeta nextMethod() {
return methodMeta;
}
}



/**
* Add JaxRs Bean when {@code AddJaxRs} annotation is used.
*/
private static final class AddBeanJaxRsLiteral extends AnnotationLiteral<AddBean> implements AddBean {

static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral();

private static final long serialVersionUID = 1L;

@Override
public Class<?> value() {
return org.glassfish.jersey.weld.se.WeldRequestScope.class;
}

@Override
public Class<? extends Annotation> scope() {
return RequestScoped.class;
}
}


/**
* Add JaxRs Extension when {@code AddJaxRs} annotation is used.
*/
private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral<AddExtension> implements AddExtension {

static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral();

private static final long serialVersionUID = 1L;

@Override
public Class<? extends Extension> value() {
return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class;
}
}
}
2 changes: 2 additions & 0 deletions microprofile/testing/testng/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
requires jakarta.cdi;
requires jakarta.inject;
requires jakarta.ws.rs;
requires jersey.cdi1x;
requires jersey.weld2.se;
requires microprofile.config.api;
requires org.testng;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.helidon.microprofile.tests.testing.junit5;

import io.helidon.microprofile.server.JaxRsCdiExtension;
import io.helidon.microprofile.server.ServerCdiExtension;
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.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@HelidonTest
@DisableDiscovery
@AddExtension(ServerCdiExtension.class)
@AddExtension(JaxRsCdiExtension.class)
@AddExtension(CdiComponentProvider.class)

// 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.helidon.microprofile.tests.testing.testng;


import io.helidon.microprofile.server.JaxRsCdiExtension;
import io.helidon.microprofile.server.ServerCdiExtension;
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.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;

@HelidonTest
@DisableDiscovery
@AddExtension(ServerCdiExtension.class)
@AddExtension(JaxRsCdiExtension.class)
@AddExtension(CdiComponentProvider.class)

// 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();
}
}
}

0 comments on commit ec3b0db

Please sign in to comment.