Skip to content
Snippets Groups Projects
Commit 699e670c authored by Stefan Herrnleben's avatar Stefan Herrnleben
Browse files

extend dni model parser to handle byte arrays

parent fded1d83
No related branches found
No related tags found
No related merge requests found
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();
......
......@@ -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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment