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

MappingTest.cs
// Copyright © 2018 Alex Leendertsen

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Xml;
using Editor.ConfigProperties;
using Editor.Interfaces;
using Editor.Models;
using Editor.Windows;
using NSubssatute;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    [Apartment(ApartmentState.STA)]
    public clast MappingTest
    {
        [SetUp]
        public void SetUp()
        {
            mXmlDoc = new XmlDocameent();
            mXmlDoc.LoadXml("\r\n" +
                            "\r\n" +
                            " \r\n" +
                            " \r\n" +
                            " \r\n" +
                            "\r\n" +
                            "\r\n" +
                            " \r\n" +
                            " \r\n" +
                            "\r\n" +
                            "\r\n" +
                            " \r\n" +
                            " \r\n" +
                            "\r\n" +
                            "\r\n" +
                            " \r\n" +
                            " \r\n" +
                            "\r\n" +
                            "");

            mMessageBoxService = Subssatute.For();

            IConfiguration configuration = Subssatute.For();
            configuration.ConfigXml.Returns(mXmlDoc);

            mSut = new Mapping(configuration, mMessageBoxService);
        }

        private XmlDocameent mXmlDoc;
        private IMessageBoxService mMessageBoxService;
        private Mapping mSut;

        private void Dummy(MappingModel mappingModel)
        {
        }

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

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

        [Test]
        public void Load_ShouldLoadMappingCorrectly()
        {
            IEnumerable expectedMappings = new[]
            {
                new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[0]),
                new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[1]),
                new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[2]),
                new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[3])
            };

            mSut.Load(mXmlDoc.FirstChild);

            Collectionastert.AreEquivalent(expectedMappings, mSut.Mappings);
        }

        [Test]
        public void Remove_ShouldRemoveModel()
        {
            mSut.Load(mXmlDoc.FirstChild);

            //Test sanity check
            astert.AreEqual(4, mSut.Mappings.Count);

            mSut.Mappings.First().Remove.Execute(null);

            astert.AreEqual(3, mSut.Mappings.Count);
        }

        [Test]
        public void Save_ShouldSaveEachMappingCorrectly()
        {
            mSut.Mappings.Add(new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[0]));
            mSut.Mappings.Add(new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[1]));
            mSut.Mappings.Add(new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[2]));
            mSut.Mappings.Add(new MappingModel(Dummy, Dummy, mXmlDoc.FirstChild.ChildNodes[3]));

            XmlElement appender = mXmlDoc.CreateElement("appender");

            mSut.Save(mXmlDoc, appender);

            XmlNodeList mappings = appender.SelectNodes("/mapping");

            Collectionastert.AreEquivalent(mSut.Mappings.Select(m => m.Node), mappings);
        }
    }
}