using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CurrencyExchangeLib;
using Microsoft.QualityTools.Testing.Fakes;

namespace CurrencyExchangeTestLib
{
    /// <summary>
    /// Summary description for VendorRateWebServiceTest
    /// </summary>
    [TestClass]
    public class VendorRateWebServiceTest
    {
        public VendorRateWebServiceTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ServiceRequestSendsProperQuery()
        {
            var queryString = "";
            IVendorRateWebService service = new VendorRateWebService();

            using (ShimsContext.Create())
            {
                //Edit the System.fakes to ask VS to create shim class for WebClient
                System.Net.Fakes.ShimWebClient.AllInstances.DownloadStringString =
                    (webClient, theQueryString) =>
                    {
                        queryString = theQueryString;
                        return "1.0";
                    };

                service.GetExchange("vendor1", "NOK");
            }

            Assert.AreEqual("http://www.agiledeveloper.com:8084?vendor=vendor1&currency=NOK", queryString);
        }

        [TestMethod]
        public void GetExchangeReturnsProperValueAsString()
        {
            IVendorRateWebService service = new VendorRateWebService();

            using (ShimsContext.Create())
            {
                //Edit the System.fakes to ask VS to create shim class for WebClient
                System.Net.Fakes.ShimWebClient.AllInstances.DownloadStringString =
                    (webClient, theQueryString) => "2.2";

                Assert.AreEqual(2.2, service.GetExchange("vendor1", "NOK"), 0.001);
            }
        }

        [TestMethod]
        public void GetExchangeThrowsApplicationExceptionOnBadResult()
        {
            IVendorRateWebService service = new VendorRateWebService();

            using (ShimsContext.Create())
            {
                //Edit the System.fakes to ask VS to create shim class for WebClient
                System.Net.Fakes.ShimWebClient.AllInstances.DownloadStringString =
                    (webClient, theQueryString) => "blah";

                try
                {
                    service.GetExchange("vendor1", "NOK");
                    Assert.Fail("expected exception");
                }
                catch (ApplicationException)
                {
                    //Success
                }
            }
        }
    }
}
