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;
}
......
......@@ -15,8 +15,11 @@ import com.jcraft.jsch.KeyPair;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.utils.ToscaHelper;
import nl.uva.sne.drip.model.Message;
import nl.uva.sne.drip.model.NodeTemplateMap;
......@@ -32,6 +35,7 @@ import nl.uva.sne.drip.model.tosca.ToscaTemplate;
import static nl.uva.sne.drip.provisioner.CloudStormService.APP_FOLDER_NAME;
import static nl.uva.sne.drip.provisioner.CloudStormService.INFRASTUCTURE_CODE_FILE_NAME;
import static nl.uva.sne.drip.provisioner.CloudStormService.INFS_FOLDER_NAME;
import static nl.uva.sne.drip.provisioner.CloudStormService.TOPOLOGY_FOLDER_NAME;
import static nl.uva.sne.drip.provisioner.CloudStormService.TOPOLOGY_RELATIVE_PATH;
import static nl.uva.sne.drip.provisioner.CloudStormService.TOP_TOPOLOGY_FILE_NAME;
import static nl.uva.sne.drip.provisioner.CloudStormService.UC_FOLDER_NAME;
......@@ -192,6 +196,8 @@ public class CloudStormServiceTest {
/**
* Test of writeCloudStormInfrasCodeFiles method, of class
* CloudStormService.
*
* @throws java.lang.Exception
*/
@Test
public void testWriteCloudStormInfrasCodeFiles() throws Exception {
......@@ -286,20 +292,41 @@ public class CloudStormServiceTest {
/**
* Test of addCloudStromArtifacts method, of class CloudStormService.
*
* @throws java.lang.Exception
*/
@Test
public void testAddCloudStromArtifacts() throws Exception {
if (ToscaHelper.isServiceUp(sureToscaBasePath)) {
}
System.out.println("addCloudStromArtifacts");
initPaths();
testWriteCloudStormInfrasFiles(messageExampleProvisioneRequestFilePath, CloudsStormSubTopology.StatusEnum.FRESH, OpCode.OperationEnum.PROVISION);
CloudStormService instance = getService(messageExampleProvisioneRequestFilePath);
List<NodeTemplateMap> vmTopologiesMaps = instance.getHelper().getVMTopologyTemplates();
for (NodeTemplateMap vmTopologyMap : vmTopologiesMaps) {
vmTopologyMap = instance.addCloudStromArtifacts(vmTopologyMap, tempInputDirPath);
}
File zipFile = new File(tempInputDirPath + File.separator + TOPOLOGY_FOLDER_NAME + ".zip");
assertTrue(zipFile.exists());
assertTrue(zipFile.length() > 1);
String contentType = Files.probeContentType(Paths.get(zipFile.getAbsolutePath()));
assertEquals("application/zip", contentType);
Map<String, Object> artifacts = vmTopologyMap.getNodeTemplate().getArtifacts();
assertNotNull(artifacts);
assertTrue(artifacts.containsKey("provisioned_files"));
Map<String, String> provisionedFiles = (Map<String, String>) artifacts.get("provisioned_files");
assertNotNull(provisionedFiles);
}
assertTrue(provisionedFiles.containsKey("file_contents"));
String cloudStormZipFileContentsAsBase64 = provisionedFiles.get("file_contents");
String testZipPath = tempInputDirPath + File.separator + "test.zip";
Converter.decodeBase64BToFile(cloudStormZipFileContentsAsBase64, testZipPath);
contentType = Files.probeContentType(Paths.get(testZipPath));
assertEquals("application/zip", contentType);
assertEquals(Converter.getFileMD5(zipFile.getAbsolutePath()), Converter.getFileMD5(testZipPath));
assertTrue(provisionedFiles.containsKey("encoding"));
assertTrue(provisionedFiles.containsKey("file_ext"));
}
}
}
}
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