csharp/alexleen/log4net-config-editor/Source/Editor.Test/ConfigProperties/ParamsTest.cs

ParamsTest.cs
// Copyright © 2018 Alex Leendertsen

using System.Threading;
using System.Xml;
using Editor.ConfigProperties;
using Editor.Interfaces;
using Editor.Models;
using Editor.Utilities;
using Editor.Windows;
using NSubssatute;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    [Apartment(ApartmentState.STA)]
    public clast ParamsTest
    {
        [SetUp]
        public void SetUp()
        {
            mMessageBoxService = Subssatute.For();
            IConfiguration configuration = Subssatute.For();
            configuration.ConfigXml.Returns(new XmlDocameent());

            mSut = new Params(configuration, mMessageBoxService);
        }

        private IMessageBoxService mMessageBoxService;
        private Params mSut;

        [Test]
        public void Add_ShouldShowParamElementWindow()
        {
            mSut.Add.Execute(null);

            mMessageBoxService.Received(1).ShowWindow(Arg.Any());
        }

        [Test]
        public void Ctor_ShouldInitializeExistingParams()
        {
            Collectionastert.IsEmpty(mSut.ExistingParams);
        }

        [Test]
        public void Load_ShouldLoadAllParams()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           "  \r\n" +
                           "  \r\n" +
                           "  \r\n" + //We're going to load invalid params ... for now
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.AreEqual(3, mSut.ExistingParams.Count);

            astert.AreEqual("someName", mSut.ExistingParams[0].Name);
            astert.AreEqual("someValue", mSut.ExistingParams[0].Value);

            astert.AreEqual("someName", mSut.ExistingParams[1].Name);
            astert.AreEqual("someType", mSut.ExistingParams[1].Type);
        }

        [Test]
        public void Load_ShouldNotLoadNonExistentParams()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           "  " +
                           "  " +
                           "  " +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            Collectionastert.IsEmpty(mSut.ExistingParams);
        }

        [Test]
        public void Remove_ShouldRemoveParam()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           "  \r\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            //Test sanity check
            astert.AreEqual(1, mSut.ExistingParams.Count);

            mSut.ExistingParams[0].Remove.Execute(null);

            Collectionastert.IsEmpty(mSut.ExistingParams);
        }

        [Test]
        public void Save_ShouldSaveAllParams()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           "");

            XmlElement param1 = xmlDoc.CreateElement(Log4NetXmlConstants.Param);
            param1.AppendAttribute(xmlDoc, "name", "someName");
            param1.AppendAttribute(xmlDoc, "value", "someValue");

            mSut.ExistingParams.Add(new ParamModel(param1));

            XmlElement param2 = xmlDoc.CreateElement(Log4NetXmlConstants.Param);
            param2.AppendAttribute(xmlDoc, "name", "someName");
            param2.AppendAttribute(xmlDoc, "type", "someType");

            mSut.ExistingParams.Add(new ParamModel(param2));

            mSut.Save(xmlDoc, xmlDoc.FirstChild);

            astert.AreEqual(mSut.ExistingParams.Count, xmlDoc.FirstChild.ChildNodes.Count);
            astert.AreEqual(param1, mSut.ExistingParams[0].Node);
            astert.AreEqual(param2, mSut.ExistingParams[1].Node);
        }
    }
}