Skip to content

Commit

Permalink
Config refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Komissarov committed Apr 25, 2024
1 parent f2d3bfc commit 992618f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
40 changes: 18 additions & 22 deletions Tms.Adapter.Core/Configurator/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static class Configurator
private const string TmsAutomaticCreationTestCases = "TMS_AUTOMATIC_CREATION_TEST_CASES";
private const string TmsCertValidation = "TMS_CERT_VALIDATION";
private const string ConfigFile = "TMS_CONFIG_FILE";

private static readonly JsonSerializerOptions SerializerOptions = new() { PropertyNameCaseInsensitive = true };

public static TmsSettings GetConfig()
{
var config = new TmsSettings
Expand All @@ -28,12 +29,7 @@ public static TmsSettings GetConfig()

if (File.Exists(defaultJsonConfigPath))
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var fileConfig = JsonSerializer.Deserialize<TmsSettings>(File.ReadAllText(defaultJsonConfigPath), options);
var fileConfig = JsonSerializer.Deserialize<TmsSettings>(File.ReadAllText(defaultJsonConfigPath), SerializerOptions);

if (fileConfig != null)
{
Expand All @@ -58,10 +54,10 @@ private static string GetConfigFileName()
return envConfigFileName ?? DefaultFileName;
}

private static TmsSettings ApplyEnv(TmsSettings settings)
private static void ApplyEnv(TmsSettings settings)
{
var url = Environment.GetEnvironmentVariable(TmsUrl);
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
if (!string.IsNullOrWhiteSpace(url) && Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
settings.Url = url;
}
Expand All @@ -73,19 +69,19 @@ private static TmsSettings ApplyEnv(TmsSettings settings)
}

var projectId = Environment.GetEnvironmentVariable(TmsProjectId);
if (Guid.TryParse(projectId, out var _))
if (!string.IsNullOrWhiteSpace(projectId) && Guid.TryParse(projectId, out _))
{
settings.ProjectId = projectId;
}

var configurationId = Environment.GetEnvironmentVariable(TmsConfigurationId);
if (Guid.TryParse(configurationId, out var _))
if (!string.IsNullOrWhiteSpace(configurationId) && Guid.TryParse(configurationId, out _))
{
settings.ConfigurationId = configurationId;
}

var testRunId = Environment.GetEnvironmentVariable(TmsTestRunId);
if (Guid.TryParse(testRunId, out var _))
if (!string.IsNullOrWhiteSpace(testRunId) && Guid.TryParse(testRunId, out _))
{
settings.TestRunId = testRunId;
}
Expand All @@ -106,8 +102,6 @@ private static TmsSettings ApplyEnv(TmsSettings settings)
{
settings.CertValidation = false;
}

return settings;
}

private static void Validate(TmsSettings settings)
Expand All @@ -123,23 +117,25 @@ private static void Validate(TmsSettings settings)
throw new ConfigurationErrorsException("Private token is invalid");
}

if (!Guid.TryParse(settings.ProjectId, out var _))
if (!Guid.TryParse(settings.ProjectId, out _))
{
throw new ConfigurationErrorsException("Project id is invalid");
}

if (!Guid.TryParse(settings.ConfigurationId, out var _))
if (!Guid.TryParse(settings.ConfigurationId, out _))
{
throw new ConfigurationErrorsException("Configuration id is invalid");
}

if (!string.IsNullOrWhiteSpace(settings.TestRunId))
if (string.IsNullOrWhiteSpace(settings.TestRunId))
{
if (!Guid.TryParse(settings.TestRunId, out var _))
{
throw new ConfigurationErrorsException(
"Config contains not valid test run id.");
}
return;
}

if (!Guid.TryParse(settings.TestRunId, out _))
{
throw new ConfigurationErrorsException(
"Config contains not valid test run id.");
}
}
}
12 changes: 6 additions & 6 deletions TmsRunner/Managers/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ private static void Validate(TmsSettings settings)
throw new ConfigurationErrorsException("Private token is invalid");
}

if (!Guid.TryParse(settings.ProjectId, out var _))
if (!Guid.TryParse(settings.ProjectId, out _))
{
throw new ConfigurationErrorsException("Project id is invalid");
}

if (!Guid.TryParse(settings.ConfigurationId, out var _))
if (!Guid.TryParse(settings.ConfigurationId, out _))
{
throw new ConfigurationErrorsException("Configuration id is invalid");
}
Expand All @@ -86,7 +86,7 @@ private static void Validate(TmsSettings settings)
{
case 0:
{
if (!Guid.TryParse(settings.TestRunId, out var _))
if (!Guid.TryParse(settings.TestRunId, out _))
{
throw new ConfigurationErrorsException(
"Adapter works in mode 0. Config should contains valid test run id.");
Expand All @@ -96,7 +96,7 @@ private static void Validate(TmsSettings settings)
}
case 1:
{
if (!Guid.TryParse(settings.TestRunId, out var _))
if (!Guid.TryParse(settings.TestRunId, out _))
{
throw new ConfigurationErrorsException(
"Adapter works in mode 1. Config should contains valid test run id.");
Expand All @@ -105,15 +105,15 @@ private static void Validate(TmsSettings settings)
break;
}
case 2:
if (Guid.TryParse(settings.TestRunId, out var _))
if (Guid.TryParse(settings.TestRunId, out _))
{
throw new ConfigurationErrorsException(
"Adapter works in mode 2. Config should not contains test run id.");
}

break;
default:
throw new Exception($"Incorrect adapter mode: {settings.AdapterMode}");
throw new ConfigurationErrorsException($"Incorrect adapter mode: {settings.AdapterMode}");
}
}

Expand Down

0 comments on commit 992618f

Please sign in to comment.