/// <summary>
        /// Creates an account and resource group for use with the Scenario tests
        /// </summary>
        public static BatchAccountContext CreateTestAccountAndResourceGroup(BatchController controller, string resourceGroupName, string accountName, string location)
        {
            controller.ResourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup()
            {
                Location = location
            });
            BatchAccountCreateResponse createResponse = controller.BatchManagementClient.Accounts.Create(resourceGroupName, accountName, new BatchAccountCreateParameters()
            {
                Location = location
            });
            BatchAccountContext         context  = BatchAccountContext.ConvertAccountResourceToNewAccountContext(createResponse.Resource);
            BatchAccountListKeyResponse response = controller.BatchManagementClient.Accounts.ListKeys(resourceGroupName, accountName);

            context.PrimaryAccountKey   = response.PrimaryKey;
            context.SecondaryAccountKey = response.SecondaryKey;
            return(context);
        }
        /// <summary>
        /// The Get Account Create Operation Status operation returns the
        /// status of the account creation operation. After calling an
        /// asynchronous operation, you can call this method to determine
        /// whether the operation has succeeded, failed, or is still in
        /// progress.
        /// </summary>
        /// <param name='operationStatusLink'>
        /// Required. Location value returned by the BeginCreating operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Values returned by the Create operation.
        /// </returns>
        public async Task <BatchAccountCreateResponse> GetAccountCreateOperationStatusAsync(string operationStatusLink, CancellationToken cancellationToken)
        {
            // Validate
            if (operationStatusLink == null)
            {
                throw new ArgumentNullException("operationStatusLink");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("operationStatusLink", operationStatusLink);
                TracingAdapter.Enter(invocationId, this, "GetAccountCreateOperationStatusAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + operationStatusLink;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-09-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    BatchAccountCreateResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new BatchAccountCreateResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            AccountResource resourceInstance = new AccountResource();
                            result.Resource = resourceInstance;

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                resourceInstance.Id = idInstance;
                            }

                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                resourceInstance.Type = typeInstance;
                            }

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                resourceInstance.Name = nameInstance;
                            }

                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                resourceInstance.Location = locationInstance;
                            }

                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property in tagsSequenceElement)
                                {
                                    string tagsKey   = ((string)property.Name);
                                    string tagsValue = ((string)property.Value);
                                    resourceInstance.Tags.Add(tagsKey, tagsValue);
                                }
                            }

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                AccountProperties propertiesInstance = new AccountProperties();
                                resourceInstance.Properties = propertiesInstance;

                                JToken accountEndpointValue = propertiesValue["accountEndpoint"];
                                if (accountEndpointValue != null && accountEndpointValue.Type != JTokenType.Null)
                                {
                                    string accountEndpointInstance = ((string)accountEndpointValue);
                                    propertiesInstance.AccountEndpoint = accountEndpointInstance;
                                }

                                JToken provisioningStateValue = propertiesValue["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    AccountProvisioningState provisioningStateInstance = ((AccountProvisioningState)Enum.Parse(typeof(AccountProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }

                                JToken coreQuotaValue = propertiesValue["coreQuota"];
                                if (coreQuotaValue != null && coreQuotaValue.Type != JTokenType.Null)
                                {
                                    int coreQuotaInstance = ((int)coreQuotaValue);
                                    propertiesInstance.CoreQuota = coreQuotaInstance;
                                }

                                JToken poolQuotaValue = propertiesValue["poolQuota"];
                                if (poolQuotaValue != null && poolQuotaValue.Type != JTokenType.Null)
                                {
                                    int poolQuotaInstance = ((int)poolQuotaValue);
                                    propertiesInstance.PoolQuota = poolQuotaInstance;
                                }

                                JToken activeJobAndJobScheduleQuotaValue = propertiesValue["activeJobAndJobScheduleQuota"];
                                if (activeJobAndJobScheduleQuotaValue != null && activeJobAndJobScheduleQuotaValue.Type != JTokenType.Null)
                                {
                                    int activeJobAndJobScheduleQuotaInstance = ((int)activeJobAndJobScheduleQuotaValue);
                                    propertiesInstance.ActiveJobAndJobScheduleQuota = activeJobAndJobScheduleQuotaInstance;
                                }
                            }

                            BatchManagementError errorInstance = new BatchManagementError();
                            result.Error = errorInstance;

                            JToken codeValue = responseDoc["code"];
                            if (codeValue != null && codeValue.Type != JTokenType.Null)
                            {
                                string codeInstance = ((string)codeValue);
                                errorInstance.Code = codeInstance;
                            }

                            JToken messageValue = responseDoc["message"];
                            if (messageValue != null && messageValue.Type != JTokenType.Null)
                            {
                                string messageInstance = ((string)messageValue);
                                errorInstance.Message = messageInstance;
                            }

                            JToken targetValue = responseDoc["target"];
                            if (targetValue != null && targetValue.Type != JTokenType.Null)
                            {
                                string targetInstance = ((string)targetValue);
                                errorInstance.Target = targetInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    if (result.Resource != null && result.Resource.Properties != null && result.Resource.Properties.ProvisioningState == AccountProvisioningState.Succeeded)
                    {
                        result.Status = OperationStatus.Succeeded;
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }