Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArchUnit Tests #41

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'
testImplementation group: 'com.tngtech.archunit', name: 'archunit-junit5', version: '1.0.1'

implementation 'com.github.slugify:slugify:2.4'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.hhu.propra.link.archtests;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import de.hhu.propra.link.LinkApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tngtech.archunit.core.importer.ImportOption;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;

@AnalyzeClasses(packagesOf = LinkApplication.class,
importOptions = ImportOption.DoNotIncludeTests.class)
public class ControllerArchTests {

@ArchTest
static final ArchRule getMappingMethodsInControllersShouldReturnStringOrModelAndView =
methods()
.that()
.areAnnotatedWith(GetMapping.class)
.or()
.areAnnotatedWith(PostMapping.class)
.and()
.areDeclaredInClassesThat()
.areAnnotatedWith(Controller.class)
.should()
.haveRawReturnType(String.class)
.orShould()
.haveRawReturnType(ModelAndView.class);
@ArchTest
public ArchRule controllerClassNames =
classes()
.that()
.areAnnotatedWith(Controller.class)
.or()
.haveSimpleNameEndingWith("Controller")
.should()
.beAnnotatedWith(Controller.class)
.andShould()
.haveSimpleNameEndingWith("Controller");

@ArchTest
public ArchRule controllerClassLocation =
classes()
.that()
.areAnnotatedWith(Controller.class)
.or()
.haveSimpleNameEndingWith("Controller")
.should()
.resideInAPackage("de.hhu.propra.link.controllers");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package de.hhu.propra.link.archtests;

import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAPackage;
import static com.tngtech.archunit.lang.conditions.ArchConditions.*;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.CompositeArchRule;
import de.hhu.propra.link.LinkApplication;


@AnalyzeClasses(packagesOf = LinkApplication.class,
importOptions = ImportOption.DoNotIncludeTests.class)
public class LayeredArchitectureTests {

@ArchTest
public ArchRule layeredArchitectureForRepository =
CompositeArchRule.of(
classes()
.that()
.resideInAPackage("de.hhu.propra.link.repositories")
.should(
not(
dependOnClassesThat(
resideInAPackage("de.hhu.propra.link.services"))))
.andShould(
not(dependOnClassesThat(resideInAPackage("de.hhu.propra.link.controllers"))))
.andShould(
onlyBeAccessedByClassesThat(resideInAPackage("de.hhu.propra.link")))
.because("Persistence layer is the bottommost layer."));

@ArchTest
public ArchRule layeredArchitectureForDomain =
CompositeArchRule.of(
classes()
.that()
.resideInAPackage("de.hhu.propra.link.entities")
.should(
not(
dependOnClassesThat(
resideInAPackage("de.hhu.propra.link.services"))))
.andShould(
not(dependOnClassesThat(resideInAPackage("de.hhu.propra.link.controllers"))))
.andShould(
onlyBeAccessedByClassesThat(
resideInAPackage("de.hhu.propra.link.entities")
.or(resideInAPackage("de.hhu.propra.link.services"))
.or(resideInAPackage("de.hhu.propra.link.controllers"))))
.because("Domain layer is above persistence layer."));

@ArchTest
public ArchRule layeredArchitectureForServices =
CompositeArchRule.of(
classes()
.that()
.resideInAPackage("de.hhu.propra.link.services")
.should(
not(dependOnClassesThat(resideInAPackage("de.hhu.propra.link.controller"))))
.andShould(
onlyBeAccessedByClassesThat(
resideInAPackage("de.hhu.propra.link.services")
.or(resideInAPackage("de.hhu.propra.link.validation"))
.or(resideInAPackage("de.hhu.propra.link.controllers"))))
.because("Service layer is above domain layer."));

@ArchTest
public ArchRule layeredArchitectureForController =
CompositeArchRule.of(
classes()
.that()
.resideInAPackage("de.hhu.propra.link.controllers")
.should(
onlyBeAccessedByClassesThat(
resideInAPackage("de.hhu.propra.link.controllers")))
.because("Controller layer is the topmost layer."));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.hhu.propra.link.archtests;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import de.hhu.propra.link.LinkApplication;
import org.springframework.stereotype.Repository;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

@AnalyzeClasses(packagesOf = LinkApplication.class,
importOptions = ImportOption.DoNotIncludeTests.class)
public class RepositoryArchTests {


@ArchTest
public ArchRule repositoryClassNames =
classes()
.that()
.areAnnotatedWith(Repository.class)
.or()
.haveSimpleNameEndingWith("Repository")
.should()
.beAnnotatedWith(Repository.class)
.andShould()
.haveSimpleNameEndingWith("Repository");

@ArchTest
public ArchRule repositoryClassLocation =
classes()
.that()
.areAnnotatedWith(Repository.class)
.or()
.haveSimpleNameEndingWith("Repository")
.should()
.resideInAPackage("de.hhu.propra.link.repositories");
}
38 changes: 38 additions & 0 deletions src/test/java/de/hhu/propra/link/archtests/ServiceArchTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.hhu.propra.link.archtests;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import de.hhu.propra.link.LinkApplication;
import org.springframework.stereotype.Service;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

@AnalyzeClasses(packagesOf = LinkApplication.class,
importOptions = ImportOption.DoNotIncludeTests.class)
public class ServiceArchTests {

@ArchTest
public ArchRule serviceClassNames =
classes()
.that()
.areAnnotatedWith(Service.class)
.or()
.haveSimpleNameEndingWith("Service")
.should()
.beAnnotatedWith(Service.class)
.andShould()
.haveSimpleNameEndingWith("Service");

@ArchTest
public ArchRule serviceClassLocation =
classes()
.that()
.areAnnotatedWith(Service.class)
.or()
.haveSimpleNameEndingWith("Service")
.should()
.resideInAPackage("de.hhu.propra.link.services");

}