Prepares only the in memory file paths while building AASX in MetamodelToAASXConverter

Signed-off-by: Ashfaqul Haque <ashfaqul.haque@iese.fraunhofer.de>
Change-Id: I2c2d5074c0e0f59a881bf6772c1b4f7f7921512d
diff --git a/sdks/java/basys.sdk/src/main/java/org/eclipse/basyx/aas/factory/aasx/AASXFactory.java b/sdks/java/basys.sdk/src/main/java/org/eclipse/basyx/aas/factory/aasx/AASXFactory.java
index 1213ccb..99362b9 100644
--- a/sdks/java/basys.sdk/src/main/java/org/eclipse/basyx/aas/factory/aasx/AASXFactory.java
+++ b/sdks/java/basys.sdk/src/main/java/org/eclipse/basyx/aas/factory/aasx/AASXFactory.java
@@ -15,6 +15,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
@@ -85,7 +86,7 @@
 	public static void buildAASX(Collection<IAssetAdministrationShell> aasList, Collection<IAsset> assetList, 
 			Collection<IConceptDescription> conceptDescriptionList, Collection<ISubmodel> submodelList, Collection<InMemoryFile> files, OutputStream os) throws IOException, TransformerException, ParserConfigurationException {
 		
-		prepareFilePaths(submodelList);
+		prepareFilePaths(submodelList, files);
 		
 		OPCPackage rootPackage = OPCPackage.create(os);
 		
@@ -236,13 +237,26 @@
 	}
 	
 	/**
-	 * Replaces the path in all File Elements with the result of preparePath
+	 * Find files which has a valid in memory file path
+	 * @param elements
+	 * @param inMemoryFiles
+	 * @return
+	 */
+	private static Collection<File> findInMemoryFileElements(Collection<ISubmodelElement> elements, Collection<InMemoryFile> inMemoryFiles) {
+		Collection<File> files = findFileElements(elements);
+		return files.stream().filter(f -> 
+			isInMemoryFile(inMemoryFiles, f.getValue()))
+				.collect(Collectors.toList());
+	}
+	
+	/**
+	 * Replaces the path in File Elements which has an in memory file with the result of preparePath
 	 * 
 	 * @param submodels the Submodels
 	 */
-	private static void prepareFilePaths(Collection<ISubmodel> submodels) {
+	private static void prepareFilePaths(Collection<ISubmodel> submodels, Collection<InMemoryFile> inMemoryFiles) {
 		submodels.stream()
-			.forEach(sm -> findFileElements(sm.getSubmodelElements().values()).stream().forEach(f -> f.setValue(preparePath(f.getValue()))));
+			.forEach(sm -> findInMemoryFileElements(sm.getSubmodelElements().values(), inMemoryFiles).stream().forEach(f -> f.setValue(preparePath(f.getValue()))));
 	}
 	
 	/**
@@ -274,4 +288,20 @@
 		}
 		throw new ResourceNotFoundException("The wanted file '" + path + "' was not found in the given files.");
 	}
+	
+	/**
+	 * Finds an InMemoryFile by its path
+	 * 
+	 * @param files the InMemoryFiles
+	 * @param path the path of the wanted file
+	 * @return the InMemoryFile if it was found; else null
+	 */
+	private static boolean isInMemoryFile(Collection<InMemoryFile> files, String path) {
+		for(InMemoryFile file: files) {
+			if(VABPathTools.stripSlashes(file.getPath()).equals(VABPathTools.stripSlashes(path))) {
+				return true;
+			}
+		}
+		return false;
+	}
 }
diff --git a/sdks/java/basys.sdk/src/test/java/org/eclipse/basyx/testsuite/regression/aas/factory/aasx/AASXFactoryTest.java b/sdks/java/basys.sdk/src/test/java/org/eclipse/basyx/testsuite/regression/aas/factory/aasx/AASXFactoryTest.java
index 6a6925c..2939e97 100644
--- a/sdks/java/basys.sdk/src/test/java/org/eclipse/basyx/testsuite/regression/aas/factory/aasx/AASXFactoryTest.java
+++ b/sdks/java/basys.sdk/src/test/java/org/eclipse/basyx/testsuite/regression/aas/factory/aasx/AASXFactoryTest.java
@@ -10,6 +10,7 @@
 package org.eclipse.basyx.testsuite.regression.aas.factory.aasx;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -50,6 +51,7 @@
 
 	private static final String XML_PATH = "aasx/xml/content.xml";
 	private static final String ORIGIN_PATH = "aasx/aasx-origin";
+	private static final String EXTERNAL_FILE_URL = "http://localhost:8080/image.png";
 	
 	
 	private AssetAdministrationShell aas;
@@ -73,7 +75,7 @@
 		sm1 = new Submodel("sm1", new ModelUrn("SM1_ID"));
 		sm2 = new Submodel("sm2", new ModelUrn("SM2_ID"));
 		
-		File file1 = new File("http://localhost:8080/image.png", "image/png");
+        File file1 = new File(EXTERNAL_FILE_URL, "image/png");
 		file1.setIdShort("file1");
 		File file2 = new File("aasx/Document/docu.pdf", "application/pdf");
 		file2.setIdShort("file2");
@@ -98,16 +100,12 @@
 		assetList.add(asset);
 		
 		
-		byte[] content1 = {0,1,2,3,4};
-		InMemoryFile file = new InMemoryFile(content1, "/image.png");
+		byte[] content1 = {5,6,7,8,9};
+		InMemoryFile file = new InMemoryFile(content1, "/aasx/Document/docu.pdf");
 		fileList.add(file);
 		
-		byte[] content2 = {5,6,7,8,9};
-		file = new InMemoryFile(content2, "/aasx/Document/docu.pdf");
-		fileList.add(file);
-		
-		byte[] content3 = {10,11,12,13,14};
-		file = new InMemoryFile(content3, "aasx/Document/docu2.pdf");
+		byte[] content2 = {10,11,12,13,14};
+		file = new InMemoryFile(content2, "aasx/Document/docu2.pdf");
 		fileList.add(file);
 	}
 	
@@ -158,6 +156,7 @@
 			assertTrue(filePaths.contains(VABPathTools.stripSlashes(file.getPath())));
 		}
 		
+		assertFalse(filePaths.contains(VABPathTools.stripSlashes(EXTERNAL_FILE_URL)));	
 	}
 	
 }