Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions PCAxis.Sql.UnitTest/ApiUtilStaticTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>(() => InvokeValidateIdString(null));
}

[TestMethod]
[TestCategory("Unit")]
public void ValidateIdString_GivenInvalidCharacters_ThrowsArgumentException()
{
// Arrange
var invalidId = "table/id";

Assert.ThrowsExactly<ArgumentException>(() => 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;
}
}
}
}
2 changes: 1 addition & 1 deletion PCAxis.Sql.UnitTest/PCAxis.Sql.UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down
14 changes: 4 additions & 10 deletions PCAxis.Sql/ApiUtils/ApiUtilStatic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ namespace PCAxis.Sql.ApiUtils
public static class ApiUtilStatic
{

private static readonly List<string> LanguagesInDbConfig;
static ApiUtilStatic()
{
Console.WriteLine("Start ApiUtilStatic");
var config = SqlDbConfigsStatic.DefaultDatabase;
LanguagesInDbConfig = config.ListAllLanguages();
}
private static readonly Lazy<List<string>> LanguagesInDbConfig = new Lazy<List<string>>(() => SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages());

//Exceptions ? What if the valueset only exists in another language: Exceptions!
static public ValueSet GetValueSet(string valueSetId, string language)
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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;
}
Expand Down
Loading