Skip to content

Commit

Permalink
Development: Execute architecture tests during java style action (#7160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strohgelaender authored Sep 14, 2023
1 parent 9acb62f commit 21e611b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 38 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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<RequestMappingInfo, HandlerMethod> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RequestMappingInfo, HandlerMethod> 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);
}
}

0 comments on commit 21e611b

Please sign in to comment.