Search in sources :

Example 91 with Workspace

use of com.structurizr.Workspace in project dsl by structurizr.

the class FilteredViewParser method parse.

FilteredView parse(DslContext context, Tokens tokens) {
    if (tokens.hasMoreThan(DESCRIPTION_INDEX)) {
        throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
    }
    if (!tokens.includes(TAGS_INDEX)) {
        throw new RuntimeException("Expected: " + GRAMMAR);
    }
    Workspace workspace = context.getWorkspace();
    String key;
    StaticView baseView;
    String baseKey = tokens.get(BASE_KEY_INDEX);
    String mode = tokens.get(MODE_INDEX);
    String tagsAsString = tokens.get(TAGS_INDEX);
    Set<String> tags = new HashSet<>();
    DecimalFormat format = new DecimalFormat("000");
    for (String tag : tagsAsString.split(",")) {
        if (!StringUtils.isNullOrEmpty(tag)) {
            tags.add(tag.trim());
        }
    }
    String description = "";
    if (tokens.includes(DESCRIPTION_INDEX)) {
        description = tokens.get(DESCRIPTION_INDEX);
    }
    FilterMode filterMode;
    if (FILTER_MODE_INCLUDE.equalsIgnoreCase(mode)) {
        filterMode = FilterMode.Include;
    } else if (FILTER_MODE_EXCLUDE.equalsIgnoreCase(mode)) {
        filterMode = FilterMode.Exclude;
    } else {
        throw new RuntimeException("Filter mode should be include or exclude");
    }
    if (workspace.getViews().getViews().stream().noneMatch(v -> v.getKey().equals(baseKey))) {
        throw new RuntimeException("The view \"" + baseKey + "\" does not exist");
    }
    baseView = (StaticView) workspace.getViews().getViews().stream().filter(v -> v instanceof StaticView && v.getKey().equals(baseKey)).findFirst().orElse(null);
    if (baseView == null) {
        throw new RuntimeException("The view \"" + baseKey + "\" must be a System Landscape, System Context, Container, or Component view");
    }
    if (tokens.includes(KEY_INDEX)) {
        key = tokens.get(KEY_INDEX);
    } else {
        key = baseView.getKey() + "-" + VIEW_TYPE + "-" + format.format(workspace.getViews().getDynamicViews().size() + 1);
    }
    validateViewKey(key);
    return workspace.getViews().createFilteredView(baseView, key, description, filterMode, tags.toArray(new String[0]));
}
Also used : DecimalFormat(java.text.DecimalFormat) FilterMode(com.structurizr.view.FilterMode) StaticView(com.structurizr.view.StaticView) Workspace(com.structurizr.Workspace) HashSet(java.util.HashSet)

Example 92 with Workspace

use of com.structurizr.Workspace in project dsl by structurizr.

the class DeploymentViewParser method parse.

DeploymentView parse(DslContext context, Tokens tokens) {
    if (tokens.hasMoreThan(DESCRIPTION_INDEX)) {
        throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
    }
    if (!tokens.includes(ENVIRONMENT_INDEX)) {
        throw new RuntimeException("Expected: " + GRAMMAR);
    }
    Workspace workspace = context.getWorkspace();
    String key = "";
    String scopeIdentifier = tokens.get(SCOPE_IDENTIFIER_INDEX);
    String environment = tokens.get(ENVIRONMENT_INDEX);
    if (context.getElement(environment) != null && context.getElement(environment) instanceof DeploymentEnvironment) {
        environment = context.getElement(environment).getName();
    }
    // check that the deployment environment exists in the model
    final String env = environment;
    if (context.getWorkspace().getModel().getDeploymentNodes().stream().noneMatch(dn -> dn.getEnvironment().equals(env))) {
        throw new RuntimeException("The environment \"" + environment + "\" does not exist");
    }
    String description = "";
    if (tokens.includes(DESCRIPTION_INDEX)) {
        description = tokens.get(DESCRIPTION_INDEX);
    }
    DeploymentView view;
    if ("*".equals(scopeIdentifier)) {
        if (tokens.includes(KEY_INDEX)) {
            key = tokens.get(KEY_INDEX);
        } else {
            key = removeNonWordCharacters(environment) + "-" + VIEW_TYPE;
        }
        validateViewKey(key);
        view = workspace.getViews().createDeploymentView(key, description);
    } else {
        Element element = context.getElement(scopeIdentifier);
        if (element == null) {
            throw new RuntimeException("The software system \"" + scopeIdentifier + "\" does not exist");
        }
        if (element instanceof SoftwareSystem) {
            if (tokens.includes(KEY_INDEX)) {
                key = tokens.get(KEY_INDEX);
            } else {
                key = removeNonWordCharacters(element.getName()) + "-" + removeNonWordCharacters(environment) + "-" + VIEW_TYPE;
            }
            validateViewKey(key);
            view = workspace.getViews().createDeploymentView((SoftwareSystem) element, key, description);
        } else {
            throw new RuntimeException("The element \"" + scopeIdentifier + "\" is not a software system");
        }
    }
    view.setEnvironment(environment);
    return view;
}
Also used : Element(com.structurizr.model.Element) SoftwareSystem(com.structurizr.model.SoftwareSystem) Workspace(com.structurizr.Workspace) DeploymentView(com.structurizr.view.DeploymentView)

