diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs index 5e8048a..fa8f23c 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs @@ -1,4 +1,8 @@ using Microsoft.Extensions.DependencyInjection; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions; +using Mohaymen.GiteaClient.Gitea.Repository.Common.Facade; +using Mohaymen.GiteaClient.Gitea.Repository.Common.Facade.Abstractions; using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions; using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions; using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData; @@ -17,6 +21,7 @@ public static IServiceCollection AddGiteaIntegrationTestsServices(this IServiceC httpClient.BaseAddress = new Uri(baseApiUrl); }); serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); return serviceCollection; diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestBranchCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestBranchCreator.cs new file mode 100644 index 0000000..0f34067 --- /dev/null +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestBranchCreator.cs @@ -0,0 +1,9 @@ +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; +using Refit; + +namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; + +public interface ITestBranchCreator +{ + Task CreateBranchAsync(string repositoryName, string branchName, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestRepositoryCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestRepositoryCreator.cs index aab92fc..e718032 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestRepositoryCreator.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/Abstractions/ITestRepositoryCreator.cs @@ -1,6 +1,9 @@ -namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; +using Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Dtos; +using Refit; + +namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; internal interface ITestRepositoryCreator { - Task CreateRepository(string repositoryName); + Task CreateRepositoryAsync(string repositoryName, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestBranchCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestBranchCreator.cs new file mode 100644 index 0000000..e204830 --- /dev/null +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestBranchCreator.cs @@ -0,0 +1,30 @@ +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions; +using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; +using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; +using Mohaymen.GiteaClient.IntegrationTests.Common.Models; +using Refit; + +namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData; + +public class TestBranchCreator : ITestBranchCreator +{ + private readonly IBranchFacade _branchFacade; + + public TestBranchCreator(IBranchFacade branchFacade) + { + _branchFacade = branchFacade ?? throw new ArgumentNullException(nameof(branchFacade)); + } + + public async Task CreateBranchAsync(string repositoryName, string branchName, + CancellationToken cancellationToken) + { + var createBranchCommandDto = new CreateBranchCommandDto + { + RepositoryName = repositoryName, + NewBranchName = branchName, + OldReferenceName = GiteaTestConstants.DefaultBranch + }; + await _branchFacade.CreateBranchAsync(createBranchCommandDto, cancellationToken); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestRepositoryCreator.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestRepositoryCreator.cs index d25a01d..a9b9e66 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestRepositoryCreator.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Initializers/TestData/TestRepositoryCreator.cs @@ -1,40 +1,30 @@ -using System.Net.Http.Headers; -using Microsoft.Extensions.Options; -using Mohaymen.GiteaClient.Core.Configs; +using Mohaymen.GiteaClient.Gitea.Repository.Common.Facade.Abstractions; +using Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Dtos; using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; using Mohaymen.GiteaClient.IntegrationTests.Common.Models; -using Mohaymen.GiteaClient.IntegrationTests.Common.Models.Requests; -using Newtonsoft.Json; +using Refit; namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData; internal class TestRepositoryCreator : ITestRepositoryCreator { - private readonly IHttpClientFactory _httpClientFactory; - private readonly IOptions _giteaOptions; - - public TestRepositoryCreator(IHttpClientFactory httpClientFactory, - IOptions giteaOptions) + private readonly IRepositoryFacade _repositoryFacade; + + public TestRepositoryCreator(IRepositoryFacade repositoryFacade) { - _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); - _giteaOptions = giteaOptions ?? throw new ArgumentNullException(nameof(giteaOptions)); + _repositoryFacade = repositoryFacade ?? throw new ArgumentNullException(nameof(repositoryFacade)); } - - - public async Task CreateRepository(string repositoryName) + + public async Task CreateRepositoryAsync(string repositoryName, + CancellationToken cancellationToken) { - var httpClient = _httpClientFactory.CreateClient(GiteaTestConstants.ApiClientName); - var createRepositoryRequest = new CreateIntegrationTestRepositoryRequest + var createRepositoryCommandDto = new CreateRepositoryCommandDto { - DefaultBranch = "main", + DefaultBranch = GiteaTestConstants.DefaultBranch, Name = repositoryName, - Readme = "Default", - AutoInit = true, - IsPrivateBranch = true + IsPrivateBranch = true, + AutoInit = true }; - var jsonContent = new StringContent(JsonConvert.SerializeObject(createRepositoryRequest)); - jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", $"{_giteaOptions.Value.PersonalAccessToken}"); - await httpClient.PostAsync("user/repos", jsonContent); + await _repositoryFacade.CreateRepositoryAsync(createRepositoryCommandDto, cancellationToken); } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Models/GiteaTestConstants.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Models/GiteaTestConstants.cs index 34606ae..6b629a2 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Models/GiteaTestConstants.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Common/Models/GiteaTestConstants.cs @@ -9,4 +9,6 @@ internal class GiteaTestConstants internal const string TokenName = "test_token"; internal const string ImageName = "gitea/gitea:latest"; internal const string ApiClientName = "IntegrationTestApiClient"; + internal const string RepositoryName = "BranchUseCaseRepository"; + internal const string DefaultBranch = "main"; } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Common/Models/Requests/CreateIntegrationTestRepositoryRequest.cs b/Mohaymen.GiteaClient.IntegrationTests/Common/Models/Requests/CreateIntegrationTestRepositoryRequest.cs deleted file mode 100644 index 58a8924..0000000 --- a/Mohaymen.GiteaClient.IntegrationTests/Common/Models/Requests/CreateIntegrationTestRepositoryRequest.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Context; -using Newtonsoft.Json; - -namespace Mohaymen.GiteaClient.IntegrationTests.Common.Models.Requests; - -internal class CreateIntegrationTestRepositoryRequest : CreateRepositoryRequest -{ - [JsonProperty("readme")] - public required string Readme { get; init; } - - [JsonProperty("auto_init")] - public bool AutoInit { get; init; } -} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/BranchTestsClassFixture.cs b/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/BranchTestsClassFixture.cs deleted file mode 100644 index f872f11..0000000 --- a/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/BranchTestsClassFixture.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea; -using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; - -namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Branch; - -public class BranchTestsClassFixture : IAsyncLifetime -{ - public const string RepositoryName = "BranchUseCaseRepository"; - - private readonly ITestRepositoryCreator _testRepositoryCreator; - - public BranchTestsClassFixture(GiteaCollectionFixture giteaCollectionFixture) - { - _testRepositoryCreator = giteaCollectionFixture.ServiceProvider.GetRequiredService(); - } - - public async Task InitializeAsync() - { - await _testRepositoryCreator.CreateRepository(RepositoryName); - } - - public Task DisposeAsync() - { - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/CreateBranch/CreateBranchTests.cs b/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/CreateBranch/CreateBranchTests.cs index c25ddd7..9715abf 100644 --- a/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/CreateBranch/CreateBranchTests.cs +++ b/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/CreateBranch/CreateBranchTests.cs @@ -1,19 +1,21 @@ using System.Net; using FluentAssertions; -using FluentValidation; using Microsoft.Extensions.DependencyInjection; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; using Mohaymen.GiteaClient.Gitea.Client.Abstractions; using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions; using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea; +using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; +using Mohaymen.GiteaClient.IntegrationTests.Common.Models; namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Branch.CreateBranch; [Collection("GiteaIntegrationTests")] -public class CreateBranchTests : IClassFixture +public class CreateBranchTests { private readonly IGiteaClient _sut; private readonly ITestBranchChecker _branchChecker; + private readonly ITestRepositoryCreator _repositoryCreator; private readonly GiteaCollectionFixture _giteaCollectionFixture; public CreateBranchTests(GiteaCollectionFixture giteaCollectionFixture) @@ -21,16 +23,20 @@ public CreateBranchTests(GiteaCollectionFixture giteaCollectionFixture) _giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture)); _sut = giteaCollectionFixture.ServiceProvider.GetRequiredService(); _branchChecker = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); + _repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); } [Fact] public async Task CreateBranch_ShouldCreateBranchWithCreatedStatusCode_WhenInputsAreProvidedProperly() { // Arrange + const string repositoryName = GiteaTestConstants.RepositoryName; + await _repositoryCreator.CreateRepositoryAsync(repositoryName, _giteaCollectionFixture.CancellationToken); + const string newBranchName = "feature/test_new_branch"; var createBranchCommandDto = new CreateBranchCommandDto { - RepositoryName = BranchTestsClassFixture.RepositoryName, + RepositoryName = repositoryName, NewBranchName = newBranchName, OldReferenceName = "main" }; @@ -41,7 +47,7 @@ public async Task CreateBranch_ShouldCreateBranchWithCreatedStatusCode_WhenInput // Assert actual.StatusCode.Should().Be(HttpStatusCode.Created); actual.Content!.BranchName.Should().Be(newBranchName); - var branchExist = await _branchChecker.ContainsBranch(BranchTestsClassFixture.RepositoryName, newBranchName, _giteaCollectionFixture.CancellationToken); + var branchExist = await _branchChecker.ContainsBranch(repositoryName, newBranchName, _giteaCollectionFixture.CancellationToken); branchExist.Should().BeTrue(); } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/GetBranchList/GetBranchListTests.cs b/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/GetBranchList/GetBranchListTests.cs new file mode 100644 index 0000000..5c98062 --- /dev/null +++ b/Mohaymen.GiteaClient.IntegrationTests/Gitea/Branch/GetBranchList/GetBranchListTests.cs @@ -0,0 +1,62 @@ +using System.Net; +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; +using Mohaymen.GiteaClient.Gitea.Client.Abstractions; +using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea; +using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; +using Mohaymen.GiteaClient.IntegrationTests.Common.Models; + +namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Branch.GetBranchList; + +[Collection("GiteaIntegrationTests")] +public class GetBranchListTests +{ + private readonly IGiteaClient _sut; + private readonly ITestRepositoryCreator _repositoryCreator; + private readonly ITestBranchCreator _branchCreator; + private readonly GiteaCollectionFixture _giteaCollectionFixture; + + public GetBranchListTests(GiteaCollectionFixture giteaCollectionFixture) + { + _giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture)); + _repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); + _branchCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); + _sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService(); + } + + [Fact] + public async Task GetBranchList_ShouldGetBranchListOfRepo_WhenInputsAreProvidedProperly() + { + // Arrange + const string repositoryName = GiteaTestConstants.RepositoryName; + const string branch1 = "branch1"; + const string branch2 = "branch2"; + const string branch3 = "branch3"; + var cancellationToken = _giteaCollectionFixture.CancellationToken; + + await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken); + await _branchCreator.CreateBranchAsync(repositoryName, branch1, cancellationToken); + await _branchCreator.CreateBranchAsync(repositoryName, branch2, cancellationToken); + await _branchCreator.CreateBranchAsync(repositoryName, branch3, cancellationToken); + + var getBranchListCommandDto = new GetBranchListCommandDto + { + RepositoryName = repositoryName + }; + var expectedBranchNames = new List + { + GiteaTestConstants.DefaultBranch, + branch1, + branch2, + branch3 + }; + + // Act + var actual = await _sut.BranchClient.GetBranchListAsync(getBranchListCommandDto, cancellationToken); + + // Assert + actual.StatusCode.Should().Be(HttpStatusCode.OK); + actual.Content!.Select(x => x.BranchName).Should().BeEquivalentTo(expectedBranchNames); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.Tests/Gitea/Branch/Common/Facade/BranchFacadeTests.cs b/Mohaymen.GiteaClient.Tests/Gitea/Branch/Common/Facade/BranchFacadeTests.cs index ce8ed59..0ad472f 100644 --- a/Mohaymen.GiteaClient.Tests/Gitea/Branch/Common/Facade/BranchFacadeTests.cs +++ b/Mohaymen.GiteaClient.Tests/Gitea/Branch/Common/Facade/BranchFacadeTests.cs @@ -3,6 +3,8 @@ using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Commands; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; using NSubstitute; using Xunit; @@ -41,4 +43,21 @@ await _mediator.Received(1).Send(Arg.Is(x => x.RepositoryNa && x.NewBranchName == newBranchName && x.OldReferenceName == oldReferenceName)); } + + [Fact] + public async Task GetBranchListAsync_ShouldCallSend_WhenEver() + { + // Arrange + const string repositoryName = "repo"; + var commandDto = new GetBranchListCommandDto + { + RepositoryName = repositoryName + }; + + // Act + await _sut.GetBranchListAsync(commandDto, default); + + // Assert + await _mediator.Received(1).Send(Arg.Is(x => x.RepositoryName == repositoryName)); + } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient.Tests/Gitea/Branch/CreateBranch/Commands/CreateBranchCommandHandlerTests.cs b/Mohaymen.GiteaClient.Tests/Gitea/Branch/CreateBranch/Commands/CreateBranchCommandHandlerTests.cs index c43045c..b9c7ef8 100644 --- a/Mohaymen.GiteaClient.Tests/Gitea/Branch/CreateBranch/Commands/CreateBranchCommandHandlerTests.cs +++ b/Mohaymen.GiteaClient.Tests/Gitea/Branch/CreateBranch/Commands/CreateBranchCommandHandlerTests.cs @@ -4,9 +4,9 @@ using Microsoft.Extensions.Options; using Mohaymen.GiteaClient.Core.Configs; using Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Commands; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Context; -using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; using NSubstitute; using Refit; using Xunit; @@ -18,7 +18,7 @@ public class CreateBranchCommandHandlerTests private readonly IBranchRestClient _branchRestClient; private readonly IOptions _options; private readonly InlineValidator _validator; - private readonly IRequestHandler> _sut; + private readonly IRequestHandler> _sut; public CreateBranchCommandHandlerTests() { diff --git a/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Commands/GetBranchListCommandHandlerTests.cs b/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Commands/GetBranchListCommandHandlerTests.cs new file mode 100644 index 0000000..220d32e --- /dev/null +++ b/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Commands/GetBranchListCommandHandlerTests.cs @@ -0,0 +1,70 @@ +using FluentAssertions; +using FluentValidation; +using MediatR; +using Microsoft.Extensions.Options; +using Mohaymen.GiteaClient.Core.Configs; +using Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; +using NSubstitute; +using Refit; +using Xunit; + +namespace Mohaymen.GiteaClient.Tests.Gitea.Branch.GetBranchList.Commands; + +public class GetBranchListCommandHandlerTests +{ + private readonly IBranchRestClient _branchRestClient; + private readonly IOptions _options; + private readonly InlineValidator _validator; + private readonly IRequestHandler>> _sut; + + public GetBranchListCommandHandlerTests() + { + _branchRestClient = Substitute.For(); + _options = Substitute.For>(); + _validator = new InlineValidator(); + _sut = new GetBranchListCommandHandler(_branchRestClient, _options, _validator); + } + + [Fact] + public async Task Handle_ShouldThrowsValidationException_WhenInputIsInvalid() + { + // Arrange + _validator.RuleFor(x => x).Must(_ => false); + var command = new GetBranchListCommand + { + RepositoryName = "repo" + }; + + // Act + var actual = async () => await _sut.Handle(command, default); + + // Assert + await actual.Should().ThrowAsync(); + } + + [Fact] + public async Task Handle_ShouldCallGetBranchListAsync_AndInputsAreValid() + { + // Arrange + _validator.RuleFor(x => x).Must(_ => true); + const string owner = "owner"; + const string repositoryName = "repo"; + var command = new GetBranchListCommand + { + RepositoryName = repositoryName + }; + _options.Value.Returns(new GiteaApiConfiguration + { + BaseUrl = "url", + PersonalAccessToken = "token", + RepositoriesOwner = owner + }); + + // Act + await _sut.Handle(command, default); + // Assert + await _branchRestClient.Received(1).GetBranchListAsync(owner, repositoryName); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidatorTests.cs b/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidatorTests.cs new file mode 100644 index 0000000..de3ad22 --- /dev/null +++ b/Mohaymen.GiteaClient.Tests/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidatorTests.cs @@ -0,0 +1,52 @@ +using FluentAssertions; +using FluentValidation; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Validators; +using Xunit; + +namespace Mohaymen.GiteaClient.Tests.Gitea.Branch.GetBranchList.Validators; + +public class GetBranchListCommandValidatorTests +{ + private readonly IValidator _sut; + + public GetBranchListCommandValidatorTests() + { + _sut = new GetBranchListCommandValidator(); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public void Validate_ShouldReturnEmptyRepositoryNameErrorCode_WhenRepositoryNameIsNullOrEmpty(string repositoryName) + { + // Arrange + var command = new GetBranchListCommand + { + RepositoryName = repositoryName + }; + + // Act + var actual = _sut.Validate(command); + + // Assert + actual.Errors.Select(x => x.ErrorCode) + .Should().Contain(GetBranchListErrorCodes.EmptyRepositoryNameErrorCode); + } + + [Fact] + public void Validate_ShouldReturnValidResult_WhenEveryThingIsProvidedProperly() + { + // Arrange + var command = new GetBranchListCommand + { + RepositoryName = "repo" + }; + + // Act + var actual = _sut.Validate(command); + + // Assert + actual.IsValid.Should().BeTrue(); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/Common/ApiCall/Abstractions/IBranchRestClient.cs b/Mohaymen.GiteaClient/Gitea/Branch/Common/ApiCall/Abstractions/IBranchRestClient.cs index d25416f..690a257 100644 --- a/Mohaymen.GiteaClient/Gitea/Branch/Common/ApiCall/Abstractions/IBranchRestClient.cs +++ b/Mohaymen.GiteaClient/Gitea/Branch/Common/ApiCall/Abstractions/IBranchRestClient.cs @@ -1,7 +1,8 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Mohaymen.GiteaClient.Core.ApiCall.Abstractions; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Context; -using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; using Refit; namespace Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; @@ -9,8 +10,13 @@ namespace Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; internal interface IBranchRestClient : IRefitClientInterface { [Post("/repos/{owner}/{repo}/branches")] - Task> CreateBranchAsync( + Task> CreateBranchAsync( [AliasAs("owner")] string owner, [AliasAs("repo")] string repositoryName, [Body] CreateBranchRequest createBranchRequest); + + [Get("/repos/{owner}/{repo}/branches")] + Task>> GetBranchListAsync( + [AliasAs("owner")] string owner, + [AliasAs("repo")] string repositoryName); } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Dtos/CreateBranchResponseDto.cs b/Mohaymen.GiteaClient/Gitea/Branch/Common/Dtos/BranchResponseDto.cs similarity index 52% rename from Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Dtos/CreateBranchResponseDto.cs rename to Mohaymen.GiteaClient/Gitea/Branch/Common/Dtos/BranchResponseDto.cs index a01a1e6..fd506bb 100644 --- a/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Dtos/CreateBranchResponseDto.cs +++ b/Mohaymen.GiteaClient/Gitea/Branch/Common/Dtos/BranchResponseDto.cs @@ -1,8 +1,8 @@ using Newtonsoft.Json; -namespace Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; +namespace Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; -public class CreateBranchResponseDto +public class BranchResponseDto { [JsonProperty("name")] public required string BranchName { get; init; } diff --git a/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/Abstractions/IBranchFacade.cs b/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/Abstractions/IBranchFacade.cs index 063fe24..c7e0ce9 100644 --- a/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/Abstractions/IBranchFacade.cs +++ b/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/Abstractions/IBranchFacade.cs @@ -1,11 +1,15 @@ +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; using Refit; namespace Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions; public interface IBranchFacade { - Task> CreateBranchAsync(CreateBranchCommandDto createBranchCommandDto, CancellationToken cancellationToken); + Task> CreateBranchAsync(CreateBranchCommandDto createBranchCommandDto, CancellationToken cancellationToken); + Task>> GetBranchListAsync(GetBranchListCommandDto getBranchListCommandDto, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/BranchFacade.cs b/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/BranchFacade.cs index a5323f8..eabca94 100644 --- a/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/BranchFacade.cs +++ b/Mohaymen.GiteaClient/Gitea/Branch/Common/Facade/BranchFacade.cs @@ -1,10 +1,14 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using MediatR; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Mappers; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Mappers; using Refit; namespace Mohaymen.GiteaClient.Gitea.Branch.Common.Facade; @@ -18,10 +22,17 @@ public BranchFacade(IMediator mediator) _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); } - public async Task> CreateBranchAsync(CreateBranchCommandDto createBranchCommandDto, + public async Task> CreateBranchAsync(CreateBranchCommandDto createBranchCommandDto, CancellationToken cancellationToken) { var command = createBranchCommandDto.ToCreateBranchCommand(); return await _mediator.Send(command, cancellationToken); } + + public async Task>> GetBranchListAsync(GetBranchListCommandDto getBranchListCommandDto, + CancellationToken cancellationToken) + { + var command = getBranchListCommandDto.ToGetBranchListCommand(); + return await _mediator.Send(command, cancellationToken); + } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Commands/CreateBranchCommand.cs b/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Commands/CreateBranchCommand.cs index d7b389a..6a403f6 100644 --- a/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Commands/CreateBranchCommand.cs +++ b/Mohaymen.GiteaClient/Gitea/Branch/CreateBranch/Commands/CreateBranchCommand.cs @@ -6,20 +6,20 @@ using Microsoft.Extensions.Options; using Mohaymen.GiteaClient.Core.Configs; using Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; -using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Dtos; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Mappers; using Refit; namespace Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Commands; -internal class CreateBranchCommand : IRequest> +internal class CreateBranchCommand : IRequest> { public required string RepositoryName { get; init; } public required string NewBranchName { get; init; } public required string OldReferenceName { get; init; } } -internal class CreateBranchCommandHandler : IRequestHandler> +internal class CreateBranchCommandHandler : IRequestHandler> { private readonly IBranchRestClient _branchRestClient; private readonly IOptions _options; @@ -34,7 +34,7 @@ public CreateBranchCommandHandler(IBranchRestClient branchRestClient, _validator = validator ?? throw new ArgumentNullException(nameof(validator)); } - public async Task> Handle(CreateBranchCommand command, CancellationToken cancellationToken) + public async Task> Handle(CreateBranchCommand command, CancellationToken cancellationToken) { _validator.ValidateAndThrow(command); var createBranchRequest = command.ToCreateBranchRequest(); diff --git a/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Commands/GetBranchListCommand.cs b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Commands/GetBranchListCommand.cs new file mode 100644 index 0000000..c583f9a --- /dev/null +++ b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Commands/GetBranchListCommand.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using FluentValidation; +using MediatR; +using Microsoft.Extensions.Options; +using Mohaymen.GiteaClient.Core.Configs; +using Mohaymen.GiteaClient.Gitea.Branch.Common.ApiCall.Abstractions; +using Mohaymen.GiteaClient.Gitea.Branch.Common.Dtos; +using Refit; + +namespace Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; + +internal class GetBranchListCommand : IRequest>> +{ + public required string RepositoryName { get; init; } +} + +internal class GetBranchListCommandHandler : IRequestHandler>> +{ + private readonly IBranchRestClient _branchRestClient; + private readonly IOptions _options; + private readonly IValidator _validator; + + public GetBranchListCommandHandler(IBranchRestClient branchRestClient, + IOptions options, + IValidator validator) + { + _branchRestClient = branchRestClient ?? throw new ArgumentNullException(nameof(branchRestClient)); + _options = options ?? throw new ArgumentNullException(nameof(options)); + _validator = validator ?? throw new ArgumentNullException(nameof(validator)); + } + + public async Task>> Handle(GetBranchListCommand command, + CancellationToken cancellationToken) + { + _validator.ValidateAndThrow(command); + var owner = _options.Value.RepositoriesOwner; + return await _branchRestClient.GetBranchListAsync(owner, command.RepositoryName).ConfigureAwait(false); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Dtos/GetBranchListCommandDto.cs b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Dtos/GetBranchListCommandDto.cs new file mode 100644 index 0000000..82adf8a --- /dev/null +++ b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Dtos/GetBranchListCommandDto.cs @@ -0,0 +1,6 @@ +namespace Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; + +public class GetBranchListCommandDto +{ + public required string RepositoryName { get; init; } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Mappers/GetBranchListCommandMapper.cs b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Mappers/GetBranchListCommandMapper.cs new file mode 100644 index 0000000..26afe2e --- /dev/null +++ b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Mappers/GetBranchListCommandMapper.cs @@ -0,0 +1,18 @@ +using System; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Dtos; + +namespace Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Mappers; + +internal static class GetBranchListCommandMapper +{ + internal static GetBranchListCommand ToGetBranchListCommand(this GetBranchListCommandDto getBranchListCommandDto) + { + ArgumentNullException.ThrowIfNull(getBranchListCommandDto); + + return new GetBranchListCommand + { + RepositoryName = getBranchListCommandDto.RepositoryName, + }; + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidator.cs b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidator.cs new file mode 100644 index 0000000..8d5f08c --- /dev/null +++ b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListCommandValidator.cs @@ -0,0 +1,15 @@ +using FluentValidation; +using Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Commands; + +namespace Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Validators; + +internal sealed class GetBranchListCommandValidator : AbstractValidator +{ + public GetBranchListCommandValidator() + { + RuleFor(x => x.RepositoryName) + .NotEmpty() + .WithErrorCode(GetBranchListErrorCodes.EmptyRepositoryNameErrorCode) + .WithMessage("repository name should not be empty"); + } +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListErrorCodes.cs b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListErrorCodes.cs new file mode 100644 index 0000000..ed54ed6 --- /dev/null +++ b/Mohaymen.GiteaClient/Gitea/Branch/GetBranchList/Validators/GetBranchListErrorCodes.cs @@ -0,0 +1,6 @@ +namespace Mohaymen.GiteaClient.Gitea.Branch.GetBranchList.Validators; + +internal static class GetBranchListErrorCodes +{ + internal const string EmptyRepositoryNameErrorCode = "EmptyRepositoryNameErrorCode"; +} \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Commands/CreateRepositoryCommandHandler.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Commands/CreateRepositoryCommandHandler.cs index 094ed13..f4b3da9 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Commands/CreateRepositoryCommandHandler.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Commands/CreateRepositoryCommandHandler.cs @@ -17,6 +17,8 @@ internal class CreateRepositoryCommand : IRequest> diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Context/CreateRepositoryRequest.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Context/CreateRepositoryRequest.cs index fc8342a..b02a4c3 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Context/CreateRepositoryRequest.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Context/CreateRepositoryRequest.cs @@ -12,4 +12,7 @@ internal class CreateRepositoryRequest [JsonProperty("private")] public bool IsPrivateBranch { get; init; } + + [JsonProperty("auto_init")] + public bool AutoInit { get; init; } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryCommandDto.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryCommandDto.cs index d603653..e8e26f9 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryCommandDto.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryCommandDto.cs @@ -6,9 +6,11 @@ namespace Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Dtos; public class CreateRepositoryCommandDto { - public string DefaultBranch { get; init; } + public required string DefaultBranch { get; init; } - public string Name { get; init; } + public required string Name { get; init; } public bool IsPrivateBranch { get; init; } + + public bool AutoInit { get; init; } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryResponseDto.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryResponseDto.cs index 8f3c57f..5a013ff 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryResponseDto.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Dtos/CreateRepositoryResponseDto.cs @@ -8,5 +8,5 @@ public sealed class CreateRepositoryResponseDto public long RepositoryId { get; init; } [JsonProperty("name")] - public string RepositoryName { get; init; } + public required string RepositoryName { get; init; } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryCommandMapper.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryCommandMapper.cs index e912d96..f6a2f56 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryCommandMapper.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryCommandMapper.cs @@ -14,7 +14,8 @@ public static CreateRepositoryCommand Map(this CreateRepositoryCommandDto create { DefaultBranch = createRepositoryCommandDto.DefaultBranch, Name = createRepositoryCommandDto.Name, - IsPrivateBranch = createRepositoryCommandDto.IsPrivateBranch + IsPrivateBranch = createRepositoryCommandDto.IsPrivateBranch, + AutoInit = createRepositoryCommandDto.AutoInit }; } } \ No newline at end of file diff --git a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryRequestMapper.cs b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryRequestMapper.cs index abd3f9b..f6b1469 100644 --- a/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryRequestMapper.cs +++ b/Mohaymen.GiteaClient/Gitea/Repository/CreateRepository/Mappers/CreateRepositoryRequestMapper.cs @@ -14,7 +14,8 @@ public static CreateRepositoryRequest Map(CreateRepositoryCommand createReposito { DefaultBranch = createRepositoryCommand.DefaultBranch, Name = createRepositoryCommand.Name, - IsPrivateBranch = createRepositoryCommand.IsPrivateBranch + IsPrivateBranch = createRepositoryCommand.IsPrivateBranch, + AutoInit = createRepositoryCommand.AutoInit }; } } \ No newline at end of file