diff --git a/Tests/TestSamples/Samples.cs b/Tests/TestSamples/Samples.cs index 9223003..20b8042 100644 --- a/Tests/TestSamples/Samples.cs +++ b/Tests/TestSamples/Samples.cs @@ -104,21 +104,21 @@ public static List ProvideSampleMetaInfos() var sample = new List() { { - new TrustCenterMetaInfo("Sample1", "https://www.speedtestx.de/testfiles/data_1mb.test", + new TrustCenterMetaInfo("Sample1", "https://www.test1Url.de", new DateTime(2020, 1, 1)) }, { - new TrustCenterMetaInfo("Sample2", "https://www.speedtestx.de/testfiles/data_1mb.test", + new TrustCenterMetaInfo("Sample2", "https://www.test2Url.de", new DateTime(2020, 1, 1)) }, { - new TrustCenterMetaInfo("Sample3", "https://www.speedtestx.de/testfiles/data_1mb.test", + new TrustCenterMetaInfo("Sample3", "https://www.test3Url.de", new DateTime(2020, 1, 1)) } }; return sample; } - + public static Task ProvideSampleTaskByteArray() { async Task GetByteArray() @@ -138,6 +138,5 @@ async Task> ProvideSampleOfCertificatesAsync() return ProvideSampleOfCertificatesAsync(); } - } } diff --git a/Tests/TrustCenterSearch.Core.Test/ConfigManagerTest.cs b/Tests/TrustCenterSearch.Core.Test/ConfigManagerTest.cs index fc29fce..396a713 100644 --- a/Tests/TrustCenterSearch.Core.Test/ConfigManagerTest.cs +++ b/Tests/TrustCenterSearch.Core.Test/ConfigManagerTest.cs @@ -1,12 +1,13 @@ -using System; +using FluentAssertions; +using System; +using System.Collections.Generic; using System.Linq; -using Xunit; -using TrustCenterSearch.Core; +using TestSamples; using TrustCenterSearch.Core.DataManagement.Configuration; using TrustCenterSearch.Core.Models; -using TestSamples; +using Xunit; -namespace TrustCenterSearchCore.Test +namespace TrustCenterSearch.Core.Test { public class ConfigManagerTest { @@ -20,7 +21,6 @@ public void AddTrustCenterToConfigTest() #endregion - #region Act core.ConfigManager.AddTrustCenterToConfig(new TrustCenterMetaInfo("test123", "test567", DateTime.Now), @@ -28,7 +28,6 @@ public void AddTrustCenterToConfigTest() #endregion - #region Assert var result = new Config(); @@ -49,14 +48,12 @@ public void ConfigIsEmptyTest() #endregion - #region Act var result = core.ConfigManager.IsConfigEmpty(config); #endregion - #region Assert Assert.True(result); @@ -76,14 +73,12 @@ public void ConfigIsNotEmptyTest() #endregion - #region Act var result = core.ConfigManager.IsConfigEmpty(config); #endregion - #region Assert Assert.False(result); @@ -104,19 +99,43 @@ public void DeleteCertificatesOfTrustCenterTest() #endregion - #region Act trustCenterManager.DeleteTrustCenterFromConfig(metaDataToDelete, configSample); #endregion - #region Assert Assert.DoesNotContain(configSample.TrustCenterMetaInfos, - trustCentermetaInfo => trustCentermetaInfo.Name.Equals(metaDataToDelete.Name)); + trustCenterMetaInfo => trustCenterMetaInfo.Name.Equals(metaDataToDelete.Name)); + + #endregion + } + + [Fact(DisplayName = "GetRelativeTrustCenterMetaInfoComplementTest")] + public void GetRelativeTrustCenterMetaInfoComplementTest() + { + #region Arrange + + var config = Samples.ProvideSampleConfig(); + var newConfig = Samples.ProvideSampleConfig(); + newConfig.TrustCenterMetaInfos = Samples.ProvideSampleMetaInfos().TakeLast(2).ToList(); + + var expectedResult = new List() { Samples.ProvideSampleMetaInfos()[0] }; + + #endregion + + #region Act + + var result = ConfigManager.GetRelativeTrustCenterMetaInfoComplement(config, newConfig); + + #endregion + + # region Assert + expectedResult.Should().BeEquivalentTo(result); + #endregion } } diff --git a/Tests/TrustCenterSearch.Core.Test/CoreTest.cs b/Tests/TrustCenterSearch.Core.Test/CoreTest.cs index d6a1f77..62e4ba9 100644 --- a/Tests/TrustCenterSearch.Core.Test/CoreTest.cs +++ b/Tests/TrustCenterSearch.Core.Test/CoreTest.cs @@ -1,41 +1,39 @@ -using Moq; -using System; +using System; using System.Linq; +using Moq; using TestSamples; -using TrustCenterSearch.Core; using TrustCenterSearch.Core.DataManagement.Configuration; using TrustCenterSearch.Core.DataManagement.TrustCenters; using TrustCenterSearch.Core.Models; using Xunit; -namespace TrustCenterSearchCore.Test +namespace TrustCenterSearch.Core.Test { public class CoreTest { [Fact(DisplayName = "ImportAllCertificatesFromTrustCentersAsyncTest")] - public void ImportAllCertificatesFromTrustCentersAsyncTest() + public async void ImportAllCertificatesFromTrustCentersAsyncTest() { #region Arrange - var moqTrustCenterManager = new Mock(); + var moqTrustCenterManager = new Mock {CallBase = true}; moqTrustCenterManager.Setup(m => m.ImportCertificatesAsync(It.IsAny())) .Returns(Samples.ProvideTaskIEnumerableCertificate); - var core = new Core(); - core.TrustCenterManager = moqTrustCenterManager.Object; - - core.Config = Samples.ProvideSampleConfig(); + var core = new Core + { + TrustCenterManager = moqTrustCenterManager.Object, + Config = Samples.ProvideSampleConfig() + }; #endregion - #region Act - var result = core.ImportAllCertificatesFromTrustCentersAsync(); + var result = await core.ImportAllCertificatesFromTrustCentersAsync(); #endregion - #region Assert Assert.Equal(9, core.Certificates.Count); @@ -52,8 +50,7 @@ public void AddTrustCenterAsyncTest() moqCore.Setup(m => m.IsTrustCenterInputValid(It.IsAny(), It.IsAny())) .Returns(true); - var moqConfigManager = new Mock(); - moqConfigManager.CallBase = true; + var moqConfigManager = new Mock {CallBase = true}; moqConfigManager.Setup(m => m.SaveConfig(It.IsAny())) .Returns(Samples.ProvideSampleConfig); @@ -67,18 +64,16 @@ public void AddTrustCenterAsyncTest() core.ConfigManager = moqConfigManager.Object; core.TrustCenterManager = moqTrustCenterManager.Object; - core.Config.TrustCenterMetaInfos.RemoveAll(m=>true); + core.Config.TrustCenterMetaInfos.RemoveAll(m => true); #endregion - #region Act var result = core.AddTrustCenterAsync("name", "url"); #endregion - #region Assert var expectedCount = 1; @@ -96,14 +91,12 @@ public void DeleteTrustCenterTest() { #region Arrange - var moqConfigManager = new Mock(); - moqConfigManager.CallBase = true; + var moqConfigManager = new Mock {CallBase = true}; moqConfigManager.Setup(m => m.SaveConfig(It.IsAny())).Returns(Samples.ProvideSampleConfig); - var moqTrustCenterManager = new Mock(); - moqTrustCenterManager.CallBase = true; + var moqTrustCenterManager = new Mock {CallBase = true}; moqTrustCenterManager.Setup(m => m.DeleteTrustCenterFile(It.IsAny())).Returns(null); - + var moqCore = new Mock(); var core = moqCore.Object; @@ -115,14 +108,12 @@ public void DeleteTrustCenterTest() #endregion - #region Act core.DeleteTrustCenter(Samples.ProvideSampleMetaInfos().FirstOrDefault()); #endregion - #region Assert var certificateCount = 1; @@ -144,7 +135,6 @@ public void IsTrustCenterInputValidTest(string newTrustCenterNameTest, string ne #endregion - #region Act var core = new Core(); @@ -152,7 +142,6 @@ public void IsTrustCenterInputValidTest(string newTrustCenterNameTest, string ne #endregion - #region Assert Assert.Equal(errorMessage, exceptionResult.Exception.Message); diff --git a/Tests/TrustCenterSearch.Core.Test/DownloaderTest.cs b/Tests/TrustCenterSearch.Core.Test/DownloaderTest.cs index 5cc8281..7cd8173 100644 --- a/Tests/TrustCenterSearch.Core.Test/DownloaderTest.cs +++ b/Tests/TrustCenterSearch.Core.Test/DownloaderTest.cs @@ -1,8 +1,7 @@ -using System; -using TrustCenterSearch.Core.DataManagement.TrustCenters; +using TrustCenterSearch.Core.DataManagement.TrustCenters; using Xunit; -namespace TrustCenterSearchCore.Test +namespace TrustCenterSearch.Core.Test { public class DownloaderTest { @@ -16,20 +15,17 @@ public void IsUrlExistingTest(string url, bool expectedResult) #endregion - #region Act var result = Downloader.IsUrlExisting(url); #endregion - #region Assert Assert.Equal(result, expectedResult); #endregion - } [Theory] @@ -55,7 +51,6 @@ public void GetFilePathTest(string name, string dataFolderPath, string expected) Assert.Equal(result, expected); #endregion - } } -} +} \ No newline at end of file diff --git a/Tests/TrustCenterSearch.Core.Test/ImporterTest.cs b/Tests/TrustCenterSearch.Core.Test/ImporterTest.cs index 04fac93..eba77ff 100644 --- a/Tests/TrustCenterSearch.Core.Test/ImporterTest.cs +++ b/Tests/TrustCenterSearch.Core.Test/ImporterTest.cs @@ -1,14 +1,14 @@ using TrustCenterSearch.Core.DataManagement.TrustCenters; using Xunit; -namespace TrustCenterSearchCore.Test +namespace TrustCenterSearch.Core.Test { public class ImporterTest { [Theory] [InlineData("", "")] [InlineData("CO=TEST123", "CO=TEST123")] - [InlineData("CN=Test123, 456","CN=Test123")] + [InlineData("CN=Test123, 456", "CN=Test123")] [InlineData("CN=123, CN=456, OU=789", "CN=123 CN=456 OU=789")] [InlineData("CN=123, CN=456, OU=789,123456789", "CN=123 CN=456 OU=789")] public void GetSubjectElementsToDisplayTest(string input, string expectedResult) @@ -17,20 +17,17 @@ public void GetSubjectElementsToDisplayTest(string input, string expectedResult) #endregion - #region Act var result = Importer.GetSubjectElementsToDisplay(input); #endregion - #region Assert Assert.Equal(expectedResult, result); #endregion - } } -} +} \ No newline at end of file diff --git a/Tests/TrustCenterSearch.Core.Test/TrustCenterSearch.Core.Test.csproj b/Tests/TrustCenterSearch.Core.Test/TrustCenterSearch.Core.Test.csproj index dbf7ba0..4794bfe 100644 --- a/Tests/TrustCenterSearch.Core.Test/TrustCenterSearch.Core.Test.csproj +++ b/Tests/TrustCenterSearch.Core.Test/TrustCenterSearch.Core.Test.csproj @@ -7,6 +7,7 @@ + diff --git a/Tests/TrustCenterSearch.Presentation.Test/FilterTestData.cs b/Tests/TrustCenterSearch.Presentation.Test/FilterTestData.cs index d7a90aa..effcd83 100644 --- a/Tests/TrustCenterSearch.Presentation.Test/FilterTestData.cs +++ b/Tests/TrustCenterSearch.Presentation.Test/FilterTestData.cs @@ -1,26 +1,14 @@ -using System; -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using TestSamples; using TrustCenterSearch.Core.Models; -namespace TrustCenterSearchPresentation.Test +namespace TrustCenterSearch.Presentation.Test { class FilterTestData : IEnumerable { - - public static Task> ProvideTaskIEnumerableCertificate() - { - async Task> ProvideSampleOfCertificatesAsync() - { - return Samples.ProvideSampleCertificates(); - } - - return ProvideSampleOfCertificatesAsync(); - } - private readonly List filterTestData = new List { new object[] {null, false,null}, @@ -31,8 +19,7 @@ async Task> ProvideSampleOfCertificatesAsync() new object[] {Samples.ProvideSampleCertificates().First(),false, "01.01.22123" } }; - public IEnumerator GetEnumerator() => filterTestData.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - + public IEnumerator GetEnumerator() => this.filterTestData.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); } } diff --git a/Tests/TrustCenterSearch.Presentation.Test/ViewModelTest.cs b/Tests/TrustCenterSearch.Presentation.Test/ViewModelTest.cs index 628faba..824a6f9 100644 --- a/Tests/TrustCenterSearch.Presentation.Test/ViewModelTest.cs +++ b/Tests/TrustCenterSearch.Presentation.Test/ViewModelTest.cs @@ -1,62 +1,13 @@ -using Moq; using System.Windows.Data; +using Moq; using TestSamples; -using TrustCenterSearch.Core; using TrustCenterSearch.Core.Models; -using TrustCenterSearch.Presentation; using Xunit; -namespace TrustCenterSearchPresentation.Test +namespace TrustCenterSearch.Presentation.Test { public class ViewModelTest { - - [Fact(DisplayName = "CopySearchResultToClipboardCommandExecuteTest")] - public void CopySearchResultToClipboardCommandExecuteTest() - { - #region Arrange - - //var cer = new Certificate() - //{ - // SerialNumber = "TestNummer", - // Issuer = "TestIssuer", - // NotAfter = "TestStartDate", - // NotBefore = "TestEndDate", - // PublicKeyLength = "TestLength", - // Thumbprint = "TestThumbprint", - // TrustCenterName = "TestName", - // Subject = "Test" - //}; - - //string jsonstring = JsonConvert.SerializeObject(cer, Formatting.Indented); - //var test = "null"; - - #endregion - - #region Act - - //var viewModel = new ViewModel(); - //viewModel.CopySearchResultToClipboardCommandExecute(cer); - - #endregion - - #region Assert - - //Thread.CurrentThread.SetApartmentState(ApartmentState.STA); - //var t = new Thread((ThreadStart)(() => - //{ - // test = Clipboard.GetText(); - //})); - //t.SetApartmentState(ApartmentState.STA); - //t.Start(); - //t.Join(); - - //Assert.Equal(test, jsonstring); - - #endregion - - } - [Theory] [InlineData("Auto", "0")] [InlineData("0", "Auto")] @@ -64,25 +15,21 @@ public void CollapseSidebarCommandExecuteTest(string setMenuWidth, string expect { #region Arrange - var view = new ViewModel(); - view.MenuWidth = setMenuWidth; + var view = new ViewModel {MenuWidth = setMenuWidth}; #endregion - #region Act view.CollapseSidebarCommandExecute(); #endregion - #region Assert Assert.Equal(view.MenuWidth, expectedMenuWidth); #endregion - } @@ -93,59 +40,55 @@ public void GetTrustCenterHistoryTest() var viewModel = new ViewModel(); - var moqCore = new Mock(); + var moqCore = new Mock(); moqCore.Setup(m => m.GetTrustCenterHistory()).Returns(Samples.ProvideSampleMetaInfos); viewModel.Core = moqCore.Object; #endregion - #region Act var result = viewModel.GetTrustCenterHistory(); #endregion - #region Assert Assert.Equal(3, result.Count); #endregion - } [Theory] [ClassData(typeof(FilterTestData))] - public void FilterTest(Certificate certificate,bool expectedBoolean,string searchBarInputForTesting) + public void FilterTest(Certificate certificate, bool expectedBoolean, string searchBarInputForTesting) { #region Arrange var viewModel = new ViewModel(); - viewModel.CertificatesCollectionView = CollectionViewSource.GetDefaultView(Samples.ProvideSampleCertificates()); + viewModel.CertificatesCollectionView = + CollectionViewSource.GetDefaultView(Samples.ProvideSampleCertificates()); viewModel.CertificatesCollectionView.Filter = viewModel.Filter; viewModel.SearchBarInput = searchBarInputForTesting; viewModel.TrustCenterHistory = Samples.ProvideSampleListOfTrustCenterHistoryElements(); - #endregion + #endregion #region Act var result = viewModel.Filter(certificate); - - #endregion + #endregion #region Assert Assert.Equal(result, expectedBoolean); #endregion - } } -} +} \ No newline at end of file diff --git a/TrustCenterSearch.Core/Core.cs b/TrustCenterSearch.Core/Core.cs index f3b8f27..2631455 100644 --- a/TrustCenterSearch.Core/Core.cs +++ b/TrustCenterSearch.Core/Core.cs @@ -39,13 +39,8 @@ public Core() public async Task> ImportAllCertificatesFromTrustCentersAsync() { - var importTasks = this.Config.TrustCenterMetaInfos.Select(trustCenterMetaInfo => - this.TrustCenterManager.ImportCertificatesAsync(trustCenterMetaInfo)).ToList(); - await Task.WhenAll(importTasks); - - foreach (var importTask in importTasks) this.Certificates.AddRange(importTask.Result); - - return this.Certificates; + return await this.TrustCenterManager.ImportCertificatesAsync(this.Config.TrustCenterMetaInfos, + this.Certificates); } public async Task AddTrustCenterAsync(string newTrustCenterName, string newTrustCenterUrl) @@ -87,20 +82,34 @@ public List GetCertificates() return this.Certificates; } + public async Task> OpenConfig() + { + (ListaddedElements, ListdeletedElements, Config config) + changesInConfig = this.ConfigManager.OpenConfig(this.Config); + this.Config = changesInConfig.config; + + await this.TrustCenterManager.ImportCertificatesAsync(changesInConfig.addedElements, this.Certificates); + + foreach (var deletedElement in changesInConfig.deletedElements) + this.TrustCenterManager.DeleteCertificatesOfTrustCenter(this.Certificates, deletedElement.Name); + + return this.Certificates; + } + #endregion #region InternalMethods internal virtual bool IsTrustCenterInputValid(string newTrustCenterName, string newTrustCenterUrl) { - if (newTrustCenterName.Length > 24) throw new ArgumentException("The entered name is too long."); if (newTrustCenterName == string.Empty) throw new ArgumentException("The entered name must not be empty."); - if (newTrustCenterName.Intersect(new char[] { '~', '#', '%', '&', '*', ':', '<', '>', '?', '/', '{', '|', '}' }).Any()) + if (newTrustCenterName.Intersect(new char[] + {'~', '#', '%', '&', '*', ':', '<', '>', '?', '/', '{', '|', '}'}).Any()) throw new ArgumentException("Invalid file characters are: ~ #% & *: <>? / {|}."); if (!Downloader.IsUrlExisting(newTrustCenterUrl)) diff --git a/TrustCenterSearch.Core/DataManagement/Configuration/ConfigManager.cs b/TrustCenterSearch.Core/DataManagement/Configuration/ConfigManager.cs index 95a7fa1..ebf1bab 100644 --- a/TrustCenterSearch.Core/DataManagement/Configuration/ConfigManager.cs +++ b/TrustCenterSearch.Core/DataManagement/Configuration/ConfigManager.cs @@ -1,12 +1,15 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Linq; using Newtonsoft.Json; using TrustCenterSearch.Core.Interfaces.Configuration; using TrustCenterSearch.Core.Models; namespace TrustCenterSearch.Core.DataManagement.Configuration { - internal class ConfigManager:IConfigManager + internal class ConfigManager : IConfigManager { #region Properties @@ -20,9 +23,13 @@ internal class ConfigManager:IConfigManager private readonly string _trustCenterSearchGuiPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\TrustCenterSearch"; + private readonly string _configFolderPath = + $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\TrustCenterSearch\Config.JSON"; + #endregion #region ICongigManagerMethods + public Config LoadConfig() { if (!File.Exists(ConfigPath)) @@ -35,16 +42,16 @@ public Config LoadConfig() public Config AddTrustCenterToConfig(TrustCenterMetaInfo trustCenterMetaInfo, Config config) { - config.TrustCenterMetaInfos.Add(trustCenterMetaInfo); - return config; + config.TrustCenterMetaInfos.Add(trustCenterMetaInfo); + return config; } public virtual Config SaveConfig(Config config) { - if (!Directory.Exists(_trustCenterSearchGuiPath)) - Directory.CreateDirectory(_trustCenterSearchGuiPath); + if (!Directory.Exists(this._trustCenterSearchGuiPath)) + Directory.CreateDirectory(this._trustCenterSearchGuiPath); - var jsonString = JsonConvert.SerializeObject(config,Formatting.Indented); + var jsonString = JsonConvert.SerializeObject(config, Formatting.Indented); File.WriteAllText(ConfigPath, jsonString); return config; @@ -61,6 +68,46 @@ public bool IsConfigEmpty(Config config) return config.TrustCenterMetaInfos.Count == 0; } + public (List addedTrustCenterHistoryElements, List + removedTrustCenterHistoryElements, Config config) OpenConfig(Config config) + { + if (!File.Exists(this._configFolderPath)) + { + config = new Config(); + this.SaveConfig(config); + } + + var psi = new ProcessStartInfo + { + FileName = this._configFolderPath, + UseShellExecute = true, + Verb = "open" + }; + + var p = Process.Start(psi); + p.WaitForInputIdle(); + p.WaitForExit(); + + if (p.HasExited) + { + var newConfig = this.LoadConfig(); + + config = newConfig; + return (GetRelativeTrustCenterMetaInfoComplement(newConfig, config), GetRelativeTrustCenterMetaInfoComplement(config, newConfig), config); + } + + return (new List(), new List(), config); + } + + #endregion + + #region internalMethods + internal static List GetRelativeTrustCenterMetaInfoComplement(Config config, Config newConfig) + { + return config.TrustCenterMetaInfos.Where(newConfigElement => + newConfig.TrustCenterMetaInfos.All(x => x.Name != newConfigElement.Name)).ToList(); + } #endregion } + } \ No newline at end of file diff --git a/TrustCenterSearch.Core/DataManagement/TrustCenters/TrustCenterManager.cs b/TrustCenterSearch.Core/DataManagement/TrustCenters/TrustCenterManager.cs index 2d5e81c..7207c97 100644 --- a/TrustCenterSearch.Core/DataManagement/TrustCenters/TrustCenterManager.cs +++ b/TrustCenterSearch.Core/DataManagement/TrustCenters/TrustCenterManager.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.ConstrainedExecution; using System.Threading.Tasks; using TrustCenterSearch.Core.Interfaces.TrustCenters; using TrustCenterSearch.Core.Models; @@ -31,12 +30,23 @@ public virtual async Task DownloadCertificatesAsync(TrustCenterMetaInfo public virtual async Task> ImportCertificatesAsync(TrustCenterMetaInfo trustCenterMetaInfo) { - return await ImportManager.ImportCertificatesAsync(trustCenterMetaInfo, _dataFolderPath).ConfigureAwait(false); + return await this.ImportManager.ImportCertificatesAsync(trustCenterMetaInfo, _dataFolderPath).ConfigureAwait(false); + } + public virtual async Task> ImportCertificatesAsync(List trustCenterMetaInfos, List certificates) + { + var importTasks = trustCenterMetaInfos.Select(this.ImportCertificatesAsync).ToList(); + await Task.WhenAll(importTasks); + + foreach (var importTask in importTasks) certificates.AddRange(importTask.Result); + + return certificates; } public virtual bool DeleteTrustCenterFile(string trustCenterName) { - File.Delete(this.DownloadManager.GetFilePath(trustCenterName, this._dataFolderPath)); + if(File.Exists(this.DownloadManager.GetFilePath(trustCenterName, this._dataFolderPath))) + File.Delete(this.DownloadManager.GetFilePath(trustCenterName, this._dataFolderPath)); + return true; } diff --git a/TrustCenterSearch.Core/Interfaces/Configuration/IConfigManager.cs b/TrustCenterSearch.Core/Interfaces/Configuration/IConfigManager.cs index 4fccd63..5a7ffaf 100644 --- a/TrustCenterSearch.Core/Interfaces/Configuration/IConfigManager.cs +++ b/TrustCenterSearch.Core/Interfaces/Configuration/IConfigManager.cs @@ -17,5 +17,8 @@ internal interface IConfigManager Config DeleteTrustCenterFromConfig(TrustCenterMetaInfo trustCenterMetaInfo, Config config); bool IsConfigEmpty(Config config); + + (List addedTrustCenterHistoryElements, List + removedTrustCenterHistoryElements, Config config) OpenConfig(Config config); } -} +} \ No newline at end of file diff --git a/TrustCenterSearch.Core/Interfaces/ICore.cs b/TrustCenterSearch.Core/Interfaces/ICore.cs index e33ebde..b37a0f8 100644 --- a/TrustCenterSearch.Core/Interfaces/ICore.cs +++ b/TrustCenterSearch.Core/Interfaces/ICore.cs @@ -17,5 +17,6 @@ public interface ICore List GetTrustCenterHistory(); List GetCertificates(); + Task> OpenConfig(); } } diff --git a/TrustCenterSearch.Core/Interfaces/TrustCenters/ITrustCenterManager.cs b/TrustCenterSearch.Core/Interfaces/TrustCenters/ITrustCenterManager.cs index e8c5c1e..dbd215b 100644 --- a/TrustCenterSearch.Core/Interfaces/TrustCenters/ITrustCenterManager.cs +++ b/TrustCenterSearch.Core/Interfaces/TrustCenters/ITrustCenterManager.cs @@ -10,6 +10,9 @@ internal interface ITrustCenterManager Task> ImportCertificatesAsync(TrustCenterMetaInfo trustCenterMetaInfo); + Task> ImportCertificatesAsync(List trustCenterMetaInfos, + List certificates); + bool DeleteTrustCenterFile(string trustCenterName); List DeleteCertificatesOfTrustCenter(List certificates, string trustCenterName); diff --git a/TrustCenterSearch.Presentation/Resources/Styles/TextBlockStyles.xaml b/TrustCenterSearch.Presentation/Resources/Styles/TextBlockStyles.xaml index ce66a58..a08b835 100644 --- a/TrustCenterSearch.Presentation/Resources/Styles/TextBlockStyles.xaml +++ b/TrustCenterSearch.Presentation/Resources/Styles/TextBlockStyles.xaml @@ -54,7 +54,7 @@ diff --git a/TrustCenterSearch.Presentation/ViewModel.cs b/TrustCenterSearch.Presentation/ViewModel.cs index 80f0fb7..373e843 100644 --- a/TrustCenterSearch.Presentation/ViewModel.cs +++ b/TrustCenterSearch.Presentation/ViewModel.cs @@ -24,50 +24,57 @@ public class ViewModel : ViewModelBase public ICore Core { get; set; } = new Core.Core(); private bool _userInputIsEnabled = true; + public bool UserInputIsEnabled { get => this._userInputIsEnabled; - set => base.Set(ref this._userInputIsEnabled, value); + set => this.Set(ref this._userInputIsEnabled, value); } public List TrustCenterHistory { get; set; } = new List(); private ICollectionView _trustCenterHistoryCollectionView; + public ICollectionView TrustCenterHistoryCollectionView { get => this._trustCenterHistoryCollectionView; - set => Set(() => this.TrustCenterHistoryCollectionView, ref this._trustCenterHistoryCollectionView, value); + set => this.Set(() => this.TrustCenterHistoryCollectionView, ref this._trustCenterHistoryCollectionView, + value); } private ICollectionView _certificatesCollectionView; + public ICollectionView CertificatesCollectionView { get => this._certificatesCollectionView; - set => Set(ref this._certificatesCollectionView, value); + set => this.Set(ref this._certificatesCollectionView, value); } - private string _searchBarInput = String.Empty; + private string _searchBarInput = string.Empty; + public string SearchBarInput { get => this._searchBarInput; set { - Set(ref this._searchBarInput, value); - CertificatesCollectionView.Refresh(); + this.Set(ref this._searchBarInput, value); + this.CertificatesCollectionView.Refresh(); } } - private string _addTrustCenterName = String.Empty; + private string _addTrustCenterName = string.Empty; + public string AddTrustCenterName { get => this._addTrustCenterName; - set => Set(ref this._addTrustCenterName, value); + set => this.Set(ref this._addTrustCenterName, value); } - private string _addTrustCenterUrl = String.Empty; + private string _addTrustCenterUrl = string.Empty; + public string AddTrustCenterUrl { get => this._addTrustCenterUrl; - set => Set(ref this._addTrustCenterUrl, value); + set => this.Set(ref this._addTrustCenterUrl, value); } #endregion @@ -87,26 +94,29 @@ public string AddTrustCenterUrl private string _menuWidth = "Auto"; private readonly string _gitHubWikiUrl = "https://github.com/haevg-rz/TrustCenterSearchGui/wiki/wiki"; - private readonly string _configFolderPath = - $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\TrustCenterSearch\Config.JSON"; public string MenuWidth { get => this._menuWidth; - set => base.Set(ref this._menuWidth, value); + set => this.Set(ref this._menuWidth, value); } #endregion #region Constructor + public ViewModel() { this.AddTrustCenterButtonCommand = new RelayCommand(this.AddTrustCenterAsyncCommandExecute); this.LoadDataCommand = new RelayCommand(this.LoadDataAsyncCommandExecute); - this.ToggleTrustCenterHistoryFilterCommand = new RelayCommand(this.ToggleTrustCenterHistoryFilterCommandExecute); - this.DeleteTrustCenterFromHistoryCommand = new RelayCommand(this.DeleteTrustCenterFromHistoryCommandExecute); - this.InfoAboutTrustCenterCommand = new RelayCommand(InfoAboutTrustCenterCommandExecute); - this.ReloadCertificatesOfTrustCenterCommand = new RelayCommand(this.ReloadCertificatesOfTrustCenterCommandExecute); + this.ToggleTrustCenterHistoryFilterCommand = + new RelayCommand(this.ToggleTrustCenterHistoryFilterCommandExecute); + this.DeleteTrustCenterFromHistoryCommand = + new RelayCommand(this.DeleteTrustCenterFromHistoryCommandExecute); + this.InfoAboutTrustCenterCommand = + new RelayCommand(InfoAboutTrustCenterCommandExecute); + this.ReloadCertificatesOfTrustCenterCommand = + new RelayCommand(this.ReloadCertificatesOfTrustCenterCommandExecute); this.CollapseSideBarCommand = new RelayCommand(this.CollapseSidebarCommandExecute); this.OpenWikiWebpageCommand = new RelayCommand(this.OpenWikiWebpageCommandExecute); this.CopyToClipboardCommand = new RelayCommand(this.CopySearchResultToClipboardCommandExecute); @@ -116,21 +126,23 @@ public ViewModel() #endregion #region Commandhandling - + private async void LoadDataAsyncCommandExecute() { this.UserInputIsEnabled = false; - var certs = await this.Core.ImportAllCertificatesFromTrustCentersAsync(); + await this.Core.ImportAllCertificatesFromTrustCentersAsync(); this.TrustCenterHistoryCollectionView = CollectionViewSource.GetDefaultView(this.GetTrustCenterHistory()); - this.TrustCenterHistoryCollectionView.SortDescriptions.Add(new SortDescription(nameof(TrustCenterHistoryElement.Active), ListSortDirection.Descending)); + this.TrustCenterHistoryCollectionView.SortDescriptions.Add( + new SortDescription(nameof(TrustCenterHistoryElement.Active), ListSortDirection.Descending)); - this.CertificatesCollectionView = CollectionViewSource.GetDefaultView(certs); + this.CertificatesCollectionView = CollectionViewSource.GetDefaultView(this.Core.GetCertificates()); this.CertificatesCollectionView.Filter = this.Filter; this.UserInputIsEnabled = true; } + internal void CopySearchResultToClipboardCommandExecute(Certificate certificate) { var jsonString = JsonConvert.SerializeObject(certificate, Formatting.Indented); @@ -156,9 +168,9 @@ private async void ReloadCertificatesOfTrustCenterCommandExecute(TrustCenterHist MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } - + this.GetTrustCenterHistory(); - RefreshCollectionViews(); + this.RefreshCollectionViews(); this.UserInputIsEnabled = true; } @@ -170,7 +182,8 @@ private async void AddTrustCenterAsyncCommandExecute() TrustCenterMetaInfo newTrustCenterMetaInfo; try { - newTrustCenterMetaInfo = await this.Core.AddTrustCenterAsync(this.AddTrustCenterName, this.AddTrustCenterUrl); + newTrustCenterMetaInfo = + await this.Core.AddTrustCenterAsync(this.AddTrustCenterName, this.AddTrustCenterUrl); } catch (ArgumentException e) { @@ -181,17 +194,18 @@ private async void AddTrustCenterAsyncCommandExecute() this.TrustCenterHistory.Add(new TrustCenterHistoryElement(newTrustCenterMetaInfo)); - RefreshCollectionViews(); + this.RefreshCollectionViews(); - this.AddTrustCenterName = String.Empty; - this.AddTrustCenterUrl = String.Empty; + this.AddTrustCenterName = string.Empty; + this.AddTrustCenterUrl = string.Empty; this.UserInputIsEnabled = true; } private void DeleteTrustCenterFromHistoryCommandExecute(TrustCenterHistoryElement trustCenterToDelete) { - var deleteConfirmation = MessageBox.Show("Are you sure you want to delete this Trust Center?", "Delete Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question); + var deleteConfirmation = MessageBox.Show("Are you sure you want to delete this Trust Center?", + "Delete Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (!deleteConfirmation.Equals(MessageBoxResult.OK)) return; @@ -200,7 +214,7 @@ private void DeleteTrustCenterFromHistoryCommandExecute(TrustCenterHistoryElemen this.TrustCenterHistory.Remove(trustCenterToDelete); - RefreshCollectionViews(); + this.RefreshCollectionViews(); this.UserInputIsEnabled = true; } @@ -224,15 +238,16 @@ private void OpenWikiWebpageCommandExecute() Process.Start(psi); } - private void OpenConfigCommandExecute() + private async void OpenConfigCommandExecute() { - var psi = new ProcessStartInfo - { - FileName = this._configFolderPath, - UseShellExecute = true, - Verb = "open" - }; - Process.Start(psi); + this.UserInputIsEnabled = false; + + await this.Core.OpenConfig(); + this.GetTrustCenterHistory(); + + this.RefreshCollectionViews(); + + this.UserInputIsEnabled = true; } #endregion @@ -243,8 +258,8 @@ internal List GetTrustCenterHistory() { this.TrustCenterHistory.Clear(); foreach (var trustCenterMetaInfo in this.Core.GetTrustCenterHistory()) - TrustCenterHistory.Add(new TrustCenterHistoryElement(trustCenterMetaInfo)); - return TrustCenterHistory; + this.TrustCenterHistory.Add(new TrustCenterHistoryElement(trustCenterMetaInfo)); + return this.TrustCenterHistory; } private static void InfoAboutTrustCenterCommandExecute(TrustCenterHistoryElement trustCenterMetaInfo) @@ -261,14 +276,15 @@ internal bool Filter(object obj) bool IsEnabled(Certificate certificate) { - return this.TrustCenterHistory.Any(x => x.TrustCenterMetaInfo.Name.Equals(certificate.TrustCenterName) && x.Active.Equals(true)); + return this.TrustCenterHistory.Any(x => + x.TrustCenterMetaInfo.Name.Equals(certificate.TrustCenterName) && x.Active.Equals(true)); } - if (TrustCenterHistory.Count > 0) + if (this.TrustCenterHistory.Count > 0) if (!IsEnabled(entry)) return false; - if (string.IsNullOrWhiteSpace(SearchBarInput)) + if (string.IsNullOrWhiteSpace(this.SearchBarInput)) return true; var certificateAttributes = new HashSet