Skip to content

Commit

Permalink
add integration test for Organization Report
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhossein18121380 committed Jun 6, 2024
1 parent bdd78dd commit 7435018
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,9 @@ public async Task<OrganizationReportDto> Handle(GetOrganizationReportByIdRequest
#endregion

#region Board Reports
var boards = new List<GetBoardDto>();
var boardsGrpcCall = _getBoardsByOrganizationIdGrpcServiceClient.Handle(
new GetBoardsByOrganizationIdGrpcRequest { OrganizationId = organization.Id }
);

await foreach (var response in boardsGrpcCall.ResponseStream.ReadAllAsync())
boards.Add(_mapper.Map<GetBoardDto>(response));

organizationReportDto.BoardsCount = boards?.Count ?? 0;
var boards = (await GetBoardsAsync(organization.Id)).ToList();
organizationReportDto.BoardsCount = boards.Any() ? boards.Count : 0;
#endregion

#region Task Reports
Expand Down Expand Up @@ -198,6 +192,19 @@ private async Task<IEnumerable<GetTaskDto>> GetTasksAsync(string cardId)
return tasks;
}

private async Task<IEnumerable<GetBoardDto>> GetBoardsAsync(string organizationId)
{
var boards = new List<GetBoardDto>();
var boardsGrpcCall = _getBoardsByOrganizationIdGrpcServiceClient.Handle(
new GetBoardsByOrganizationIdGrpcRequest { OrganizationId = organizationId }
);

await foreach (var response in boardsGrpcCall.ResponseStream.ReadAllAsync())
boards.Add(_mapper.Map<GetBoardDto>(response));

return boards;
}

private GetCardDto MapToCard(GetCardGrpcResponse cardGrpcResponse)
{
return _mapper.Map<GetCardDto>(cardGrpcResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using AutoMapper;
using Google.Protobuf.WellKnownTypes;
using System;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Boards;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Cards;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Common;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Owners;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Projects;
using TaskoMask.BuildingBlocks.Contracts.Dtos.Tasks;
using TaskoMask.BuildingBlocks.Contracts.Protos;
using TaskoMask.Services.Owners.Read.Api.Domain;

Expand All @@ -23,6 +27,27 @@ public MappingProfile()

CreateMap<Organization, GetOrganizationDto>();

CreateMap<GetBoardGrpcResponse, GetBoardDto>()
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description ?? string.Empty))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name ?? string.Empty))
.ForMember(dest => dest.ProjectId, opt => opt.MapFrom(src => src.ProjectId ?? string.Empty))
.ForMember(dest => dest.OrganizationName, opt => opt.MapFrom(src => src.OrganizationName ?? string.Empty))
.ForMember(dest => dest.ProjectName, opt => opt.MapFrom(src => src.ProjectName ?? string.Empty))
.ForMember(dest => dest.OwnerId, opt => opt.MapFrom(src => src.OwnerId ?? string.Empty))
.ForMember(dest => dest.OrganizationId, opt => opt.MapFrom(src => src.OrganizationId ?? string.Empty));

CreateMap<CreationTimeGrpcResponse, CreationTimeDto>()
.ForMember(dest => dest.CreateDateTime, opt => opt.MapFrom(src => ConvertTimestamp(src.CreateDateTime)))
.ForMember(dest => dest.CreateDateTimeString, opt => opt.MapFrom(src => src.CreateDateTimeString ?? string.Empty))
.ForMember(dest => dest.ModifiedDateTime, opt => opt.MapFrom(src => ConvertTimestamp(src.ModifiedDateTime)))
.ForMember(dest => dest.ModifiedDateTimeString, opt => opt.MapFrom(src => src.ModifiedDateTimeString ?? string.Empty))
.ForMember(dest => dest.CreateDay, opt => opt.MapFrom(src => src.CreateDay))
.ForMember(dest => dest.CreateMonth, opt => opt.MapFrom(src => src.CreateMonth))
.ForMember(dest => dest.CreateYear, opt => opt.MapFrom(src => src.CreateYear));

CreateMap<GetCardGrpcResponse, GetCardDto>();
CreateMap<GetTaskGrpcResponse, GetTaskDto>();

