|
| 1 | +package com.datadoghq.stickerlandia.stickeraward; |
| 2 | + |
| 3 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; |
| 4 | + |
| 5 | +import com.tngtech.archunit.core.domain.JavaClasses; |
| 6 | +import com.tngtech.archunit.core.importer.ClassFileImporter; |
| 7 | +import com.tngtech.archunit.lang.ArchRule; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * ArchUnit tests to validate architectural rules based on the CodeQL arch tests. These tests ensure |
| 12 | + * domain boundaries are respected and REST API classes don't directly import entity types. |
| 13 | + */ |
| 14 | +public class ArchitectureTest { |
| 15 | + |
| 16 | + private static final JavaClasses classes = |
| 17 | + new ClassFileImporter().importPackages("com.datadoghq.stickerlandia.stickeraward"); |
| 18 | + |
| 19 | + /** |
| 20 | + * Cross-Domain Boundary Violations. Based on cross-domain-issues.ql Ensures sticker and award |
| 21 | + * domains don't import from each other. |
| 22 | + */ |
| 23 | + @Test |
| 24 | + void sticker_domain_should_not_import_award_domain() { |
| 25 | + ArchRule rule = |
| 26 | + noClasses() |
| 27 | + .that() |
| 28 | + .resideInAPackage("..sticker..") |
| 29 | + .should() |
| 30 | + .dependOnClassesThat() |
| 31 | + .resideInAPackage("..award..") |
| 32 | + .because( |
| 33 | + "Sticker domain should not import from award domain."); |
| 34 | + |
| 35 | + rule.check(classes); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void award_domain_should_not_import_sticker_domain() { |
| 40 | + ArchRule rule = |
| 41 | + noClasses() |
| 42 | + .that() |
| 43 | + .resideInAPackage("..award..") |
| 44 | + .should() |
| 45 | + .dependOnClassesThat() |
| 46 | + .resideInAPackage("..sticker..") |
| 47 | + .because( |
| 48 | + "Award domain should not import from sticker domain."); |
| 49 | + |
| 50 | + rule.check(classes); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * REST API classes should not import entity types. Based on rest-api-no-entities.ql and |
| 55 | + * rest-api-imports-simple.ql Ensures REST resources don't directly use entity classes. |
| 56 | + */ |
| 57 | + @Test |
| 58 | + void rest_api_should_not_import_entities() { |
| 59 | + ArchRule rule = |
| 60 | + noClasses() |
| 61 | + .that() |
| 62 | + .haveSimpleNameEndingWith("Resource") |
| 63 | + .should() |
| 64 | + .dependOnClassesThat() |
| 65 | + .resideInAPackage("..entity..") |
| 66 | + .because( |
| 67 | + "REST API classes should not import entity types."); |
| 68 | + |
| 69 | + rule.check(classes); |
| 70 | + } |
| 71 | +} |
0 commit comments