Skip to content

Commit

Permalink
Add Jax-Rs 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 Nov 20, 2023
1 parent c34b411 commit 30a11a2
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 34 deletions.
23 changes: 4 additions & 19 deletions microprofile/tests/common/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020, 2023 Oracle and/or its affiliates.
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.
Expand All @@ -27,29 +27,14 @@
</parent>

<artifactId>helidon-microprofile-tests-common</artifactId>
<name>Helidon Microprofile Testing JUnit5</name>
<name>Helidon Microprofile Testing Common support</name>

<description>
Integration with Junit5 to support tests with CDI injection
Helidon Microprofile Testing Common support
</description>

<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.enterprise.inject.spi.Extension;


/**
* Common CDI Extension.
Expand All @@ -39,5 +37,5 @@
*
* @return The CDI Extension Class.
*/
Class<? extends Extension> value();
Class<?> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.stream.Collectors;

import javax.enterprise.inject.spi.Extension;

/**
* Common Utility class for tests.
Expand All @@ -37,9 +36,9 @@ private CommonTestUtil() {
* @param testClass Class
* @return List with Extension classes
*/
public static List<Class<? extends Extension>> getFeatureExtensions(Class<?> testClass) {
public static List<Class<?>> getFeatureExtensions(Class<?> testClass) {

List<Class<? extends Extension>> result = Arrays.stream(testClass.getDeclaredAnnotations())
List<Class<?>> result = Arrays.stream(testClass.getDeclaredAnnotations())
.flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations()))
.filter(a -> a instanceof CommonCdiExtension)
.map(CommonCdiExtension.class::cast)
Expand Down
6 changes: 2 additions & 4 deletions microprofile/tests/common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* 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.
Expand All @@ -15,12 +15,10 @@
*/

/**
* JUnit5 extension module to run CDI tests.
* Helidon Common testing support.
*/
module io.helidon.microprofile.tests.common {

requires javax.inject;

exports io.helidon.microprofile.tests.common;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* JAX_RS Testing annotation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Target({ElementType.TYPE})
@Inherited
@CommonCdiExtension(ServerCdiExtension.class)
@CommonCdiExtension(JaxRsCdiExtension.class)
Expand Down
4 changes: 1 addition & 3 deletions microprofile/tests/jaxrs/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/

/**
* JUnit5 extension module to run CDI tests.
*/
* Helidon Testing JAX-RS Support
module io.helidon.microprofile.tests.jaxrs {

requires io.helidon.config.mp;
requires io.helidon.config.yaml.mp;
requires io.helidon.microprofile.cdi;
requires io.helidon.microprofile.server;
requires io.helidon.microprofile.tests.common;
requires javax.inject;
requires jersey.cdi1x;
requires jersey.weld2.se;
requires org.junit.jupiter.api;
Expand Down
5 changes: 5 additions & 0 deletions microprofile/tests/junit5-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-jaxrs</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.junit5;

import io.helidon.microprofile.tests.jaxrs.AddJaxRs;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.junit.jupiter.api.Test;

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

@HelidonTest
@DisableDiscovery

@AddBean(TestReqScopeDisabledDiscovery.MyController.class)
@AddJaxRs
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();
}
}
}
4 changes: 4 additions & 0 deletions microprofile/tests/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml-mp</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-common</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.junit5;

import io.helidon.microprofile.tests.common.JaxRsValidator;

/**
* An implementation for JaxRs Validator for Junit.
*/
public class JunitJaxRsValidator extends JaxRsValidator {

@Override
public void validate(Class<?> testClass) throws RuntimeException {
if (checkAddJaxRs(testClass)){
DisableDiscovery disableDiscovery = testClass.getAnnotation(DisableDiscovery.class);
if (disableDiscovery == null) {
throw new RuntimeException("@AddJaxRs annotation may only be used with @DisableDiscovery annotation");
}
}
}
}
3 changes: 2 additions & 1 deletion microprofile/tests/junit5/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
requires io.helidon.microprofile.cdi;
requires io.helidon.config.mp;
requires io.helidon.config.yaml.mp;
requires io.helidon.microprofile.tests.common;
requires org.junit.jupiter.api;
requires transitive jakarta.enterprise.cdi.api;
requires transitive java.ws.rs;
Expand Down

0 comments on commit 30a11a2

Please sign in to comment.