Search in sources :

Example 1 with StructurizrPlantUMLExporter

use of com.structurizr.export.plantuml.StructurizrPlantUMLExporter in project kroki by yuzutech.

the class Structurizr method convert.

static byte[] convert(String source, FileFormat fileFormat, StructurizrPlantUMLExporter structurizrPlantUMLExporter, JsonObject options) {
    StructurizrDslParser parser = new StructurizrDslParser();
    try {
        parser.parse(source);
        Collection<View> views = parser.getWorkspace().getViews().getViews();
        if (views.isEmpty()) {
            throw new BadRequestException("Empty diagram, does not have any view.");
        }
        View selectedView;
        String viewKey = options.getString("view-key");
        if (viewKey != null && !viewKey.trim().isEmpty()) {
            Optional<View> viewFound = views.stream().filter(view -> Objects.equals(view.getKey(), viewKey)).findFirst();
            if (!viewFound.isPresent()) {
                throw new BadRequestException("Unable to find view for key: " + viewKey + ".");
            }
            selectedView = viewFound.get();
        } else {
            // take the first view if not specified
            selectedView = views.iterator().next();
        }
        final Diagram diagram;
        if (selectedView instanceof DynamicView) {
            diagram = structurizrPlantUMLExporter.export((DynamicView) selectedView);
        } else if (selectedView instanceof DeploymentView) {
            diagram = structurizrPlantUMLExporter.export((DeploymentView) selectedView);
        } else if (selectedView instanceof ComponentView) {
            diagram = structurizrPlantUMLExporter.export((ComponentView) selectedView);
        } else if (selectedView instanceof ContainerView) {
            diagram = structurizrPlantUMLExporter.export((ContainerView) selectedView);
        } else if (selectedView instanceof SystemContextView) {
            diagram = structurizrPlantUMLExporter.export((SystemContextView) selectedView);
        } else if (selectedView instanceof SystemLandscapeView) {
            diagram = structurizrPlantUMLExporter.export((SystemLandscapeView) selectedView);
        } else {
            throw new BadRequestException("View type is not supported: " + selectedView.getClass().getSimpleName() + ", must be a DynamicView, DeploymentView, ComponentView, ContainerView, SystemContextView or SystemLandscapeView.");
        }
        return Plantuml.convert(diagram.getDefinition(), fileFormat, new JsonObject());
    } catch (StructurizrDslParserException e) {
        String cause = e.getMessage();
        final String message;
        if (cause != null && !cause.trim().isEmpty()) {
            message = "Unable to parse the Structurizr DSL. " + cause + ".";
        } else {
            message = "Unable to parse the Structurizr DSL.";
        }
        throw new BadRequestException(message, e);
    }
}
Also used : Arrays(java.util.Arrays) DecodeException(io.kroki.server.error.DecodeException) DiagramSource(io.kroki.server.decode.DiagramSource) StructurizrDslParserException(com.structurizr.dsl.StructurizrDslParserException) View(com.structurizr.view.View) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) DeploymentView(com.structurizr.view.DeploymentView) Diagram(com.structurizr.export.Diagram) BadRequestException(io.kroki.server.error.BadRequestException) ComponentView(com.structurizr.view.ComponentView) ContainerView(com.structurizr.view.ContainerView) DynamicView(com.structurizr.view.DynamicView) Collection(java.util.Collection) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Vertx(io.vertx.core.Vertx) FileFormat(io.kroki.server.format.FileFormat) SourceDecoder(io.kroki.server.decode.SourceDecoder) Objects(java.util.Objects) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) StructurizrDslParser(com.structurizr.dsl.StructurizrDslParser) SystemContextView(com.structurizr.view.SystemContextView) Optional(java.util.Optional) Handler(io.vertx.core.Handler) StructurizrDslParser(com.structurizr.dsl.StructurizrDslParser) JsonObject(io.vertx.core.json.JsonObject) StructurizrDslParserException(com.structurizr.dsl.StructurizrDslParserException) View(com.structurizr.view.View) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) DeploymentView(com.structurizr.view.DeploymentView) ComponentView(com.structurizr.view.ComponentView) ContainerView(com.structurizr.view.ContainerView) DynamicView(com.structurizr.view.DynamicView) SystemContextView(com.structurizr.view.SystemContextView) Diagram(com.structurizr.export.Diagram) ComponentView(com.structurizr.view.ComponentView) SystemContextView(com.structurizr.view.SystemContextView) BadRequestException(io.kroki.server.error.BadRequestException) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) DynamicView(com.structurizr.view.DynamicView) ContainerView(com.structurizr.view.ContainerView) DeploymentView(com.structurizr.view.DeploymentView)

