From 2a62370d0b7884774f733842d6b335f9d5af70b1 Mon Sep 17 00:00:00 2001 From: Stijn Dejongh Date: Tue, 19 Nov 2024 05:52:21 +0100 Subject: [PATCH] First implementation of validation spec parsing -- WIP --- core/pom.xml | 18 +++- .../validation/core/ConstrainedTest.java | 3 + .../validation/core/ConstraintTest.java | 3 + .../validation/core/ContrainsTest.java | 3 + .../core/InvalidObjectExceptionTest.java | 3 + .../code_quality/CodeConventionsTest.java | 86 ------------------- .../ConventionsAdherenceTests.java | 36 ++++++++ .../specs/usage/BasicUsageTest.java | 3 + dsl/pom.xml | 30 ++++++- .../validation/dsl/ValidatorParser.java | 21 +++++ .../dsl/ConventionsAdherenceTests.java | 17 ++++ .../validation/dsl/ValidatorParserTest.java | 31 +++++++ pom.xml | 21 ++++- 13 files changed, 185 insertions(+), 90 deletions(-) delete mode 100644 core/src/test/java/be/sddevelopment/validation/specs/code_quality/CodeConventionsTest.java create mode 100644 core/src/test/java/be/sddevelopment/validation/specs/code_quality/ConventionsAdherenceTests.java create mode 100644 dsl/src/main/java/be/sddevelopment/validation/dsl/ValidatorParser.java create mode 100644 dsl/src/test/java/be/sddevelopment/validation/dsl/ConventionsAdherenceTests.java create mode 100644 dsl/src/test/java/be/sddevelopment/validation/dsl/ValidatorParserTest.java diff --git a/core/pom.xml b/core/pom.xml index 9af4b2f..31dedfe 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ 4.0.0 - be.sddevelopment + be.sddevelopment.validation modular-validators 1.1.0-SNAPSHOT ../pom.xml @@ -15,6 +15,22 @@ + + be.sddevelopment.commons + commons-kernel + + + be.sddevelopment.commons + commons + + + + + be.sddevelopment.commons + commons-testing + test + + org.junit.jupiter diff --git a/core/src/test/java/be/sddevelopment/validation/core/ConstrainedTest.java b/core/src/test/java/be/sddevelopment/validation/core/ConstrainedTest.java index a0442a0..8f67d40 100644 --- a/core/src/test/java/be/sddevelopment/validation/core/ConstrainedTest.java +++ b/core/src/test/java/be/sddevelopment/validation/core/ConstrainedTest.java @@ -1,7 +1,9 @@ package be.sddevelopment.validation.core; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.platform.commons.util.StringUtils; @@ -21,6 +23,7 @@ @DisplayName("Constrainables") @ExtendWith({MockitoExtension.class}) +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class ConstrainedTest implements WithAssertions { @Test diff --git a/core/src/test/java/be/sddevelopment/validation/core/ConstraintTest.java b/core/src/test/java/be/sddevelopment/validation/core/ConstraintTest.java index 30b576a..1bfcdca 100644 --- a/core/src/test/java/be/sddevelopment/validation/core/ConstraintTest.java +++ b/core/src/test/java/be/sddevelopment/validation/core/ConstraintTest.java @@ -1,8 +1,10 @@ package be.sddevelopment.validation.core; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; import be.sddevelopment.validation.CheckedTestUtils; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Test; import org.junit.platform.commons.util.StringUtils; @@ -14,6 +16,7 @@ import static be.sddevelopment.validation.core.Reason.passed; @DisplayName("Constraints / Rules") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class ConstraintTest implements WithAssertions { @Test diff --git a/core/src/test/java/be/sddevelopment/validation/core/ContrainsTest.java b/core/src/test/java/be/sddevelopment/validation/core/ContrainsTest.java index 3247c4a..cfbe118 100644 --- a/core/src/test/java/be/sddevelopment/validation/core/ContrainsTest.java +++ b/core/src/test/java/be/sddevelopment/validation/core/ContrainsTest.java @@ -1,12 +1,15 @@ package be.sddevelopment.validation.core; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Test; import static be.sddevelopment.validation.core.Checks.haveNonNullField; @DisplayName("Constraints utility class") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class ContrainsTest implements WithAssertions { @Test diff --git a/core/src/test/java/be/sddevelopment/validation/core/InvalidObjectExceptionTest.java b/core/src/test/java/be/sddevelopment/validation/core/InvalidObjectExceptionTest.java index 3d51e20..5f5c5d9 100644 --- a/core/src/test/java/be/sddevelopment/validation/core/InvalidObjectExceptionTest.java +++ b/core/src/test/java/be/sddevelopment/validation/core/InvalidObjectExceptionTest.java @@ -1,13 +1,16 @@ package be.sddevelopment.validation.core; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Test; import static be.sddevelopment.validation.core.Reason.failed; import static be.sddevelopment.validation.core.Reason.passed; @DisplayName("InvalidObject Exceptions") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class InvalidObjectExceptionTest implements WithAssertions { @Test diff --git a/core/src/test/java/be/sddevelopment/validation/specs/code_quality/CodeConventionsTest.java b/core/src/test/java/be/sddevelopment/validation/specs/code_quality/CodeConventionsTest.java deleted file mode 100644 index e3364c0..0000000 --- a/core/src/test/java/be/sddevelopment/validation/specs/code_quality/CodeConventionsTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package be.sddevelopment.validation.specs.code_quality; - -import com.tngtech.archunit.core.domain.JavaClass; -import com.tngtech.archunit.core.domain.JavaClasses; -import com.tngtech.archunit.core.domain.JavaConstructor; -import com.tngtech.archunit.junit.AnalyzeClasses; -import com.tngtech.archunit.junit.ArchTest; -import com.tngtech.archunit.lang.ArchCondition; -import com.tngtech.archunit.lang.ArchRule; -import com.tngtech.archunit.lang.ConditionEvents; -import org.assertj.core.api.WithAssertions; -import org.junit.jupiter.api.DisplayName; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -import static com.tngtech.archunit.core.domain.JavaModifier.FINAL; -import static com.tngtech.archunit.lang.SimpleConditionEvent.satisfied; -import static com.tngtech.archunit.lang.SimpleConditionEvent.violated; -import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; -import static com.tngtech.archunit.library.GeneralCodingRules.*; -import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible; - -@DisplayName("Complies with code conventions") -@AnalyzeClasses(packages = "be.sddevelopment.validation.core") -class CodeConventionsTest implements WithAssertions { - - @ArchTest - static final ArchRule CLASSES_DO_NOT_ACCESS_STANDARD_STREAMS = NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS; - - @ArchTest - void noAccessToStandardStreamsAsMethod(JavaClasses classes) { - noClasses().should(ACCESS_STANDARD_STREAMS).check(classes); - } - - @ArchTest - static final ArchRule NO_CLASSES_USE_FIELD_INJECTION = NO_CLASSES_SHOULD_USE_FIELD_INJECTION; - - @ArchTest - static final ArchRule NO_GENERIC_EXCEPTION = NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS; - - @ArchTest - static final ArchRule NO_JAVA_UTIL_LOGGING = NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING; - - @ArchTest - static final ArchRule NO_JODATIME = NO_CLASSES_SHOULD_USE_JODATIME; - - @ArchTest - static final ArchRule UTILITY_CLASSES_CAN_NOT_BE_INSTANTIATED = noClasses() - .that().haveModifier(FINAL) - .and().haveSimpleNameNotContaining("Test") - .and().areNotEnums() - .should(beInstantiatable()); - - static ArchCondition beInstantiatable() { - return new ArchCondition<>("be instantiatable") { - @Override - public void check(JavaClass item, ConditionEvents events) { - if (isInstantiatable(item)) { - events.add(satisfied(item, "Class " + item.getName() + " is instantiatable")); - } else { - events.add(violated(item, "Class " + item.getName() + " is not instantiatable")); - } - } - }; - } - - static boolean isInstantiatable(JavaClass classToCheck) { - - try { - if (classToCheck.getConstructors().stream() - .map(JavaConstructor::getParameters) - .anyMatch(List::isEmpty)) { - var constructor = classToCheck.getConstructor().reflect(); - makeAccessible(constructor); - constructor.newInstance(); - return true; - } else { - return false; - } - } catch (UnsupportedOperationException | InstantiationException | - IllegalAccessException | InvocationTargetException e) { - return false; - } - } -} diff --git a/core/src/test/java/be/sddevelopment/validation/specs/code_quality/ConventionsAdherenceTests.java b/core/src/test/java/be/sddevelopment/validation/specs/code_quality/ConventionsAdherenceTests.java new file mode 100644 index 0000000..a73d79e --- /dev/null +++ b/core/src/test/java/be/sddevelopment/validation/specs/code_quality/ConventionsAdherenceTests.java @@ -0,0 +1,36 @@ +package be.sddevelopment.validation.specs.code_quality; + +import be.sddevelopment.commons.testing.conventions.CodeConventions; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaConstructor; +import com.tngtech.archunit.junit.AnalyzeClasses; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchCondition; +import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.ConditionEvents; +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import static com.tngtech.archunit.core.domain.JavaModifier.FINAL; +import static com.tngtech.archunit.lang.SimpleConditionEvent.satisfied; +import static com.tngtech.archunit.lang.SimpleConditionEvent.violated; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; +import static com.tngtech.archunit.library.GeneralCodingRules.*; +import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible; + +@DisplayName("Complies with code conventions") +@AnalyzeClasses(packages = "be.sddevelopment.validation.core") +public class ConventionsAdherenceTests implements CodeConventions { + + /* This class can be empty, it is used to run the test of its parent interface + * CodeConventions. This interface contains the tests for the code conventions, + * and will execute them based on the {@code @AnalyzeClasses} annotation. + */ + +} diff --git a/core/src/test/java/be/sddevelopment/validation/specs/usage/BasicUsageTest.java b/core/src/test/java/be/sddevelopment/validation/specs/usage/BasicUsageTest.java index 4b47f02..f0a75a6 100644 --- a/core/src/test/java/be/sddevelopment/validation/specs/usage/BasicUsageTest.java +++ b/core/src/test/java/be/sddevelopment/validation/specs/usage/BasicUsageTest.java @@ -1,8 +1,10 @@ package be.sddevelopment.validation.specs.usage; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; import be.sddevelopment.validation.core.*; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -23,6 +25,7 @@ import static org.apache.commons.lang3.StringUtils.containsIgnoreCase; @DisplayName("Basic Usage") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) class BasicUsageTest implements WithAssertions { @Nested diff --git a/dsl/pom.xml b/dsl/pom.xml index dd8eb7b..d2289a5 100644 --- a/dsl/pom.xml +++ b/dsl/pom.xml @@ -26,11 +26,24 @@ - be.sddevelopment + be.sddevelopment.validation core + + be.sddevelopment.commons + commons-kernel + + + be.sddevelopment.commons + commons + + + be.sddevelopment.commons + commons-testing + test + org.junit.jupiter junit-jupiter-api @@ -51,6 +64,21 @@ mockito-junit-jupiter test + + com.tngtech.archunit + archunit + test + + + com.tngtech.archunit + archunit-junit5 + test + + + com.tngtech.archunit + archunit-junit5-engine + test + diff --git a/dsl/src/main/java/be/sddevelopment/validation/dsl/ValidatorParser.java b/dsl/src/main/java/be/sddevelopment/validation/dsl/ValidatorParser.java new file mode 100644 index 0000000..a603216 --- /dev/null +++ b/dsl/src/main/java/be/sddevelopment/validation/dsl/ValidatorParser.java @@ -0,0 +1,21 @@ +package be.sddevelopment.validation.dsl; + +import be.sddevelopment.commons.access.AccessProtectionUtils; +import be.sddevelopment.commons.annotations.Utility; +import be.sddevelopment.validation.core.ModularRuleset; + +import java.nio.file.Path; + +import static be.sddevelopment.commons.access.AccessProtectionUtils.utilityClassConstructor; + +@Utility +public final class ValidatorParser { + + private ValidatorParser() { + utilityClassConstructor(); + } + + public static ModularRuleset fromSpecification(Path validationSpec) { + return null; + } +} diff --git a/dsl/src/test/java/be/sddevelopment/validation/dsl/ConventionsAdherenceTests.java b/dsl/src/test/java/be/sddevelopment/validation/dsl/ConventionsAdherenceTests.java new file mode 100644 index 0000000..52c3794 --- /dev/null +++ b/dsl/src/test/java/be/sddevelopment/validation/dsl/ConventionsAdherenceTests.java @@ -0,0 +1,17 @@ +package be.sddevelopment.validation.dsl; + +import be.sddevelopment.commons.testing.conventions.CodeConventions; +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; +import com.tngtech.archunit.junit.AnalyzeClasses; +import org.junit.jupiter.api.DisplayNameGeneration; + +@AnalyzeClasses(packages = "be.sddevelopment.commons") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) +public class ConventionsAdherenceTests implements CodeConventions { + + /* This class can be empty, it is used to run the test of its parent interface + * CodeConventions. This interface contains the tests for the code conventions, + * and will execute them based on the {@code @AnalyzeClasses} annotation. + */ + +} diff --git a/dsl/src/test/java/be/sddevelopment/validation/dsl/ValidatorParserTest.java b/dsl/src/test/java/be/sddevelopment/validation/dsl/ValidatorParserTest.java new file mode 100644 index 0000000..2d59397 --- /dev/null +++ b/dsl/src/test/java/be/sddevelopment/validation/dsl/ValidatorParserTest.java @@ -0,0 +1,31 @@ +package be.sddevelopment.validation.dsl; + +import be.sddevelopment.commons.testing.naming.ReplaceUnderscoredCamelCasing; +import be.sddevelopment.validation.core.ModularRuleset; +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +@DisplayName("Parsing of validation rules") +@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class) +class ValidatorParserTest implements WithAssertions { + + @Test + void createsAValidatorBasedOnSpecifications() throws IOException { + var validationSpec = Paths.get("src/test/resources/parsing/star_wars/STARWARS_VALIDATOR.puml"); + assertThat(validationSpec).exists(); + assertThat(validationSpec).isRegularFile(); + + var validator = ValidatorParser.fromSpecification(validationSpec); + + assertThat(validator).isNotNull() + .extracting(ModularRuleset::getRules); + } + + +} diff --git a/pom.xml b/pom.xml index 29006bb..b5de8b2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - be.sddevelopment + be.sddevelopment.validation modular-validators 1.1.0-SNAPSHOT pom @@ -60,6 +60,7 @@ sddevelopment_modular-validators ${jacoco.build.dir}/jacoco.xml + 1.0.0-SNAPSHOT 5.36.0 @@ -69,14 +70,30 @@ core ${version} - be.sddevelopment dsl ${version} + + be.sddevelopment.commons + commons-kernel + ${code.utils.version} + + + be.sddevelopment.commons + commons + ${code.utils.version} + + + + be.sddevelopment.commons + commons-testing + test + 1.0.0-SNAPSHOT + org.junit.jupiter junit-jupiter-api