This is an automated email from the ASF dual-hosted git repository. lizhanhui pushed a commit to branch csharp_dev in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
commit b8f1a7defad86d0288e426e62ea9a8df37a5b6a7 Author: Zhanhui Li <[email protected]> AuthorDate: Tue Aug 30 16:51:10 2022 +0800 Add unit test for ConfigFileCredentialsProvider --- .../ConfigFileCredentialsProvider.cs | 45 ++++++++++++++-------- csharp/rocketmq-client.sln.DotSettings.user | 6 +++ csharp/tests/ConfigFileCredentialsProviderTest.cs | 26 ++++++++++++- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/csharp/rocketmq-client-csharp/ConfigFileCredentialsProvider.cs b/csharp/rocketmq-client-csharp/ConfigFileCredentialsProvider.cs index 39dfd7e..0b80fce 100644 --- a/csharp/rocketmq-client-csharp/ConfigFileCredentialsProvider.cs +++ b/csharp/rocketmq-client-csharp/ConfigFileCredentialsProvider.cs @@ -18,6 +18,7 @@ using System.IO; using System; using System.Text.Json; using System.Collections.Generic; +using NLog; namespace Org.Apache.Rocketmq { @@ -29,45 +30,59 @@ namespace Org.Apache.Rocketmq */ public class ConfigFileCredentialsProvider : ICredentialsProvider { + private static readonly Logger Logger = MqLogManager.Instance.GetCurrentClassLogger(); public ConfigFileCredentialsProvider() { - var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - string configFileRelativePath = "/.rocketmq/config"; - if (!File.Exists(home + configFileRelativePath)) + var configFilePath = DefaultConfigFilePath(); + + if (!File.Exists(configFilePath)) { + Logger.Warn("Config file[{}] does not exist", configFilePath); return; } try { - using (var reader = new StreamReader(home + configFileRelativePath)) + using var reader = new StreamReader(configFilePath); + string json = reader.ReadToEnd(); + var kv = JsonSerializer.Deserialize<Dictionary<string, string>>(json); + if (null == kv) { - string json = reader.ReadToEnd(); - var kv = JsonSerializer.Deserialize<Dictionary<string, string>>(json); - accessKey = kv["AccessKey"]; - accessSecret = kv["AccessSecret"]; - valid = true; + Logger.Error($"Failed to parse JSON configuration: {json}"); + return; } + + _accessKey = kv["AccessKey"]; + _accessSecret = kv["AccessSecret"]; + _valid = true; } - catch (IOException) + catch (IOException e) { + Logger.Error($"Failed to read cofig file. Cause: {e.Message}"); } } public Credentials getCredentials() { - if (!valid) + if (!_valid) { return null; } - return new Credentials(accessKey, accessSecret); + return new Credentials(_accessKey, _accessSecret); + } + + public static String DefaultConfigFilePath() + { + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string[] pathSegments = {home, ".rocketmq", "config"}; + return String.Join(Path.DirectorySeparatorChar, pathSegments); } - private string accessKey; - private string accessSecret; + private readonly string _accessKey; + private readonly string _accessSecret; - private bool valid = false; + private readonly bool _valid; } } \ No newline at end of file diff --git a/csharp/rocketmq-client.sln.DotSettings.user b/csharp/rocketmq-client.sln.DotSettings.user new file mode 100644 index 0000000..fef13ba --- /dev/null +++ b/csharp/rocketmq-client.sln.DotSettings.user @@ -0,0 +1,6 @@ +<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=10e53398_002Da45e_002D40cb_002D929b_002D19728055dd16/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="testGetCredentials" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>MSTest::92248517-D1FD-4C65-A691-647C696B9F85::net5.0::Org.Apache.Rocketmq.ConfigFileCredentialsProviderTest</TestId> + </TestAncestor> +</SessionState></s:String></wpf:ResourceDictionary> \ No newline at end of file diff --git a/csharp/tests/ConfigFileCredentialsProviderTest.cs b/csharp/tests/ConfigFileCredentialsProviderTest.cs index 7741295..abb3e05 100644 --- a/csharp/tests/ConfigFileCredentialsProviderTest.cs +++ b/csharp/tests/ConfigFileCredentialsProviderTest.cs @@ -16,19 +16,41 @@ */ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System.IO; namespace Org.Apache.Rocketmq { [TestClass] public class ConfigFileCredentialsProviderTest { + [TestInitialize] + public void Setup() + { + var configFilePath = ConfigFileCredentialsProvider.DefaultConfigFilePath(); + FileInfo fileInfo = new FileInfo(configFilePath); + var dir = fileInfo.Directory; + if (!dir.Exists) + { + dir.Create(); + } + + string json = "{\"AccessKey\": \"key\", \"AccessSecret\": \"secret\"}"; + File.WriteAllText(configFilePath, json); + } + [TestMethod] - public void testGetCredentials() + public void TestGetCredentials() { var provider = new ConfigFileCredentialsProvider(); var credentials = provider.getCredentials(); Assert.IsNotNull(credentials); } + + [TestCleanup] + public void TearDown() + { + var configFilePath = ConfigFileCredentialsProvider.DefaultConfigFilePath(); + File.Delete(configFilePath); + } } } \ No newline at end of file
