Skip to content

Commit

Permalink
Provide a new Spring Rule checking that repositories do not depend on…
Browse files Browse the repository at this point in the history
… services

CLoses gh-32
  • Loading branch information
mnhock authored and mnhock committed Jun 19, 2024
1 parent 75d52d7 commit 6ffbf8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Architecture rules are defined using Taikai's fluent API, allowing developers to
| | Repositories | `namesShouldEndWithRepository` | Repositories should end with "Repository" | Default (WITHOUT_TESTS) |
| | Repositories | `namesShouldMatch` | Repositories should match a regex pattern | Default (WITHOUT_TESTS) |
| | Repositories | `shouldBeAnnotatedWithRepository` | Repositories should be annotated with `@Repository` | Default (WITHOUT_TESTS) |
| | Repositories | `shouldNotDependOnServices` | Repositories should not depend on service classes annotated with `@Service.` | Default (WITHOUT_TESTS) |
| | Services | `namesShouldEndWithService` | Services should end with "Service" | Default (WITHOUT_TESTS) |
| | Services | `namesShouldMatch` | Services should match a regex pattern | Default (WITHOUT_TESTS) |
| | Services | `shouldBeAnnotatedWithService` | Services should be annotated with `@Service` | Default (WITHOUT_TESTS) |
Expand Down Expand Up @@ -362,13 +363,14 @@ Taikai.builder()
.check();
```

- **Repositories Configuration**: Ensure that repository classes end with "Repository" or match a specific regex pattern and are annotated with `@Repository`.
- **Repositories Configuration**: Ensure that repository classes end with "Repository" or match a specific regex pattern and are annotated with `@Repository` and not depend on classes annotated with `@Service`.

```java
Taikai.builder()
.namespace("com.company.yourproject")
.spring(spring -> spring
.repositories(repositories -> repositories
.shouldNotDependOnServices()
.shouldBeAnnotatedWithRepository()
.namesShouldMatch("regex")
.namesShouldEndWithRepository()))
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/enofex/taikai/spring/RepositoriesConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static com.enofex.taikai.spring.SpringPredicates.ANNOTATION_REPOSITORY;
import static com.enofex.taikai.spring.SpringPredicates.annotatedWithRepository;
import static com.enofex.taikai.spring.SpringPredicates.annotatedWithService;
import static com.tngtech.archunit.lang.conditions.ArchConditions.be;
import static com.tngtech.archunit.lang.conditions.ArchConditions.dependOnClassesThat;
import static com.tngtech.archunit.lang.conditions.ArchConditions.not;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.are;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

Expand Down Expand Up @@ -58,5 +61,17 @@ public RepositoriesConfigurer shouldBeAnnotatedWithRepository(String regex,
.as("Repositories should be annotated with %s".formatted(ANNOTATION_REPOSITORY)),
configuration));
}

public RepositoriesConfigurer shouldNotDependOnServices() {
return shouldNotDependOnServices(null);
}

public RepositoriesConfigurer shouldNotDependOnServices(Configuration configuration) {
return addRule(TaikaiRule.of(classes()
.that(are(annotatedWithRepository(true)))
.should(not(dependOnClassesThat(annotatedWithService(true))))
.as("Repositories should not depend on Services"),
configuration));
}
}

1 change: 1 addition & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void main(String[] args) {
.namesShouldMatch("regex")
.namesShouldEndWithService())
.repositories(repositories -> repositories
.shouldNotDependOnServices()
.shouldBeAnnotatedWithRepository()
.namesShouldMatch("regex")
.namesShouldEndWithRepository()))
Expand Down

0 comments on commit 6ffbf8e

Please sign in to comment.