package alien4cloud.it.provider;

import java.io.IOException;

import org.jclouds.openstack.cinder.v1.domain.Volume;
import org.junit.Assert;

import alien4cloud.it.Context;
import alien4cloud.it.utils.JsonTestUtil;
import alien4cloud.paas.function.FunctionEvaluator;
import alien4cloud.rest.model.RestResponse;
import alien4cloud.topology.TopologyDTO;
import cucumber.api.java.en.And;

public class OpenStacksStepDefinitions {

    private String getVolumeId(String propertyName, String nodeName) throws IOException {
        String topologyResponseText = Context.getRestClientInstance().get("/rest/topologies/" + Context.getInstance().getTopologyId());
        RestResponse<TopologyDTO> topologyResponse = JsonTestUtil.read(topologyResponseText, TopologyDTO.class);
        String volumeId = FunctionEvaluator.getScalarValue(topologyResponse.getData().getTopology().getNodeTemplates().get(nodeName).getProperties()
                .get(propertyName));
        int indexOfEndRegion = volumeId.indexOf('/');
        if (indexOfEndRegion > 0) {
            volumeId = volumeId.substring(indexOfEndRegion + 1);
        }
        return volumeId;
    }

    @And("^I should have a volume on OpenStack with id defined in property \"([^\"]*)\" of the node \"([^\"]*)\"$")
    public void I_should_have_a_volume_on_OpenStack_with_id_defined_in_property_of_the_node(String propertyName, String nodeName) throws Throwable {
        Volume volume = Context.getInstance().getOpenStackClient().getVolume(getVolumeId(propertyName, nodeName));
        Assert.assertNotNull(volume);
    }

    @And("^I delete the volume on OpenStack with id defined in property \"([^\"]*)\" of the node \"([^\"]*)\"$")
    public void I_delete_the_volume_on_OpenStack_with_id_defined_in_property_of_the_node(String propertyName, String nodeName) throws Throwable {
        Assert.assertTrue(Context.getInstance().getOpenStackClient().deleteVolume(getVolumeId(propertyName, nodeName)));
    }
}

@@ -56,16 +56,74 @@
 ption;%0A%0A
+import alien4cloud.rest.deployment.DeploymentTopologyDTO;%0A
 import o
@@ -539,32 +539,48 @@
  String nodeName
+, String appName
 ) throws IOExcep
@@ -671,60 +671,184 @@
 est/
-topologies/%22 + Context.getInstance().getTopologyId()
+applications/%22 + Context.getInstance().getApplicationId(appName) + %22/environments/%22 + Context.getInstance().getDefaultApplicationEnvironmentId(appName) + %22/deployment-topology%22
 );%0A 
@@ -867,16 +867,26 @@
 esponse%3C
+Deployment
 Topology
@@ -949,16 +949,26 @@
 seText, 
+Deployment
 Topology
@@ -1466,32 +1466,49 @@
 ode %5C%22(%5B%5E%5C%22%5D*)%5C%22
+ for %5C%22(%5B%5E%5C%22%5D*)%5C%22
 $%22)%0A    public v
@@ -1615,32 +1615,48 @@
  String nodeName
+, String appName
 ) throws Throwab
@@ -1767,24 +1767,33 @@
 me, nodeName
+, appName
 ));%0A        
@@ -1930,32 +1930,49 @@
 ode %5C%22(%5B%5E%5C%22%5D*)%5C%22
+ for %5C%22(%5B%5E%5C%22%5D*)%5C%22
 $%22)%0A    public v
@@ -2080,24 +2080,40 @@
 ing nodeName
+, String appName
 ) throws Thr
@@ -2237,16 +2237,25 @@
 nodeName
+, appName
 )));%0A   

Improve the OpenStacksStepDefinitions