Skip to content

Commit

Permalink
extract validation
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Aleksandrov <[email protected]>
  • Loading branch information
dalexandrov committed Nov 17, 2023
1 parent 38c50db commit f6f00f5
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 18 deletions.
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@
<artifactId>helidon-microprofile-testing-common</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-validator</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-jaxrs</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions microprofile/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-validator</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import io.helidon.config.mp.MpConfigSources;
import io.helidon.config.yaml.mp.YamlMpConfigSource;
import io.helidon.microprofile.testing.common.JaxRsValidator;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand Down Expand Up @@ -139,7 +138,8 @@ public void beforeAll(ExtensionContext context) {
}
validatePerClass();

JaxRsValidator.validate(testClass);
JunitJaxRsValidator junitJaxRsValidator = new JunitJaxRsValidator();
junitJaxRsValidator.validate(testClass);

configure(classLevelConfigMeta);

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.testing.junit5;

import io.helidon.microprofile.testing.validator.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");
}
}
}
}
1 change: 1 addition & 0 deletions microprofile/testing/junit5/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
requires io.helidon.config.yaml.mp;
requires io.helidon.microprofile.cdi;
requires io.helidon.microprofile.testing.common;
requires io.helidon.microprofile.testing.validator;
requires jakarta.inject;
requires org.junit.jupiter.api;

Expand Down
1 change: 1 addition & 0 deletions microprofile/testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
<module>junit5</module>
<module>testng</module>
<module>jaxrs</module>
<module>validator</module>
</modules>
</project>
4 changes: 4 additions & 0 deletions microprofile/testing/testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-validator</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import io.helidon.config.mp.MpConfigSources;
import io.helidon.config.yaml.mp.YamlMpConfigSource;
import io.helidon.microprofile.testing.common.JaxRsValidator;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand Down Expand Up @@ -130,7 +129,8 @@ public void onBeforeClass(ITestClass iTestClass) {
}
validatePerClass();

JaxRsValidator.validate(testClass);
TestNgJaxRsValidator testNgJaxRsValidator = new TestNgJaxRsValidator();
testNgJaxRsValidator.validate(testClass);

configure(classLevelConfigMeta);

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.testing.testng;

import io.helidon.microprofile.testing.validator.JaxRsValidator;

/**
* An implementation for JaxRs Validator for Junit.
*/
public class TestNgJaxRsValidator 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");
}
}
}
}
1 change: 1 addition & 0 deletions microprofile/testing/testng/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
requires io.helidon.config.yaml.mp;
requires io.helidon.microprofile.testing.common;
requires io.helidon.microprofile.testing.jaxrs;
requires io.helidon.microprofile.testing.validator;
requires jakarta.cdi;
requires jakarta.inject;
requires jakarta.ws.rs;
Expand Down
36 changes: 36 additions & 0 deletions microprofile/testing/validator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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-validator</artifactId>
<name>Helidon Microprofile Testing JUnit5</name>

<description>
Common validation for Helidon Testing
</description>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,26 @@
* limitations under the License.
*/

package io.helidon.microprofile.testing.common;
package io.helidon.microprofile.testing.validator;


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 class JaxRsValidator {

private JaxRsValidator() {
}
public abstract class JaxRsValidator implements TestValidator {

/**
* Perform validation. Runtime exception is thrown if something is wrong.
* @param testClass the annotated test class
*/
public static void validate(Class<?> testClass){
protected boolean checkAddJaxRs(Class<?> testClass){
Set<String> testClassAnnotations = Arrays.stream(testClass.getDeclaredAnnotations())
.map(Annotation::annotationType)
.map(Class::getName)
.collect(Collectors.toSet());
if (testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs"))
&& testClassAnnotations.stream().noneMatch(i -> i.contains("DisableDiscovery"))){
throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation.");
}
//FIXME:THIS IS VERY BAD!
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.testing.validator;

/**
* 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 Validation for Helidon Testing.
*/
package io.helidon.microprofile.testing.validator;
24 changes: 24 additions & 0 deletions microprofile/testing/validator/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

/**
* Tests validators.
*/
module io.helidon.microprofile.testing.validator {

exports io.helidon.microprofile.testing.validator;

}

0 comments on commit f6f00f5

Please sign in to comment.