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

fix: add tracing #37

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
371 changes: 371 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions Directory.Packages.props
alireza13811 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageVersion Include="Refit" Version="7.1.2" />
<PackageVersion Include="Refit.HttpClientFactory" Version="7.1.2" />
<PackageVersion Include="Refit.Newtonsoft.Json" Version="7.1.2" />
<PackageVersion Include="Riok.Mapperly" Version="3.6.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="MediatR" Version="12.0.0" />
<PackageVersion Include="FluentValidation" Version="11.9.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageVersion Include="xunit" Version="2.5.3"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="Testcontainers" Version="3.9.0"/>
<PackageVersion Include="PolySharp" Version="1.13.1"/>
</ItemGroup>
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Include="FluentValidation" Version="11.9.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageVersion Include="MediatR" Version="12.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="OpenTelemetry.Api" Version="1.10.0" />
<PackageVersion Include="PolySharp" Version="1.13.1" />
<PackageVersion Include="Refit" Version="7.1.2" />
<PackageVersion Include="Refit.HttpClientFactory" Version="7.1.2" />
<PackageVersion Include="Refit.Newtonsoft.Json" Version="7.1.2" />
<PackageVersion Include="Riok.Mapperly" Version="3.6.0" />
<PackageVersion Include="Testcontainers" Version="3.9.0" />
<PackageVersion Include="xunit" Version="2.5.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MediatR;
using System.Diagnostics;
using MediatR;
using Mohaymen.GiteaClient.Commons.Observability.Abstraction;
using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade;
using Mohaymen.GiteaClient.Gitea.Branch.Common.Facade.Abstractions;
using Mohaymen.GiteaClient.Gitea.Branch.CreateBranch.Commands;
Expand All @@ -12,52 +14,54 @@ namespace Mohaymen.GiteaClient.Tests.Gitea.Branch.Common.Facade;

public class BranchFacadeTests
{
private readonly IMediator _mediator;
private readonly IBranchFacade _sut;

public BranchFacadeTests()
{
_mediator = Substitute.For<IMediator>();
_sut = new BranchFacade(_mediator);
}

[Fact]
public async Task CreateBranchAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "repo";
const string newBranchName = "new_branch";
const string oldReferenceName = "old_ref";
var commandDto = new CreateBranchCommandDto
{
RepositoryName = repositoryName,
NewBranchName = newBranchName,
OldReferenceName = oldReferenceName
};

// Act
await _sut.CreateBranchAsync(commandDto, default);

// Assert
await _mediator.Received(1).Send(Arg.Is<CreateBranchCommand>(x => x.RepositoryName == repositoryName
&& 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));
}
}
private readonly IMediator _mediator;
private readonly IBranchFacade _sut;

public BranchFacadeTests()
{
_mediator = Substitute.For<IMediator>();
var traceInstrumentation = Substitute.For<ITraceInstrumentation>();
_sut = new BranchFacade(_mediator, traceInstrumentation);
traceInstrumentation.ActivitySource.Returns(new ActivitySource("test"));
}

[Fact]
public async Task CreateBranchAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "repo";
const string newBranchName = "new_branch";
const string oldReferenceName = "old_ref";
var commandDto = new CreateBranchCommandDto
{
RepositoryName = repositoryName,
NewBranchName = newBranchName,
OldReferenceName = oldReferenceName
};

// Act
await _sut.CreateBranchAsync(commandDto, default);

