-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Mohaymen.GiteaClient.Tests/Gitea/File/Common/Facade/FileFacadeTests.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,44 @@ | ||
using MediatR; | ||
using Mohaymen.GiteaClient.Gitea.File.Common.Facade; | ||
using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands; | ||
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.File.Common.Facade; | ||
|
||
public class FileFacadeTests | ||
{ | ||
private readonly IMediator _mediator; | ||
private readonly IFileFacade _sut; | ||
|
||
public FileFacadeTests() | ||
{ | ||
_mediator = Substitute.For<IMediator>(); | ||
_sut = new FileFacade(_mediator); | ||
} | ||
|
||
[Fact] | ||
public async Task GetFileAsync_ShouldCallSend_WhenEver() | ||
{ | ||
// Arrange | ||
const string repositoryName = "repo"; | ||
const string filePath = "file_path"; | ||
const string referenceName = "ref"; | ||
var commandDto = new GetFileCommandDto | ||
{ | ||
RepositoryName = repositoryName, | ||
FilePath = filePath, | ||
ReferenceName = referenceName, | ||
}; | ||
|
||
// Act | ||
await _sut.GetFileAsync(commandDto, default); | ||
|
||
// Assert | ||
await _mediator.Received(1).Send(Arg.Is<GetFileCommand>(x => x.RepositoryName == repositoryName | ||
&& x.FilePath == filePath | ||
&& x.ReferenceName == referenceName)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
Mohaymen.GiteaClient.Tests/Gitea/File/GetFile/Commands/GetFileCommandHandlerTests.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,81 @@ | ||
using FluentAssertions; | ||
using FluentValidation; | ||
using MediatR; | ||
using Microsoft.Extensions.Options; | ||
using Mohaymen.GiteaClient.Core.Configs; | ||
using Mohaymen.GiteaClient.Gitea.File.Common.ApiCall.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands; | ||
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Context; | ||
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos; | ||
using NSubstitute; | ||
using Refit; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.File.GetFile.Commands; | ||
|
||
public class GetFileCommandHandlerTests | ||
{ | ||
private readonly IFileRestClient _fileRestClient; | ||
private readonly IOptions<GiteaApiConfiguration> _options; | ||
private readonly InlineValidator<GetFileCommand> _validator; | ||
private readonly IRequestHandler<GetFileCommand, ApiResponse<GetFileResponseDto>> _sut; | ||
|
||
public GetFileCommandHandlerTests() | ||
{ | ||
_fileRestClient = Substitute.For<IFileRestClient>(); | ||
_options = Substitute.For<IOptions<GiteaApiConfiguration>>(); | ||
_validator = new InlineValidator<GetFileCommand>(); | ||
_sut = new GetFileCommandHandler(_fileRestClient, _options, _validator); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldThrowsValidationException_WhenInputIsInvalid() | ||
{ | ||
// Arrange | ||
_validator.RuleFor(x => x).Must(_ => false); | ||
var command = new GetFileCommand | ||
{ | ||
RepositoryName = "repo", | ||
FilePath = "path" | ||
}; | ||
|
||
// Act | ||
var actual = async () => await _sut.Handle(command, default); | ||
|
||
// Assert | ||
await actual.Should().ThrowAsync<ValidationException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldCallGetFileAsync_AndInputsAreValid() | ||
{ | ||
// Arrange | ||
_validator.RuleFor(x => x).Must(_ => true); | ||
const string owner = "owner"; | ||
const string repositoryName = "repo"; | ||
const string filePath = "file_path"; | ||
const string referenceName = "ref"; | ||
var command = new GetFileCommand | ||
{ | ||
RepositoryName = repositoryName, | ||
FilePath = filePath, | ||
ReferenceName = referenceName | ||
}; | ||
_options.Value.Returns(new GiteaApiConfiguration | ||
{ | ||
BaseUrl = "url", | ||
PersonalAccessToken = "token", | ||
RepositoriesOwner = owner | ||
}); | ||
|
||
// Act | ||
await _sut.Handle(command, default); | ||
|
||
// Assert | ||
await _fileRestClient.Received(1).GetFileAsync(owner, | ||
repositoryName, | ||
filePath, | ||
Arg.Is<GetFileRequest>(x => | ||
x.ReferenceName == referenceName)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
Mohaymen.GiteaClient.Tests/Gitea/File/GetFile/Validators/GetFileCommandValidatorTests.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,76 @@ | ||
using FluentAssertions; | ||
using FluentValidation; | ||
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands; | ||
using Mohaymen.GiteaClient.Gitea.File.GetFile.Validators; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.File.GetFile.Validators; | ||
|
||
public class GetFileCommandValidatorTests | ||
{ | ||
private readonly IValidator<GetFileCommand> _sut; | ||
|
||
public GetFileCommandValidatorTests() | ||
{ | ||
_sut = new GetFileCommandValidator(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void Validate_ShouldReturnEmptyRepositoryNameErrorCode_WhenRepositoryNameIsNullOrEmpty(string repositoryName) | ||
{ | ||
// Arrange | ||
var command = new GetFileCommand | ||
{ | ||
RepositoryName = repositoryName, | ||
FilePath = "filePath" | ||
}; | ||
|
||
// Act | ||
var actual = _sut.Validate(command); | ||
|
||
// Assert | ||
actual.IsValid.Should().BeFalse(); | ||
actual.Errors.Select(x => x.ErrorCode) | ||
.Should().Contain(GetFileErrorCodes.EmptyRepositoryNameErrorCode); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void Validate_ShouldReturnEmptyFilePathErrorCode_WhenFilePathIsNullOrEmpty(string filePath) | ||
{ | ||
// Arrange | ||
var command = new GetFileCommand | ||
{ | ||
RepositoryName = "repo", | ||
FilePath = filePath | ||
}; | ||
|
||
// Act | ||
var actual = _sut.Validate(command); | ||
|
||
// Assert | ||
actual.IsValid.Should().BeFalse(); | ||
actual.Errors.Select(x => x.ErrorCode) | ||
.Should().Contain(GetFileErrorCodes.EmptyFilePathErrorCode); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnValidResult_WhenEveryThingIsProvidedProperly() | ||
{ | ||
// Arrange | ||
var command = new GetFileCommand | ||
{ | ||
RepositoryName = "repo", | ||
FilePath = "path" | ||
}; | ||
|
||
// Act | ||
var actual = _sut.Validate(command); | ||
|
||
// Assert | ||
actual.IsValid.Should().BeTrue(); | ||
} | ||
} |
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