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");

View File

@ -43,22 +43,22 @@ class RbacRoleControllerAcceptanceTest {
// @formatter:off // @formatter:off
RestAssured RestAssured
.given() .given()
.header("current-user", "mike@hostsharing.net") .header("current-user", "mike@hostsharing.net")
.port(port) .port(port)
.when() .when()
.get("http://localhost/api/rbac-roles") .get("http://localhost/api/rbac-roles")
.then().assertThat() .then().assertThat()
.statusCode(200) .statusCode(200)
.contentType("application/json") .contentType("application/json")
.body("[0].roleName", is("customer#aaa.admin")) .body("[0].roleName", is("customer#aaa.admin"))
.body("[1].roleName", is("customer#aaa.owner")) .body("[1].roleName", is("customer#aaa.owner"))
.body("[2].roleName", is("customer#aaa.tenant")) .body("[2].roleName", is("customer#aaa.tenant"))
// ... // ...
.body("", hasItem(hasEntry("roleName", "global#hostsharing.admin"))) .body("", hasItem(hasEntry("roleName", "global#hostsharing.admin")))
.body("", hasItem(hasEntry("roleName", "customer#aab.admin"))) .body("", hasItem(hasEntry("roleName", "customer#aab.admin")))
.body("", hasItem(hasEntry("roleName", "package#aab00.admin"))) .body("", hasItem(hasEntry("roleName", "package#aab00.admin")))
.body("", hasItem(hasEntry("roleName", "unixuser#aab00-aaaa.owner"))) .body("", hasItem(hasEntry("roleName", "unixuser#aab00-aaaa.owner")))
.body( "size()", is(73)); // increases with new test data .body( "size()", is(73)); // increases with new test data
// @formatter:on // @formatter:on
} }