Skip to content

Commit

Permalink
resolves yuzutech#510 integrate Structurizr
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Sep 9, 2021
1 parent f11a6cd commit c1823f6
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
<artifactId>svgSalamander</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-dsl</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-plantuml</artifactId>
<version>1.6.3</version>
<exclusions>
<exclusion>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-core</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
90 changes: 90 additions & 0 deletions server/src/main/java/io/kroki/server/service/Structurizr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.kroki.server.service;

import com.structurizr.dsl.StructurizrDslParser;
import com.structurizr.dsl.StructurizrDslParserException;
import com.structurizr.io.plantuml.StructurizrPlantUMLWriter;
import com.structurizr.view.View;
import io.kroki.server.action.Delegator;
import io.kroki.server.decode.DiagramSource;
import io.kroki.server.decode.SourceDecoder;
import io.kroki.server.error.BadRequestException;
import io.kroki.server.error.DecodeException;
import io.kroki.server.format.FileFormat;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public class Structurizr implements DiagramService {

private final Vertx vertx;
private final StructurizrPlantUMLWriter structurizrPlantUMLWriter;
private final SourceDecoder sourceDecoder;

// same as PlantUML since we convert Structurizr DSL to PlantUML
private static final List<FileFormat> SUPPORTED_FORMATS = Arrays.asList(FileFormat.PNG, FileFormat.SVG, FileFormat.JPEG, FileFormat.BASE64, FileFormat.TXT, FileFormat.UTXT);

public Structurizr(Vertx vertx) {
this.vertx = vertx;
this.structurizrPlantUMLWriter = new StructurizrPlantUMLWriter();
this.sourceDecoder = new SourceDecoder() {
@Override
public String decode(String encoded) throws DecodeException {
return DiagramSource.decode(encoded);
}
};
}

@Override
public List<FileFormat> getSupportedFormats() {
return SUPPORTED_FORMATS;
}

@Override
public SourceDecoder getSourceDecoder() {
return sourceDecoder;
}

@Override
public String getVersion() {
return "1.6.3";
}

@Override
public void convert(String sourceDecoded, String serviceName, FileFormat fileFormat, Handler<AsyncResult<Buffer>> handler) {
vertx.executeBlocking(future -> {
try {
byte[] data = convert(sourceDecoded, fileFormat, this.structurizrPlantUMLWriter);
future.complete(data);
} catch (IllegalStateException e) {
future.fail(e);
}
}, res -> handler.handle(res.map(o -> Buffer.buffer((byte[]) o))));
}

static byte[] convert(String source, FileFormat fileFormat, StructurizrPlantUMLWriter structurizrPlantUMLWriter) {
StructurizrDslParser parser = new StructurizrDslParser();
try {
parser.parse(source);
Collection<View> views = parser.getWorkspace().getViews().getViews();
Optional<View> optionalView = views.stream().findFirst();
if (optionalView.isPresent()) {
// for now, take the first view
View firstView = optionalView.get();
StringWriter stringWriter = new StringWriter();
structurizrPlantUMLWriter.write(firstView, stringWriter);
String plantumlDiagram = stringWriter.toString();
return Plantuml.convert(plantumlDiagram, fileFormat);
}
throw new BadRequestException("Empty diagram, does not have any view.");
} catch (StructurizrDslParserException e) {
throw new BadRequestException("Unable to parse the Structurizr DSL.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.kroki.server.service;

import com.structurizr.io.plantuml.StructurizrPlantUMLWriter;
import io.kroki.server.format.FileFormat;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class StructurizrServiceTest {

@Test
public void should_convert_getting_started_example() throws IOException {
String source = read("./gettingstarted.structurizr");
String expected = read("./gettingstarted.expected.svg");
byte[] result = Structurizr.convert(source, FileFormat.SVG, new StructurizrPlantUMLWriter());
assertThat(new String(result)).isEqualToIgnoringNewLines(expected);
}

@Test
public void should_convert_bigbank_example() throws IOException {
String source = read("./bigbank.structurizr");
String expected = read("./bigbank.expected.svg");
byte[] result = Structurizr.convert(source, FileFormat.SVG, new StructurizrPlantUMLWriter());
assertThat(new String(result)).isEqualToIgnoringNewLines(expected);
}

private String read(String name) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(StructurizrServiceTest.class.getClassLoader().getResourceAsStream(name)))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
}
Loading

0 comments on commit c1823f6

Please sign in to comment.