Skip to content

Commit

Permalink
added new guards for latin characters only and new exception for upst…
Browse files Browse the repository at this point in the history
…ream errors
  • Loading branch information
Mahmoud Swehli committed Feb 17, 2024
1 parent 15ef1bf commit 858aa90
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
23 changes: 23 additions & 0 deletions Muljin.Common/Exceptions/UpstreamException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Muljin.Exceptions
{
/// <summary>
/// Exception for selected user not found
/// </summary>
public class UpstreamException : MuljinException
{
public UpstreamException(){
new UserNotFoundException("Exception occured when calling upstream service");
}

public UpstreamException(string message)
{
new UserNotFoundException(message, "UPS001");
}

public UpstreamException(string message, string errorCode): base(message, errorCode) { }
}
}
2 changes: 1 addition & 1 deletion Muljin.Common/Muljin.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Copyright>Muljin Pte. Ltd.</Copyright>
<PackageProjectUrl>https://www.muljin.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/Muljin/Muljin.Net</RepositoryUrl>
<Version>2.2.10</Version>
<Version>2.2.11</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

Expand Down
29 changes: 23 additions & 6 deletions Muljin.Common/Utils/Guards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@ namespace Muljin.Utils
{
public static class Guards
{
/// <summary>
/// Ensure a string consists only of latin alpha characters (a-zA-Z)
/// </summary>
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <param name="message"></param>
public static void LatinOnly(string obj, string paramName = null, string message = null)
{
foreach(var o in obj){
if((o >= 'a' && o <='z') || (o >= 'A' && o <= 'Z')){
continue;
}

throw new ArgumentException(message ?? "Invalid input. Input must consist of letters only", paramName);
}
}

/// <summary>
/// Ensure a string only consists of alpha numeric characters
/// </summary>
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <param name="message"></param>
/// <exception cref="ArgumentException"></exception>
public static void AlphaNumeric(string obj, string paramName, string message = null)
public static void AlphaNumeric(string obj, string paramName = null, string message = null)
{
foreach(var o in obj)
{
Expand All @@ -31,7 +48,7 @@ public static void AlphaNumeric(string obj, string paramName, string message = n
/// <param name="paramName"></param>
/// <param name="message"></param>
/// <exception cref="ArgumentException"></exception>
public static void Numeric(string obj, string paramName, string message = null)
public static void Numeric(string obj, string paramName = null, string message = null)
{
foreach(var o in obj)
{
Expand Down Expand Up @@ -108,7 +125,7 @@ public static void NotDefault(string obj, string paramName, string message = nul
}
}

public static void NotNull(object obj, string paramName, string message = null)
public static void NotNull(object obj, string paramName = null, string message = null)
{
if (obj == null)
{
Expand All @@ -117,7 +134,7 @@ public static void NotNull(object obj, string paramName, string message = null)
}


public static void NotNullOrDefault(object obj, string paramName, string message = null)
public static void NotNullOrDefault(object obj, string paramName = null, string message = null)
{
Guards.NotNull(obj, paramName, message);

Expand All @@ -131,7 +148,7 @@ public static void NotNullOrDefault(object obj, string paramName, string message
/// Check if string is null or whitespace, throw exception if either
/// </summary>
/// <param name="obj"></param>
public static void NotNullOrDefault(string obj, string paramName, string message = null)
public static void NotNullOrDefault(string obj, string paramName = null, string message = null)
{
Guards.NotNull(obj, paramName, message);

Expand All @@ -141,7 +158,7 @@ public static void NotNullOrDefault(string obj, string paramName, string message
}
}

public static void NotNullOrDefault(Guid obj, string paramName, string message = null)
public static void NotNullOrDefault(Guid obj, string paramName = null, string message = null)
{
Guards.NotNull(obj, paramName, message);
if(obj == default)
Expand Down
18 changes: 18 additions & 0 deletions Tests/Muljin.Common.Tests/GuardsTests/GuardsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public void WhenNotAlphaNumericFails(string value)
Assert.Throws<ArgumentException>(()=>Guards.AlphaNumeric(value, nameof(value)));
}

[TestCase("ABC")]
[TestCase("ABc")]
[TestCase("aZAz")]
[TestCase("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")]
public void WhenLatinSucceeds(string value){
Assert.DoesNotThrow(()=>Guards.LatinOnly(value));

}

[TestCase("ABC ")]
[TestCase("ABc1")]
[TestCase("aZAz,")]
[TestCase("غا")]
[TestCase("123")]
public void WhenNotLatinFails(string value){
Assert.Throws<ArgumentException>(()=>Guards.LatinOnly(value));
}

[TestCase("01234567890")]
[TestCase("0")]
public void WhenNumericSucceeds(string value)
Expand Down

0 comments on commit 858aa90

Please sign in to comment.