from unittest import TestCase

import jsonpickle
from mock import Mock, MagicMock
from cloudshell.cp.aws.aws_shell import AWSShell
from cloudshell.cp.aws.models.aws_ec2_cloud_provider_resource_model import AWSEc2CloudProviderResourceModel
from cloudshell.cp.aws.models.deploy_aws_ec2_ami_instance_resource_model import DeployAWSEc2AMIInstanceResourceModel


class TestAWSShell(TestCase):
    def setUp(self):
        self.aws_shell_api = AWSShell()

    def test_deploying_ami_returns_deploy_result(self):
        name = 'my instance name'
        result = Mock()
        result.instance_id = 'my instance id'

        deploymock = DeployAWSEc2AMIInstanceResourceModel()
        deploymock.auto_power_on = "True"
        deploymock.auto_power_off = "True"
        deploymock.wait_for_ip = "True"
        deploymock.auto_delete = "True"
        deploymock.autoload = "True"
        deploymock.aws_ec2 = "some_name"

        self.aws_shell_api._convert_to_deployment_resource_model = \
            Mock(return_value=(deploymock, name))
        self.aws_shell_api.convert_to_aws_resource_model = \
            Mock(return_value=(AWSEc2CloudProviderResourceModel()))

        self.aws_shell_api.aws_api.create_ec2_session = Mock(return_value=Mock())
        self.aws_shell_api.deploy_ami_operation.deploy = Mock(return_value=(result, name))

        aws_cloud_provider = AWSEc2CloudProviderResourceModel()
        res = self.aws_shell_api.deploy_ami(deploymock, aws_cloud_provider)

        ami_res_name = jsonpickle.decode(res)['vm_name']
        instance_id = jsonpickle.decode(res)['vm_uuid']

        self.assertEqual(ami_res_name, name)
        self.assertEqual(instance_id, result.instance_id)
        self.assertEqual(instance_id, result.instance_id)
        self.assertEqual(jsonpickle.decode(res)['auto_power_on'], deploymock.auto_power_on)
        self.assertEqual(jsonpickle.decode(res)['auto_power_off'], deploymock.auto_power_off)
        self.assertEqual(jsonpickle.decode(res)['wait_for_ip'], deploymock.wait_for_ip)
        self.assertEqual(jsonpickle.decode(res)['auto_delete'], deploymock.auto_delete)
        self.assertEqual(jsonpickle.decode(res)['autoload'], deploymock.autoload)
        self.assertEqual(jsonpickle.decode(res)['cloud_provider_resource_name'],deploymock.aws_ec2)

@@ -1387,32 +1387,33 @@
 ResourceModel()%0A
+%0A
         res = se
@@ -1407,16 +1407,19 @@
      res
+ult
  = self.
@@ -1475,25 +1475,24 @@
 ovider)%0A
-%0A
         
 ami_res_
@@ -1487,79 +1487,25 @@
     
-ami_res_name = jsonpickle.decode(res)%5B'vm_name'%5D%0A        instance_id = 
+self.assertEqual(
 json
@@ -1525,59 +1525,23 @@
 (res
+ult
 )%5B'vm_
-uuid'%5D%0A%0A        self.assertEqual(ami_res_
 name
+'%5D
 , na
@@ -1573,77 +1573,44 @@
 ual(
-instance_id, result.instance_id)%0A        self.assertEqual(instance_id
+jsonpickle.decode(result)%5B'vm_uuid'%5D
 , re
@@ -1665,32 +1665,35 @@
 ickle.decode(res
+ult
 )%5B'auto_power_on
@@ -1760,32 +1760,35 @@
 ickle.decode(res
+ult
 )%5B'auto_power_of
@@ -1865,16 +1865,19 @@
 code(res
+ult
 )%5B'wait_
@@ -1952,24 +1952,27 @@
 e.decode(res
+ult
 )%5B'auto_dele
@@ -2047,16 +2047,19 @@
 code(res
+ult
 )%5B'autol
@@ -2132,16 +2132,19 @@
 code(res
+ult
 )%5B'cloud

test deploy