﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Infrastructure.Override;
using System.IO;
using PolicyModifyAnalyser;
using System.Xml.Serialization;
using System.Xml;

namespace Ctrip.SOA.Comm.Test
{
    [TestClass]
    public class CtripStreamReaderTest
    {
        [TestMethod]
        [DeploymentItem("Test References/响应short.xml")]
        [DeploymentItem("Test References/正确响应short.xml")]
        public void TestShortSymbol()
        {
            using (FileStream fs = File.OpenRead("响应short.xml"))
            {
                CtripStreamReader reader = new CtripStreamReader(fs, Encoding.UTF8, false, 512);

                int bufferSize = 4096;

                using (FileStream fsExpect = File.OpenRead("正确响应short.xml"))
                {
                    StreamReader sr = new StreamReader(fsExpect);
                    string strExpect = sr.ReadToEnd();
                    char[] buffer = new char[bufferSize];
                    StringBuilder sbResult = new StringBuilder();
                    int resultCount = 0;
                    while ((resultCount = reader.Read(buffer, 0, bufferSize)) > 0)
                    {
                        sbResult.Append(new string(buffer, 0, resultCount));
                    }

                    Assert.AreEqual<string>(sbResult.ToString().Trim(), strExpect.Trim());
                }
            }
        }

        [TestMethod]
        [DeploymentItem("Test References/响应long.xml")]
        [DeploymentItem("Test References/正确响应long.xml")]
        public void TestLongSymbol()
        {
            using (FileStream fs = File.OpenRead("响应long.xml"))
            {
                //CtripStreamReader reader = new CtripStreamReader(fs, Encoding.UTF8, false, 512);
                MyStreamReader reader = new MyStreamReader(fs, Encoding.UTF8);
                int bufferSize = 4097;

                using (FileStream fsExpect = File.OpenRead("正确响应long.xml"))
                {
                    StreamReader sr = new StreamReader(fsExpect);
                    string strExpect = sr.ReadToEnd();
                    char[] buffer = new char[bufferSize];
                    StringBuilder sbResult = new StringBuilder();
                    int resultCount = 0;
                    while ((resultCount = reader.Read(buffer, 0, 4096)) > 0)
                    {
                        sbResult.Append(new string(buffer, 0, resultCount));
                    }
                    string strResult = sbResult.ToString();
                    Assert.AreEqual<int>(strResult.Trim().Length, strExpect.Trim().Length);
                    Assert.AreEqual<string>(strResult.Trim(), strExpect.Trim());
                }
            }
        }

        [TestMethod]
        [DeploymentItem("Test References/响应long.xml")]
        public void TestXml()
        {
            using (FileStream fs = File.OpenRead("响应long.xml"))
            {
                //CtripStreamReader reader = new CtripStreamReader(fs, Encoding.UTF8, false, 512);
                MyStreamReader reader = new MyStreamReader(fs, Encoding.UTF8);
                XmlSerializer serializer = new XmlSerializer(typeof(Package.Tour.ShoppingService.Contract.Response));
                Package.Tour.ShoppingService.Contract.Response returnValue = (Package.Tour.ShoppingService.Contract.Response)serializer.Deserialize(reader);
                Assert.IsNotNull(returnValue);
            }
        }
    }
}
