Skip to content

Commit a7037da

Browse files
committed
Small improvements for serialization
1 parent cdbde20 commit a7037da

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/main/java/org/contextmapper/discovery/ContextMapSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.contextmapper.discovery.cml.ContextMapToCMLConverter;
2020
import org.contextmapper.discovery.model.ContextMap;
2121
import org.contextmapper.dsl.ContextMappingDSLStandaloneSetup;
22-
import org.contextmapper.dsl.contextMappingDSL.ContextMappingDSLFactory;
2322
import org.contextmapper.dsl.contextMappingDSL.ContextMappingModel;
2423
import org.eclipse.emf.common.util.URI;
2524
import org.eclipse.emf.ecore.resource.Resource;
@@ -39,6 +38,8 @@ public class ContextMapSerializer {
3938
public void serializeContextMap(ContextMap contextMap, File cmlFile) throws IOException {
4039
if (!FilenameUtils.getExtension(cmlFile.toString()).equals("cml"))
4140
throw new IllegalArgumentException("The CML file must end with the file extension '*.cml'!");
41+
if (contextMap.getBoundedContexts().size() <= 0)
42+
throw new IllegalArgumentException("The Context Map must at least contain one Bounded Context to be serialized!");
4243

4344
ContextMappingDSLStandaloneSetup.doSetup();
4445
Resource resource = new ResourceSetImpl().createResource(URI.createURI(cmlFile.toURI().toString()));

src/main/java/org/contextmapper/discovery/cml/ContextMapToCMLConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public ContextMappingModel convert(org.contextmapper.discovery.model.ContextMap
3636
model.setMap(contextMap);
3737

3838
for (org.contextmapper.discovery.model.BoundedContext boundedContext : inputMap.getBoundedContexts()) {
39-
model.getBoundedContexts().add(convert(boundedContext));
39+
BoundedContext bc = convert(boundedContext);
40+
model.getBoundedContexts().add(bc);
41+
contextMap.getBoundedContexts().add(bc);
4042
}
4143

4244
for (Relationship relationship : inputMap.getRelationships()) {

src/test/java/org/contextmapper/discovery/ContextMapSerializerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.contextmapper.discovery.strategies.boundedcontexts.SpringBootBoundedContextDiscoveryStrategy;
2020
import org.contextmapper.discovery.strategies.names.SeparatorToCamelCaseBoundedContextNameMappingStrategy;
2121
import org.contextmapper.discovery.strategies.relationships.DockerComposeRelationshipDiscoveryStrategy;
22+
import org.junit.jupiter.api.Assertions;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425

@@ -68,4 +69,28 @@ public void canSaveDiscoveredModelAsCMLFile() throws IOException {
6869
assertTrue(new File(TEST_CML_FILE).exists());
6970
}
7071

72+
@Test
73+
public void cannotSerializeOtherThanCMLFile() {
74+
// given
75+
ContextMapDiscoverer discoverer = new ContextMapDiscoverer()
76+
.usingBoundedContextDiscoveryStrategies(
77+
new SpringBootBoundedContextDiscoveryStrategy("test.microservice.spring.boot"));
78+
79+
// when, then
80+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
81+
new ContextMapSerializer().serializeContextMap(discoverer.discoverContextMap(), new File("test.ext"));
82+
});
83+
}
84+
85+
@Test
86+
public void cannotSerializeEmptyContextMap() {
87+
// given
88+
ContextMap contextMap = new ContextMap();
89+
90+
// when, then
91+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
92+
new ContextMapSerializer().serializeContextMap(contextMap, new File("test.cml"));
93+
});
94+
}
95+
7196
}

src/test/java/org/contextmapper/discovery/strategies/boundedcontexts/SpringBootBoundedContextDiscoveryStrategyTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.contextmapper.discovery.ContextMapDiscoverer;
1919
import org.contextmapper.discovery.model.BoundedContext;
20-
import org.contextmapper.discovery.strategies.boundedcontexts.SpringBootBoundedContextDiscoveryStrategy;
2120
import org.junit.jupiter.api.Test;
2221

2322
import java.util.Set;

0 commit comments

Comments
 (0)