示例#1
0
        public void GetConfig_WhenValidFileIsProvided_ShouldReturnSingleConfiguration()
        {
            // arrange
            var expectedConfiguration = new EConfiguration()
            {
                Name = "file", Content = "TestContent"
            };

            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);

            fileManagerMoq.Setup(
                fm => fm.Exists(It.IsAny <string>())).Returns(true);

            fileManagerMoq.Setup(
                fm => fm.ReadAllText(It.IsAny <string>()))
            .Returns("TestContent").Verifiable();


            // act
            var configuration = model.ReadExternalConfig("D:\\file.host");

            // assert
            Assert.AreEqual(configuration, expectedConfiguration);
        }
示例#2
0
        public void LoadConfig_WhenIsNull_ShouldThrowArgumentNullException()
        {
            // arrange
            var            fileManagerMoq = new Mock <IFileHelper>();
            var            model          = new ModelStub(fileManagerMoq.Object);
            EConfiguration configuration  = null;

            // act
            model.LoadConfig(configuration);
        }
示例#3
0
        public void GetConfig_WhenNotExistingFileIsProvided_ShouldThrowFileNotFoundException()
        {
            // arrange
            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);

            fileManagerMoq.Setup(
                fm => fm.Exists(It.IsAny <string>())).Returns(false);

            // act
            var configuration = model.ReadExternalConfig("D:\\file.host");
        }
示例#4
0
        public void GetAll_ShouldReturnListOfConfigurations()
        {
            // arrange
            var expectedConfigurations = new List <EConfiguration>()
            {
                new EConfiguration()
                {
                    Name = "file", Content = "TestContent"
                },
                new EConfiguration()
                {
                    Name = "file2", Content = "TestContent"
                },
                new EConfiguration()
                {
                    Name = "file3", Content = "TestContent"
                }
            };

            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);

            fileManagerMoq.Setup(
                fm => fm.GetFiles(
                    It.Is <string>(s => s == Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "*.host"))))
            .Returns(new[] { "D:\\file.host", "D:\\file2.host", "D:\\file3.host" })
            .Verifiable();

            fileManagerMoq.Setup(
                fm => fm.Exists(It.IsAny <string>())).Returns(true);

            fileManagerMoq.Setup(
                fm => fm.ReadAllText(It.IsAny <string>()))
            .Returns("TestContent").Verifiable();


            // act
            var configList = model.GetAll();

            // assert
            foreach (var configuration in configList)
            {
                Assert.IsTrue(expectedConfigurations.Any(l => l.Equals(configuration)));
            }
        }
示例#5
0
        public void AddConfig_WhenValidConfigurationIsProvided_ShouldSaveConfigurationIntoFile()
        {
            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);
            var configuration  = new EConfiguration()
            {
                Content = "#File content \\n 192.28.129.100\tsomepage.com",
                Name    = "test"
            };
            string expectedFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                   Path.ChangeExtension(configuration.Name, ".host"));

            fileManagerMoq.Setup(fm => fm.WriteAllText(expectedFilename, configuration.Content)).Verifiable();

            // act
            model.AddConfig(configuration);

            // assert
            fileManagerMoq.Verify();
        }
示例#6
0
        public void LoadConfig_WhenConfigurationContentIsEmptyOrNull_ShouldThrowArgumentNullException()
        {
            // arrange
            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);
            var configuration  = new EConfiguration()
            {
                Name    = "file",
                Content = ""
            };

            fileManagerMoq.Setup(
                fm => fm.WriteAllText(It.Is <string>(s => s.ToLower() == "C:\\Windows\\system32\\drivers\\etc\\hosts".ToLower()),
                                      It.Is <string>(s => s == configuration.Content)))
            .Verifiable();

            // act
            model.LoadConfig(configuration);

            // assert
            fileManagerMoq.Verify();
        }
示例#7
0
        public void LoadConfig_WhenValidConfigIsProvided_ShouldReplaceWindowsHostsFile()
        {
            // arrange
            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);
            var configuration  = new EConfiguration()
            {
                Name    = "file",
                Content = "#File content \\n 192.28.129.100\tsomepage.com"
            };

            fileManagerMoq.Setup(
                fm => fm.WriteAllText(It.Is <string>(p => p.ToLower() == "C:\\Windows\\system32\\drivers\\etc\\hosts".ToLower()),
                                      configuration.Content))
            .Verifiable();

            // act
            model.LoadConfig(configuration);

            // assert
            fileManagerMoq.Verify();
        }
示例#8
0
        public void Exist_WhenConfigurationDontExists_ShouldReturnFalse()
        {
            // arrange
            var fileManagerMoq = new Mock <IFileHelper>();
            var model          = new ModelStub(fileManagerMoq.Object);
            var configuration  = new EConfiguration()
            {
                Content = "#File content \\n 192.28.129.100\tsomepage.com",
                Name    = "test"
            };
            string expectedFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                   Path.ChangeExtension(configuration.Name, ".host"));

            fileManagerMoq.Setup(
                fm => fm.Exists(It.Is <string>(s => s == expectedFilename))
                ).Returns(false).Verifiable();

            // act
            var result = model.Exists(configuration);

            // assert
            fileManagerMoq.Verify();
            Assert.IsFalse(result);
        }