Example 93 with Workspace

use of com.structurizr.Workspace in project dsl by structurizr.

the class DslTests method test_utf8.

@Test
void test_utf8() throws Exception {
    StructurizrDslParser parser = new StructurizrDslParser();
    parser.parse(new File("src/test/dsl/utf8.dsl"));
    Workspace workspace = parser.getWorkspace();
    Model model = workspace.getModel();
    assertEquals(1, model.getPeople().size());
    Person user = model.getPersonWithName("你好 Usér \uD83D\uDE42");
    assertNotNull(user);
}
Also used : File(java.io.File) Workspace(com.structurizr.Workspace) Test(org.junit.jupiter.api.Test)

Example 94 with Workspace

use of com.structurizr.Workspace in project dsl by structurizr.

the class DslTests method test_extendWorkspaceFromDslFiles.

@Test
void test_extendWorkspaceFromDslFiles() throws Exception {
    StructurizrDslParser parser = new StructurizrDslParser();
    parser.parse(new File("src/test/dsl/extend/4.dsl"));
    Workspace workspace = parser.getWorkspace();
    Model model = workspace.getModel();
    ViewSet views = workspace.getViews();
    assertEquals(3, model.getPeople().size());
    assertEquals(1, views.getViews().size());
}
Also used : File(java.io.File) Workspace(com.structurizr.Workspace) Test(org.junit.jupiter.api.Test)

Example 95 with Workspace

use of com.structurizr.Workspace in project dsl by structurizr.

the class DslTests method test_extendWorkspaceFromDslUrl.

@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();
    ViewSet views = workspace.getViews();
    assertEquals("A new name", workspace.getName());
    assertEquals("A new description", workspace.getDescription());
    assertEquals(1, model.getPeople().size());
    Person user = model.getPersonWithName("User");
    assertEquals("A user of my software system.", user.getDescription());
    assertEquals(1, workspace.getModel().getSoftwareSystems().size());
    SoftwareSystem softwareSystem = model.getSoftwareSystemWithName("Software System");
    assertEquals("My software system.", softwareSystem.getDescription());
    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());
    assertEquals(1, views.getViews().size());
    assertEquals(1, views.getSystemContextViews().size());
    SystemContextView view = views.getSystemContextViews().iterator().next();
    assertEquals("SystemContext", view.getKey());
    assertEquals("An example of a System Context diagram.", view.getDescription());
    assertEquals(2, view.getElements().size());
    assertEquals(1, view.getRelationships().size());
    assertEquals(2, views.getConfiguration().getStyles().getElements().size());
    ElementStyle personStyle = views.getConfiguration().getStyles().getElements().stream().filter(es -> es.getTag().equals("Person")).findFirst().get();
    assertEquals(Shape.Person, personStyle.getShape());
    assertEquals("#08427b", personStyle.getBackground());
    assertEquals("#ffffff", personStyle.getColor());
    ElementStyle softwareSystemStyle = views.getConfiguration().getStyles().getElements().stream().filter(es -> es.getTag().equals("Software System")).findFirst().get();
    assertEquals("#1168bd", softwareSystemStyle.getBackground());
    assertEquals("#ffffff", softwareSystemStyle.getColor());
}
Also used : File(java.io.File) Workspace(com.structurizr.Workspace) Test(org.junit.jupiter.api.Test)

Aggregations

Workspace (com.structurizr.Workspace)155 Test (org.junit.Test)98 Test (org.junit.jupiter.api.Test)36 File (java.io.File)23 Container (com.structurizr.model.Container)22 SoftwareSystem (com.structurizr.model.SoftwareSystem)22 Component (com.structurizr.model.Component)12 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Application (org.archifacts.core.model.Application)12 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10 ArrayList (java.util.ArrayList)6 EncryptedWorkspace (com.structurizr.encryption.EncryptedWorkspace)5 Element (com.structurizr.model.Element)5 Model (com.structurizr.model.Model)5 Artifact (org.archifacts.core.model.Artifact)5 MiscArtifact (org.archifacts.core.model.MiscArtifact)5 Person (com.structurizr.model.Person)4 Relationship (com.structurizr.model.Relationship)4 SystemContextView (com.structurizr.view.SystemContextView)4