Skip to content

Commit

Permalink
Add relationship name and implementation technology to provide enhanc…
Browse files Browse the repository at this point in the history
…ed relationship labels (#4)
  • Loading branch information
stefan-ka authored Mar 28, 2020
1 parent 4dc2551 commit 4a5296c
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ private MutableGraph createGraph(ContextMap contextMap) {
MutableNode node2 = this.bcNodesMap.get(rel.getSecondParticipant().getName());

if (rel instanceof Partnership) {
node1.addLink(to(node2).with(createLabel("Partnership"))
node1.addLink(to(node2).with(createLabel("Partnership", rel.getName(), rel.getImplementationTechnology()))
.add(attr("fontname", "sans-serif"))
.add(attr("style", "bold"))
.add(attr("fontsize", "12")));
} else if (rel instanceof SharedKernel) {
node1.addLink(to(node2).with(createLabel("Shared Kernel"))
node1.addLink(to(node2).with(createLabel("Shared Kernel", rel.getName(), rel.getImplementationTechnology()))
.add(attr("fontname", "sans-serif"))
.add(attr("style", "bold"))
.add(attr("fontsize", "12")));
} else {
UpstreamDownstreamRelationship upDownRel = (UpstreamDownstreamRelationship) rel;
node1.addLink(to(node2).with(
createLabel(upDownRel.isCustomerSupplier() ? "Customer/Supplier" : ""),
createLabel(upDownRel.isCustomerSupplier() ? "Customer/Supplier" : "", rel.getName(), rel.getImplementationTechnology()),
attr("labeldistance", "0"),
attr("fontname", "sans-serif"),
attr("fontsize", "12"),
Expand All @@ -183,7 +183,26 @@ private MutableGraph createGraph(ContextMap contextMap) {
return graph;
}

private Label createLabel(String label) {
private Label createLabel(String relationshipType, String relationshipName, String implementationTechnology) {
boolean relationshipTypeDefined = relationshipType != null && !"".equals(relationshipType);
boolean nameDefined = relationshipName != null && !"".equals(relationshipName);
boolean implementationTechnologyDefined = implementationTechnology != null && !"".equals(implementationTechnology);

String label = relationshipType;

if (relationshipTypeDefined && nameDefined && implementationTechnologyDefined)
label = relationshipName + " (" + relationshipType + " implemented with " + implementationTechnology + ")";
else if (nameDefined && implementationTechnologyDefined)
label = relationshipName + " (" + implementationTechnology + ")";
else if (relationshipTypeDefined && implementationTechnologyDefined)
label = relationshipType + " (" + implementationTechnology + ")";
else if (relationshipTypeDefined && nameDefined)
label = relationshipName + " (" + relationshipType + ")";
else if (nameDefined)
label = relationshipName;
else if (implementationTechnologyDefined)
label = implementationTechnology;

if (!"".equals(label))
return Label.of(label);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 The Context Mapper Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.contextmapper.contextmap.generator.model;

/**
* Represents a DDD relationship (that can have a name and an implementation technology).
*
* @author Stefan Kapferer
*/
public abstract class AbstractRelationship implements Relationship {

private String name = "";
private String implementationTechnology = "";

public AbstractRelationship setName(String name) {
this.name = name;
return this;
}

public AbstractRelationship setImplementationTechnology(String implementationTechnology) {
this.implementationTechnology = implementationTechnology;
return this;
}

@Override
public String getName() {
return name;
}

@Override
public String getImplementationTechnology() {
return implementationTechnology;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Stefan Kapferer
*/
public class Partnership implements Relationship {
public class Partnership extends AbstractRelationship implements Relationship {

private BoundedContext bc1;
private BoundedContext bc2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2020 The Context Mapper Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.contextmapper.contextmap.generator.model;

/**
Expand All @@ -11,4 +26,8 @@ public interface Relationship {

BoundedContext getSecondParticipant();

String getName();

String getImplementationTechnology();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Stefan Kapferer
*/
public class SharedKernel implements Relationship {
public class SharedKernel extends AbstractRelationship implements Relationship {

private BoundedContext bc1;
private BoundedContext bc2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Stefan Kapferer
*/
public class UpstreamDownstreamRelationship implements Relationship {
public class UpstreamDownstreamRelationship extends AbstractRelationship implements Relationship {

private BoundedContext upstreamBoundedContext;
private BoundedContext downstreamBoundedContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2020 The Context Mapper Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.contextmapper.contextmap.generator;

import guru.nidi.graphviz.engine.Format;
import org.contextmapper.contextmap.generator.model.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import static org.contextmapper.contextmap.generator.model.DownstreamPatterns.ANTICORRUPTION_LAYER;
import static org.contextmapper.contextmap.generator.model.DownstreamPatterns.CONFORMIST;
import static org.contextmapper.contextmap.generator.model.UpstreamPatterns.OPEN_HOST_SERVICE;
import static org.contextmapper.contextmap.generator.model.UpstreamPatterns.PUBLISHED_LANGUAGE;
import static org.junit.jupiter.api.Assertions.*;

public class ContextMapGeneratorLabelsTest {

private void deleteFileIfExisting(String filename) {
File file = new File(filename);
if (file.exists())
file.delete();
}

@Test
public void canGenerateContextMapWithFullLabel() throws IOException {
// given
String filename = "./src-gen/FullLabelTest.png";
ContextMapGenerator generator = new ContextMapGenerator();
BoundedContext testContext1 = new BoundedContext("TestContext1");
BoundedContext testContext2 = new BoundedContext("TestContext2");
ContextMap contextMap = new ContextMap()
.addBoundedContext(testContext1)
.addBoundedContext(testContext2)
.addRelationship(new Partnership(testContext1, testContext2)
.setName("TestRelName")
.setImplementationTechnology("Java"));

// when
deleteFileIfExisting(filename);
assertFalse(new File(filename + ".png").exists());
generator.setLabelSpacingFactor(10)
.generateContextMapGraphic(contextMap, Format.PNG, filename);

// then
assertTrue(new File(filename).exists());
}

@Test
public void canGenerateContextMapLabelWithNameAndTechnology() throws IOException {
// given
String filename = "./src-gen/NameAndTechnologyLabelTest.png";
ContextMapGenerator generator = new ContextMapGenerator();
BoundedContext testContext1 = new BoundedContext("TestContext1");
BoundedContext testContext2 = new BoundedContext("TestContext2");
ContextMap contextMap = new ContextMap()
.addBoundedContext(testContext1)
.addBoundedContext(testContext2)
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
.setName("TestRelName")
.setImplementationTechnology("Java"));

// when
deleteFileIfExisting(filename);
assertFalse(new File(filename + ".png").exists());
generator.setLabelSpacingFactor(10)
.generateContextMapGraphic(contextMap, Format.PNG, filename);

// then
assertTrue(new File(filename).exists());
}

@Test
public void canGenerateContextMapLabelWithTypeAndTechnology() throws IOException {
// given
String filename = "./src-gen/TypeAndTechnologyLabelTest.png";
ContextMapGenerator generator = new ContextMapGenerator();
BoundedContext testContext1 = new BoundedContext("TestContext1");
BoundedContext testContext2 = new BoundedContext("TestContext2");
ContextMap contextMap = new ContextMap()
.addBoundedContext(testContext1)
.addBoundedContext(testContext2)
.addRelationship(new SharedKernel(testContext1, testContext2)
.setImplementationTechnology("Java Library"));

// when
deleteFileIfExisting(filename);
assertFalse(new File(filename + ".png").exists());
generator.setLabelSpacingFactor(10)
.generateContextMapGraphic(contextMap, Format.PNG, filename);

// then
assertTrue(new File(filename).exists());
}

@Test
public void canGenerateContextMapLabelWithNameOnly() throws IOException {
// given
String filename = "./src-gen/NameOnlyLabelTest.png";
ContextMapGenerator generator = new ContextMapGenerator();
BoundedContext testContext1 = new BoundedContext("TestContext1");
BoundedContext testContext2 = new BoundedContext("TestContext2");
ContextMap contextMap = new ContextMap()
.addBoundedContext(testContext1)
.addBoundedContext(testContext2)
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
.setName("MyUpDownRel"));

// when
deleteFileIfExisting(filename);
assertFalse(new File(filename + ".png").exists());
generator.setLabelSpacingFactor(10)
.generateContextMapGraphic(contextMap, Format.PNG, filename);

// then
assertTrue(new File(filename).exists());
}

@Test
public void canGenerateContextMapLabelWithImplementationTechnologyOnly() throws IOException {
// given
String filename = "./src-gen/ImplementationTechnologyOnlyLabelTest.png";
ContextMapGenerator generator = new ContextMapGenerator();
BoundedContext testContext1 = new BoundedContext("TestContext1");
BoundedContext testContext2 = new BoundedContext("TestContext2");
ContextMap contextMap = new ContextMap()
.addBoundedContext(testContext1)
.addBoundedContext(testContext2)
.addRelationship(new UpstreamDownstreamRelationship(testContext1, testContext2)
.setImplementationTechnology("RESTful HTTP"));

// when
deleteFileIfExisting(filename);
assertFalse(new File(filename + ".png").exists());
generator.setLabelSpacingFactor(10)
.generateContextMapGraphic(contextMap, Format.PNG, filename);

// then
assertTrue(new File(filename).exists());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private ContextMap createTestContextMap() {
.addRelationship(new UpstreamDownstreamRelationship(customerManagement, policyManagement)
.setUpstreamPatterns(OPEN_HOST_SERVICE, PUBLISHED_LANGUAGE)
.setDownstreamPatterns(CONFORMIST))
.addRelationship(new Partnership(riskManagement, policyManagement));
.addRelationship(new Partnership(riskManagement, policyManagement).setName("RelNameTest"));
}

}

0 comments on commit 4a5296c

Please sign in to comment.