Commit 98c7bd7a authored by Spiros Koulouzis's avatar Spiros Koulouzis

added tests

parent d790ac01
......@@ -16,27 +16,20 @@
package nl.uva.sne.drip.commons.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -105,4 +98,31 @@ public class Converter {
return new String(encodedBytes, StandardCharsets.UTF_8);
}
public static void zipFolder(String sourceDir, String zipFile) throws FileNotFoundException, IOException {
FileOutputStream fout = new FileOutputStream(zipFile);
try (ZipOutputStream zout = new ZipOutputStream(fout)) {
File fileSource = new File(sourceDir);
addDirectory(zout, fileSource);
}
}
private static void addDirectory(ZipOutputStream zout, File fileSource) throws FileNotFoundException, IOException {
File[] files = fileSource.listFiles();
for (File file : files) {
if (file.isDirectory()) {
addDirectory(zout, file);
continue;
}
byte[] buffer = new byte[1024];
try (final FileInputStream fin = new FileInputStream(file)) {
zout.putNextEntry(new ZipEntry(file.getName()));
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
}
}
}
}
......@@ -453,17 +453,16 @@ class CloudStormService {
}
Map<String, String> provisionedFiles = new HashMap<>();
provisionedFiles.put("type", ENCODED_FILE_DATATYPE);
Path zipPath = Paths.get(tempInputDirPath + TOPOLOGY_RELATIVE_PATH + File.separator + TOPOLOGY_FOLDER_NAME + ".zip");
Path sourceFolderPath = Paths.get(tempInputDirPath + TOPOLOGY_RELATIVE_PATH);
String zipPath = (tempInputDirPath + File.separator + TOPOLOGY_FOLDER_NAME + ".zip");
String sourceFolderPath = tempInputDirPath + TOPOLOGY_RELATIVE_PATH;
Converter.zipFolder(sourceFolderPath, zipPath);
String cloudStormZipFileContentsAsBase64 = Converter.encodeFileToBase64Binary(zipPath.toFile().getAbsolutePath());
String cloudStormZipFileContentsAsBase64 = Converter.encodeFileToBase64Binary(zipPath);
provisionedFiles.put("file_contents", cloudStormZipFileContentsAsBase64);
provisionedFiles.put("encoding", "base64");
provisionedFiles.put("file_ext", "zip");
artifacts.put("provisioned_files", provisionedFiles);
vmTopologyMap.getNodeTemplate().setArtifacts(artifacts);
return vmTopologyMap;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment