From e22cc4f6dc2e0d138076ef4e4a9a42d01963053a Mon Sep 17 00:00:00 2001 From: Alireza Eiji Date: Tue, 6 Aug 2024 11:18:54 +0330 Subject: [PATCH] refactor integration test for checking the file sha --- ...GiteaIntegrationTestDependencyInjection.cs | 2 +- .../TestData/Abstractions/ITestFileCreator.cs | 4 ++- .../Abstractions/ITestFileMetadataGetter.cs | 10 +++++++ .../Initializers/TestData/TestFileCreator.cs | 6 +++-- .../TestData/TestFileMetadataGetter.cs | 27 +++++++++++++++++++ .../GetFilesMetadata/GetFilesMetadataTests.cs | 14 +++++----- .../Gitea/File/CreateFile/Models/Content.cs | 3 +++ 7 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileMetadataGetter.cs create mode 100644 Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileMetadataGetter.cs diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs index f00ba7f..d20f419 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs @@ -21,8 +21,8 @@ public static IServiceCollection AddGiteaIntegrationTestsServices(this IServiceC serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileCreator.cs index 2c32580..e5e353b 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileCreator.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileCreator.cs @@ -1,8 +1,10 @@ using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos; +using Refit; namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; public interface ITestFileCreator { - Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken); + Task> CreateFileAsync(CreateFileCommandDto createFileCommandDto, + CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileMetadataGetter.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileMetadataGetter.cs new file mode 100644 index 0000000..09eee75 --- /dev/null +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestFileMetadataGetter.cs @@ -0,0 +1,10 @@ +using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos; + +namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; + +internal interface ITestFileMetadataGetter +{ + Task GetFileMetadataAsync(string repositoryName, + string filePath, + CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileCreator.cs index 51e279c..4747b03 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileCreator.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileCreator.cs @@ -1,6 +1,7 @@ using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions; using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos; using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; +using Refit; namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData; @@ -13,8 +14,9 @@ public TestFileCreator(IFileFacade fileFacade) _fileFacade = fileFacade ?? throw new ArgumentNullException(nameof(fileFacade)); } - public async Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken) + public async Task> CreateFileAsync(CreateFileCommandDto createFileCommandDto, + CancellationToken cancellationToken) { - await _fileFacade.CreateFileAsync(createFileCommandDto, cancellationToken); + return await _fileFacade.CreateFileAsync(createFileCommandDto, cancellationToken); } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileMetadataGetter.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileMetadataGetter.cs new file mode 100644 index 0000000..9673573 --- /dev/null +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestFileMetadataGetter.cs @@ -0,0 +1,27 @@ +using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions; +using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos; +using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; + +namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData; + +internal class TestFileMetadataGetter : ITestFileMetadataGetter +{ + private readonly IFileFacade _fileFacade; + + public TestFileMetadataGetter(IFileFacade fileFacade) + { + _fileFacade = fileFacade ?? throw new ArgumentNullException(nameof(fileFacade)); + } + + public async Task GetFileMetadataAsync(string repositoryName, + string filePath, + CancellationToken cancellationToken) + { + var fileMetadata = await _fileFacade.GetFileAsync(new GetFileMetadataQueryDto + { + RepositoryName = repositoryName, + FilePath = filePath + }, cancellationToken).ConfigureAwait(false); + return fileMetadata.Content!; + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs b/Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs index d80561a..734279b 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs @@ -16,6 +16,7 @@ public class GetFilesMetadataTests private readonly IGiteaClient _sut; private readonly ITestRepositoryCreator _repositoryCreator; private readonly ITestFileCreator _fileCreator; + private readonly ITestFileMetadataGetter _fileMetadataGetter; private readonly GiteaCollectionFixture _giteaCollectionFixture; public GetFilesMetadataTests(GiteaCollectionFixture giteaCollectionFixture) @@ -23,6 +24,7 @@ public GetFilesMetadataTests(GiteaCollectionFixture giteaCollectionFixture) _giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture)); _repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); _fileCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); + _fileMetadataGetter = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); _sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); } @@ -35,34 +37,35 @@ public async Task GetFile_ShouldGetFileWithOkStatusCode_WhenInputsAreProvidedPro const string content = "Hello, World!"; var cancellationToken = _giteaCollectionFixture.CancellationToken; await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken); + var readmeFileMetadata = await _fileMetadataGetter.GetFileMetadataAsync(repositoryName, "README.md", cancellationToken); var createFileCommandDto = new CreateFileCommandDto { RepositoryName = repositoryName, FilePath = filePath, Content = content }; - await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken); + var newFileMetadata = await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken); var getFileCommandDto = new GetFilesMetadataQueryDto { RepositoryName = repositoryName, BranchName = "main" }; - var expectedFiles = new List() + var expectedFiles = new List { new() { Content = null, FileName = "README.md", FilePath = "README.md", - FileSha = "" + FileSha = readmeFileMetadata.FileSha }, new() { Content = null, FileName = filePath, FilePath = filePath, - FileSha = "" + FileSha = newFileMetadata.Content!.Content.FileSha } }; @@ -71,7 +74,6 @@ public async Task GetFile_ShouldGetFileWithOkStatusCode_WhenInputsAreProvidedPro // Assert actual.StatusCode.Should().Be(HttpStatusCode.OK); - actual.Content.Should().BeEquivalentTo(expectedFiles, options => options.Excluding(x => x.FileSha)); + actual.Content.Should().BeEquivalentTo(expectedFiles); } - } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/File/CreateFile/Models/Content.cs b/Mohaymen.GiteaClient/Gitea/File/CreateFile/Models/Content.cs index d33ba8f..2199d38 100644 --- a/Mohaymen.GiteaClient/Gitea/File/CreateFile/Models/Content.cs +++ b/Mohaymen.GiteaClient/Gitea/File/CreateFile/Models/Content.cs @@ -12,4 +12,7 @@ public class Content [JsonProperty("content")] public required string StringContent { get; init; } + + [JsonProperty("sha")] + public required string FileSha { get; init; } } \ No newline at end of file