public void TestCreateNewAzureMediaServiceAsync()
        {
            var fakeHttpHandler = new FakeHttpMessageHandler();

            string responseText = "{\"AccountId\":\"abe5afa0-704b-4d07-b5d8-5b0b039474e7\",\"AccountName\":\"tmp\",\"StatusCode\":201,\"Subscription\":\"f7190519-c29e-47f2-9019-c5a94c8e75f9\"}";

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new FakeHttpContent(responseText)
            };

            fakeHttpHandler.Send = request => response;

            var fakeHttpClient = fakeHttpHandler.CreateIMediaServicesHttpClient();

            var target = new MediaServicesClient(
                new SubscriptionData { SubscriptionId = _subscriptionId },
                null,
                fakeHttpClient,
                fakeHttpClient);

            var creationRequest = new AccountCreationRequest { AccountName = _accountName };

            var result = target.CreateNewAzureMediaServiceAsync(creationRequest).Result;
            Assert.AreEqual("tmp", result.Name);
        }
        public override void ExecuteCmdlet()
        {
            MediaServicesClient = MediaServicesClient ?? new MediaServicesClient(CurrentSubscription, WriteDebug);

            AccountCreationResult result = null;
            var request = new AccountCreationRequest()
            {
                AccountName = Name,
                BlobStorageEndpointUri = BlobStorageEndpointUri,
                Region = Region,
                StorageAccountKey = StorageAccountKey,
                StorageAccountName = StorageAccountName
            };

            InvokeInOperationContext(() =>
            {
                result = RetryCall(s => MediaServicesClient.NewAzureMediaService(request));

            });
            WriteObject(result, false);
        }
        public void TestCreateNewAzureMediaServiceAsyncInvalidAccount()
        {
            var fakeHttpHandler = new FakeHttpMessageHandler();

            string responseText = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">{\"Code\":\"BadRequest\",\"Message\":\"Account Creation Request contains an invalid account name.\"}</string>";

            var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new FakeHttpContent(responseText)
            };

            fakeHttpHandler.Send = request => response;

            var fakeHttpClient = fakeHttpHandler.CreateIMediaServicesHttpClient();

            var target = new MediaServicesClient(
                new SubscriptionData { SubscriptionId = _subscriptionId },
                null,
                fakeHttpClient,
                fakeHttpClient);

            var creationRequest = new AccountCreationRequest { AccountName = _accountName };

            try
            {
                var result = target.CreateNewAzureMediaServiceAsync(creationRequest).Result;
            }
            catch (AggregateException ax)
            {
                ServiceManagementClientException x = (ServiceManagementClientException)ax.InnerExceptions.Single();

                Assert.AreEqual(HttpStatusCode.BadRequest, x.HttpStatus);
                return;
            }

            Assert.Fail("ServiceManagementClientException expected");
        }
        public override void ExecuteCmdlet()
        {
            MediaServicesClient = MediaServicesClient ?? new MediaServicesClient(CurrentSubscription, WriteDebug);

            StorageService storage = null;
            Uri storageEndPoint = null;
            string storageAccountKey = null;

            CatchAggregatedExceptionFlattenAndRethrow(() => { storage = MediaServicesClient.GetStorageServiceKeys(StorageAccountName).Result; });
            storageAccountKey = storage.StorageServiceKeys.Primary;

            CatchAggregatedExceptionFlattenAndRethrow(() => { storage = MediaServicesClient.GetStorageServiceProperties(StorageAccountName).Result; });

            if (storage.StorageServiceProperties != null && storage.StorageServiceProperties.Endpoints.Count > 0)
            {
                storageEndPoint = new Uri(storage.StorageServiceProperties.Endpoints[0]);
            }
            else
            {
                throw new Exception(string.Format(Utilities.Properties.Resources.EndPointNotFoundForBlobStorage, Name));
            }

            AccountCreationResult result = null;
            var request = new AccountCreationRequest
            {
                AccountName = Name,
                BlobStorageEndpointUri = storageEndPoint.ToString(),
                Region = Location,
                StorageAccountKey = storageAccountKey,
                StorageAccountName = StorageAccountName
            };
            CatchAggregatedExceptionFlattenAndRethrow(() => { result = MediaServicesClient.CreateNewAzureMediaServiceAsync(request).Result; });
            WriteObject(result, false);
        }
        public AccountCreationResult NewAzureMediaService(AccountCreationRequest request)
        {
            AccountCreationResult result = null;
            using (HttpClient client = CreateIMediaServicesHttpClient())
            {
                HttpResponseMessage message = client.PostAsJsonAsyncWithoutEnsureSuccessCode(UriElements.Accounts, JObject.FromObject(request), Logger);
                string content =message.Content.ReadAsStringAsync().Result;
                if (message.IsSuccessStatusCode)
                {
                    result = JsonConvert.DeserializeObject(content, typeof (AccountCreationResult)) as AccountCreationResult;
                }
                else
                {

                    XmlDocument  doc =new XmlDocument();
                    doc.LoadXml(content);
                    content = doc.InnerText;
                    var serviceError = JsonConvert.DeserializeObject(content, typeof(ServiceError)) as ServiceError;
                    //Websites.Services.ServiceError serviceError = (ServiceError) serializer.Deserialize(message.Content.ReadAsStreamAsync().Result);
                    throw new ServiceManagementClientException(message.StatusCode, new ServiceManagementError() { Code = message.StatusCode.ToString(), Message = serviceError.Message}, string.Empty);
                }
            }
            return result;
        }
        public void NewMediaServiceAccountShouldPassWithValidParameters()
        {
            // Setup
            var clientMock = new Mock<IMediaServicesClient>();

            const string storageAccountName = "teststorage";
            const string storageAccountKey = "key";
            const string accountName = "testaccount";
            const string region = "West US";
            const string blobStorageEndpointUri = "http://awesome.blob.core.windows.net/";

            AccountCreationRequest request = new AccountCreationRequest()
            {
                AccountName = accountName,
                BlobStorageEndpointUri = blobStorageEndpointUri,
                Region = region,
                StorageAccountKey = storageAccountKey,
                StorageAccountName = storageAccountName

            };

            clientMock.Setup(f => f.CreateNewAzureMediaServiceAsync(It.Is<AccountCreationRequest>(creationRequest => request.AccountName == accountName))).Returns(Task.Factory.StartNew(() =>
            {
                return new AccountCreationResult()
                {
                    AccountId = Guid.NewGuid().ToString(),
                    Name = request.AccountName,
                    Subscription = Guid.NewGuid().ToString()
                };
            }));

            clientMock.Setup(f => f.GetStorageServiceKeys(storageAccountName)).Returns(Task.Factory.StartNew(() =>
            {
                return new StorageService()
                {
                    StorageServiceKeys = new StorageServiceKeys()
                    {
                        Primary = storageAccountKey,
                        Secondary = storageAccountKey

                    }
                };
            }));

            clientMock.Setup(f => f.GetStorageServiceProperties(storageAccountName)).Returns(Task.Factory.StartNew(() =>
            {
                return new StorageService()
                {
                    StorageServiceProperties = new StorageServiceProperties()
                    {
                        Endpoints = new EndpointList()
                    {
                        blobStorageEndpointUri
                    }
                    }
                };
            }));

            // Test
            var command = new NewAzureMediaServiceCommand()
            {
                CommandRuntime = new MockCommandRuntime(),
                Name = accountName,
                Location = region,
                StorageAccountName = storageAccountName,
                MediaServicesClient = clientMock.Object,
            };

            command.ExecuteCmdlet();
            Assert.AreEqual(1, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
            var accountCreationResult = (AccountCreationResult)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();
            Assert.IsNotNull(accountCreationResult);
            Assert.AreEqual(accountName, accountCreationResult.Name);
        }