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

feat: get branch list api #6

Merged
merged 9 commits into from
Jul 25, 2024
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +21,7 @@ public static IServiceCollection AddGiteaIntegrationTestsServices(this IServiceC
httpClient.BaseAddress = new Uri(baseApiUrl);
});
serviceCollection.AddSingleton<ITestRepositoryCreator, TestRepositoryCreator>();
serviceCollection.AddSingleton<ITestBranchCreator, TestBranchCreator>();
serviceCollection.AddSingleton<ITestRepositoryChecker, TestRepositoryChecker>();
serviceCollection.AddSingleton<ITestBranchChecker, TestBranchChecker>();
return serviceCollection;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<GiteaApiConfiguration> _giteaOptions;

public TestRepositoryCreator(IHttpClientFactory httpClientFactory,
IOptions<GiteaApiConfiguration> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
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<BranchTestsClassFixture>
public class CreateBranchTests
{
private readonly IGiteaClient _sut;
private readonly ITestBranchChecker _branchChecker;
private readonly ITestRepositoryCreator _repositoryCreator;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public CreateBranchTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_sut = giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
_branchChecker = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestBranchChecker>();
_repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>();
}

[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"
};
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ITestRepositoryCreator>();
_branchCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestBranchCreator>();
_sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
}

[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<string>
HamedSY marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,4 +43,21 @@ await _mediator.Received(1).Send(Arg.Is<CreateBranchCommand>(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<GetBranchListCommand>(x => x.RepositoryName == repositoryName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +18,7 @@ public class CreateBranchCommandHandlerTests
private readonly IBranchRestClient _branchRestClient;
private readonly IOptions<GiteaApiConfiguration> _options;
private readonly InlineValidator<CreateBranchCommand> _validator;
private readonly IRequestHandler<CreateBranchCommand, ApiResponse<CreateBranchResponseDto>> _sut;
private readonly IRequestHandler<CreateBranchCommand, ApiResponse<BranchResponseDto>> _sut;

public CreateBranchCommandHandlerTests()
{
Expand Down
Loading