Skip to content

Commit

Permalink
allow for empty layers in OnionArchitecture (#271)
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Hanke <Manfred.Hanke@tngtech.com>
  • Loading branch information
hankem authored and codecholeric committed Jan 9, 2020
1 parent e698a71 commit 9db4047
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ public static final class OnionArchitecture implements ArchRule {
private static final String APPLICATION_SERVICE_LAYER = "application service";
private static final String ADAPTER_LAYER = "adapter";

private final Optional<String> overriddenDescription;
private String[] domainModelPackageIdentifiers = new String[0];
private String[] domainServicePackageIdentifiers = new String[0];
private String[] applicationPackageIdentifiers = new String[0];
private Map<String, String[]> adapterPackageIdentifiers = new LinkedHashMap<>();

private final Optional<String> overriddenDescription;
private boolean allowEmptyLayers = false;

private OnionArchitecture() {
overriddenDescription = Optional.absent();
Expand Down Expand Up @@ -438,6 +438,12 @@ public OnionArchitecture adapter(String name, String... packageIdentifiers) {
return this;
}

@PublicAPI(usage = ACCESS)
public OnionArchitecture allowEmptyLayers(boolean allowEmptyLayers) {
this.allowEmptyLayers = allowEmptyLayers;
return this;
}

private LayeredArchitecture layeredArchitectureDelegate() {
LayeredArchitecture layeredArchitectureDelegate = layeredArchitecture()
.layer(DOMAIN_MODEL_LAYER).definedBy(domainModelPackageIdentifiers)
Expand All @@ -446,7 +452,8 @@ private LayeredArchitecture layeredArchitectureDelegate() {
.layer(ADAPTER_LAYER).definedBy(concatenateAll(adapterPackageIdentifiers.values()))
.whereLayer(DOMAIN_MODEL_LAYER).mayOnlyBeAccessedByLayers(DOMAIN_SERVICE_LAYER, APPLICATION_SERVICE_LAYER, ADAPTER_LAYER)
.whereLayer(DOMAIN_SERVICE_LAYER).mayOnlyBeAccessedByLayers(APPLICATION_SERVICE_LAYER, ADAPTER_LAYER)
.whereLayer(APPLICATION_SERVICE_LAYER).mayOnlyBeAccessedByLayers(ADAPTER_LAYER);
.whereLayer(APPLICATION_SERVICE_LAYER).mayOnlyBeAccessedByLayers(ADAPTER_LAYER)
.allowEmptyLayers(allowEmptyLayers);

for (Map.Entry<String, String[]> adapter : adapterPackageIdentifiers.entrySet()) {
String adapterLayer = getAdapterLayer(adapter.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,32 @@ public void onion_architecture_gathers_all_violations() {
assertPatternMatches(result.getFailureReport().getDetails(), expectedViolations.toPatterns());
}

@Test
@DataProvider(value = {"true", "false", "null"})
public void onion_architecture_allows_or_rejects_empty_layers(Boolean allowEmptyLayers) {
OnionArchitecture architecture = onionArchitecture()
.domainModels(absolute("onionarchitecture.domain.model.does.not.exist"))
.domainServices(absolute("onionarchitecture.domain.service.not.there"))
.applicationServices(absolute("onionarchitecture.application.http410"));
if (allowEmptyLayers != null) {
architecture.allowEmptyLayers(allowEmptyLayers);
}

JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));

EvaluationResult result = architecture.evaluate(classes);
boolean expectViolation = allowEmptyLayers != Boolean.TRUE;
assertThat(result.hasViolation()).as("result of evaluating empty layers has violation").isEqualTo(expectViolation);
if (expectViolation) {
assertPatternMatches(result.getFailureReport().getDetails(), ImmutableSet.of(
expectedEmptyLayer("adapter"), expectedEmptyLayer("application service"),
expectedEmptyLayer("domain model"), expectedEmptyLayer("domain service")
));
} else {
assertThat(result.getFailureReport().isEmpty()).as("failure report").isTrue();
}
}

private String singleLine(EvaluationResult result) {
return Joiner.on(NEW_LINE_REPLACE).join(result.getFailureReport().getDetails()).replace("\n", NEW_LINE_REPLACE);
}
Expand Down

0 comments on commit 9db4047

Please sign in to comment.