Skip to content

Commit

Permalink
refactor: update TestBase (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d authored Sep 19, 2023
1 parent 0ed7896 commit 88a198d
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions Vonage.Test.Unit/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class TestBase
{
private static readonly Regex TokenReplacementRegEx = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
private const string MockedMethod = "SendAsync";
protected string ApiUrl => this.Configuration.Settings["appSettings:Vonage.Url.Api"];
protected string RestUrl => this.Configuration.Settings["appSettings:Vonage.Url.Rest"];
private const string JsonRegexPattern = "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+";
protected string ApiUrl => this.configuration.Settings["appSettings:Vonage.Url.Api"];
protected string RestUrl => this.configuration.Settings["appSettings:Vonage.Url.Rest"];
protected readonly string ApiKey = Environment.GetEnvironmentVariable("VONAGE_API_KEY") ?? "testkey";
protected readonly string ApiSecret = Environment.GetEnvironmentVariable("VONAGE_API_Secret") ?? "testSecret";

Expand All @@ -31,10 +32,7 @@ public class TestBase
protected readonly string PrivateKey = Environment.GetEnvironmentVariable("PRIVATE_KEY") ??
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey");

protected TestBase()
{
this.Configuration = new Configuration();
}
protected TestBase() => this.configuration = new Configuration();

#if NETCOREAPP2_0_OR_GREATER
private static readonly Assembly ThisAssembly = typeof(TestBase).GetTypeInfo().Assembly;
Expand All @@ -43,10 +41,10 @@ protected TestBase()
#endif

private static readonly string TestAssemblyName = ThisAssembly.GetName().Name;
public Configuration Configuration { get; }
private readonly Configuration configuration;

protected VonageClient BuildVonageClient(Credentials credentials) =>
new VonageClient(credentials, this.Configuration);
new VonageClient(credentials, this.configuration);

protected Credentials BuildCredentialsForBasicAuthentication() =>
Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret);
Expand All @@ -56,9 +54,7 @@ protected Credentials BuildCredentialsForBearerAuthentication() =>

private static string GetAssemblyDirectory()
{
var codeBase = ThisAssembly.CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var path = Uri.UnescapeDataString(new UriBuilder(ThisAssembly.CodeBase).Path);
return Path.GetDirectoryName(path);
}

Expand Down Expand Up @@ -94,60 +90,40 @@ private void Setup(string uri, HttpContent httpContent, HttpStatusCode expectedC
Content = httpContent,
})
.Verifiable();
this.Configuration.ClientHandler = mockHandler.Object;
this.configuration.ClientHandler = mockHandler.Object;
}

protected string GetResponseJson([CallerMemberName] string name = null)
protected string GetResponseJson([CallerMemberName] string name = null) => this.ReadJsonFile(name, "response");

protected string GetRequestJson([CallerMemberName] string name = null) => this.ReadJsonFile(name, "request");

private string ReadJsonFile(string name, string fileType)
{
var type = this.GetType().Name;
var ns = this.GetType().Namespace;
if (ns != null)
var typeNamespace = this.GetType().Namespace;
if (typeNamespace is null)
{
var projectFolder = ns.Substring(TestAssemblyName.Length);
var path = Path.Combine(GetAssemblyDirectory(), projectFolder, "Data", type, $"{name}-response.json");
if (!File.Exists(path))
{
throw new FileNotFoundException($"File not found at {path}.");
}
return string.Empty;
}

var jsonContent = File.ReadAllText(path);
jsonContent = Regex.Replace(jsonContent, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");
return jsonContent;
var path = Path.Combine(
GetAssemblyDirectory(),
typeNamespace.Substring(TestAssemblyName.Length),
"Data",
this.GetType().Name,
$"{name}-{fileType}.json");
if (!File.Exists(path))
{
throw new FileNotFoundException($"File not found at {path}.");
}

return string.Empty;
return Regex.Replace(File.ReadAllText(path), JsonRegexPattern, "$1");
}

protected string GetResponseJson(Dictionary<string, string> parameters,
[CallerMemberName] string name = null) =>
TokenReplacementRegEx.Replace(this.GetResponseJson(name), match => parameters[match.Groups[1].Value]);

protected string GetRequestJson([CallerMemberName] string name = null)
{
var type = this.GetType().Name;
var ns = this.GetType().Namespace;
if (ns != null)
{
var projectFolder = ns.Substring(TestAssemblyName.Length);
var path = Path.Combine(GetAssemblyDirectory(), projectFolder, "Data", type, $"{name}-request.json");
if (!File.Exists(path))
{
throw new FileNotFoundException($"File not found at {path}.");
}

var jsonContent = File.ReadAllText(path);
jsonContent = Regex.Replace(jsonContent, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");
return jsonContent;
}

return string.Empty;
}

protected string GetRequestJson(Dictionary<string, string> parameters, [CallerMemberName] string name = null)
{
var response = this.GetRequestJson(name);
response = TokenReplacementRegEx.Replace(response, match => parameters[match.Groups[1].Value]);
return response;
}
protected string GetRequestJson(Dictionary<string, string> parameters, [CallerMemberName] string name = null) =>
TokenReplacementRegEx.Replace(this.GetRequestJson(name), match => parameters[match.Groups[1].Value]);
}
}

0 comments on commit 88a198d

Please sign in to comment.