public void CreateDeploymentCommand_Contains_Body_Payload_StartDeployment_Default_Value()
        {
            _mockQueryManager.Setup(
                t =>
                t.MakeASyncRequest(It.IsAny<ServiceManagementRequest>(), It.IsAny<ServiceManager.AsyncResponseParser>(),
                                   It.IsAny<ServiceManager.AsyncResponseException>()));
            var command = new CreateDeploymentCommand("bob", "bill", "mypackage", "config", DeploymentSlot.Production);

            command.SitAndWait.Set();
            command.Execute();

            _mockQueryManager.Verify(
                t =>
                t.MakeASyncRequest(It.Is<ServiceManagementRequest>(tRequest => new XmlTestHelper(tRequest.Body).CheckXmlValue(Namespaces.NsWindowsAzure, "StartDeployment", "true")), It.IsAny<ServiceManager.AsyncResponseParser>(),
                                   It.IsAny<ServiceManager.AsyncResponseException>()), Times.Once());
        }
        /// <summary>
        /// Creates a deplopment based on the specific criteria specified in the DeploymentManager
        /// </summary>
        /// <param name="packageLocation">Where the package is left</param>
        private void CreateDeployment(string packageLocation)
        {
            bool deleted = false;

            if(!_blobClient.CheckStorageAccountHasResolved())
                throw new ApplicationException("unable to proceed storage account cannot be resolved after default 5 minute timeout");

            if (!_manager.UseExistingHostedService)
            {
                var hostedServiceCreate = new CreateCloudServiceCommand(_manager.HostedServiceName, _manager.Description ?? "Deployed by Fluent Management", _manager.Location)
                                              {
                                                  Certificate = _manager.ManagementCertificate,
                                                  SubscriptionId = _manager.SubscriptionId
                                              };
                hostedServiceCreate.Execute();
                _manager.WriteComplete(EventPoint.HostedServiceCreated, "Hosted service with name " + _manager.HostedServiceName + " created");
            }

            // send up service certificate - whatever happens we want the certificate up there - sometimes we may get a request but not need to alter the config of the SSL
            if (_manager.EnableSsl)
            {
                byte[] export = _manager.ServiceCertificate.Certificate.Export(X509ContentType.Pkcs12, _manager.ServiceCertificate.PvkPassword);

                var addCertificate =
                    new AddServiceCertificateCommand(export, _manager.ServiceCertificate.PvkPassword, _manager.HostedServiceName)
                        {
                            SubscriptionId = _manager.SubscriptionId,
                            Certificate = _manager.ManagementCertificate
                        };
                addCertificate.Execute();
            }

            // read in the enum value for the additional params
            bool startImmediately = true, treatErrorsAsWarnings = false;
            if (_manager.DeploymentParams.HasValue)
            {
                startImmediately = (_manager.DeploymentParams.Value & DeploymentParams.StartImmediately) == DeploymentParams.StartImmediately;
                treatErrorsAsWarnings = (_manager.DeploymentParams.Value & DeploymentParams.WarningsAsErrors) == DeploymentParams.WarningsAsErrors;
            }
            // read in the config and convert Base64
            var deployment = new CreateDeploymentCommand(_manager.HostedServiceName, _manager.DeploymentName,packageLocation,
                                                         _manager.Base64CsfgFile, _manager.DeploymentSlot, startImmediately, treatErrorsAsWarnings)
                                 {
                                     Certificate = _manager.ManagementCertificate,
                                     SubscriptionId = _manager.SubscriptionId
                                 };

            try
            {
                deployment.Execute();
                _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
            }
            catch (Exception)
            {
                DeleteDeployment();
                deleted = true;
            }
            finally
            {
                if (deleted)
                {
                    deployment.Execute();
                    _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
                }
                // check here and execute on a timer to see if the role are ready and running
                if (_manager.WaitUntilAllRoleInstancesAreRunning)
                {
                    var command = new GetAggregateDeploymentStatusCommand(_manager.HostedServiceName, _manager.DeploymentSlot)
                                      {
                                          Certificate = _manager.ManagementCertificate,
                                          SubscriptionId = _manager.SubscriptionId
                                      };
                    while (!command.AllDeploymentNodesRunning)
                    {
                        command.Execute();
                        // TODO: put a 5 second timer in here for now but replace with a timeout and exception method if over a certain value
                        Thread.Sleep(5000);
                    }
                }
            }
        }
        public void CreateDeploymentCommand_Contains_Correct_Uri()
        {
            string commandUri =
                "https://management.core.windows.net/aaaaaaaa-8130-49d7-95f9-aaaaaaaaaaaa/services/hostedservices/bob/deploymentslots/production";
            _mockQueryManager.Setup(
                t =>
                t.MakeASyncRequest(It.IsAny<ServiceManagementRequest>(), It.IsAny<ServiceManager.AsyncResponseParser>(),
                                   It.IsAny<ServiceManager.AsyncResponseException>()));
            var command = new CreateDeploymentCommand("bob", "bill", "mypackage", "config", DeploymentSlot.Production)
                {
                    SubscriptionId = "aaaaaaaa-8130-49d7-95f9-aaaaaaaaaaaa"
                };

            command.SitAndWait.Set();
            command.Execute();

            _mockQueryManager.Verify(
                t =>
                t.MakeASyncRequest(It.Is<ServiceManagementRequest>(tRequest => command.RequestUri.ToString() == commandUri),
                    It.IsAny<ServiceManager.AsyncResponseParser>(),
                    It.IsAny<ServiceManager.AsyncResponseException>()), Times.Once());
        }
        public void CreateDeploymentCommand_HttpVerb_Post()
        {
            _mockQueryManager.Setup(
                t =>
                t.MakeASyncRequest(It.IsAny<ServiceManagementRequest>(), It.IsAny<ServiceManager.AsyncResponseParser>(),
                                   It.IsAny<ServiceManager.AsyncResponseException>()));
            var command = new CreateDeploymentCommand("bob", "bill", "mypackage", "config", DeploymentSlot.Production);

            command.SitAndWait.Set();
            command.Execute();

            _mockQueryManager.Verify(
                t =>
                t.MakeASyncRequest(It.Is<ServiceManagementRequest>(tRequest => tRequest.HttpVerb == "POST"), It.IsAny<ServiceManager.AsyncResponseParser>(),
                                   It.IsAny<ServiceManager.AsyncResponseException>()), Times.Once());
        }