From 699e670cc85508ff37423b283e15b714b66d256a Mon Sep 17 00:00:00 2001 From: Stefan Herrnleben <s.herrnleben@syslex.de> Date: Wed, 19 Jul 2017 23:54:14 +0200 Subject: [PATCH] extend dni model parser to handle byte arrays --- .../descartes/dni/dnimm3/tools/Parser.java | 28 +++++++++++++++++-- .../dni/dnimm3/tools/ParserTest.java | 23 +++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/tools.descartes.dni.core/src/main/java/tools/descartes/dni/dnimm3/tools/Parser.java b/tools.descartes.dni.core/src/main/java/tools/descartes/dni/dnimm3/tools/Parser.java index e3416323..bfcf4d6e 100644 --- a/tools.descartes.dni.core/src/main/java/tools/descartes/dni/dnimm3/tools/Parser.java +++ b/tools.descartes.dni.core/src/main/java/tools/descartes/dni/dnimm3/tools/Parser.java @@ -1,5 +1,7 @@ package tools.descartes.dni.dnimm3.tools; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Collections; @@ -16,7 +18,7 @@ import tools.descartes.dni.dnimm3.impl.NetworkInfrastructureImpl; public class Parser { - public static NetworkInfrastructureImpl convertFileToModel(File file) { + public static NetworkInfrastructureImpl convertToModel(File file) { ResourceSet resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); @@ -25,7 +27,29 @@ public class Parser { return (NetworkInfrastructureImpl) resource.getContents().get(0); } - public static File convertModelToFile(NetworkInfrastructure networkInfrastructure) throws IOException { + public static NetworkInfrastructureImpl convertToModel(byte[] data) throws IOException { + ResourceSet resourceSet = new ResourceSetImpl(); + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() + .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); + resourceSet.getPackageRegistry().put(DNIPackage.eNS_URI, DNIPackage.eINSTANCE); + Resource resource = resourceSet.createResource(URI.createURI("")); + resource.load(new ByteArrayInputStream(data), Collections.EMPTY_MAP); + return (NetworkInfrastructureImpl) resource.getContents().get(0); + } + + public static byte[] convertToByteArray(NetworkInfrastructure networkInfrastructure) throws IOException { + ResourceSet resourceSet = new ResourceSetImpl(); + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() + .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); + resourceSet.getPackageRegistry().put(DNIPackage.eNS_URI, DNIPackage.eINSTANCE); + Resource resource = resourceSet.createResource(URI.createURI("")); + resource.getContents().add(networkInfrastructure); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + resource.save(baos, Collections.EMPTY_MAP); + return baos.toByteArray(); + } + + public static File convertToTempFile(NetworkInfrastructure networkInfrastructure) throws IOException { File output = File.createTempFile("dni-", ".dni"); output.deleteOnExit(); ResourceSet resourceSet = new ResourceSetImpl(); diff --git a/tools.descartes.dni.core/src/test/java/tools/descartes/dni/dnimm3/tools/ParserTest.java b/tools.descartes.dni.core/src/test/java/tools/descartes/dni/dnimm3/tools/ParserTest.java index a17a1600..0edcee18 100644 --- a/tools.descartes.dni.core/src/test/java/tools/descartes/dni/dnimm3/tools/ParserTest.java +++ b/tools.descartes.dni.core/src/test/java/tools/descartes/dni/dnimm3/tools/ParserTest.java @@ -2,6 +2,9 @@ package tools.descartes.dni.dnimm3.tools; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import org.junit.Assert; import org.junit.Test; @@ -15,7 +18,7 @@ public class ParserTest { @Test public void shouldConvertFileToModel() { File file = new File("src/test/resources/scenario0A.dni"); - NetworkInfrastructureImpl networkInfrastructure = Parser.convertFileToModel(file); + NetworkInfrastructureImpl networkInfrastructure = Parser.convertToModel(file); Assert.assertEquals(6, networkInfrastructure.getStructure().getNodes().size()); } @@ -23,8 +26,24 @@ public class ParserTest { public void shouldConvertModelToFile() throws IOException { DNIFactoryImpl factory = new DNIFactoryImpl(); NetworkInfrastructure model = factory.createNetworkInfrastructure(); - File file = Parser.convertModelToFile(model); + File file = Parser.convertToTempFile(model); Assert.assertTrue(file.length() > 100); } + @Test + public void shouldConvertModelToByteArray() throws IOException { + DNIFactoryImpl factory = new DNIFactoryImpl(); + NetworkInfrastructure model = factory.createNetworkInfrastructure(); + byte[] bytes = Parser.convertToByteArray(model); + Assert.assertTrue(bytes.length > 100); + } + + @Test + public void shouldConvertByteArrayToModel() throws IOException { + Path path = Paths.get("src/test/resources/scenario0A.dni"); + byte[] data = Files.readAllBytes(path); + NetworkInfrastructureImpl networkInfrastructure = Parser.convertToModel(data); + Assert.assertEquals(6, networkInfrastructure.getStructure().getNodes().size()); + } + } -- GitLab