Skip to content

Commit

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

CLoses gh-33
  • Loading branch information
mnhock authored and mnhock committed Jun 19, 2024
1 parent 22b1ef7 commit 75d52d7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Architecture rules are defined using Taikai's fluent API, allowing developers to
| | Configurations | `namesShouldMatch` | Configuration classes should match a regex pattern | Default (WITHOUT_TESTS) |
| | Controllers | `namesShouldEndWithController` | Controllers should end with "Controller" | Default (WITHOUT_TESTS) |
| | Controllers | `namesShouldMatch` | Controllers should match a regex pattern | Default (WITHOUT_TESTS) |
| | Controllers | `shouldBeAnnotatedWithController` | Controllers should be annotated with `@Controller` | Default (WITHOUT_TESTS) |
| | Controllers | `shouldBeAnnotatedWithRestController` | Controllers should be annotated with `@RestController` | Default (WITHOUT_TESTS) |
| | Controllers | `shouldBePackagePrivate` | Controllers should be package-private | Default (WITHOUT_TESTS) |
| | Controllers | `shouldNotDependOnOtherControllers` | Controllers should not depend on other controllers | Default (WITHOUT_TESTS) |
Expand All @@ -63,6 +64,7 @@ Architecture rules are defined using Taikai's fluent API, allowing developers to
| | 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) |
| | Services | `shouldNotDependOnControllers` | Services should not depend on Controllers | Default (WITHOUT_TESTS) |

### Java Configuration

Expand Down Expand Up @@ -345,14 +347,15 @@ Taikai.builder()
.check();
```

- **Services Configuration**: Ensure that service classes end with "Service" or match a specific regex pattern and are annotated with `@Service`.
- **Services Configuration**: Ensure that service classes end with "Service" or match a specific regex pattern and are annotated with `@Service` and do not depend on other controllers.

```java
Taikai.builder()
.namespace("com.company.yourproject")
.spring(spring -> spring
.services(services -> services
.shouldBeAnnotatedWithService()
.shouldBeAnnotatedWithService()
.shouldNotDependOnControllers()
.namesShouldMatch("regex")
.namesShouldEndWithService()))
.build()
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/enofex/taikai/spring/ServicesConfigurer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.enofex.taikai.spring;

import static com.enofex.taikai.spring.SpringPredicates.ANNOTATION_SERVICE;
import static com.enofex.taikai.spring.SpringPredicates.annotatedWithControllerOrRestController;
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 ServicesConfigurer shouldBeAnnotatedWithService(String regex,
.as("Services should be annotated with %s".formatted(ANNOTATION_SERVICE)),
configuration));
}

public ServicesConfigurer shouldNotDependOnControllers() {
return shouldNotDependOnControllers(null);
}

public ServicesConfigurer shouldNotDependOnControllers(Configuration configuration) {
return addRule(TaikaiRule.of(classes()
.that(are(annotatedWithService(true)))
.should(not(dependOnClassesThat(annotatedWithControllerOrRestController(true))))
.as("Services should not depend on Controllers or RestControllers"),
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 @@ -23,6 +23,7 @@ public static void main(String[] args) {
.namesShouldMatch("regex"))
.services(services -> services
.shouldBeAnnotatedWithService()
.shouldNotDependOnControllers()
.namesShouldMatch("regex")
.namesShouldEndWithService())
.repositories(repositories -> repositories
Expand Down

0 comments on commit 75d52d7

Please sign in to comment.