-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed512d7
commit 5f98ee1
Showing
7 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/ch/xxx/aidoclibchat/domain/exceptions/DocumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch.xxx.aidoclibchat.domain.exceptions; | ||
|
||
public class DocumentException extends RuntimeException { | ||
private static final long serialVersionUID = -5601313921637319936L; | ||
|
||
public DocumentException(String message, Throwable th) { | ||
super(message, th); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
backend/src/test/java/ch/xxx/aidoclibchat/architecture/MyArchitectureTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* Copyright 2023 Sven Loesekann | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package ch.xxx.aidoclibchat.architecture; | ||
|
||
import static com.tngtech.archunit.lang.conditions.ArchConditions.beAnnotatedWith; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.domain.JavaField; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.core.importer.ImportOption; | ||
import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests; | ||
import com.tngtech.archunit.core.importer.Location; | ||
import com.tngtech.archunit.junit.AnalyzeClasses; | ||
import com.tngtech.archunit.junit.ArchTest; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
import com.tngtech.archunit.lang.CompositeArchRule; | ||
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; | ||
import com.tngtech.archunit.library.Architectures; | ||
import com.tngtech.archunit.library.GeneralCodingRules; | ||
import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition; | ||
|
||
import ch.xxx.aidoclibchat.architecture.MyArchitectureTests.DoNotIncludeGenerated; | ||
import jakarta.annotation.PostConstruct; | ||
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; | ||
|
||
@AnalyzeClasses(packages = "ch.xxx.aaidoclibchat", importOptions = { DoNotIncludeTests.class, | ||
DoNotIncludeGenerated.class }) | ||
public class MyArchitectureTests { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MyArchitectureTests.class); | ||
private static final ArchRule NO_CLASSES_SHOULD_USE_FIELD_INJECTION = createNoFieldInjectionRule(); | ||
|
||
private JavaClasses importedClasses = new ClassFileImporter() | ||
.withImportOptions(List.of(new DoNotIncludeTests(), new DoNotIncludeGenerated())) | ||
.importPackages("ch.xxx.aidoclibchat"); | ||
|
||
@ArchTest | ||
static final ArchRule clean_architecture_respected = Architectures.onionArchitecture().domainModels("..domain..") | ||
.applicationServices("..usecase..").adapter("rest", "..adapter.controller..") | ||
// .adapter("cron", "..adapter.cron..") | ||
.adapter("repo", "..adapter.repository..") | ||
// .adapter("events", "..adapter.events..") | ||
// .adapter("client", "..adapter.client..") | ||
.adapter("config", "..adapter.config..").withOptionalLayers(true); | ||
|
||
@Test | ||
public void cyclesDomain() { | ||
SlicesRuleDefinition.slices().matching("..domain.(*)..").should().beFreeOfCycles(); | ||
} | ||
|
||
@Test | ||
public void cyclesUseCases() { | ||
SlicesRuleDefinition.slices().matching("..usecase.(*)..").should().beFreeOfCycles(); | ||
} | ||
|
||
@Test | ||
public void cyclesAdapter() { | ||
SlicesRuleDefinition.slices().matching("..adapter.(*)..").should().beFreeOfCycles(); | ||
} | ||
|
||
@Test | ||
public void ruleControllerAnnotations() { | ||
ArchRule beAnnotatedWith = ArchRuleDefinition.classes().that().resideInAPackage("..adapter.controller..") | ||
.should().beAnnotatedWith(RestController.class).orShould().beAnnotatedWith(Configuration.class); | ||
beAnnotatedWith.check(this.importedClasses); | ||
} | ||
|
||
@Test | ||
public void ruleExceptionsType() { | ||
ArchRule exceptionType = ArchRuleDefinition.classes().that().resideInAPackage("..domain.exceptions..").should() | ||
.beAssignableTo(RuntimeException.class).orShould().beAssignableTo(DefaultErrorAttributes.class); | ||
exceptionType.check(this.importedClasses); | ||
} | ||
|
||
// @Test | ||
public void ruleCronJobMethodsAnnotations() { | ||
ArchRule exceptionType = ArchRuleDefinition.methods().that().arePublic().and().areDeclaredInClassesThat() | ||
.resideInAPackage("..adapter.cron..").should().beAnnotatedWith(PostConstruct.class).orShould() | ||
.beAnnotatedWith(Scheduled.class).andShould().beAnnotatedWith(SchedulerLock.class).orShould() | ||
.beAnnotatedWith(Order.class); | ||
exceptionType.check(this.importedClasses); | ||
} | ||
|
||
@Test | ||
public void ruleGeneralCodingRulesLoggers() { | ||
ArchRuleDefinition.fields().that().haveRawType(Logger.class).should().bePrivate().andShould().beStatic() | ||
.andShould().beFinal().because("we agreed on this convention").check(this.importedClasses); | ||
} | ||
|
||
@Test | ||
public void ruleGeneralCodingRules() { | ||
ArchRule archRule = CompositeArchRule.of(GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS) | ||
.and(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).because("Good practice"); | ||
JavaClasses classesToCheck = this.importedClasses | ||
.that(JavaClass.Predicates.resideOutsideOfPackages("..adapter.clients.test..")); | ||
archRule.check(classesToCheck); | ||
} | ||
|
||
private static ArchRule createNoFieldInjectionRule() { | ||
ArchCondition<JavaField> annotatedWithSpringAutowired = beAnnotatedWith( | ||
"org.springframework.beans.factory.annotation.Autowired"); | ||
ArchCondition<JavaField> annotatedWithGuiceInject = beAnnotatedWith("com.google.inject.Inject"); | ||
ArchCondition<JavaField> annotatedWithJakartaInject = beAnnotatedWith("javax.inject.Inject"); | ||
ArchRule beAnnotatedWithAnInjectionAnnotation = ArchRuleDefinition.noFields() | ||
.should(annotatedWithSpringAutowired.or(annotatedWithGuiceInject).or(annotatedWithJakartaInject) | ||
.as("be annotated with an injection annotation")); | ||
return beAnnotatedWithAnInjectionAnnotation; | ||
} | ||
|
||
static final class DoNotIncludeGenerated implements ImportOption { | ||
private static final Pattern GENERATED_CLASSES_PATTERN = Pattern.compile(".+\\$\\d+\\.class$"); | ||
private static final Pattern AOT_GENERATED_PATTERN = Pattern | ||
.compile(".*(__BeanDefinitions|SpringCGLIB\\$\\$0)\\.class$"); | ||
private static final Pattern AOT_TEST_GENERATED_PATTERN = Pattern.compile(".*__TestContext.*\\.class$"); | ||
|
||
@Override | ||
public boolean includes(Location location) { | ||
return !(location.matches(AOT_GENERATED_PATTERN) || location.matches(AOT_TEST_GENERATED_PATTERN) || location.matches(GENERATED_CLASSES_PATTERN)); | ||
} | ||
} | ||
} |