Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update TestBase #523

Merged
merged 1 commit into from
Sep 19, 2023
Merged
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
82 changes: 29 additions & 53 deletions Vonage.Test.Unit/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;

Check warning on line 1 in Vonage.Test.Unit/TestBase.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Primitive Obsession

In this module, 60.0% of all function arguments are primitive types, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.

Check notice on line 1 in Vonage.Test.Unit/TestBase.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: String Heavy Function Arguments

The ratio of strings in function arguments increases from 55.56% to 60.00%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.

Check notice on line 1 in Vonage.Test.Unit/TestBase.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Code Duplication

The module no longer contains too many functions with similar structure
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand All @@ -20,8 +20,9 @@
{
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 @@
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 @@
#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 @@

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 @@
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]);
}
}
Loading