diff --git a/PCAxis.Sql.UnitTest/ApiUtilStaticTests.cs b/PCAxis.Sql.UnitTest/ApiUtilStaticTests.cs new file mode 100644 index 0000000..e82a517 --- /dev/null +++ b/PCAxis.Sql.UnitTest/ApiUtilStaticTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Reflection; +using System.Runtime.ExceptionServices; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using PCAxis.Sql.ApiUtils; + +namespace PCAxis.Sql.UnitTest +{ + + [TestClass] + public class ApiUtilStaticTests + { + + [TestMethod] + [TestCategory("Unit")] + //[DataRow("valid_id_string")] + //[DataRow("1.1")] + //[DataRow("v123")] + //[DataRow("123")] + //[DataRow("COICOP MI1301 (fin)")] + //[DataRow("COFOG2+3-siffer")] + [DataRow("F_AntalBarn<6Bakgr")] + [DataRow("F_AntalBarn>6Bakgr")] + public void ValidateIdString_GivenValidValues_ReturnsSameValue(string value) + { + string result = InvokeValidateIdString(value); + Assert.AreEqual(value, result); + } + + [TestMethod] + [TestCategory("Unit")] + public void ValidateIdString_GivenNull_ThrowsArgumentException() + { + // Act + Assert + Assert.ThrowsExactly(() => InvokeValidateIdString(null)); + } + + [TestMethod] + [TestCategory("Unit")] + public void ValidateIdString_GivenInvalidCharacters_ThrowsArgumentException() + { + // Arrange + var invalidId = "table/id"; + + Assert.ThrowsExactly(() => InvokeValidateIdString(invalidId)); + } + + private static string InvokeValidateIdString(string input) + { + var method = typeof(ApiUtilStatic).GetMethod("ValidateIdString", BindingFlags.NonPublic | BindingFlags.Static); + Assert.IsNotNull(method); + + try + { + return (string)method.Invoke(null, new object[] { input }); + } + catch (TargetInvocationException ex) when (ex.InnerException != null) + { + ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); + throw; + } + } + } +} diff --git a/PCAxis.Sql.UnitTest/PCAxis.Sql.UnitTest.csproj b/PCAxis.Sql.UnitTest/PCAxis.Sql.UnitTest.csproj index 3aef7ae..c9d3da5 100644 --- a/PCAxis.Sql.UnitTest/PCAxis.Sql.UnitTest.csproj +++ b/PCAxis.Sql.UnitTest/PCAxis.Sql.UnitTest.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs b/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs index f523788..0847d4c 100644 --- a/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs +++ b/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs @@ -19,13 +19,7 @@ namespace PCAxis.Sql.ApiUtils public static class ApiUtilStatic { - private static readonly List LanguagesInDbConfig; - static ApiUtilStatic() - { - Console.WriteLine("Start ApiUtilStatic"); - var config = SqlDbConfigsStatic.DefaultDatabase; - LanguagesInDbConfig = config.ListAllLanguages(); - } + private static readonly Lazy> LanguagesInDbConfig = new Lazy>(() => SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages()); //Exceptions ? What if the valueset only exists in another language: Exceptions! static public ValueSet GetValueSet(string valueSetId, string language) @@ -118,7 +112,7 @@ private static string ValidateLangCodeString(string input) { throw new ArgumentException("The language cannot be null."); } - if (!LanguagesInDbConfig.Contains(input)) + if (!LanguagesInDbConfig.Value.Contains(input)) { throw new ArgumentException("Cant find language in config."); } @@ -133,9 +127,9 @@ private static string ValidateIdString(string input) throw new ArgumentException("The id string cannot be null."); } - if (!Regex.IsMatch(input, @"^[\w\t \-:.]+$", RegexOptions.None, TimeSpan.FromSeconds(2))) + if (!Regex.IsMatch(input, @"^[\w\t \-:.\(\)\+<>]+$", RegexOptions.None, TimeSpan.FromSeconds(2))) { - throw new ArgumentException("The string contains invalid characters. Only letters, digits, underscores, tabs, spaces, hyphens, colons and periods are allowed."); + throw new ArgumentException("The string contains invalid characters. Only letters, digits, underscores, tabs, spaces, hyphens, colons, periods, parentheses, plus and angle brackets are allowed."); } return input; }