public void Apply_ShouldNotPassFailedResultsOnToCacheManager() { //---------------Set up test pack------------------- var source1 = GetRandomHttpUrl(); var source2 = GetRandomHttpUrl(); var config = SettingsBuilder.Create() .WithSources(source1, source2) .Build(); var downloader = Substitute.For <IFileDownloader>(); var result1 = GetRandomBytes(); downloader.SetDownloadResult(source1, result1); downloader.SetFailedDownloadResultFor(source2); var cacheManager = Substitute.For <IBlocklistCacheManager>(); var sut = Create(config, downloader, blocklistCacheManager: cacheManager); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- sut.Apply(); //---------------Test Result ----------------------- cacheManager.Received().Set(source1, result1); cacheManager.DidNotReceive().Set(source2, Arg.Any <byte[]>()); }
public void Apply_ShouldApplyUserWhitelistItems() { //---------------Set up test pack------------------- var blacklist = GetRandomCollection(GetRandomHostname, 2, 5); var whitelist = GetRandomCollection(GetRandomHostname, 2, 5); var settings = SettingsBuilder.Create() .WithRedirectIp(GetRandomIPv4Address()) .WithWhitelist(whitelist.ToArray()) .WithBlacklist(blacklist.ToArray()) .Build(); var hostFile = Substitute.For <IHostFile>(); var factory = CreateHostFileFactoryFor(hostFile); var sut = Create(settings, hostFileFactory: factory); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- sut.Apply(); //---------------Test Result ----------------------- Received.InOrder(() => { blacklist.ForEach(e => hostFile.Redirect(e, settings.RedirectIp)); whitelist.ForEach(e => hostFile.Whitelist(e)); hostFile.Persist(); }); }
public void Apply_WhenHaveNoBlackListsOrWhiteLists_ShouldMergeAllHostsFromAllSourcesKnownToCacheManager() { //---------------Set up test pack------------------- var source1 = GetRandomHttpUrl(); var source2 = GetRandomHttpUrl(); var expectedIp1 = GetRandomIPv4Address(); var expectedHost1 = GetRandomHostname(); var expectedIp2 = GetRandomIPv4Address(); var expectedHost2 = GetRandomHostname(); var cacheManager = Substitute.For <IBlocklistCacheManager>(); var mergeReader1 = ReaderFor($"{expectedIp1} {expectedHost1}"); cacheManager.GetReaderFor(source1).Returns(mergeReader1); var mergeReader2 = ReaderFor($"{expectedIp2} {expectedHost2}"); cacheManager.GetReaderFor(source2).Returns(mergeReader2); var hostFilePath = GetRandomWindowsPath(); var settings = SettingsBuilder.Create() .WithSources(source1, source2) .WithHostFile(hostFilePath) .WithRedirectIp(GetRandomIPv4Address()) .Build(); var reader = Substitute.For <ITextFileReader>(); var writer = Substitute.For <ITextFileWriter>(); var readerFactory = CreateReaderFactoryFor(hostFilePath, reader); var writerFactory = CreateWriterFactoryFor(hostFilePath, writer); var hostFile = Substitute.For <IHostFile>(); var hostFileFactory = CreateHostFileFactoryFor(hostFile); var sut = Create(settings, hostFileFactory: hostFileFactory, blocklistCacheManager: cacheManager, textFileReaderFactory: readerFactory, textFileWriterFactory: writerFactory); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- sut.Apply(); //---------------Test Result ----------------------- Received.InOrder(() => { readerFactory.Open(hostFilePath); writerFactory.Open(hostFilePath); hostFileFactory.Create(reader, writer); cacheManager.GetReaderFor(source1); hostFile.Merge(mergeReader1); cacheManager.GetReaderFor(source2); hostFile.Merge(mergeReader2); hostFile.SetRedirectIp(settings.RedirectIp); hostFile.Persist(); }); }
public void Build_OneAdapter_CreatesSettingsObject() { // Assemble var builder = SettingsBuilder.Create() .AddMemoryAdapter(); // Act var settings = builder.Build <IAppSettings>(); // Assert settings.Name = "Douglas"; Assert.AreEqual("Douglas", settings.Name); }
public void Load_DefaultConverter_ConvertsTimeSpan() { // Assemble var args = new[] { "--Date", "2012-3-4" }; var settings = SettingsBuilder.Create() .LoadFromCommandLine(args) .Build <ITestSettings>(); // Act var date = settings.Date; // Assert Assert.AreEqual(new DateTime(2012, 3, 4), date); }
public void Load_DefaultConverter_ConvertsInts() { // Assemble var args = new[] { "--TheAnswer", "42" }; var settings = SettingsBuilder.Create() .LoadFromCommandLine(args) .Build <ITestSettings>(); // Act var answer = settings.TheAnswer; // Assert Assert.AreEqual(42, answer); }
public void AddDefault_NonDefaultDoesNotHaveAValue_ReturnsValueFromDefault() { // Assemble var settings = SettingsBuilder.Create() .StartMapping <IAppSettings>() .Map(s => s.Name, "Adams") .Build <IAppSettings>(); // Act var name = settings.Name; // Assert Assert.AreEqual("Adams", name); }
public void Get_ValueIsInt_ReturnsInt() { // Assemble using (Env.SetVariable(nameof(IAppSettings.MaxItems), "42")) { var settings = SettingsBuilder.Create() .AddEnvironmentVariableAdapter() .Build <IAppSettings>(); // Act int maxItems = settings.MaxItems; // Assert Assert.AreEqual(42, maxItems); } }
public MySettings(IConfigurationManager configurationManager) { _configurationManager = configurationManager; _MyFirstProperty = SettingsBuilder <long> .Create(nameof(MyFirstProperty), 1345L, null, configurationManager); _MyFirstProperty.Description = "This is a description"; _MySecondProperty = SettingsBuilder <bool> .Create(nameof(MySecondProperty), default(bool), null, configurationManager); _MyThirdProperty = SettingsBuilder <string> .Create(nameof(MyThirdProperty), "Test", null, configurationManager); _MyThirdProperty.Description = "This is another description"; _MyFourthProperty = SettingsBuilder <TimeSpan> .ParseAndCreate(nameof(MyFourthProperty), "02:00", typeof(TimeSpanConverter), configurationManager); _MyFifthProperty = SettingsBuilder <bool> .ParseAndCreate(nameof(MyFifthProperty), "Ja", typeof(JaNeeConverter), configurationManager); }
public void Unapply_ShouldReturnHostsFileBackToOriginalState() { //---------------Set up test pack------------------- var settings = SettingsBuilder.Create() .WithHostFile(GetRandomWindowsPath()) .Build(); var reader = Substitute.For <ITextFileReader>(); var writer = Substitute.For <ITextFileWriter>(); var readerFactory = CreateReaderFactoryFor(settings.HostsFile, reader); var writerFactory = CreateWriterFactoryFor(settings.HostsFile, writer); var unexpected1 = GetRandomHostname(); var unexpected2 = GetAnother(unexpected1, GetRandomHostname); reader.SetData( "# original header", "127.0.0.1 localhost", "192.168.1.100 squishy", Constants.MERGE_MARKER, $"127.0.0.1 {unexpected1}", $"127.0.0.1 {unexpected2}" ); var hostFile = Substitute.For <IHostFile>(); var hostFileFactory = CreateHostFileFactoryFor(hostFile); var sut = Create(settings, textFileReaderFactory: readerFactory, textFileWriterFactory: writerFactory, hostFileFactory: hostFileFactory); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- sut.Unapply(); //---------------Test Result ----------------------- Received.InOrder(() => { readerFactory.Open(settings.HostsFile); writerFactory.Open(settings.HostsFile); hostFileFactory.Create(reader, writer); hostFile.Revert(); hostFile.Persist(); }); }
public void Read_MemoryAndEnvAdapters_ReadsFromMemory() { using (Env.SetVariable("Test.Name", "Douglas")) { var memData = new Dictionary <string, object> { ["Name"] = "Adams", }; var settings = SettingsBuilder.Create() .AddMemoryAdapter(memData) .AddEnvironmentVariableAdapter("Test.") .Build <IAppSettings>(); // Act var name = settings.Name; // Assert Assert.AreEqual("Adams", name); } }
public void AddDefault_InstanceSuppliedForDefault_CanReadDefault() { // Assemble var defaultValues = new DefaultTestSettings { Name = "Adams", }; var settings = SettingsBuilder.Create() .LoadFromObject(defaultValues) .Build <ITestSettings>(); // Act var name = settings.Name; var answer = settings.TheAnswer; // Assert Assert.AreEqual("Adams", name); Assert.AreEqual(42, answer); }
public void Read_ValueInFirstAndSecondAdapters_ReadsValueFromFirstAdapter() { // Assemble var mem1 = new MemoryAdapter(); var mem2 = new MemoryAdapter(); var settings = SettingsBuilder.Create() .Add(mem1) .Add(mem2) .Build <IAppSettings>(); mem1.Data["Name"] = "Douglas"; mem2.Data["Name"] = "Adams"; // Act var name = settings.Name; // Assert Assert.AreEqual("Douglas", name); }
public void Read_ValueOnlyInSecondAdapter_ReadsValueFromSecondAdapter() { // Assemble var mem1 = new MemoryAdapter(); var mem2 = new MemoryAdapter(); var builder = SettingsBuilder.Create() .Add(mem1) .Add(mem2); var settings = builder.Build <IAppSettings>(); mem2.Data["Name"] = "Adams"; // Act var name = settings.Name; // Assert Assert.AreEqual("Adams", name); }
public void Load_DefaultConverter_ConvertsDateTime() { // Assemble var args = new[] { "--TotalTime", "7:1:20:33.4" }; var settings = SettingsBuilder.Create() .LoadFromCommandLine(args) .Build <ITestSettings>(); var expected = new TimeSpan( days: 7, hours: 1, minutes: 20, seconds: 33, milliseconds: 400); // Act var date = settings.TotalTime; // Assert Assert.AreEqual(expected, date); }
public void Write_MultipleAdaptersAddedAtOnce_WritesToFirstAdapter() { // Assemble var mem1 = new MemoryAdapter(); var mem2 = new MemoryAdapter(); var builder = SettingsBuilder.Create() .Add(mem1) .Add(mem2); var settings = builder.Build <IAppSettings>(); // Act settings.Name = "Douglas"; // Assert object neverSet; Assert.AreEqual("Douglas", mem1.Data["Name"]); Assert.IsFalse(mem2.TryRead("Name", out neverSet)); }
public void Write_FirstAdapterIsReadOnly_WritesToSecondAdapter() { // Assemble var mem1 = new MemoryAdapter(); var mem2 = new MemoryAdapter(); var settings = SettingsBuilder.Create() .AddReadOnly(mem1) .Add(mem2) .Build <IAppSettings>(); mem1.Data["Name"] = "Unset"; mem2.Data["Name"] = "Unset"; // Act settings.Name = "Adams"; // Assert Assert.AreEqual("Unset", mem1.Data["Name"]); Assert.AreEqual("Adams", mem2.Data["Name"]); }