-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
structurizr-dsl:
description
and technology
now work inside `!ele…
…ments` blocks.
- Loading branch information
1 parent
d95879f
commit b6fd8cd
Showing
5 changed files
with
129 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
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
50 changes: 50 additions & 0 deletions
50
structurizr-dsl/src/main/java/com/structurizr/dsl/ElementsParser.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,50 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.*; | ||
|
||
final class ElementsParser extends AbstractParser { | ||
|
||
private final static int DESCRIPTION_INDEX = 1; | ||
private final static int TECHNOLOGY_INDEX = 1; | ||
|
||
void parseDescription(ElementsDslContext context, Tokens tokens) { | ||
// description <description> | ||
if (tokens.hasMoreThan(DESCRIPTION_INDEX)) { | ||
throw new RuntimeException("Too many tokens, expected: description <description>"); | ||
} | ||
|
||
if (!tokens.includes(DESCRIPTION_INDEX)) { | ||
throw new RuntimeException("Expected: description <description>"); | ||
} | ||
|
||
String description = tokens.get(DESCRIPTION_INDEX); | ||
for (Element element : context.getElements()) { | ||
element.setDescription(description); | ||
} | ||
} | ||
|
||
void parseTechnology(ElementsDslContext context, Tokens tokens) { | ||
// technology <technology> | ||
if (tokens.hasMoreThan(TECHNOLOGY_INDEX)) { | ||
throw new RuntimeException("Too many tokens, expected: technology <technology>"); | ||
} | ||
|
||
if (!tokens.includes(TECHNOLOGY_INDEX)) { | ||
throw new RuntimeException("Expected: technology <technology>"); | ||
} | ||
|
||
String technology = tokens.get(TECHNOLOGY_INDEX); | ||
for (Element element : context.getElements()) { | ||
if (element instanceof Container) { | ||
((Container)element).setTechnology(technology); | ||
} else if (element instanceof Component) { | ||
((Component)element).setTechnology(technology); | ||
} else if (element instanceof DeploymentNode) { | ||
((DeploymentNode)element).setTechnology(technology); | ||
} else if (element instanceof InfrastructureNode) { | ||
((InfrastructureNode)element).setTechnology(technology); | ||
} | ||
} | ||
} | ||
|
||
} |
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
70 changes: 70 additions & 0 deletions
70
structurizr-dsl/src/test/java/com/structurizr/dsl/ElementsParserTests.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,70 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
class ElementsParserTests extends AbstractTests { | ||
|
||
private final ElementsParser parser = new ElementsParser(); | ||
|
||
@Test | ||
void test_parseTechnology_ThrowsAnException_WhenNoTechnologyIsSpecified() { | ||
try { | ||
parser.parseTechnology(null, tokens("technology")); | ||
fail(); | ||
} catch (Exception e) { | ||
assertEquals("Expected: technology <technology>", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
void test_parseTechnology() { | ||
SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System"); | ||
Container container = softwareSystem.addContainer("Container"); | ||
Component component = container.addComponent("Component"); | ||
DeploymentNode deploymentNode = model.addDeploymentNode("Deployment Node"); | ||
InfrastructureNode infrastructureNode = deploymentNode.addInfrastructureNode("Infrastructure Node"); | ||
|
||
ElementsDslContext context = new ElementsDslContext(null, Set.of(softwareSystem, container, component, deploymentNode, infrastructureNode)); | ||
|
||
parser.parseTechnology(context, tokens("technology", "Technology")); | ||
assertEquals("Technology", container.getTechnology()); | ||
assertEquals("Technology", component.getTechnology()); | ||
assertEquals("Technology", deploymentNode.getTechnology()); | ||
assertEquals("Technology", infrastructureNode.getTechnology()); | ||
} | ||
|
||
@Test | ||
void test_parseDescription_ThrowsAnException_WhenNoDescriptionIsSpecified() { | ||
try { | ||
parser.parseDescription(null, tokens("description")); | ||
fail(); | ||
} catch (Exception e) { | ||
assertEquals("Expected: description <description>", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
void test_parseDescription() { | ||
SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System"); | ||
Container container = softwareSystem.addContainer("Container"); | ||
Component component = container.addComponent("Component"); | ||
DeploymentNode deploymentNode = model.addDeploymentNode("Deployment Node"); | ||
InfrastructureNode infrastructureNode = deploymentNode.addInfrastructureNode("Infrastructure Node"); | ||
|
||
ElementsDslContext context = new ElementsDslContext(null, Set.of(softwareSystem, container, component, deploymentNode, infrastructureNode)); | ||
|
||
parser.parseDescription(context, tokens("description", "Description")); | ||
assertEquals("Description", softwareSystem.getDescription()); | ||
assertEquals("Description", container.getDescription()); | ||
assertEquals("Description", component.getDescription()); | ||
assertEquals("Description", deploymentNode.getDescription()); | ||
assertEquals("Description", infrastructureNode.getDescription()); | ||
} | ||
|
||
} |