From 57f036f5f7bb7e94725d2f0be89dff957623e022 Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sat, 26 Aug 2023 10:34:59 +0100 Subject: [PATCH] Adds support for loading hierarchical identifiers too. --- build.gradle | 1 + .../com/structurizr/dsl/WorkspaceParser.java | 14 +--- .../extend/extend-workspace-from-dsl-file.dsl | 1 + .../extend/extend-workspace-from-dsl-url.dsl | 1 + .../extend-workspace-from-json-file.dsl | 1 + .../extend/extend-workspace-from-json-url.dsl | 1 + src/test/dsl/extend/workspace.dsl | 9 +++ src/test/dsl/extend/workspace.json | 30 ++++++- .../java/com/structurizr/dsl/DslTests.java | 79 ++++--------------- 9 files changed, 58 insertions(+), 79 deletions(-) diff --git a/build.gradle b/build.gradle index 3edcf01..89a8c4c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,7 @@ dependencies { testImplementation 'org.jruby:jruby-core:9.4.2.0' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2' } compileJava.options.encoding = 'UTF-8' diff --git a/src/main/java/com/structurizr/dsl/WorkspaceParser.java b/src/main/java/com/structurizr/dsl/WorkspaceParser.java index 9052d6d..6218a4a 100644 --- a/src/main/java/com/structurizr/dsl/WorkspaceParser.java +++ b/src/main/java/com/structurizr/dsl/WorkspaceParser.java @@ -95,24 +95,14 @@ private void registerIdentifiers(Workspace workspace, DslParserContext context) for (Element element : workspace.getModel().getElements()) { if (element.getProperties().containsKey(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME)) { String identifier = element.getProperties().get(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME); - try { - context.identifiersRegister.validateIdentifierName(identifier); - context.identifiersRegister.register(identifier, element); - } catch (Exception e) { - // ignore, don't register the identifier - } + context.identifiersRegister.register(identifier, element); } } for (Relationship relationship : workspace.getModel().getRelationships()) { if (relationship.getProperties().containsKey(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME)) { String identifier = relationship.getProperties().get(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME); - try { - context.identifiersRegister.validateIdentifierName(identifier); - context.identifiersRegister.register(identifier, relationship); - } catch (Exception e) { - // ignore, don't register the identifier - } + context.identifiersRegister.register(identifier, relationship); } } } diff --git a/src/test/dsl/extend/extend-workspace-from-dsl-file.dsl b/src/test/dsl/extend/extend-workspace-from-dsl-file.dsl index 8bbd4d3..4260ec1 100644 --- a/src/test/dsl/extend/extend-workspace-from-dsl-file.dsl +++ b/src/test/dsl/extend/extend-workspace-from-dsl-file.dsl @@ -6,6 +6,7 @@ workspace extends workspace.dsl { } user -> softwareSystem1 "Uses" + softwareSystem3.webapp -> softwareSystem3.db } } \ No newline at end of file diff --git a/src/test/dsl/extend/extend-workspace-from-dsl-url.dsl b/src/test/dsl/extend/extend-workspace-from-dsl-url.dsl index e586f67..d5a2491 100644 --- a/src/test/dsl/extend/extend-workspace-from-dsl-url.dsl +++ b/src/test/dsl/extend/extend-workspace-from-dsl-url.dsl @@ -6,6 +6,7 @@ workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/t } user -> softwareSystem1 "Uses" + softwareSystem3.webapp -> softwareSystem3.db } } \ No newline at end of file diff --git a/src/test/dsl/extend/extend-workspace-from-json-file.dsl b/src/test/dsl/extend/extend-workspace-from-json-file.dsl index 8c6ecda..753a572 100644 --- a/src/test/dsl/extend/extend-workspace-from-json-file.dsl +++ b/src/test/dsl/extend/extend-workspace-from-json-file.dsl @@ -12,6 +12,7 @@ workspace extends workspace.json { } user -> softwareSystem1 "Uses" + softwareSystem3.webapp -> softwareSystem3.db } } \ No newline at end of file diff --git a/src/test/dsl/extend/extend-workspace-from-json-url.dsl b/src/test/dsl/extend/extend-workspace-from-json-url.dsl index 1279eeb..045bbee 100644 --- a/src/test/dsl/extend/extend-workspace-from-json-url.dsl +++ b/src/test/dsl/extend/extend-workspace-from-json-url.dsl @@ -12,6 +12,7 @@ workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/t } user -> softwareSystem1 "Uses" + softwareSystem3.webapp -> softwareSystem3.db } } \ No newline at end of file diff --git a/src/test/dsl/extend/workspace.dsl b/src/test/dsl/extend/workspace.dsl index 4fd1cf3..2986177 100644 --- a/src/test/dsl/extend/workspace.dsl +++ b/src/test/dsl/extend/workspace.dsl @@ -1,9 +1,18 @@ workspace { + !identifiers hierarchical + model { user = person "User" + softwareSystem1 = softwareSystem "Software System 1" + softwareSystem "Software System 2" + + softwareSystem3 = softwareSystem "Software System 3" { + webapp = container "Web Application" + db = container "Database" + } } } \ No newline at end of file diff --git a/src/test/dsl/extend/workspace.json b/src/test/dsl/extend/workspace.json index 2b05d53..09a9f17 100644 --- a/src/test/dsl/extend/workspace.json +++ b/src/test/dsl/extend/workspace.json @@ -3,7 +3,7 @@ "name" : "Name", "description" : "Description", "properties" : { - "structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICBtb2RlbCB7CiAgICAgICAgdXNlciA9IHBlcnNvbiAiVXNlciIKICAgICAgICBzb2Z0d2FyZVN5c3RlbTEgPSBzb2Z0d2FyZVN5c3RlbSAiU29mdHdhcmUgU3lzdGVtIDEiCiAgICAgICAgc29mdHdhcmVTeXN0ZW0gIlNvZnR3YXJlIFN5c3RlbSAyIgogICAgfQoKfQo=" + "structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICAhaWRlbnRpZmllcnMgaGllcmFyY2hpY2FsCgogICAgbW9kZWwgewogICAgICAgIHVzZXIgPSBwZXJzb24gIlVzZXIiCgogICAgICAgIHNvZnR3YXJlU3lzdGVtMSA9IHNvZnR3YXJlU3lzdGVtICJTb2Z0d2FyZSBTeXN0ZW0gMSIKCiAgICAgICAgc29mdHdhcmVTeXN0ZW0gIlNvZnR3YXJlIFN5c3RlbSAyIgoKICAgICAgICBzb2Z0d2FyZVN5c3RlbTMgPSBzb2Z0d2FyZVN5c3RlbSAiU29mdHdhcmUgU3lzdGVtIDMiIHsKICAgICAgICAgICAgd2ViYXBwID0gY29udGFpbmVyICJXZWIgQXBwbGljYXRpb24iCiAgICAgICAgICAgIGRiID0gY29udGFpbmVyICJEYXRhYmFzZSIKICAgICAgICB9CiAgICB9Cgp9Cg==" }, "configuration" : { }, "model" : { @@ -29,11 +29,37 @@ "id" : "3", "tags" : "Element,Software System", "properties" : { - "structurizr.dsl.identifier" : "9c50bae2-474c-4fb6-9cc0-ef1da359673b" + "structurizr.dsl.identifier" : "93ea2110-adec-4936-97aa-b55397325115" }, "name" : "Software System 2", "location" : "Unspecified", "documentation" : { } + }, { + "id" : "4", + "tags" : "Element,Software System", + "properties" : { + "structurizr.dsl.identifier" : "softwaresystem3" + }, + "name" : "Software System 3", + "location" : "Unspecified", + "containers" : [ { + "id" : "5", + "tags" : "Element,Container", + "properties" : { + "structurizr.dsl.identifier" : "softwaresystem3.webapp" + }, + "name" : "Web Application", + "documentation" : { } + }, { + "id" : "6", + "tags" : "Element,Container", + "properties" : { + "structurizr.dsl.identifier" : "softwaresystem3.db" + }, + "name" : "Database", + "documentation" : { } + } ], + "documentation" : { } } ] }, "documentation" : { }, diff --git a/src/test/java/com/structurizr/dsl/DslTests.java b/src/test/java/com/structurizr/dsl/DslTests.java index 5e624b6..27bc9a2 100644 --- a/src/test/java/com/structurizr/dsl/DslTests.java +++ b/src/test/java/com/structurizr/dsl/DslTests.java @@ -4,6 +4,8 @@ import com.structurizr.model.*; import com.structurizr.view.*; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.File; import java.nio.file.Files; @@ -438,34 +440,11 @@ void test_include_WhenRunningInRestrictedMode() throws Exception { assertEquals(0, model.getSoftwareSystems().size()); } - @Test - void test_extendWorkspaceFromJsonFile() throws Exception { - StructurizrDslParser parser = new StructurizrDslParser(); - parser.parse(new File("src/test/dsl/extend/extend-workspace-from-json-file.dsl")); - - Workspace workspace = parser.getWorkspace(); - Model model = workspace.getModel(); - - assertEquals(1, model.getPeople().size()); - Person user = model.getPersonWithName("User"); - - assertEquals(2, workspace.getModel().getSoftwareSystems().size()); - SoftwareSystem softwareSystem = model.getSoftwareSystemWithName("Software System 1"); - - assertEquals(2, softwareSystem.getContainers().size()); - assertNotNull(softwareSystem.getContainers().stream().filter(c -> c.getName().equals("Web Application 1")).findFirst()); - assertNotNull(softwareSystem.getContainers().stream().filter(c -> c.getName().equals("Web Application 2")).findFirst()); - - assertEquals(1, workspace.getModel().getRelationships().size()); - Relationship relationship = user.getRelationships().iterator().next(); - assertEquals("Uses", relationship.getDescription()); - assertSame(softwareSystem, relationship.getDestination()); - } - - @Test - void test_extendWorkspaceFromJsonUrl() throws Exception { + @ParameterizedTest + @ValueSource(strings = { "src/test/dsl/extend/extend-workspace-from-json-file.dsl", "src/test/dsl/extend/extend-workspace-from-json-url.dsl" }) + void test_extendWorkspaceFromJson(String dslFile) throws Exception { StructurizrDslParser parser = new StructurizrDslParser(); - parser.parse(new File("src/test/dsl/extend/extend-workspace-from-json-url.dsl")); + parser.parse(new File(dslFile)); Workspace workspace = parser.getWorkspace(); Model model = workspace.getModel(); @@ -473,17 +452,13 @@ void test_extendWorkspaceFromJsonUrl() throws Exception { assertEquals(1, model.getPeople().size()); Person user = model.getPersonWithName("User"); - assertEquals(2, workspace.getModel().getSoftwareSystems().size()); + assertEquals(3, workspace.getModel().getSoftwareSystems().size()); SoftwareSystem softwareSystem = model.getSoftwareSystemWithName("Software System 1"); + assertTrue(user.hasEfferentRelationshipWith(softwareSystem, "Uses")); assertEquals(2, softwareSystem.getContainers().size()); assertNotNull(softwareSystem.getContainers().stream().filter(c -> c.getName().equals("Web Application 1")).findFirst()); assertNotNull(softwareSystem.getContainers().stream().filter(c -> c.getName().equals("Web Application 2")).findFirst()); - - assertEquals(1, workspace.getModel().getRelationships().size()); - Relationship relationship = user.getRelationships().iterator().next(); - assertEquals("Uses", relationship.getDescription()); - assertSame(softwareSystem, relationship.getDestination()); } @Test @@ -502,10 +477,11 @@ void test_extendWorkspaceFromJsonFile_WhenRunningInRestrictedMode() throws Excep } } - @Test - void test_extendWorkspaceFromDslFile() throws Exception { + @ParameterizedTest + @ValueSource(strings = { "src/test/dsl/extend/extend-workspace-from-dsl-file.dsl", "src/test/dsl/extend/extend-workspace-from-dsl-url.dsl" }) + void test_extendWorkspaceFromDsl(String dslFile) throws Exception { StructurizrDslParser parser = new StructurizrDslParser(); - parser.parse(new File("src/test/dsl/extend/extend-workspace-from-dsl-file.dsl")); + parser.parse(new File(dslFile)); Workspace workspace = parser.getWorkspace(); Model model = workspace.getModel(); @@ -513,39 +489,12 @@ void test_extendWorkspaceFromDslFile() throws Exception { assertEquals(1, model.getPeople().size()); Person user = model.getPersonWithName("User"); - assertEquals(2, workspace.getModel().getSoftwareSystems().size()); - SoftwareSystem softwareSystem = model.getSoftwareSystemWithName("Software System 1"); - - assertEquals(1, softwareSystem.getContainers().size()); - assertEquals("Web Application", softwareSystem.getContainers().iterator().next().getName()); - - assertEquals(1, workspace.getModel().getRelationships().size()); - Relationship relationship = user.getRelationships().iterator().next(); - assertEquals("Uses", relationship.getDescription()); - assertSame(softwareSystem, relationship.getDestination()); - } - - @Test - void test_extendWorkspaceFromDslUrl() throws Exception { - StructurizrDslParser parser = new StructurizrDslParser(); - parser.parse(new File("src/test/dsl/extend/extend-workspace-from-dsl-url.dsl")); - - Workspace workspace = parser.getWorkspace(); - Model model = workspace.getModel(); - - assertEquals(1, model.getPeople().size()); - Person user = model.getPersonWithName("User"); - - assertEquals(2, workspace.getModel().getSoftwareSystems().size()); + assertEquals(3, workspace.getModel().getSoftwareSystems().size()); SoftwareSystem softwareSystem = model.getSoftwareSystemWithName("Software System 1"); + assertTrue(user.hasEfferentRelationshipWith(softwareSystem, "Uses")); assertEquals(1, softwareSystem.getContainers().size()); assertEquals("Web Application", softwareSystem.getContainers().iterator().next().getName()); - - assertEquals(1, workspace.getModel().getRelationships().size()); - Relationship relationship = user.getRelationships().iterator().next(); - assertEquals("Uses", relationship.getDescription()); - assertSame(softwareSystem, relationship.getDestination()); } @Test