forked from yuzutech/kroki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves yuzutech#510 integrate Structurizr
- Loading branch information
1 parent
f11a6cd
commit c1823f6
Showing
7 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
server/src/main/java/io/kroki/server/service/Structurizr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
server/src/test/java/io/kroki/server/service/StructurizrServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} |
Oops, something went wrong.