arch unit test for @Accepts only in AcceptanceTests

This commit is contained in:
Michael Hoennig 2022-08-17 13:21:05 +02:00
parent 2cb9375d03
commit 6be5cfd923
4 changed files with 32 additions and 19 deletions

View File

@ -3,11 +3,13 @@ package net.hostsharing.hsadminng.arch;
import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule; import com.tngtech.archunit.lang.ArchRule;
import net.hostsharing.hsadminng.Accepts;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices; import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;
@AnalyzeClasses(packages = ArchUnitTest.NET_HOSTSHARING_HSADMINNG) @AnalyzeClasses(packages = ArchUnitTest.NET_HOSTSHARING_HSADMINNG)
@ -50,6 +52,20 @@ public class ArchUnitTest {
.should().onlyBeAccessed().byClassesThat() .should().onlyBeAccessed().byClassesThat()
.resideInAnyPackage("..hs.hspackage.."); .resideInAnyPackage("..hs.hspackage..");
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule acceptsAnnotationOnMethodsRule = methods()
.that().areAnnotatedWith(Accepts.class)
.should().beDeclaredInClassesThat().haveSimpleNameEndingWith("AcceptanceTest")
.orShould().beDeclaredInClassesThat().haveSimpleNameNotContaining("AcceptanceTest$");
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule acceptsAnnotationOnClasesRule = classes()
.that().areAnnotatedWith(Accepts.class)
.should().haveSimpleNameEndingWith("AcceptanceTest")
.orShould().haveSimpleNameNotContaining("AcceptanceTest$");
@Test @Test
public void everythingShouldBeFreeOfCycles() { public void everythingShouldBeFreeOfCycles() {
slices().matching("net.hostsharing.hsadminng.(*)..").should().beFreeOfCycles(); slices().matching("net.hostsharing.hsadminng.(*)..").should().beFreeOfCycles();

View File

@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.containsString;
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { HsadminNgApplication.class, JpaAttempt.class } classes = { HsadminNgApplication.class, JpaAttempt.class }
) )
@Accepts({ "ROL:S(Schema)" }) @Accepts({ "GRT:S(Schema)" })
@Transactional(propagation = Propagation.NEVER) @Transactional(propagation = Propagation.NEVER)
class RbacGrantControllerAcceptanceTest { class RbacGrantControllerAcceptanceTest {

View File

@ -28,7 +28,6 @@ import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { RbacGrantRepository.class, Context.class, JpaAttempt.class }) @ComponentScan(basePackageClasses = { RbacGrantRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext @DirtiesContext
@Accepts({ "GRT:S(Schema)" })
class RbacGrantRepositoryIntegrationTest { class RbacGrantRepositoryIntegrationTest {
@Autowired @Autowired
@ -133,9 +132,7 @@ class RbacGrantRepositoryIntegrationTest {
@Transactional(propagation = Propagation.NEVER) @Transactional(propagation = Propagation.NEVER)
public void packageAdmin_canNotGrantPackageOwnerRole() { public void packageAdmin_canNotGrantPackageOwnerRole() {
// given // given
record Given(RbacUserEntity arbitraryUser, UUID packageOwnerRoleUuid) { record Given(RbacUserEntity arbitraryUser, UUID packageOwnerRoleUuid) {}
}
final var given = jpaAttempt.transacted(() -> { final var given = jpaAttempt.transacted(() -> {
// to find the uuids of we need to have access rights to these // to find the uuids of we need to have access rights to these
currentUser("admin@aaa.example.com"); currentUser("admin@aaa.example.com");