1+ package com .structurizr .view ;
2+
3+ import com .structurizr .Workspace ;
4+ import com .structurizr .model .Relationship ;
5+ import com .structurizr .model .SoftwareSystem ;
6+ import com .structurizr .model .Tags ;
7+ import org .junit .Assert ;
8+ import org .junit .Test ;
9+
10+ import java .util .ArrayList ;
11+ import java .util .Collection ;
12+
13+ import static org .junit .Assert .*;
14+
15+ public class ThemeUtilsTests {
16+
17+ @ Test
18+ public void test_loadThemes_DoesNothingWhenNoThemesAreDefined () throws Exception {
19+ Workspace workspace = new Workspace ("Name" , "Description" );
20+ ThemeUtils .loadThemes (workspace );
21+
22+ // there should still be zero styles in the workspace
23+ assertEquals (0 , workspace .getViews ().getConfiguration ().getStyles ().getElements ().size ());
24+ }
25+
26+ @ Test
27+ public void test_loadThemes_LoadsThemesWhenThemesAreDefined () throws Exception {
28+ Workspace workspace = new Workspace ("Name" , "Description" );
29+ SoftwareSystem softwareSystem = workspace .getModel ().addSoftwareSystem ("Name" );
30+ softwareSystem .addTags ("Amazon Web Services - Alexa For Business" );
31+ workspace .getViews ().getConfiguration ().setThemes ("https://static.structurizr.com/themes/amazon-web-services-2020.04.30/theme.json" );
32+
33+ ThemeUtils .loadThemes (workspace );
34+
35+ // there should still be zero styles in the workspace
36+ assertEquals (0 , workspace .getViews ().getConfiguration ().getStyles ().getElements ().size ());
37+
38+ // but we should be able to find a style included in the theme
39+ ElementStyle style = workspace .getViews ().getConfiguration ().getStyles ().findElementStyle (softwareSystem );
40+ assertNotNull (style );
41+ assertEquals ("#d6242d" , style .getStroke ());
42+ assertEquals ("#d6242d" , style .getColor ());
43+ assertNotNull (style .getIcon ());
44+ }
45+
46+ @ Test
47+ public void test_toJson () throws Exception {
48+ Workspace workspace = new Workspace ("Name" , "Description" );
49+ assertEquals ("{\n " +
50+ " \" name\" : \" Name\" ,\n " +
51+ " \" description\" : \" Description\" \n " +
52+ "}" , ThemeUtils .toJson (workspace ));
53+
54+ workspace .getViews ().getConfiguration ().getStyles ().addElementStyle (Tags .ELEMENT ).background ("#ff0000" );
55+ workspace .getViews ().getConfiguration ().getStyles ().addRelationshipStyle (Tags .RELATIONSHIP ).color ("#ff0000" );
56+ assertEquals ("{\n " +
57+ " \" name\" : \" Name\" ,\n " +
58+ " \" description\" : \" Description\" ,\n " +
59+ " \" elements\" : [ {\n " +
60+ " \" tag\" : \" Element\" ,\n " +
61+ " \" background\" : \" #ff0000\" \n " +
62+ " } ],\n " +
63+ " \" relationships\" : [ {\n " +
64+ " \" tag\" : \" Relationship\" ,\n " +
65+ " \" color\" : \" #ff0000\" \n " +
66+ " } ]\n " +
67+ "}" , ThemeUtils .toJson (workspace ));
68+ }
69+
70+ @ Test
71+ public void test_findElementStyle_WithThemes () {
72+ Workspace workspace = new Workspace ("Name" , "Description" );
73+ SoftwareSystem softwareSystem = workspace .getModel ().addSoftwareSystem ("Name" );
74+ workspace .getViews ().getConfiguration ().getStyles ().addElementStyle ("Element" ).shape (Shape .RoundedBox );
75+
76+ // theme 1
77+ Collection <ElementStyle > elementStyles = new ArrayList <>();
78+ Collection <RelationshipStyle > relationshipStyles = new ArrayList <>();
79+ elementStyles .add (new ElementStyle ("Element" ).shape (Shape .Box ).background ("#000000" ).color ("#ffffff" ));
80+ workspace .getViews ().getConfiguration ().getStyles ().addStylesFromTheme ("url1" , elementStyles , relationshipStyles );
81+
82+ // theme 2
83+ elementStyles = new ArrayList <>();
84+ relationshipStyles = new ArrayList <>();
85+ elementStyles .add (new ElementStyle ("Element" ).background ("#ff0000" ));
86+ workspace .getViews ().getConfiguration ().getStyles ().addStylesFromTheme ("url2" , elementStyles , relationshipStyles );
87+
88+ ElementStyle style = workspace .getViews ().getConfiguration ().getStyles ().findElementStyle (softwareSystem );
89+ assertEquals (new Integer (450 ), style .getWidth ());
90+ assertEquals (new Integer (300 ), style .getHeight ());
91+ assertEquals ("#ff0000" , style .getBackground ()); // from theme 2
92+ assertEquals ("#ffffff" , style .getColor ()); // from theme 1
93+ assertEquals (new Integer (24 ), style .getFontSize ());
94+ assertEquals (Shape .RoundedBox , style .getShape ()); // from workspace
95+ assertNull (style .getIcon ());
96+ assertEquals (Border .Solid , style .getBorder ());
97+ assertEquals ("#dddddd" , style .getStroke ());
98+ assertEquals (new Integer (100 ), style .getOpacity ());
99+ assertEquals (true , style .getMetadata ());
100+ assertEquals (true , style .getDescription ());
101+ }
102+
103+ @ Test
104+ public void test_findRelationshipStyle_WithThemes () {
105+ Workspace workspace = new Workspace ("Name" , "Description" );
106+ SoftwareSystem softwareSystem = workspace .getModel ().addSoftwareSystem ("Name" );
107+ Relationship relationship = softwareSystem .uses (softwareSystem , "Uses" );
108+ workspace .getViews ().getConfiguration ().getStyles ().addRelationshipStyle ("Relationship" ).dashed (false );
109+
110+ // theme 1
111+ Collection <ElementStyle > elementStyles = new ArrayList <>();
112+ Collection <RelationshipStyle > relationshipStyles = new ArrayList <>();
113+ relationshipStyles .add (new RelationshipStyle ("Relationship" ).color ("#ff0000" ).thickness (4 ));
114+ workspace .getViews ().getConfiguration ().getStyles ().addStylesFromTheme ("url1" , elementStyles , relationshipStyles );
115+
116+ // theme 2
117+ elementStyles = new ArrayList <>();
118+ relationshipStyles = new ArrayList <>();
119+ relationshipStyles .add (new RelationshipStyle ("Relationship" ).color ("#0000ff" ));
120+ workspace .getViews ().getConfiguration ().getStyles ().addStylesFromTheme ("url2" , elementStyles , relationshipStyles );
121+
122+ RelationshipStyle style = workspace .getViews ().getConfiguration ().getStyles ().findRelationshipStyle (relationship );
123+ assertEquals (new Integer (4 ), style .getThickness ()); // from theme 1
124+ assertEquals ("#0000ff" , style .getColor ()); // from theme 2
125+ Assert .assertFalse (style .getDashed ()); // from workspace
126+ assertEquals (Routing .Direct , style .getRouting ());
127+ assertEquals (new Integer (24 ), style .getFontSize ());
128+ assertEquals (new Integer (200 ), style .getWidth ());
129+ assertEquals (new Integer (50 ), style .getPosition ());
130+ assertEquals (new Integer (100 ), style .getOpacity ());
131+ }
132+
133+ }
0 commit comments