From 21e611bde4b188bfc199f6d0c5b4fa397a6bd0f5 Mon Sep 17 00:00:00 2001 From: Lucas Welscher Date: Thu, 14 Sep 2023 10:59:12 +0200 Subject: [PATCH] Development: Execute architecture tests during java style action (#7160) --- .github/workflows/test.yml | 10 ++++++ build.gradle | 13 +++++-- .../artemis/AbstractArchitectureTest.java | 13 ++++--- ...ava => AuthorizationArchitectureTest.java} | 31 +--------------- .../AuthorizationEndpointTest.java | 36 +++++++++++++++++++ 5 files changed, 65 insertions(+), 38 deletions(-) rename src/test/java/de/tum/in/www1/artemis/authorization/{AuthorizationTest.java => AuthorizationArchitectureTest.java} (73%) create mode 100644 src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationEndpointTest.java diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1cd3fd6437fd..5daa40581328 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -209,6 +209,16 @@ jobs: - name: Java Documentation run: ./gradlew checkstyleMain -x webapp if: success() || failure() + - name: Java Architecture Tests + run: ./gradlew test -DincludeTags='ArchitectureTest' -x webapp + if: success() || failure() + - name: Test Report + uses: dorny/test-reporter@v1 + if: success() || failure() # run this step even if previous step failed + with: + name: Java Architecture Tests + path: build/test-results/test/*.xml + reporter: java-junit client-tests: runs-on: ubuntu-latest diff --git a/build.gradle b/build.gradle index 19452c131ffa..b59c9016479f 100644 --- a/build.gradle +++ b/build.gradle @@ -96,10 +96,17 @@ modernizer { } // Execute the test cases: ./gradlew test - +// Execute only architecture tests: ./gradlew test -DincludeTags='ArchitectureTest' test { - useJUnitPlatform() - exclude "**/*IT*", "**/*IntTest*" + if (System.getProperty("includeTags")) { + useJUnitPlatform { + includeTags System.getProperty("includeTags") + } + } else { + useJUnitPlatform() + exclude "**/*IT*", "**/*IntTest*" + } + testLogging { events "FAILED", "SKIPPED" } diff --git a/src/test/java/de/tum/in/www1/artemis/AbstractArchitectureTest.java b/src/test/java/de/tum/in/www1/artemis/AbstractArchitectureTest.java index 1f0a80ac3352..ded1b8c42ab4 100644 --- a/src/test/java/de/tum/in/www1/artemis/AbstractArchitectureTest.java +++ b/src/test/java/de/tum/in/www1/artemis/AbstractArchitectureTest.java @@ -3,12 +3,14 @@ import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; import com.tngtech.archunit.core.domain.JavaClasses; import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; -public abstract class AbstractArchitectureTest extends AbstractSpringIntegrationBambooBitbucketJiraTest { +@Tag("ArchitectureTest") +public abstract class AbstractArchitectureTest { protected static final String ARTEMIS_PACKAGE = "de.tum.in.www1.artemis"; @@ -20,10 +22,11 @@ public abstract class AbstractArchitectureTest extends AbstractSpringIntegration @BeforeAll static void loadClasses() { - testClasses = new ClassFileImporter().withImportOption(new ImportOption.OnlyIncludeTests()).importPackages(ARTEMIS_PACKAGE); - productionClasses = new ClassFileImporter().withImportOption(new ImportOption.DoNotIncludeTests()).importPackages(ARTEMIS_PACKAGE); - allClasses = new ClassFileImporter().importPackages(ARTEMIS_PACKAGE); - + if (allClasses == null) { + testClasses = new ClassFileImporter().withImportOption(new ImportOption.OnlyIncludeTests()).importPackages(ARTEMIS_PACKAGE); + productionClasses = new ClassFileImporter().withImportOption(new ImportOption.DoNotIncludeTests()).importPackages(ARTEMIS_PACKAGE); + allClasses = new ClassFileImporter().importPackages(ARTEMIS_PACKAGE); + } ensureClassSetsNonEmpty(); ensureAllClassesFound(); } diff --git a/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationTest.java b/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationArchitectureTest.java similarity index 73% rename from src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationTest.java rename to src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationArchitectureTest.java index 6974f06d5f6a..49d7c38acc90 100644 --- a/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationArchitectureTest.java @@ -2,34 +2,16 @@ import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; -import java.util.stream.Collectors; - import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.method.HandlerMethod; -import org.springframework.web.servlet.mvc.method.RequestMappingInfo; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import com.tngtech.archunit.lang.ArchRule; import de.tum.in.www1.artemis.AbstractArchitectureTest; import de.tum.in.www1.artemis.security.annotations.*; -/** - * Contains the one automatic test covering all rest endpoints for authorization tests. - */ -class AuthorizationTest extends AbstractArchitectureTest { - - @Autowired - private ApplicationContext applicationContext; - - @Autowired - private AuthorizationTestService authorizationTestService; +class AuthorizationArchitectureTest extends AbstractArchitectureTest { private static final String ARTEMIS_PACKAGE = "de.tum.in.www1.artemis"; @@ -39,17 +21,6 @@ class AuthorizationTest extends AbstractArchitectureTest { private static final String REST_OPEN_PACKAGE = REST_BASE_PACKAGE + ".open"; - @Test - void testEndpoints() throws InvocationTargetException, IllegalAccessException { - var requestMappingHandlerMapping = applicationContext.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class); - Map endpointMap = requestMappingHandlerMapping.getHandlerMethods(); - // Filter out endpoints that should not be tested. - endpointMap = endpointMap.entrySet().stream().filter(entry -> authorizationTestService.validEndpointToTest(entry.getValue(), false)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - - authorizationTestService.testEndpoints(endpointMap); - } - @Test void testNoPreAuthorizeOnRestControllers() { ArchRule rule = noClasses().that().areAnnotatedWith(RestController.class).should().beAnnotatedWith(PreAuthorize.class).because( diff --git a/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationEndpointTest.java b/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationEndpointTest.java new file mode 100644 index 000000000000..365a07916d17 --- /dev/null +++ b/src/test/java/de/tum/in/www1/artemis/authorization/AuthorizationEndpointTest.java @@ -0,0 +1,36 @@ +package de.tum.in.www1.artemis.authorization; + +import java.util.Map; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest; + +/** + * Contains the one automatic test covering all rest endpoints for authorization tests. + */ +class AuthorizationEndpointTest extends AbstractSpringIntegrationBambooBitbucketJiraTest { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private AuthorizationTestService authorizationTestService; + + @Test + void testEndpoints() { + var requestMappingHandlerMapping = applicationContext.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class); + Map endpointMap = requestMappingHandlerMapping.getHandlerMethods(); + // Filter out endpoints that should not be tested. + endpointMap = endpointMap.entrySet().stream().filter(entry -> authorizationTestService.validEndpointToTest(entry.getValue(), false)) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + + authorizationTestService.testEndpoints(endpointMap); + } +}