Skip to content

Commit 4a5296c

Browse files
authored
Add relationship name and implementation technology to provide enhanced relationship labels (#4)
1 parent 4dc2551 commit 4a5296c

File tree

8 files changed

+252
-8
lines changed

8 files changed

+252
-8
lines changed

src/main/java/org/contextmapper/contextmap/generator/ContextMapGenerator.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ private MutableGraph createGraph(ContextMap contextMap) {
153153
MutableNode node2 = this.bcNodesMap.get(rel.getSecondParticipant().getName());
154154

155155
if (rel instanceof Partnership) {
156-
node1.addLink(to(node2).with(createLabel("Partnership"))
156+
node1.addLink(to(node2).with(createLabel("Partnership", rel.getName(), rel.getImplementationTechnology()))
157157
.add(attr("fontname", "sans-serif"))
158158
.add(attr("style", "bold"))
159159
.add(attr("fontsize", "12")));
160160
} else if (rel instanceof SharedKernel) {
161-
node1.addLink(to(node2).with(createLabel("Shared Kernel"))
161+
node1.addLink(to(node2).with(createLabel("Shared Kernel", rel.getName(), rel.getImplementationTechnology()))
162162
.add(attr("fontname", "sans-serif"))
163163
.add(attr("style", "bold"))
164164
.add(attr("fontsize", "12")));
165165
} else {
166166
UpstreamDownstreamRelationship upDownRel = (UpstreamDownstreamRelationship) rel;
167167
node1.addLink(to(node2).with(
168-
createLabel(upDownRel.isCustomerSupplier() ? "Customer/Supplier" : ""),
168+
createLabel(upDownRel.isCustomerSupplier() ? "Customer/Supplier" : "", rel.getName(), rel.getImplementationTechnology()),
169169
attr("labeldistance", "0"),
170170
attr("fontname", "sans-serif"),
171171
attr("fontsize", "12"),
@@ -183,7 +183,26 @@ private MutableGraph createGraph(ContextMap contextMap) {
183183
return graph;
184184
}
185185

186-
private Label createLabel(String label) {
186+
private Label createLabel(String relationshipType, String relationshipName, String implementationTechnology) {
187+
boolean relationshipTypeDefined = relationshipType != null && !"".equals(relationshipType);
188+
boolean nameDefined = relationshipName != null && !"".equals(relationshipName);
189+
boolean implementationTechnologyDefined = implementationTechnology != null && !"".equals(implementationTechnology);
190+
191+
String label = relationshipType;
192+
193+
if (relationshipTypeDefined && nameDefined && implementationTechnologyDefined)
194+
label = relationshipName + " (" + relationshipType + " implemented with " + implementationTechnology + ")";
195+
else if (nameDefined && implementationTechnologyDefined)
196+
label = relationshipName + " (" + implementationTechnology + ")";
197+
else if (relationshipTypeDefined && implementationTechnologyDefined)
198+
label = relationshipType + " (" + implementationTechnology + ")";
199+
else if (relationshipTypeDefined && nameDefined)
200+
label = relationshipName + " (" + relationshipType + ")";
201+
else if (nameDefined)
202+
label = relationshipName;
203+
else if (implementationTechnologyDefined)
204+
label = implementationTechnology;
205+
187206
if (!"".equals(label))
188207
return Label.of(label);
189208

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2020 The Context Mapper Project Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.contextmapper.contextmap.generator.model;
17+
18+
/**
19+
* Represents a DDD relationship (that can have a name and an implementation technology).
20+
*
21+
* @author Stefan Kapferer
22+
*/
23+
public abstract class AbstractRelationship implements Relationship {
24+
25+
private String name = "";
26+
private String implementationTechnology = "";
27+
28+
public AbstractRelationship setName(String name) {
29+
this.name = name;
30+
return this;
31+
}
32+
33+
public AbstractRelationship setImplementationTechnology(String implementationTechnology) {
34+
this.implementationTechnology = implementationTechnology;
35+
return this;
36+
}
37+
38+
@Override
39+
public String getName() {
40+
return name;
41+
}
42+
43+
@Override
44+
public String getImplementationTechnology() {
45+
return implementationTechnology;
46+
}
47+
}

src/main/java/org/contextmapper/contextmap/generator/model/Partnership.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Stefan Kapferer
2222
*/
23-
public class Partnership implements Relationship {
23+
public class Partnership extends AbstractRelationship implements Relationship {
2424

2525
private BoundedContext bc1;
2626
private BoundedContext bc2;

src/main/java/org/contextmapper/contextmap/generator/model/Relationship.java

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2020 The Context Mapper Project Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.contextmapper.contextmap.generator.model;
217

318
/**
@@ -11,4 +26,8 @@ public interface Relationship {
1126

1227
BoundedContext getSecondParticipant();
1328

29+
String getName();
30+
31+
String getImplementationTechnology();
32+
1433
}

src/main/java/org/contextmapper/contextmap/generator/model/SharedKernel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Stefan Kapferer
2222
*/
23-
public class SharedKernel implements Relationship {
23+
public class SharedKernel extends AbstractRelationship implements Relationship {
2424

2525
private BoundedContext bc1;
2626
private BoundedContext bc2;

src/main/java/org/contextmapper/contextmap/generator/model/UpstreamDownstreamRelationship.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @author Stefan Kapferer
2626
*/
27-
public class UpstreamDownstreamRelationship implements Relationship {
27+
public class UpstreamDownstreamRelationship extends AbstractRelationship implements Relationship {
2828

2929
private BoundedContext upstreamBoundedContext;
3030
private BoundedContext downstreamBoundedContext;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright 2020 The Context Mapper Project Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.contextmapper.contextmap.generator;
17+
18+
import guru.nidi.graphviz.engine.Format;
19+
import org.contextmapper.contextmap.generator.model.*;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.File;
24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
26+
import java.io.OutputStream;
27+
28+
import static org.contextmapper.contextmap.generator.model.DownstreamPatterns.ANTICORRUPTION_LAYER;
29+
import static org.contextmapper.contextmap.generator.model.DownstreamPatterns.CONFORMIST;
30+
import static org.contextmapper.contextmap.generator.model.UpstreamPatterns.OPEN_HOST_SERVICE;
31+
import static org.contextmapper.contextmap.generator.model.UpstreamPatterns.PUBLISHED_LANGUAGE;
32+
import static org.junit.jupiter.api.Assertions.*;
33+
34+
public class ContextMapGeneratorLabelsTest {
35+
36+
private void deleteFileIfExisting(String filename) {
37+
File file = new File(filename);
38+
if (file.exists())
39+
file.delete();
40+
}
41+
42+
@Test
43+
public void canGenerateContextMapWithFullLabel() throws IOException {
44+
// given
45+
String filename = "./src-gen/FullLabelTest.png";
46+
ContextMapGenerator generator = new ContextMapGenerator();
47+
BoundedContext testContext1 = new BoundedContext("TestContext1");
48+
BoundedContext testContext2 = new BoundedContext("TestContext2");
49+
ContextMap contextMap = new ContextMap()
50+
.addBoundedContext(testContext1)
51+
.addBoundedContext(testContext2)
52+
.addRelationship(new Partnership(testContext1, testContext2)
53+
.setName("TestRelName")
54+
.setImplementationTechnology("Java"));
55+
56+
// when
57+
deleteFileIfExisting(filename);
58+
assertFalse(new File(filename + ".png").exists());
59+
generator.setLabelSpacingFactor(10)
60+
.generateContextMapGraphic(contextMap, Format.PNG, filename);
61+
62+
// then
63+
assertTrue(new File(filename).exists());
64+
}
65+
66+
@Test
67+
public void canGenerateContextMapLabelWithNameAndTechnology() throws IOException {
68+
// given
69+
String filename = "./src-gen/NameAndTechnologyLabelTest.png";
70+
ContextMapGenerator generator = new ContextMapGenerator();
71+
BoundedContext testContext1 = new BoundedContext("TestContext1");
72+
BoundedContext testContext2 = new BoundedContext("TestContext2");
73+
ContextMap contextMap = new ContextMap()
74+
.addBoundedContext(testContext1)
75+
.addBoundedContext(testContext2)
76+
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
77+
.setName("TestRelName")
78+
.setImplementationTechnology("Java"));
79+
80+
// when
81+
deleteFileIfExisting(filename);
82+
assertFalse(new File(filename + ".png").exists());
83+
generator.setLabelSpacingFactor(10)
84+
.generateContextMapGraphic(contextMap, Format.PNG, filename);
85+
86+
// then
87+
assertTrue(new File(filename).exists());
88+
}
89+
90+
@Test
91+
public void canGenerateContextMapLabelWithTypeAndTechnology() throws IOException {
92+
// given
93+
String filename = "./src-gen/TypeAndTechnologyLabelTest.png";
94+
ContextMapGenerator generator = new ContextMapGenerator();
95+
BoundedContext testContext1 = new BoundedContext("TestContext1");
96+
BoundedContext testContext2 = new BoundedContext("TestContext2");
97+
ContextMap contextMap = new ContextMap()
98+
.addBoundedContext(testContext1)
99+
.addBoundedContext(testContext2)
100+
.addRelationship(new SharedKernel(testContext1, testContext2)
101+
.setImplementationTechnology("Java Library"));
102+
103+
// when
104+
deleteFileIfExisting(filename);
105+
assertFalse(new File(filename + ".png").exists());
106+
generator.setLabelSpacingFactor(10)
107+
.generateContextMapGraphic(contextMap, Format.PNG, filename);
108+
109+
// then
110+
assertTrue(new File(filename).exists());
111+
}
112+
113+
@Test
114+
public void canGenerateContextMapLabelWithNameOnly() throws IOException {
115+
// given
116+
String filename = "./src-gen/NameOnlyLabelTest.png";
117+
ContextMapGenerator generator = new ContextMapGenerator();
118+
BoundedContext testContext1 = new BoundedContext("TestContext1");
119+
BoundedContext testContext2 = new BoundedContext("TestContext2");
120+
ContextMap contextMap = new ContextMap()
121+
.addBoundedContext(testContext1)
122+
.addBoundedContext(testContext2)
123+
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
124+
.setName("MyUpDownRel"));
125+
126+
// when
127+
deleteFileIfExisting(filename);
128+
assertFalse(new File(filename + ".png").exists());
129+
generator.setLabelSpacingFactor(10)
130+
.generateContextMapGraphic(contextMap, Format.PNG, filename);
131+
132+
// then
133+
assertTrue(new File(filename).exists());
134+
}
135+
136+
@Test
137+
public void canGenerateContextMapLabelWithImplementationTechnologyOnly() throws IOException {
138+
// given
139+
String filename = "./src-gen/ImplementationTechnologyOnlyLabelTest.png";
140+
ContextMapGenerator generator = new ContextMapGenerator();
141+
BoundedContext testContext1 = new BoundedContext("TestContext1");
142+
BoundedContext testContext2 = new BoundedContext("TestContext2");
143+
ContextMap contextMap = new ContextMap()
144+
.addBoundedContext(testContext1)
145+
.addBoundedContext(testContext2)
146+
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
147+
.setImplementationTechnology("RESTful HTTP"));
148+
149+
// when
150+
deleteFileIfExisting(filename);
151+
assertFalse(new File(filename + ".png").exists());
152+
generator.setLabelSpacingFactor(10)
153+
.generateContextMapGraphic(contextMap, Format.PNG, filename);
154+
155+
// then
156+
assertTrue(new File(filename).exists());
157+
}
158+
159+
}

src/test/java/org/contextmapper/contextmap/generator/ContextMapGeneratorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private ContextMap createTestContextMap() {
215215
.addRelationship(new UpstreamDownstreamRelationship(customerManagement, policyManagement)
216216
.setUpstreamPatterns(OPEN_HOST_SERVICE, PUBLISHED_LANGUAGE)
217217
.setDownstreamPatterns(CONFORMIST))
218-
.addRelationship(new Partnership(riskManagement, policyManagement));
218+
.addRelationship(new Partnership(riskManagement, policyManagement).setName("RelNameTest"));
219219
}
220220

221221
}

0 commit comments

Comments
 (0)