// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsAzureMmc.Powershell.Host.Tests.Storage
{
    using System;
    using System.Collections.ObjectModel;
    using Microsoft.Samples.WindowsAzureMmc.Model.Storage;
    using Microsoft.Samples.WindowsAzureMmc.Powershell.Host;
    using Microsoft.Samples.WindowsAzureMmc.Powershell.Host.Tests.Helpers;
    using Microsoft.Samples.WindowsAzureMmc.Powershell.Host.Tests.Properties;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class BlobContainers
    {
        private const string GOODCONTAINERNAME = "btestb";
        private const string NOTEXISTSCONTAINERNAME = "fredisadog";
        private const string BADCONTAINERNAME = "&$^*&$^^$&*($^&*(";
    
        private static AzurePSHost host;

        [ClassInitialize()]
        public static void BlobContainersInitialize(TestContext context)
        {
            host = new AzurePSHost(Settings.Default.Account, Settings.Default.Key, true, new Uri(Settings.Default.BlobServiceUrl), new Uri(Settings.Default.QueueServiceUrl), new PSHostUIForTesting());
        }

        [ClassCleanup()]
        public static void BlobContainersCleanup()
        {
            ((IDisposable)host).Dispose();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            host.DeleteBlobContainer(GOODCONTAINERNAME);
        }

        [TestMethod]
        public void CreatePrivateBlobContainer()
        {
            host.CreateBlobContainer(GOODCONTAINERNAME, true);
            AzureBlobContainer container = host.RetrieveBlobContainer(GOODCONTAINERNAME);
            
            Assert.IsNotNull(container, "Failed to create container");
            Assert.AreEqual<string>(GOODCONTAINERNAME, container.Name);
            Assert.IsFalse(container.IsPublic);
        }

        [TestMethod]
        public void CreatePublicBlobContainer()
        {
            host.CreateBlobContainer(GOODCONTAINERNAME, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(GOODCONTAINERNAME);
            
            Assert.IsNotNull(container, "Failed to create container");
            Assert.AreEqual<string>(GOODCONTAINERNAME, container.Name);
            Assert.IsTrue(container.IsPublic);
        }

        [TestMethod]
        public void GetBlobContainerList()
        {
            // get a list of containers so we know how many we have to start with
            Collection<AzureBlobContainer> containers = host.RetrieveBlobContainers();
            Assert.IsNotNull(containers, "Could not get any containers");
            int count = containers.Count;

            // make a container so we have at least one
            host.CreateBlobContainer(GOODCONTAINERNAME, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(GOODCONTAINERNAME);

            Assert.IsNotNull(container, "Failed to create container");

            // get the list again - we should have 1 more 
            containers = host.RetrieveBlobContainers();
            Assert.IsNotNull(containers, "Could not get any containers");
            Assert.IsTrue(containers.Count == count + 1, "Could not find enough containers");
        }

        [TestMethod]
        public void GetBlobContainer()
        {
            // need to create a container before we can get it
            host.CreateBlobContainer(GOODCONTAINERNAME, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(GOODCONTAINERNAME);

            Assert.IsNotNull(container, "Failed to create container");

            container = host.RetrieveBlobContainer(GOODCONTAINERNAME);
            Assert.IsNotNull(container, "Failed to get container");
        }

        [TestMethod]
        public void DeleteBlobContainer()
        {
            // need to create a container before we can delete it
            host.CreateBlobContainer(GOODCONTAINERNAME, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(GOODCONTAINERNAME);

            Assert.IsNotNull(container, "Failed to create container");

            host.DeleteBlobContainer(GOODCONTAINERNAME);

            // try to get the container, this should fail
            container = host.RetrieveBlobContainer(GOODCONTAINERNAME);
            Assert.IsNull(container, "Container was not deleted");
        }

        [TestMethod]
        public void GetNotExistsContainer()
        {
            AzureBlobContainer container = host.RetrieveBlobContainer(NOTEXISTSCONTAINERNAME);
            
            Assert.IsNull(container, "Found non-existant container");
        }

        [TestMethod]
        [ExpectedException(typeof(System.Management.Automation.CmdletProviderInvocationException))]
        public void CreateContainerWithBadName()
        {
            host.CreateBlobContainer(BADCONTAINERNAME, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(BADCONTAINERNAME);
            Assert.IsNull(container, "Container was created with a bad name");
        }

        [TestMethod]
        [ExpectedException(typeof(System.ArgumentException))]
        public void CreateContainerWithBlankName()
        {
            host.CreateBlobContainer(string.Empty, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(string.Empty);

            Assert.IsNull(container, "Container was created with an empty name");
        }

        [TestMethod]
        [ExpectedException(typeof(System.Management.Automation.CmdletProviderInvocationException))]
        public void CreateContainerWithReallyLongName()
        {
            string reallyLongName = "abcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < 5; i++)
            {
                reallyLongName += reallyLongName;
            }

            host.CreateBlobContainer(reallyLongName, false);
            AzureBlobContainer container = host.RetrieveBlobContainer(reallyLongName);

            Assert.IsNull(container, "Container was created with a really long name");
        }

        [TestMethod]
        public void DeleteNonExistsContainer()
        {
            host.DeleteBlobContainer(NOTEXISTSCONTAINERNAME);

            Assert.IsTrue(true);
        }
    }
}