refactor: dip 적용으로 의존성을 느슨한 결합으로 전환 및 디자인 패턴 적용
This commit is contained in:
@@ -4,60 +4,88 @@ import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noFields;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.tngtech.archunit.core.domain.JavaClasses;
|
||||
import com.tngtech.archunit.core.importer.ClassFileImporter;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OneToOne;
|
||||
|
||||
class ArchitectureTest {
|
||||
@Test
|
||||
@DisplayName("수강신청 mutation → RegistrationLockManager 의존")
|
||||
void registrationMutationUsesLockBoundary() {
|
||||
JavaClasses classes = new ClassFileImporter().importPath(Path.of("build/classes/java/main"));
|
||||
private final JavaClasses classes =
|
||||
new ClassFileImporter().importPath(Path.of("build/classes/java/main"));
|
||||
|
||||
@Test
|
||||
@DisplayName("수강신청 UseCase → RegistrationContextLoader port 의존")
|
||||
void registrationMutationUsesContextPort() {
|
||||
classes()
|
||||
.that().haveSimpleName("CourseRegistrationService")
|
||||
.should().dependOnClassesThat().haveSimpleName("RegistrationLockManager")
|
||||
.that().haveSimpleName("RegisterCourseUseCase")
|
||||
.should().dependOnClassesThat().haveSimpleName("RegistrationContextLoader")
|
||||
.check(classes);
|
||||
|
||||
noClasses()
|
||||
.that().haveSimpleName("CourseRegistrationService")
|
||||
.should().dependOnClassesThat().haveSimpleName("LessonRepository")
|
||||
classes()
|
||||
.that().haveSimpleName("CancelRegistrationUseCase")
|
||||
.should().dependOnClassesThat().haveSimpleName("RegistrationContextLoader")
|
||||
.check(classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("새 service → registration write repository 우회 금지")
|
||||
void registrationWriteRepositoriesStayInsideApprovedServices() {
|
||||
JavaClasses classes = new ClassFileImporter().importPath(Path.of("build/classes/java/main"));
|
||||
|
||||
@DisplayName("application → Spring Data JPA·repository·web 표현 의존 금지")
|
||||
void applicationDoesNotKnowPersistenceMechanismOrWebRepresentation() {
|
||||
noClasses()
|
||||
.that().resideInAPackage("..service..")
|
||||
.and().doNotHaveSimpleName("CourseRegistrationService")
|
||||
.and().doNotHaveSimpleName("LessonQueryService")
|
||||
.should().dependOnClassesThat().haveSimpleName("UserLessonRepository")
|
||||
.that().resideInAPackage("..application..")
|
||||
.should().dependOnClassesThat().resideInAnyPackage(
|
||||
"..repository..",
|
||||
"org.springframework.data..",
|
||||
"jakarta.persistence..",
|
||||
"..web..",
|
||||
"..dto..")
|
||||
.check(classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("domain → application·infrastructure·web·repository 의존 금지")
|
||||
void domainStaysInsideDomainBoundary() {
|
||||
noClasses()
|
||||
.that().resideInAPackage("..service..")
|
||||
.and().doNotHaveSimpleName("CourseRegistrationService")
|
||||
.and().doNotHaveSimpleName("IdempotencyRetentionJob")
|
||||
.should().dependOnClassesThat().haveSimpleName("RegistrationIdempotencyRepository")
|
||||
.that().resideInAPackage("..domain..")
|
||||
.should().dependOnClassesThat().resideInAnyPackage(
|
||||
"..application..",
|
||||
"..infrastructure..",
|
||||
"..web..",
|
||||
"..repository..",
|
||||
"org.springframework.data..",
|
||||
"jakarta.persistence..")
|
||||
.check(classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("web → repository·infrastructure 직접 접근 금지")
|
||||
void webDependsOnApplicationBoundary() {
|
||||
noClasses()
|
||||
.that().resideInAPackage("..web..")
|
||||
.should().dependOnClassesThat().resideInAnyPackage(
|
||||
"..repository..",
|
||||
"..infrastructure..")
|
||||
.check(classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("JPA repository → infrastructure 밖 application에서 직접 참조 금지")
|
||||
void rawRepositoriesStayBehindInfrastructureAdapters() {
|
||||
noClasses()
|
||||
.that().resideInAPackage("..application..")
|
||||
.should().dependOnClassesThat().resideInAPackage("..repository..")
|
||||
.check(classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("엔티티 연관관계 → ManyToOne만 허용")
|
||||
void entityAssociationsUseManyToOneOnly() {
|
||||
JavaClasses classes = new ClassFileImporter().importPath(Path.of("build/classes/java/main"));
|
||||
|
||||
noFields()
|
||||
.that().areDeclaredInClassesThat().resideInAPackage("..entity..")
|
||||
.should().beAnnotatedWith(OneToOne.class)
|
||||
|
||||
Reference in New Issue
Block a user