CreateMap<GetOrganizationDto, GetOrganizationGrpcResponse>()
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description ?? string.Empty));

Expand All @@ -31,4 +56,16 @@ public MappingProfile()
CreateMap<GetProjectDto, GetProjectGrpcResponse>()
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description ?? string.Empty));
}

private DateTime? ConvertTimestamp(Timestamp timestamp)
{
try
{
return timestamp.ToDateTime();
}
catch
{
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Moq;
using AutoBogus;
using Grpc.Core;
using Moq;
using TaskoMask.BuildingBlocks.Contracts.Protos;
using TaskoMask.Services.Owners.Read.Api.Features.Organizations.GetOrganizationReportById;
using TaskoMask.Services.Owners.Read.IntegrationTests.Fixtures;
using TaskoMask.Services.Owners.Read.IntegrationTests.TestData;
using Xunit;

namespace TaskoMask.Services.Owners.Read.IntegrationTests.Features.Organizations;
Expand All @@ -11,51 +15,113 @@ public class GetOrganizationReportByIdTests
{
#region Fields
private readonly OrganizationCollectionFixture _fixture;
private readonly Mock<GetBoardsByOrganizationIdGrpcService.GetBoardsByOrganizationIdGrpcServiceClient> _mockBoardsClient;
private readonly Mock<GetCardsByBoardIdGrpcService.GetCardsByBoardIdGrpcServiceClient> _mockCardsClient;
private readonly Mock<GetTasksByCardIdGrpcService.GetTasksByCardIdGrpcServiceClient> _mockTasksClient;

#endregion

#region Ctor
public GetOrganizationReportByIdTests(OrganizationCollectionFixture fixture)
{
_fixture = fixture;
_mockBoardsClient = new Mock<GetBoardsByOrganizationIdGrpcService.GetBoardsByOrganizationIdGrpcServiceClient>();
_mockCardsClient = new Mock<GetCardsByBoardIdGrpcService.GetCardsByBoardIdGrpcServiceClient>();
_mockTasksClient = new Mock<GetTasksByCardIdGrpcService.GetTasksByCardIdGrpcServiceClient>();
}

#endregion

#region Test Methods

//[Fact]
//public async Task OrganizationReport_are_fetched_by_Id()
//{
// //Arrange
// var expectedOrganization = OrganizationObjectMother.GetOrganization();
// await _fixture.SeedOrganizationAsync(expectedOrganization);
// var getOrganizationsReportByIdHandler = new GetOrganizationReportByIdHandler(
// _fixture._dbContext,
// _mockBoardsClient.Object,
// _mockCardsClient.Object,
// _mockTasksClient.Object,
// _fixture._mapper
// );
// var request = new GetOrganizationReportByIdRequest(expectedOrganization.OwnerId);

// //Act
// var result = await getOrganizationsReportByIdHandler.Handle(request, CancellationToken.None);

// //Assert
// Assert.NotNull(result);
// Assert.True(result.ProjectsCount >= 0);
// Assert.True(result.BoardsCount >= 0);
// Assert.True(result.ToDoTasksCount >= 0);
// Assert.True(result.DoingTasksCount >= 0);
// Assert.True(result.DoneTasksCount >= 0);
// Assert.True(result.BacklogTasksCount >= 0);
//}
[Fact]
public async Task OrganizationReport_is_fetched_by_Id()
{
// Arrange
var expectedOrganization = OrganizationObjectMother.GetOrganization();
await _fixture.SeedOrganizationAsync(expectedOrganization);

var dbContext = _fixture._dbContext;
var mapper = _fixture._mapper;

var mockBoardsClient = new Mock<GetBoardsByOrganizationIdGrpcService.GetBoardsByOrganizationIdGrpcServiceClient>();
var mockCardsClient = new Mock<GetCardsByBoardIdGrpcService.GetCardsByBoardIdGrpcServiceClient>();
var mockTasksClient = new Mock<GetTasksByCardIdGrpcService.GetTasksByCardIdGrpcServiceClient>();

// Setup mock responses
var fakeBoardsResponse = new AutoFaker<GetBoardGrpcResponse>().Generate();
var fakeCardsResponse = new AutoFaker<GetCardGrpcResponse>().Generate();
var fakeTasksResponse = new AutoFaker<GetTaskGrpcResponse>().Generate();


// Create AsyncServerStreamingCall<TResponse> objects for the mocked responses
var fakeBoardsAsyncResponse = new AsyncServerStreamingCall<GetBoardGrpcResponse>(
GetAsyncStreamReader(fakeBoardsResponse),
Task.FromResult(new Metadata()),
() => Status.DefaultSuccess,
() => new Metadata(),
() => { }
);


var fakeCardsAsyncResponse = new AsyncServerStreamingCall<GetCardGrpcResponse>(
GetAsyncStreamReader(fakeCardsResponse),
Task.FromResult(new Metadata()),
() => Status.DefaultSuccess,
() => new Metadata(),
() => { }
);

var fakeTasksAsyncResponse = new AsyncServerStreamingCall<GetTaskGrpcResponse>(
GetAsyncStreamReader(fakeTasksResponse),
Task.FromResult(new Metadata()),
() => Status.DefaultSuccess,
() => new Metadata(),
() => { }
);

mockBoardsClient.Setup(client => client.Handle(It.IsAny<GetBoardsByOrganizationIdGrpcRequest>(), null, null, CancellationToken.None))
.Returns(fakeBoardsAsyncResponse);

mockCardsClient.Setup(client => client.Handle(It.IsAny<GetCardsByBoardIdGrpcRequest>(), null, null, CancellationToken.None))
.Returns(fakeCardsAsyncResponse);

mockTasksClient.Setup(client => client.Handle(It.IsAny<GetTasksByCardIdGrpcRequest>(), null, null, CancellationToken.None))
.Returns(fakeTasksAsyncResponse);


var handler = new GetOrganizationReportByIdHandler(
dbContext,
mockBoardsClient.Object,
mockCardsClient.Object,
mockTasksClient.Object,
mapper
);

var request = new GetOrganizationReportByIdRequest(expectedOrganization.Id);

// Act
var result = await handler.Handle(request, CancellationToken.None);

// Assert
Assert.NotNull(result);
Assert.True(result.ProjectsCount >= 0);
Assert.True(result.BoardsCount >= 0);
Assert.True(result.ToDoTasksCount >= 0);
Assert.True(result.DoingTasksCount >= 0);
Assert.True(result.DoneTasksCount >= 0);
Assert.True(result.BacklogTasksCount >= 0);
}


#endregion

#region Methods
private static IAsyncStreamReader<T> GetAsyncStreamReader<T>(params T[] responses)
{
var mockStreamReader = new Mock<IAsyncStreamReader<T>>();
var queue = new Queue<T>(responses);

mockStreamReader.Setup(r => r.MoveNext(It.IsAny<CancellationToken>()))
.ReturnsAsync(() => queue.Count > 0);

mockStreamReader.Setup(r => r.Current).Returns(() => queue.Dequeue());

return mockStreamReader.Object;
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TaskoMask.BuildingBlocks.Application.Bus;
using TaskoMask.BuildingBlocks.Test.TestBase;
using TaskoMask.Services.Owners.Read.Api.Infrastructure.DbContext;
using TaskoMask.Services.Owners.Read.Api.Infrastructure.DI;
Expand All @@ -11,12 +12,14 @@ public abstract class TestsBaseFixture : IntegrationTestsBase
{
public readonly IMapper _mapper;
public readonly OwnerReadDbContext _dbContext;
public readonly IInMemoryBus _inMemoryBus;

protected TestsBaseFixture(string dbNameSuffix)
: base(dbNameSuffix)
{
_mapper = GetRequiredService<IMapper>();
_dbContext = GetRequiredService<OwnerReadDbContext>();
_inMemoryBus = GetRequiredService<IInMemoryBus>();
}

/// <summary>
Expand All @@ -27,6 +30,7 @@ public override void InitialDatabase()
_serviceProvider.InitialDatabase();
}


/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoBogus" Version="2.13.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down

0 comments on commit 7435018

Please sign in to comment.