Commit b2dad407 authored by Spiros Koulouzis's avatar Spiros Koulouzis

null checks

parent 1221b093
......@@ -2,7 +2,7 @@
* Copyright 2019 S. Koulouzis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this zipfile except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
......@@ -15,21 +15,29 @@
*/
package nl.uva.sne.drip.commons.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
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.FileSystem;
import java.nio.file.FileSystems;
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.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;
......@@ -98,29 +106,55 @@ 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);
public static void zipFolder(String sourceFolder, String zipFolder) throws FileNotFoundException, IOException {
try (FileOutputStream fos = new FileOutputStream(zipFolder);
ZipOutputStream zos = new ZipOutputStream(fos)) {
Path sourcePath = Paths.get(sourceFolder);
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
if (!sourcePath.equals(dir)) {
zos.putNextEntry(new ZipEntry(sourcePath.relativize(dir).toString() + "/"));
zos.closeEntry();
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(sourcePath.relativize(file).toString()));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
}
}
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);
public static void unzipFolder(String zipFile) throws IOException {
try (ZipFile zipfile = new ZipFile(zipFile)) {
FileSystem fileSystem = FileSystems.getDefault();
Enumeration<? extends ZipEntry> zipEntries = zipfile.entries();
String uncompressedDirectory = "uncompressed/";
Files.createDirectory(fileSystem.getPath(uncompressedDirectory));
while (zipEntries.hasMoreElements()) {
ZipEntry entry = zipEntries.nextElement();
if (entry.isDirectory()) {
Files.createDirectories(fileSystem.getPath(uncompressedDirectory + entry.getName()));
} else {
InputStream is = zipfile.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
String uncompressedFileName = uncompressedDirectory + entry.getName();
Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
Files.createFile(uncompressedFilePath);
try (FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName)) {
while (bis.available() > 0) {
fileOutput.write(bis.read());
}
}
}
zout.closeEntry();
}
}
}
......
......@@ -38,7 +38,6 @@ import java.util.logging.Logger;
import nl.uva.sne.drip.model.Exceptions.TypeExeption;
import nl.uva.sne.drip.model.NodeTemplate;
import nl.uva.sne.drip.model.NodeTemplateMap;
import nl.uva.sne.drip.model.cloud.storm.CloudsStormSubTopology;
import nl.uva.sne.drip.model.tosca.Credential;
import nl.uva.sne.drip.model.tosca.ToscaTemplate;
import nl.uva.sne.drip.sure.tosca.client.DefaultApi;
......@@ -319,8 +318,11 @@ public class ToscaHelper {
if (attributes == null) {
attributes = new HashMap<>();
}
attributes.put(stateName, nodeState.toString());
node.getNodeTemplate().attributes(attributes);
if (nodeState != null) {
attributes.put(stateName, nodeState.toString());
node.getNodeTemplate().attributes(attributes);
}
return node;
}
......
......@@ -37,6 +37,7 @@ import nl.uva.sne.drip.model.NodeTemplate;
import nl.uva.sne.drip.model.NodeTemplateMap;
import nl.uva.sne.drip.model.Provisioner;
import nl.uva.sne.drip.model.cloud.storm.CloudsStormSubTopology;
import nl.uva.sne.drip.model.cloud.storm.OpCode;
import nl.uva.sne.drip.model.tosca.Credential;
import nl.uva.sne.drip.model.tosca.ToscaTemplate;
import org.junit.After;
......@@ -420,29 +421,29 @@ public class ToscaHelperTest {
}
}
/**
* Test of getKeyPairsFromVM method, of class ToscaHelper.
*/
@Test
public void testGetKeyPairsFromVM() throws Exception {
System.out.println("getKeyPairsFromVM");
instance.uploadToscaTemplate(provisionedToscaTemplate);
KeyPair keyPair;
List<NodeTemplateMap> vmTopologyTemplatesMap = instance.getVMTopologyTemplates();
assertNotNull(vmTopologyTemplatesMap);
for (NodeTemplateMap nodeTemplateMap : vmTopologyTemplatesMap) {
assertNotNull(nodeTemplateMap);
List<NodeTemplateMap> vmTemplatesMap = instance.getTemplateVMsForVMTopology(nodeTemplateMap);
assertNotNull(vmTemplatesMap);
for (NodeTemplateMap vmMap : vmTemplatesMap) {
assertNotNull(vmMap);
assertNotNull(vmMap.getNodeTemplate());
keyPair = instance.getKeyPairsFromVM(vmMap.getNodeTemplate());
assertNotNull(keyPair);
if (serviceUp) {
System.out.println("getKeyPairsFromVM");
instance.uploadToscaTemplate(provisionedToscaTemplate);
KeyPair keyPair;
List<NodeTemplateMap> vmTopologyTemplatesMap = instance.getVMTopologyTemplates();
assertNotNull(vmTopologyTemplatesMap);
for (NodeTemplateMap nodeTemplateMap : vmTopologyTemplatesMap) {
assertNotNull(nodeTemplateMap);
List<NodeTemplateMap> vmTemplatesMap = instance.getTemplateVMsForVMTopology(nodeTemplateMap);
assertNotNull(vmTemplatesMap);
for (NodeTemplateMap vmMap : vmTemplatesMap) {
assertNotNull(vmMap);
assertNotNull(vmMap.getNodeTemplate());
keyPair = instance.getKeyPairsFromVM(vmMap.getNodeTemplate());
assertNotNull(keyPair);
}
}
}
}
/**
......@@ -461,4 +462,87 @@ public class ToscaHelperTest {
}
}
/**
* Test of getNodeCurrentState method, of class ToscaHelper.
*
* @throws java.io.IOException
* @throws com.fasterxml.jackson.core.JsonProcessingException
* @throws nl.uva.sne.drip.sure.tosca.client.ApiException
*/
@Test
public void testGetNodeCurrentState() throws IOException, JsonProcessingException, ApiException {
if (serviceUp) {
System.out.println("getNodeCurrentState");
instance.uploadToscaTemplate(provisionedToscaTemplate);
NodeTemplateMap node = instance.getVMTopologyTemplates().get(0);
ToscaHelper.NODE_STATES expResult = ToscaHelper.NODE_STATES.RUNNING;
ToscaHelper.NODE_STATES result = instance.getNodeCurrentState(node);
assertEquals(expResult, result);
}
}
/**
* Test of setNodeCurrentState method, of class ToscaHelper.
*
* @throws java.io.IOException
* @throws com.fasterxml.jackson.core.JsonProcessingException
* @throws nl.uva.sne.drip.sure.tosca.client.ApiException
*/
@Test
public void testSetNodeCurrentState() throws IOException, JsonProcessingException, ApiException {
if (serviceUp) {
System.out.println("setNodeCurrentState");
instance.uploadToscaTemplate(provisionedToscaTemplate);
NodeTemplateMap node = instance.getVMTopologyTemplates().get(0);
ToscaHelper.NODE_STATES nodeState = ToscaHelper.NODE_STATES.DELETED;
NodeTemplateMap result = instance.setNodeCurrentState(node, nodeState);
assertEquals(instance.getNodeCurrentState(node), nodeState);
instance.setNodeCurrentState(node, null);
}
}
/**
* Test of NodeDesiredState2CloudStormOperation method, of class
* ToscaHelper.
*/
@Test
public void testNodeDesiredState2CloudStormOperation() {
System.out.println("NodeDesiredState2CloudStormOperation");
ToscaHelper.NODE_STATES nodeDesiredState = ToscaHelper.NODE_STATES.RUNNING;
OpCode.OperationEnum expResult = OpCode.OperationEnum.PROVISION;
OpCode.OperationEnum result = ToscaHelper.NodeDesiredState2CloudStormOperation(nodeDesiredState);
assertEquals(expResult, result);
nodeDesiredState = ToscaHelper.NODE_STATES.DELETED;
expResult = OpCode.OperationEnum.DELETE;
result = ToscaHelper.NodeDesiredState2CloudStormOperation(nodeDesiredState);
assertEquals(expResult, result);
nodeDesiredState = ToscaHelper.NODE_STATES.STOPPED;
expResult = OpCode.OperationEnum.STOP;
result = ToscaHelper.NodeDesiredState2CloudStormOperation(nodeDesiredState);
assertEquals(expResult, result);
nodeDesiredState = ToscaHelper.NODE_STATES.STARTED;
expResult = OpCode.OperationEnum.START;
result = ToscaHelper.NodeDesiredState2CloudStormOperation(nodeDesiredState);
assertEquals(expResult, result);
}
/**
* Test of nodeCurrentState2CloudStormStatus method, of class ToscaHelper.
*/
@Test
public void testNodeCurrentState2CloudStormStatus() {
System.out.println("nodeCurrentState2CloudStormStatus");
ToscaHelper.NODE_STATES currentState = ToscaHelper.NODE_STATES.CONFIGURED;
CloudsStormSubTopology.StatusEnum expResult = null;
CloudsStormSubTopology.StatusEnum result = ToscaHelper.nodeCurrentState2CloudStormStatus(currentState);
assertEquals(expResult, result);
}
}
......@@ -77,7 +77,8 @@ public class Consumer extends DefaultConsumer {
}
CloudStormService service = new CloudStormService(this.properties, message.getToscaTemplate());
ToscaTemplate toscaTemplate = service.execute();
boolean dryRun = false;
ToscaTemplate toscaTemplate = service.execute(dryRun);
responceMessage = new Message();
responceMessage.setCreationDate(System.currentTimeMillis());
......
......@@ -129,17 +129,23 @@ public class CloudStormServiceTest {
/**
* Test of execute method, of class CloudStormService.
*
* @throws java.lang.Exception
*/
// @Test
// public void testExecute() throws Exception {
// if (ToscaHelper.isServiceUp(sureToscaBasePath)) {
// System.out.println("execute");
// CloudStormService instance = getService(messageExampleProvisioneRequestFilePath);
// instance.execute();
// }
// }
@Test
public void testExecute() throws Exception {
if (ToscaHelper.isServiceUp(sureToscaBasePath)) {
System.out.println("execute");
CloudStormService instance = getService(messageExampleProvisioneRequestFilePath);
boolean dryRun = true;
instance.execute(dryRun);
}
}
/**
* Test of writeCloudStormTopologyFiles method, of class CloudStormService.
*
* @throws java.lang.Exception
*/
@Test
public void testWriteCloudStormTopologyFiles() throws Exception {
......@@ -151,6 +157,8 @@ public class CloudStormServiceTest {
/**
* Test of buildSSHKeyPair method, of class CloudStormService.
*
* @throws java.lang.Exception
*/
@Test
public void testBuildSSHKeyPair() throws Exception {
......@@ -194,6 +202,8 @@ public class CloudStormServiceTest {
/**
* Test of getKeyPair method, of class CloudStormService.
*
* @throws java.lang.Exception
*/
@Test
public void testGetKeyPair() throws Exception {
......
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