// Assert
await _mediator.Received(1).Send(Arg.Is<CreateBranchCommand>(x => x.RepositoryName == repositoryName
&& 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
@@ -1,4 +1,6 @@
using MediatR;
using System.Diagnostics;
using MediatR;
using Mohaymen.GiteaClient.Commons.Observability.Abstraction;
using Mohaymen.GiteaClient.Gitea.Commit.Common.Facades;
using Mohaymen.GiteaClient.Gitea.Commit.Common.Facades.Abstractions;
using Mohaymen.GiteaClient.Gitea.Commit.CreateCommit.Commands;
Expand All @@ -12,114 +14,116 @@ namespace Mohaymen.GiteaClient.Tests.Gitea.Commit.Common.Facades;

public class CommitFacadeTests
{
private readonly IMediator _mediator;
private readonly ICommitFacade _sut;
private readonly IMediator _mediator;
private readonly ICommitFacade _sut;

public CommitFacadeTests()
{
_mediator = Substitute.For<IMediator>();
_sut = new CommitFacade(_mediator);
}
public CommitFacadeTests()
{
_mediator = Substitute.For<IMediator>();
var traceInstrumentation = Substitute.For<ITraceInstrumentation>();
_sut = new CommitFacade(_mediator, traceInstrumentation);
traceInstrumentation.ActivitySource.Returns(new ActivitySource("test"));
}

[Fact]
public async Task LoadBranchCommitsAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "fakeRepoName";
const string branchName = "fakeBranchName";
const int page = 1;
const int limit = 5;
var queryDto = new LoadBranchCommitsQueryDto
{
RepositoryName = repositoryName,
BranchName = branchName,
Limit = limit,
Page = page
};
[Fact]
public async Task LoadBranchCommitsAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "fakeRepoName";
const string branchName = "fakeBranchName";
const int page = 1;
const int limit = 5;
var queryDto = new LoadBranchCommitsQueryDto
{
RepositoryName = repositoryName,
BranchName = branchName,
Limit = limit,
Page = page
};

// Act
await _sut.LoadBranchCommitsAsync(queryDto, default);
// Assert
await _mediator.Send(Arg.Is<LoadBranchCommitsQuery>(x => x.RepositoryName.Equals(repositoryName)
&& x.BranchName.Equals(branchName)
&& x.Page == page
&& x.Limit == limit), default);
}
// Act
await _sut.LoadBranchCommitsAsync(queryDto, default);
// Assert
await _mediator.Send(Arg.Is<LoadBranchCommitsQuery>(x => x.RepositoryName.Equals(repositoryName)
&& x.BranchName.Equals(branchName)
&& x.Page == page
&& x.Limit == limit), default);
}

[Fact]
public async Task CreateCommitAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "fakeRepoName";
const string branchName = "fakeBranchName";
const string commitMessage = "fakeMessage";
const string fakePath1 = "path1";
const string fakePath2 = "path2";
const string fakePath3 = "path3";
const string fakeContent1 = "content1";
const string fakeContent2 = "content2";
const string fakeContent3 = "content3";
const CommitActionDto operation1 = CommitActionDto.Create;
const CommitActionDto operation2 = CommitActionDto.Update;
const CommitActionDto operation3 = CommitActionDto.Delete;
var fileDtos = new List<FileCommitDto>()
{
new()
{
Path = fakePath1,
Content = fakeContent1,
CommitActionDto = operation1
},
new()
{
Path = fakePath2,
Content = fakeContent2,
CommitActionDto = operation2
},
new()
{
Path = fakePath3,
Content = fakeContent3,
CommitActionDto = operation3
}
};
var commandDto = new CreateCommitCommandDto
{
RepositoryName = repositoryName,
BranchName = branchName,
CommitMessage = commitMessage,
FileDtos = fileDtos
};
var expectedFileCommitCommandModel1 = new FileCommitCommandModel
{
Path = fakePath1,
Content = fakeContent1,
CommitActionCommand = CommitActionCommand.Create
};
var expectedFileCommitCommandModel2 = new FileCommitCommandModel
{
Path = fakePath2,
Content = fakeContent2,
CommitActionCommand = CommitActionCommand.Update
};
var expectedFileCommitCommandModel3 = new FileCommitCommandModel
{
Path = fakePath3,
Content = fakeContent3,
CommitActionCommand = CommitActionCommand.Delete
};
[Fact]
public async Task CreateCommitAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "fakeRepoName";
const string branchName = "fakeBranchName";
const string commitMessage = "fakeMessage";
const string fakePath1 = "path1";
const string fakePath2 = "path2";
const string fakePath3 = "path3";
const string fakeContent1 = "content1";
const string fakeContent2 = "content2";
const string fakeContent3 = "content3";
const CommitActionDto operation1 = CommitActionDto.Create;
const CommitActionDto operation2 = CommitActionDto.Update;
const CommitActionDto operation3 = CommitActionDto.Delete;
var fileDtos = new List<FileCommitDto>()
{
new()
{
Path = fakePath1,
Content = fakeContent1,
CommitActionDto = operation1
},
new()
{
Path = fakePath2,
Content = fakeContent2,
CommitActionDto = operation2
},
new()
{
Path = fakePath3,
Content = fakeContent3,
CommitActionDto = operation3
}
};
var commandDto = new CreateCommitCommandDto
{
RepositoryName = repositoryName,
BranchName = branchName,
CommitMessage = commitMessage,
FileDtos = fileDtos
};
var expectedFileCommitCommandModel1 = new FileCommitCommandModel
{
Path = fakePath1,
Content = fakeContent1,
CommitActionCommand = CommitActionCommand.Create
};
var expectedFileCommitCommandModel2 = new FileCommitCommandModel
{
Path = fakePath2,
Content = fakeContent2,
CommitActionCommand = CommitActionCommand.Update
};
var expectedFileCommitCommandModel3 = new FileCommitCommandModel
{
Path = fakePath3,
Content = fakeContent3,
CommitActionCommand = CommitActionCommand.Delete
};

// Act
await _sut.CreateCommitAsync(commandDto, default);
// Act
await _sut.CreateCommitAsync(commandDto, default);

// Assert
await _mediator.Received(1).Send(Arg.Is<CreateCommitCommand>(x => x.RepositoryName == repositoryName
&& x.BranchName.Equals(branchName)
&& x.CommitMessage.Equals(commitMessage)
&& x.FileCommitCommands.Count == 3
&& x.FileCommitCommands[0].Equals(expectedFileCommitCommandModel1)
&& x.FileCommitCommands[1].Equals(expectedFileCommitCommandModel2)
&& x.FileCommitCommands[2].Equals(expectedFileCommitCommandModel3)),
default);
}
}
// Assert
await _mediator.Received(1).Send(Arg.Is<CreateCommitCommand>(x => x.RepositoryName == repositoryName
&& x.BranchName.Equals(branchName)
&& x.CommitMessage.Equals(commitMessage)
&& x.FileCommitCommands.Count == 3
&& x.FileCommitCommands[0].Equals(expectedFileCommitCommandModel1)
&& x.FileCommitCommands[1].Equals(expectedFileCommitCommandModel2)
&& x.FileCommitCommands[2].Equals(expectedFileCommitCommandModel3)),
default);
}
}
Loading
Loading