Skip to content

Commit

Permalink
Adds more view inspections.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brown committed Feb 18, 2024
1 parent 3d8da34 commit 51cadb6
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.structurizr.Workspace;
import com.structurizr.inspection.model.*;
import com.structurizr.inspection.view.ContainerViewsForMultipleSoftwareSystemsInspection;
import com.structurizr.inspection.view.ElementStyleMetadataInspection;
import com.structurizr.inspection.view.EmptyViewsInspection;
import com.structurizr.inspection.view.SystemContextViewsForMultipleSoftwareSystemsInspection;
import com.structurizr.inspection.view.*;
import com.structurizr.inspection.workspace.WorkspaceScopeInspection;
import com.structurizr.inspection.workspace.WorkspaceToolingInspection;
import com.structurizr.model.*;
import com.structurizr.view.ElementStyle;
import com.structurizr.view.*;

public class DefaultInspector extends Inspector {

Expand Down Expand Up @@ -75,8 +72,58 @@ private void runModelInspections() {

private void runViewInspections() {
add(new EmptyViewsInspection(this).run());

for (CustomView view : getWorkspace().getViews().getCustomViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

for (SystemLandscapeView view : getWorkspace().getViews().getSystemLandscapeViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

add(new SystemContextViewsForMultipleSoftwareSystemsInspection(this).run());
for (SystemContextView view : getWorkspace().getViews().getSystemContextViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

add(new ContainerViewsForMultipleSoftwareSystemsInspection(this).run());
for (ContainerView view : getWorkspace().getViews().getContainerViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

for (ComponentView view : getWorkspace().getViews().getComponentViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

for (DynamicView view : getWorkspace().getViews().getDynamicViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

for (DeploymentView view : getWorkspace().getViews().getDeploymentViews()) {
add(new GeneratedKeyInspection(this).run(view));
add(new EmptyViewInspection(this).run(view));
add(new ManualLayoutInspection(this).run(view));
}

for (FilteredView view : getWorkspace().getViews().getFilteredViews()) {
add(new GeneratedKeyInspection(this).run(view));
}

for (ImageView view : getWorkspace().getViews().getImageViews()) {
add(new GeneratedKeyInspection(this).run(view));
}

for (ElementStyle elementStyle : getWorkspace().getViews().getConfiguration().getStyles().getElements()) {
add(new ElementStyleMetadataInspection(this).run(elementStyle));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.structurizr.inspection.view;

import com.structurizr.inspection.Inspection;
import com.structurizr.inspection.Inspector;
import com.structurizr.inspection.Severity;
import com.structurizr.inspection.Violation;
import com.structurizr.view.View;

abstract class AbstractViewInspection extends Inspection {

public AbstractViewInspection(Inspector inspector) {
super(inspector);
}

public final Violation run(View view) {
Severity severity = getInspector().getSeverityStrategy().getSeverity(this, view);
Violation violation = inspect(view);

return violation == null ? null : violation.withSeverity(severity);
}

protected abstract Violation inspect(View view);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.structurizr.inspection.view;

import com.structurizr.inspection.Inspector;
import com.structurizr.inspection.Violation;
import com.structurizr.view.ModelView;
import com.structurizr.view.View;

public class GeneratedKeyInspection extends AbstractViewInspection {

public GeneratedKeyInspection(Inspector inspector) {
super(inspector);
}

@Override
public Violation inspect(View view) {
if (view.isGeneratedKey()) {
return violation("The view with key \"" + view.getKey() + "\" has an automatically generated view key, which is not guaranteed to be stable over time.");
}

return noViolation();
}

@Override
protected String getType() {
return "views.view.key";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.structurizr.inspection.view;

import com.structurizr.inspection.Inspector;
import com.structurizr.inspection.Violation;
import com.structurizr.view.ModelView;

public class ManualLayoutInspection extends AbstractModelViewInspection {

public ManualLayoutInspection(Inspector inspector) {
super(inspector);
}

@Override
public Violation inspect(ModelView view) {
if (view.isGeneratedKey() && view.getAutomaticLayout() == null) {
return violation("The view with key \"" + view.getKey() + "\" has an automatically generated view key, which may cause manual layout information to be lost in the future.");
}

return noViolation();
}

@Override
protected String getType() {
return "views.view.layout";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.structurizr.inspection.view;

import com.structurizr.Workspace;
import com.structurizr.inspection.DefaultInspector;
import com.structurizr.inspection.Severity;
import com.structurizr.inspection.Violation;
import com.structurizr.view.SystemLandscapeView;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class GeneratedKeyInspectionTests {

@Test
public void run_GeneratedKey() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("", "Description");

Violation violation = new GeneratedKeyInspection(new DefaultInspector(workspace)).run(view);
Assertions.assertEquals(Severity.ERROR, violation.getSeverity());
assertEquals("views.view.key", violation.getType());
assertEquals("The view with key \"SystemLandscape-001\" has an automatically generated view key, which is not guaranteed to be stable over time.", violation.getMessage());
}

@Test
public void run_NonGeneratedKey() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("key", "Description");

Violation violation = new GeneratedKeyInspection(new DefaultInspector(workspace)).run(view);
assertNull(violation);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.structurizr.inspection.view;

import com.structurizr.Workspace;
import com.structurizr.inspection.DefaultInspector;
import com.structurizr.inspection.Severity;
import com.structurizr.inspection.Violation;
import com.structurizr.view.SystemLandscapeView;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class ManualLayoutInspectionTests {

@Test
public void run_GeneratedKeyAndManualLayout() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("", "Description");

Violation violation = new ManualLayoutInspection(new DefaultInspector(workspace)).run(view);
Assertions.assertEquals(Severity.ERROR, violation.getSeverity());
assertEquals("views.view.layout", violation.getType());
assertEquals("The view with key \"SystemLandscape-001\" has an automatically generated view key, which may cause manual layout information to be lost in the future.", violation.getMessage());
}

@Test
public void run_GeneratedKeyAndAutomaticLayout() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("", "Description");
view.enableAutomaticLayout();

Violation violation = new ManualLayoutInspection(new DefaultInspector(workspace)).run(view);
assertNull(violation);
}

@Test
public void run_NonGeneratedKeyAndManualLayout() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("key", "Description");

Violation violation = new ManualLayoutInspection(new DefaultInspector(workspace)).run(view);
assertNull(violation);
}

@Test
public void run_NonGeneratedKeyAndAutomaticLayout() {
Workspace workspace = new Workspace("Name", "Description");
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("key", "Description");
view.enableAutomaticLayout();

Violation violation = new ManualLayoutInspection(new DefaultInspector(workspace)).run(view);
assertNull(violation);
}

}

0 comments on commit 51cadb6

Please sign in to comment.