-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate Validation logic into a shared builder for re-use across …
…modules (#35) * Added in validation builder and unit tests before changing modules * converted all modules to use new validation builder * Tests for validation builder
- Loading branch information
Showing
21 changed files
with
639 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 4 additions & 11 deletions
15
src/backend/CodeHub.Platform.Azure/Models/AzureSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
namespace CodeHub.Platform.Azure.Models; | ||
using CodeHub.Shared.Models; | ||
|
||
internal sealed record AzureSettings | ||
{ | ||
public required bool IsEnabled { get; init; } | ||
namespace CodeHub.Platform.Azure.Models; | ||
|
||
public static AzureSettings CreateDisabled() | ||
{ | ||
return new AzureSettings | ||
{ | ||
IsEnabled = false | ||
}; | ||
} | ||
internal sealed class AzureSettings : Settings | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 6 additions & 15 deletions
21
src/backend/CodeHub.Platform.AzureDevOps/Models/AzureDevOpsSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
namespace CodeHub.Platform.AzureDevOps.Models; | ||
using CodeHub.Shared.Models; | ||
|
||
internal sealed record AzureDevOpsSettings | ||
{ | ||
public required string PersonalAccessToken { get; init; } | ||
public required string Organization { get; init; } | ||
public required bool IsEnabled { get; init; } | ||
namespace CodeHub.Platform.AzureDevOps.Models; | ||
|
||
public static AzureDevOpsSettings CreateDisabled() | ||
{ | ||
return new AzureDevOpsSettings | ||
{ | ||
IsEnabled = false, | ||
PersonalAccessToken = string.Empty, | ||
Organization = string.Empty, | ||
}; | ||
} | ||
internal sealed class AzureDevOpsSettings : Settings | ||
{ | ||
public string PersonalAccessToken { get; init; } = string.Empty; | ||
public string Organization { get; init; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/backend/CodeHub.Platform.GitHub.Tests/CodeHub.Platform.GitHub.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector"/> | ||
<PackageReference Include="coverlet.msbuild"/> | ||
<PackageReference Include="Microsoft.Extensions.Configuration"/> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
<PackageReference Include="xunit"/> | ||
<PackageReference Include="xunit.runner.visualstudio"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CodeHub.Platform.GitHub\CodeHub.Platform.GitHub.csproj" /> | ||
</ItemGroup> | ||
</Project> |
115 changes: 115 additions & 0 deletions
115
src/backend/CodeHub.Platform.GitHub.Tests/Extensions/GitHubExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using CodeHub.Platform.GitHub.Extensions; | ||
using CodeHub.Platform.GitHub.Services; | ||
using CodeHub.Shared.Services; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace CodeHub.Platform.GitHub.Tests.Extensions; | ||
|
||
public sealed class GitHubExtensionsTests | ||
{ | ||
[Fact] | ||
public void RegisterGitHubServices_WhenEnabledWithValidSettings_RegistersCorrectServices() | ||
{ | ||
// Arrange | ||
var serviceCollection = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(GetValidGitHubConfiguration(true)) | ||
.Build(); | ||
|
||
// Act | ||
serviceCollection.RegisterGitHub(configuration); | ||
|
||
// Assert | ||
Assert.Contains(serviceCollection, | ||
service => service.ServiceType == typeof(IDiscoveryService) && | ||
service.Lifetime == ServiceLifetime.Singleton && | ||
service.ImplementationType == typeof(GitHubDiscoveryService)); | ||
Assert.Contains(serviceCollection, | ||
service => service.ServiceType == typeof(IGitHubService) && | ||
service.Lifetime == ServiceLifetime.Singleton && | ||
service.ImplementationType == typeof(GitHubService)); | ||
Assert.Contains(serviceCollection, | ||
service => service.ServiceType == typeof(IGitHubConnectionService) && | ||
service.Lifetime == ServiceLifetime.Singleton && | ||
service.ImplementationType == typeof(GitHubConnectionService)); | ||
Assert.Contains(serviceCollection, | ||
service => service.ServiceType == typeof(IMemoryCache) && | ||
service.ImplementationType == typeof(MemoryCache)); | ||
} | ||
|
||
[Fact] | ||
public void RegisterGitHubServices_WhenDisabledWithValidSettings_DoesNotRegisterServices() | ||
{ | ||
// Arrange | ||
var serviceCollection = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(GetValidGitHubConfiguration(false)) | ||
.Build(); | ||
|
||
// Act | ||
var serviceCountBefore = serviceCollection.Count; | ||
serviceCollection.RegisterGitHub(configuration); | ||
|
||
// Assert | ||
Assert.Equal(serviceCountBefore, serviceCollection.Count); | ||
} | ||
|
||
[Fact] | ||
public void RegisterGitHubServices_WhenDisabledWithValidSettingsButNotWholeSection_DoesNotRegisterServices() | ||
{ | ||
// Arrange | ||
var serviceCollection = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(GetValidWithoutOtherGitHubConfiguration(false)) | ||
.Build(); | ||
|
||
// Act | ||
var serviceCountBefore = serviceCollection.Count; | ||
serviceCollection.RegisterGitHub(configuration); | ||
|
||
// Assert | ||
Assert.Equal(serviceCountBefore, serviceCollection.Count); | ||
} | ||
|
||
|
||
[Fact] | ||
public void RegisterGitHubServices_WhenEnabledButCalledWithMissingSettings_ThrowsException() | ||
{ | ||
// Arrange | ||
var serviceCollection = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(GetInvalidGitHubSettings(true)) | ||
.Build(); | ||
|
||
// Act + Assert | ||
Assert.Throws<InvalidOperationException>(() => serviceCollection.RegisterGitHub(configuration)); | ||
} | ||
|
||
private static Dictionary<string, string?> GetValidGitHubConfiguration(bool enabled) | ||
{ | ||
return new Dictionary<string, string?> | ||
{ | ||
{ "GitHubSettings:AgentName", "TestAgentName" }, | ||
{ "GitHubSettings:Token", "TestToken" }, | ||
{ "GitHubSettings:IsEnabled", enabled.ToString() } | ||
}; | ||
} | ||
|
||
private static Dictionary<string, string?> GetValidWithoutOtherGitHubConfiguration(bool enabled) | ||
{ | ||
return new Dictionary<string, string?> | ||
{ | ||
{ "GitHubSettings:IsEnabled", enabled.ToString() } | ||
}; | ||
} | ||
|
||
private static Dictionary<string, string?> GetInvalidGitHubSettings(bool enabled) | ||
{ | ||
return new Dictionary<string, string?> | ||
{ | ||
{ "GitHubSettings:IsEnabled", enabled.ToString() } | ||
}; | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/backend/CodeHub.Platform.GitHub/CodeHub.Platform.GitHub.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Octokit" /> | ||
<PackageReference Include="Octokit"/> | ||
<PackageReference Include="System.Text.Json"/> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions"/> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Memory"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CodeHub.Shared\CodeHub.Shared.csproj"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="CodeHub.Platform.GitHub.Tests"/> | ||
</ItemGroup> | ||
</Project> |
21 changes: 6 additions & 15 deletions
21
src/backend/CodeHub.Platform.GitHub/Models/GitHubSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
namespace CodeHub.Platform.GitHub.Models; | ||
using CodeHub.Shared.Models; | ||
|
||
internal sealed record GitHubSettings | ||
{ | ||
public required string AgentName { get; init; } | ||
public required string Token { get; init; } | ||
public required bool IsEnabled { get; init; } | ||
namespace CodeHub.Platform.GitHub.Models; | ||
|
||
public static GitHubSettings CreateDisabled() | ||
{ | ||
return new GitHubSettings | ||
{ | ||
IsEnabled = false, | ||
AgentName = string.Empty, | ||
Token = string.Empty | ||
}; | ||
} | ||
internal sealed class GitHubSettings : Settings | ||
{ | ||
public string AgentName { get; init; } = string.Empty; | ||
public string Token { get; init; } = string.Empty; | ||
} |
Oops, something went wrong.