Skip to content

Commit

Permalink
Add support for generating Structurizr legend.
Browse files Browse the repository at this point in the history
Resolves yuzutech#1686.
  • Loading branch information
willson556 committed Feb 9, 2024
1 parent 29e9fca commit 529b2d6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions ci/tests/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const tests = [
{ engine: 'excalidraw', file: 'venn.excalidraw', options: {}, outputFormat: ['svg'] },
{ engine: 'pikchr', file: 'sqlite-architecture.pikchr', options: {}, outputFormat: ['svg'] },
{ engine: 'structurizr', file: 'gettingstarted.structurizr', options: {}, outputFormat: ['svg'] },
{ engine: 'structurizr', file: 'gettingstarted.structurizr', options: { output: 'legend' }, outputFormat: ['svg'] },
{ engine: 'diagramsnet', file: 'diagramsnet-infography.xml', options: {}, outputFormat: ['svg', 'png'] },
{ engine: 'diagramsnet', file: 'diagramsnet-mindmap.xml', options: {}, outputFormat: ['svg', 'png'] },
{ engine: 'diagramsnet', file: 'diagramsnet-network.xml', options: {}, outputFormat: ['svg', 'png'] },
Expand Down
7 changes: 7 additions & 0 deletions docs/modules/setup/pages/diagram-options.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ empty string ("")
|_string_
|Key of the view (if the workspace contains more than one view)

|output
|_string_

- `diagram`
- `legend`
|Select the output for the specified view.

|===

== Svgbob
Expand Down
17 changes: 16 additions & 1 deletion server/src/main/java/io/kroki/server/service/Structurizr.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,22 @@ static byte[] convert(String source, FileFormat fileFormat, PlantumlCommand plan
} else {
throw new BadRequestException("View type is not supported: " + selectedView.getClass().getSimpleName() + ", must be a DynamicView, DeploymentView, ComponentView, ContainerView, SystemContextView or SystemLandscapeView.");
}
return plantumlCommand.convert(diagram.getDefinition(), fileFormat, new JsonObject());

String outputOption = options.getString("output");
if (outputOption != null) {
outputOption = outputOption.trim();
}

String diagramPlantUML;
if (outputOption == null || outputOption.equals("diagram")) {
diagramPlantUML = diagram.getDefinition();
} else if (outputOption.equals("legend")) {
diagramPlantUML = diagram.getLegend().getDefinition();
} else {
throw new BadRequestException("Unknown output option: " + outputOption);
}

return plantumlCommand.convert(diagramPlantUML, fileFormat, new JsonObject());
} catch (StructurizrDslParserException e) {
String cause = e.getMessage();
final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -50,12 +52,19 @@ static void prepare(VertxTestContext context, Vertx vertx) throws InterruptedExc
});
}

@Test
public void should_convert_getting_started_example() throws IOException, InterruptedException {
@ParameterizedTest
@ValueSource(strings = {"diagram", ""})
public void should_convert_getting_started_example(String output) throws IOException, InterruptedException {
if (Files.isExecutable(Paths.get("/usr/bin/dot"))) {
String source = read("./gettingstarted.structurizr");
String expected = read("./gettingstarted.expected.svg");
byte[] result = Structurizr.convert(source, FileFormat.SVG, plantumlCommand, new StructurizrPlantUMLExporter(), new JsonObject());

JsonObject options = new JsonObject();
if (!output.isEmpty()) {
options.put("output", output);
}

byte[] result = Structurizr.convert(source, FileFormat.SVG, plantumlCommand, new StructurizrPlantUMLExporter(), options);
assertThat(stripComments(new String(result))).isEqualToIgnoringNewLines(expected);
} else {
logger.info("/usr/bin/dot not found, skipping test.");
Expand Down Expand Up @@ -90,6 +99,21 @@ public void should_convert_bigbank_example_systemcontext_view() throws IOExcepti
}
}

@Test
public void should_convert_bigbank_example_systemcontext_legend() throws IOException, InterruptedException {
if (Files.isExecutable(Paths.get("/usr/bin/dot"))) {
String source = read("./bigbank.structurizr");
String expected = read("./bigbank.systemcontext.legend.expected.svg");
JsonObject options = new JsonObject();
options.put("view-key", "SystemContext");
options.put("output", "legend");
byte[] result = Structurizr.convert(source, FileFormat.SVG, plantumlCommand, new StructurizrPlantUMLExporter(), options);
assertThat(stripComments(new String(result))).isEqualToIgnoringNewLines(expected);
} else {
logger.info("/usr/bin/dot not found, skipping test.");
}
}

@Test
public void should_convert_aws_example() throws IOException, InterruptedException {
if (Files.isExecutable(Paths.get("/usr/bin/dot"))) {
Expand Down Expand Up @@ -134,6 +158,20 @@ public void should_throw_exception_when_script_directive_used() throws IOExcepti
.hasMessageStartingWith("Unable to parse the Structurizr DSL. Error running inline script, caused by java.lang.RuntimeException: Could not load a scripting engine for extension \"kts\" at line 5");
}

@Test
public void should_throw_exception_when_unknown_output_specified() throws IOException {
String source = read("./bigbank.structurizr");

JsonObject options = new JsonObject();
options.put("output", "invalid");

assertThatThrownBy(() -> {
Structurizr.convert(source, FileFormat.SVG, plantumlCommand, new StructurizrPlantUMLExporter(), options);
})
.isInstanceOf(BadRequestException.class)
.hasMessageStartingWith("Unknown output option: invalid");
}

@Test
public void should_preserve_styles_defined_in_workspace_while_applying_theme() throws IOException, InterruptedException {
String source = read("./workspace-style-with-theme.structurizr");
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 529b2d6

Please sign in to comment.