-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArchitectureTest.java
243 lines (197 loc) · 9.98 KB
/
ArchitectureTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package br.com.helpdev.sample;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.library.Architectures;
/**
* This class is responsible for testing the architecture of the application.
* ---
* The architecture is based on the hexagonal architecture pattern.
* The hexagonal architecture pattern is a way to structure your application to make it more testable, maintainable, and flexible.
* Don't change the tests in this class, they are used to validate the architecture of the application.
* If you want to change make sure you understand the consequences.
* ---
*/
@DisplayName("Architecture Tests")
class ArchitectureTest {
private static final String CORE_LAYER = "Core";
private static final String ADAPTERS_LAYER = "Adapters";
private static final String CONFIG_LAYER = "Config";
private static final String CORE_PORTS_INPUT_LAYER = "Core Ports Input";
private static final String CORE_PORTS_OUTPUT_LAYER = "Core Ports Output";
private static final String CORE_USE_CASES_LAYER = "Core Use Cases";
private static final String CORE_DOMAIN_LAYER = "Core Domain";
private static final String ADAPTER_INPUT_LAYER = "Adapter Input";
private static final String ADAPTER_OUTPUT_LAYER = "Adapter Output";
/**
* Get the base package of the class.
* If you move this class to another package, you need to fix this method to return the correct package code.
*
* @return The base package of the class.
*/
private String getBasePackage() {
return this.getClass().getPackageName();
}
@Test
@DisplayName("The layers should have the correct dependencies. The Core layer should only be accessed by the Config and Adapters layers. The "
+ "Adapters layer should only be accessed by the Config layer. The Config layer should not be accessed by any layer.")
void hexagonalArchitectureLayersHaveCorrectDependencies() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter().importPackages(basePackage);
final var hexagonalArchitecture = Architectures
.layeredArchitecture()
.consideringOnlyDependenciesInLayers()
.layer(CORE_LAYER)
.definedBy(basePackage + ".core..")
.layer(ADAPTERS_LAYER)
.definedBy(basePackage + ".adapters..")
.layer(CONFIG_LAYER)
.definedBy(basePackage + ".config..")
.whereLayer(CONFIG_LAYER)
.mayNotBeAccessedByAnyLayer()
.whereLayer(ADAPTERS_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER)
.whereLayer(CORE_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER, ADAPTERS_LAYER)
.allowEmptyShould(true);
hexagonalArchitecture.check(importedClasses);
}
@Test
@DisplayName("Check the details of the dependencies between the layers. All details should be correct.")
void hexagonalArchitectureLayersHaveCorrectDependenciesDetails() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter().importPackages(basePackage);
final var detailedArchitecture = Architectures
.layeredArchitecture()
.consideringOnlyDependenciesInLayers()
.layer(CORE_PORTS_INPUT_LAYER)
.definedBy(basePackage + ".core.ports.input..")
.layer(CORE_PORTS_OUTPUT_LAYER)
.definedBy(basePackage + ".core.ports.output..")
.layer(CORE_USE_CASES_LAYER)
.definedBy(basePackage + ".core.usecases..")
.layer(CORE_DOMAIN_LAYER)
.definedBy(basePackage + ".core.domain..")
.layer(ADAPTER_INPUT_LAYER)
.definedBy(basePackage + ".adapter.input..")
.layer(ADAPTER_OUTPUT_LAYER)
.definedBy(basePackage + ".adapter.output..")
.layer(CONFIG_LAYER)
.definedBy(basePackage + ".config..")
.whereLayer(CONFIG_LAYER)
.mayNotBeAccessedByAnyLayer()
.whereLayer(ADAPTER_INPUT_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER)
.whereLayer(ADAPTER_OUTPUT_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER)
.whereLayer(CORE_USE_CASES_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER)
.whereLayer(CORE_PORTS_INPUT_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER, CORE_USE_CASES_LAYER, ADAPTER_INPUT_LAYER)
.whereLayer(CORE_PORTS_OUTPUT_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER, CORE_USE_CASES_LAYER, ADAPTER_OUTPUT_LAYER)
.whereLayer(CORE_DOMAIN_LAYER)
.mayOnlyBeAccessedByLayers(CONFIG_LAYER, CORE_USE_CASES_LAYER, CORE_PORTS_INPUT_LAYER, CORE_PORTS_OUTPUT_LAYER, ADAPTER_INPUT_LAYER,
ADAPTER_OUTPUT_LAYER)
.allowEmptyShould(true);
detailedArchitecture.check(importedClasses);
}
@Test
@DisplayName("The ports should be interfaces and end with Port. It is a good practice to use the suffix Port to identify the ports.")
void portsShouldBeInterfacesAndEndWithPort() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(basePackage);
classes()
.that()
.resideInAnyPackage(basePackage + ".core.ports..")
.should()
.beInterfaces()
.andShould()
.haveSimpleNameEndingWith("Port")
.check(importedClasses);
}
@Test
@DisplayName("The classes that implement any Port interface should be package-private. Remove the public modifier from the class and constructor"
+ ". Its to guarantee that implementation details are hidden.")
void everyClassThatImplementAnyPortShouldBePackagePrivate() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(basePackage);
classes().that(new DescribedPredicate<JavaClass>("implement any Port interface") {
@Override
public boolean test(JavaClass javaClass) {
return javaClass.getInterfaces().stream().anyMatch(i -> i.getName().endsWith("Port"));
}
}).should().bePackagePrivate().check(importedClasses);
}
@Test
@DisplayName("The use cases should implement the input ports and be package-private and end with UseCase; Remove the public modifier from the "
+ "class and constructor.")
void useCasesShouldImplementInputPortsAndBePackagePrivateAndEndWithUseCase() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(basePackage);
classes()
.that()
.resideInAnyPackage(basePackage + ".core.usecases..")
.should()
.haveSimpleNameEndingWith("UseCase")
.andShould(shouldImplementAnyClassInPackage(basePackage + ".core.ports.input"))
.check(importedClasses);
}
@Test
@DisplayName("One use case should not use another use case or input port because it is a violation of the hexagonal architecture pattern with "
+ "cross dependencies. Instead, use a domain service.")
void shouldNotUseCaseUsageOtherUseCase() {
String basePackage = getBasePackage();
JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(basePackage);
classes().that().resideInAnyPackage(basePackage + ".core.usecases..").should(new ArchCondition<JavaClass>("not usage other UseCase") {
@Override
public void check(JavaClass javaClass, ConditionEvents conditionEvents) {
javaClass
.getFields()
.stream()
.filter(javaField -> javaField.getRawType().getPackageName().contains(".core.ports.input") || javaField
.getRawType()
.getPackageName()
.contains(".core.usecases"))
.forEach(javaField -> {
conditionEvents.add(SimpleConditionEvent.violated(javaClass, "The " + javaClass.getName() + " should not use other UseCase"));
});
}
}).check(importedClasses);
}
private static ArchCondition<JavaClass> shouldImplementAnyClassInPackage(final String basePackage) {
return new ArchCondition<JavaClass>("implement any class in the package " + basePackage) {
@Override
public void check(JavaClass javaClass, ConditionEvents conditionEvents) {
final var useCaseInterfaces = javaClass.getInterfaces();
if (useCaseInterfaces.isEmpty()) {
conditionEvents.add(SimpleConditionEvent.violated(javaClass,
"The " + javaClass.getName() + " should implement any class in the package " + basePackage));
return;
}
for (var useCaseInterface : useCaseInterfaces) {
if (!useCaseInterface.getName().startsWith(basePackage)) {
conditionEvents.add(SimpleConditionEvent.violated(javaClass,
"The " + javaClass.getName() + " should implement any class in the package " + basePackage));
}
}
}
};
}
}