Example 2 with StructurizrPlantUMLExporter

use of com.structurizr.export.plantuml.StructurizrPlantUMLExporter in project kroki by yuzutech.

the class StructurizrServiceTest method should_convert_getting_started_example.

@Test
public void should_convert_getting_started_example() throws IOException {
    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, new StructurizrPlantUMLExporter(), new JsonObject());
        assertThat(stripComments(new String(result))).isEqualToIgnoringNewLines(expected);
    } else {
        logger.info("/usr/bin/dot not found, skipping test.");
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Test(org.junit.jupiter.api.Test)

Example 3 with StructurizrPlantUMLExporter

use of com.structurizr.export.plantuml.StructurizrPlantUMLExporter in project kroki by yuzutech.

the class StructurizrServiceTest method should_throw_exception_when_view_does_not_exist.

@Test
public void should_throw_exception_when_view_does_not_exist() throws IOException {
    String source = read("./bigbank.structurizr");
    JsonObject options = new JsonObject();
    options.put("view-key", "NonExisting");
    assertThatThrownBy(() -> Structurizr.convert(source, FileFormat.SVG, new StructurizrPlantUMLExporter(), options)).isInstanceOf(BadRequestException.class).hasMessage("Unable to find view for key: NonExisting.");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) BadRequestException(io.kroki.server.error.BadRequestException) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Test(org.junit.jupiter.api.Test)

Example 4 with StructurizrPlantUMLExporter

use of com.structurizr.export.plantuml.StructurizrPlantUMLExporter in project kroki by yuzutech.

the class StructurizrServiceTest method should_convert_bigbank_example_systemcontext_view.

@Test
public void should_convert_bigbank_example_systemcontext_view() throws IOException {
    if (Files.isExecutable(Paths.get("/usr/bin/dot"))) {
        String source = read("./bigbank.structurizr");
        String expected = read("./bigbank.systemcontext.expected.svg");
        JsonObject options = new JsonObject();
        options.put("view-key", "SystemContext");
        byte[] result = Structurizr.convert(source, FileFormat.SVG, new StructurizrPlantUMLExporter(), options);
        assertThat(stripComments(new String(result))).isEqualToIgnoringNewLines(expected);
    } else {
        logger.info("/usr/bin/dot not found, skipping test.");
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Test(org.junit.jupiter.api.Test)

Example 5 with StructurizrPlantUMLExporter

use of com.structurizr.export.plantuml.StructurizrPlantUMLExporter in project kroki by yuzutech.

the class StructurizrServiceTest method should_convert_bigbank_example_container_view.

@Test
public void should_convert_bigbank_example_container_view() throws IOException {
    if (Files.isExecutable(Paths.get("/usr/bin/dot"))) {
        String source = read("./bigbank.structurizr");
        String expected = read("./bigbank.containers.expected.svg");
        JsonObject options = new JsonObject();
        options.put("view-key", "Containers");
        byte[] result = Structurizr.convert(source, FileFormat.SVG, new StructurizrPlantUMLExporter(), options);
        assertThat(stripComments(new String(result))).isEqualToIgnoringNewLines(expected);
    } else {
        logger.info("/usr/bin/dot not found, skipping test.");
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Test(org.junit.jupiter.api.Test)

Aggregations

StructurizrPlantUMLExporter (com.structurizr.export.plantuml.StructurizrPlantUMLExporter)5 JsonObject (io.vertx.core.json.JsonObject)5 Test (org.junit.jupiter.api.Test)4 BadRequestException (io.kroki.server.error.BadRequestException)2 StructurizrDslParser (com.structurizr.dsl.StructurizrDslParser)1 StructurizrDslParserException (com.structurizr.dsl.StructurizrDslParserException)1 Diagram (com.structurizr.export.Diagram)1 ComponentView (com.structurizr.view.ComponentView)1 ContainerView (com.structurizr.view.ContainerView)1 DeploymentView (com.structurizr.view.DeploymentView)1 DynamicView (com.structurizr.view.DynamicView)1 SystemContextView (com.structurizr.view.SystemContextView)1 SystemLandscapeView (com.structurizr.view.SystemLandscapeView)1 View (com.structurizr.view.View)1 DiagramSource (io.kroki.server.decode.DiagramSource)1 SourceDecoder (io.kroki.server.decode.SourceDecoder)1 DecodeException (io.kroki.server.error.DecodeException)1 FileFormat (io.kroki.server.format.FileFormat)1 AsyncResult (io.vertx.core.AsyncResult)1 Handler (io.vertx.core.Handler)1