Skip to content

Commit

Permalink
JAX-RS Support for Helidon 3 testing
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 21, 2023
1 parent 400954b commit 6c2ea0d
Show file tree
Hide file tree
Showing 28 changed files with 797 additions and 7 deletions.
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,16 @@
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-common</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-jaxrs</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.messaging.mock</groupId>
<artifactId>helidon-messaging-mock</artifactId>
Expand Down
44 changes: 44 additions & 0 deletions microprofile/tests/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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</groupId>
<artifactId>helidon-microprofile-project</artifactId>
<version>3.2.4-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-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.tests.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.tests.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.tests.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();
}
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.tests.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* Common CDI Extensions for testing.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Inherited
public @interface CommonCdiExtensions {

/**
* Return CDI Extension.
*
* @return CDIExtension[]
*/
CommonCdiExtension[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.spi.Extension;

/**
* Common Utility class for tests.
*/
public class CommonTestUtil {

private CommonTestUtil() {
}

/**
* Extract all Extensions from the given testClass.
*
* @param testClass Class
* @return List with Extension classes
*/
public static List<Class<? extends Extension>> getFeatureExtensions(Class<?> testClass) {

List<Class<? extends Extension>> result = Arrays.stream(testClass.getDeclaredAnnotations())
.flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations()))
.filter(a -> a instanceof CommonCdiExtension)
.map(CommonCdiExtension.class::cast)
.map(CommonCdiExtension::value)
.collect(Collectors.toList());

result.addAll(Arrays.stream(testClass.getDeclaredAnnotations())
.flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations()))
.filter(a -> a instanceof CommonCdiExtensions)
.map(CommonCdiExtensions.class::cast)
.flatMap(e -> Arrays.stream(e.value()))
.map(CommonCdiExtension::value)
.collect(Collectors.toList()));

return result;
}

/**
* Extract all Beans from the given testClass.
*
* @param testClass Class
* @return List with Beans classes
*/
public static List<Class<?>> getFeatureBeans(Class<?> testClass) {

ArrayList<Class<?>> result = Arrays.stream(testClass.getDeclaredAnnotations())
.flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations()))
.filter(a -> a instanceof CommonAddBean)
.map(CommonAddBean.class::cast)
.map(CommonAddBean::value).collect(Collectors.toCollection(ArrayList::new));

result.addAll(Arrays.stream(testClass.getDeclaredAnnotations())
.flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations()))
.filter(a -> a instanceof CommonAddBeans)
.map(CommonAddBeans.class::cast)
.flatMap(e -> Arrays.stream(e.value()))
.map(CommonAddBean::value)
.collect(Collectors.toList()));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.common;


import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;


/**
* Validator for JAX-RS annotation usage.
*/
public abstract class JaxRsValidator implements TestValidator {

protected boolean checkAddJaxRs(Class<?> testClass){
Set<String> testClassAnnotations = Arrays.stream(testClass.getDeclaredAnnotations())
.map(Annotation::annotationType)
.map(Class::getName)
.collect(Collectors.toSet());
return testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.common;

/**
* Validator for test class.
*/
public interface TestValidator {

/**
* Perform Validation.
* @param testClass for verification.
*/
void validate(Class<?> testClass) throws RuntimeException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

/**
* Common Testing Extensions.
*/
package io.helidon.microprofile.tests.common;
Loading

0 comments on commit 6c2ea0d

Please sign in to comment.