Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Adds support for loading hierarchical identifiers too.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 26, 2023
1 parent e9fe513 commit 57f036f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 79 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/structurizr/dsl/WorkspaceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/dsl/extend/extend-workspace-from-dsl-file.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ workspace extends workspace.dsl {
}

user -> softwareSystem1 "Uses"
softwareSystem3.webapp -> softwareSystem3.db
}

}
1 change: 1 addition & 0 deletions src/test/dsl/extend/extend-workspace-from-dsl-url.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/t
}

user -> softwareSystem1 "Uses"
softwareSystem3.webapp -> softwareSystem3.db
}

}
1 change: 1 addition & 0 deletions src/test/dsl/extend/extend-workspace-from-json-file.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace extends workspace.json {
}

user -> softwareSystem1 "Uses"
softwareSystem3.webapp -> softwareSystem3.db
}

}
1 change: 1 addition & 0 deletions src/test/dsl/extend/extend-workspace-from-json-url.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/t
}

user -> softwareSystem1 "Uses"
softwareSystem3.webapp -> softwareSystem3.db
}

}
9 changes: 9 additions & 0 deletions src/test/dsl/extend/workspace.dsl
Original file line number Diff line number Diff line change
@@ -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"
}
}

}
30 changes: 28 additions & 2 deletions src/test/dsl/extend/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name" : "Name",
"description" : "Description",
"properties" : {
"structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICBtb2RlbCB7CiAgICAgICAgdXNlciA9IHBlcnNvbiAiVXNlciIKICAgICAgICBzb2Z0d2FyZVN5c3RlbTEgPSBzb2Z0d2FyZVN5c3RlbSAiU29mdHdhcmUgU3lzdGVtIDEiCiAgICAgICAgc29mdHdhcmVTeXN0ZW0gIlNvZnR3YXJlIFN5c3RlbSAyIgogICAgfQoKfQo="
"structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICAhaWRlbnRpZmllcnMgaGllcmFyY2hpY2FsCgogICAgbW9kZWwgewogICAgICAgIHVzZXIgPSBwZXJzb24gIlVzZXIiCgogICAgICAgIHNvZnR3YXJlU3lzdGVtMSA9IHNvZnR3YXJlU3lzdGVtICJTb2Z0d2FyZSBTeXN0ZW0gMSIKCiAgICAgICAgc29mdHdhcmVTeXN0ZW0gIlNvZnR3YXJlIFN5c3RlbSAyIgoKICAgICAgICBzb2Z0d2FyZVN5c3RlbTMgPSBzb2Z0d2FyZVN5c3RlbSAiU29mdHdhcmUgU3lzdGVtIDMiIHsKICAgICAgICAgICAgd2ViYXBwID0gY29udGFpbmVyICJXZWIgQXBwbGljYXRpb24iCiAgICAgICAgICAgIGRiID0gY29udGFpbmVyICJEYXRhYmFzZSIKICAgICAgICB9CiAgICB9Cgp9Cg=="
},
"configuration" : { },
"model" : {
Expand All @@ -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" : { },
Expand Down
79 changes: 14 additions & 65 deletions src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -438,52 +440,25 @@ 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();

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
Expand All @@ -502,50 +477,24 @@ 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();

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
Expand Down

0 comments on commit 57f036f

Please sign in to comment.