From f3638363d7eba76f3784691d74e8e3d744743020 Mon Sep 17 00:00:00 2001 From: Simon Brown <1009874+simonbrowndotje@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:26:09 +0100 Subject: [PATCH] Adds support for `!elements group` (https://github.com/structurizr/java/issues/351). --- changelog.md | 1 + .../com/structurizr/dsl/ExpressionParser.java | 6 ++++ .../java/com/structurizr/dsl/DslTests.java | 29 +++++++++++++++- .../dsl/find-elements-in-flat-group.dsl | 17 ++++++++++ .../dsl/find-elements-in-nested-group.dsl | 34 +++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 structurizr-dsl/src/test/resources/dsl/find-elements-in-flat-group.dsl create mode 100644 structurizr-dsl/src/test/resources/dsl/find-elements-in-nested-group.dsl diff --git a/changelog.md b/changelog.md index 2975057d..29cc8c17 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ - structurizr-dsl: Adds the ability to define a PlantUML/Mermaid image view that is an export of a workspace view. - structurizr-dsl: Adds support for `url`, `properties`, and `perspectives` nested inside `!elements` and `!relationships`. - structurizr-dsl: Fixes https://github.com/structurizr/java/issues/347 (`->container->` expression does not work as expected in deployment view). +- structurizr-dsl: Adds support for `!elements group` (https://github.com/structurizr/java/issues/351). ## 3.0.0 (19th September 2024) diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/ExpressionParser.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/ExpressionParser.java index 8ee65bbc..cac37094 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/ExpressionParser.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/ExpressionParser.java @@ -274,6 +274,12 @@ private Set evaluateExpression(String expr, DslContext context) { modelItems.add(relationship); } }); + } else { + // fallback that the expression is an identifier + Set elements = getElements(expr, context); + if (!elements.isEmpty()) { + modelItems.addAll(elements); + } } return modelItems; diff --git a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java index 0c73de74..ec37f2af 100644 --- a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java +++ b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java @@ -531,7 +531,7 @@ void test_findElement() throws Exception { } @Test - void test_findElement_Hierachical() throws Exception { + void test_findElement_Hierarchical() throws Exception { File dslFile = new File("src/test/resources/dsl/find-element-hierarchical.dsl"); StructurizrDslParser parser = new StructurizrDslParser(); @@ -543,6 +543,33 @@ void test_findElement_Hierachical() throws Exception { assertEquals("Value3", component.getProperties().get("Name3")); } + @Test + void test_findElements_InFlatGroup() throws Exception { + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(new File("src/test/resources/dsl/find-elements-in-flat-group.dsl")); + + Person user = parser.getWorkspace().getModel().getPersonWithName("User"); + assertTrue(user.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("A"), "Uses")); + assertTrue(user.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("B"), "Uses")); + assertTrue(user.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("C"), "Uses")); + } + + @Test + void test_findElements_InNestedGroup() throws Exception { + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(new File("src/test/resources/dsl/find-elements-in-nested-group.dsl")); + + Person user1 = parser.getWorkspace().getModel().getPersonWithName("User 1"); + assertTrue(user1.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("A"), "Uses")); + assertTrue(user1.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("B"), "Uses")); + assertTrue(user1.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("C"), "Uses")); + + Person user2 = parser.getWorkspace().getModel().getPersonWithName("User 2"); + assertTrue(user2.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("A"), "Uses")); + assertFalse(user2.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("B"), "Uses")); + assertFalse(user2.hasEfferentRelationshipWith(parser.getWorkspace().getModel().getSoftwareSystemWithName("C"), "Uses")); + } + @Test void test_parallel1() throws Exception { StructurizrDslParser parser = new StructurizrDslParser(); diff --git a/structurizr-dsl/src/test/resources/dsl/find-elements-in-flat-group.dsl b/structurizr-dsl/src/test/resources/dsl/find-elements-in-flat-group.dsl new file mode 100644 index 00000000..a64eccbc --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/find-elements-in-flat-group.dsl @@ -0,0 +1,17 @@ +workspace { + + model { + user = person "User" + + group = group "Group" { + softwareSystem "A" + softwareSystem "B" + softwareSystem "C" + } + + !elements group { + user -> this "Uses" + } + } + +} \ No newline at end of file diff --git a/structurizr-dsl/src/test/resources/dsl/find-elements-in-nested-group.dsl b/structurizr-dsl/src/test/resources/dsl/find-elements-in-nested-group.dsl new file mode 100644 index 00000000..daae8b27 --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/find-elements-in-nested-group.dsl @@ -0,0 +1,34 @@ +workspace { + + model { + properties { + "structurizr.groupSeparator" "/" + } + + user1 = person "User 1" + user2 = person "User 2" + + department1 = group "Department 1" { + team1 = group "Team 1" { + softwareSystem "A" + } + + team2 = group "Team 2" { + softwareSystem "B" + } + + team3 = group "Team 3" { + softwareSystem "C" + } + } + + !elements department1 { + user1 -> this "Uses" + } + + !elements team1 { + user2 -> this "Uses" + } + } + +} \ No newline at end of file