Skip to content

Commit

Permalink
test: integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
HamedSY committed Jul 31, 2024
1 parent 01f2397 commit e4ecf43
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

public interface ITestFileCreator
{
Task CreateFileAsync(string repositoryName, string filePath, string content, CancellationToken cancellationToken);
Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@ public TestFileCreator(IFileFacade fileFacade)
_fileFacade = fileFacade ?? throw new ArgumentNullException(nameof(fileFacade));
}

public async Task CreateFileAsync(string repositoryName, string filePath, string content, CancellationToken cancellationToken)
public async Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken)
{
var createFileCommandDto = new CreateFileCommandDto
{
RepositoryName = repositoryName,
FilePath = filePath,
Content = content
};

await _fileFacade.CreateFileAsync(createFileCommandDto, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public async Task GetFile_ShouldGetFileWithOkStatusCode_WhenInputsAreProvidedPro
const string content = "Hello, World!";
var cancellationToken = _giteaCollectionFixture.CancellationToken;
await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken);
await _fileCreator.CreateFileAsync(repositoryName, filePath, content, cancellationToken);
var createFileCommandDto = new CreateFileCommandDto
{
RepositoryName = repositoryName,
FilePath = filePath,
Content = content
};
await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken);

var getFileCommandDto = new GetFileCommandDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Net;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Mohaymen.GiteaClient.Gitea.Client.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos;
using Mohaymen.GiteaClient.Gitea.PullRequest.Common.Enums;
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Dtos;
using Mohaymen.GiteaClient.Gitea.PullRequest.GetPullRequestList.Dtos;
using Mohaymen.GiteaClient.Gitea.PullRequest.MergePullRequest.Dtos;
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.PullRequest.MergePullRequest;

[Collection("GiteaIntegrationTests")]
public class MergePullRequestTests
{
private readonly IGiteaClient _sut;
private readonly ITestRepositoryCreator _repositoryCreator;
private readonly ITestBranchCreator _branchCreator;
private readonly ITestPullRequestCreator _pullRequestCreator;
private readonly ITestFileCreator _fileCreator;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public MergePullRequestTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>();
_branchCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestBranchCreator>();
_pullRequestCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestPullRequestCreator>();
_fileCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestFileCreator>();
_sut = giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
}

[Fact]
public async Task MergePullRequest_ShouldMergePullRequest_WhenInputsAreProvidedProperly()
{
// Arrange
const string repositoryName = "merge_pull_request_repo";
const string headBranch = "merge_pull_request_branch";
const string baseBranch = GiteaTestConstants.DefaultBranch;
const string title = "merge_pull_request_title";
const string assignee = GiteaTestConstants.Username;
var createPullRequestCommandDto = new CreatePullRequestCommandDto
{
RepositoryName = repositoryName,
HeadBranch = headBranch,
BaseBranch = baseBranch,
Title = title,
Assignee = assignee
};
var cancellationToken = _giteaCollectionFixture.CancellationToken;
const string filePath = "merge_pull_request_file.txt";
const string content = "merge_pull_request_content.txt";
var createFileCommandDto = new CreateFileCommandDto
{
RepositoryName = repositoryName,
FilePath = filePath,
Content = content,
BranchName = headBranch
};

await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken);
await _branchCreator.CreateBranchAsync(repositoryName, headBranch, cancellationToken);
await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken);
await _pullRequestCreator.CreatePullRequestAsync(createPullRequestCommandDto, cancellationToken);

var mergePullRequestCommandDto = new MergePullRequestCommandDto
{
RepositoryName = repositoryName,
Index = 1,
MergeType = MergeType.Merge
};
var expectedGetPullRequestListCommandDto = new GetPullRequestListCommandDto
{
RepositoryName = repositoryName
};

// Act
var actual = await _sut.PullRequestClient.MergePullRequestAsync(mergePullRequestCommandDto, cancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.OK);
var getPullRequestListResponse = await _sut.PullRequestClient.GetPullRequestListAsync(
expectedGetPullRequestListCommandDto,
_giteaCollectionFixture.CancellationToken);
getPullRequestListResponse.StatusCode.Should().Be(HttpStatusCode.OK);
getPullRequestListResponse.Content.Should().NotBeNull();
getPullRequestListResponse.Content!.First().Merged.Should().BeTrue();
}
}

0 comments on commit e4ecf43

Please sign in to comment.