Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…hase-starter-projects into aait.mobile.g1.fikremariam-fikadu.Article-imageUpload
  • Loading branch information
fikremariamF committed Sep 4, 2023
2 parents c542540 + 732bbc7 commit c7f0aa7
Show file tree
Hide file tree
Showing 709 changed files with 19,900 additions and 5,254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Features\PostFeatureTest\Queries\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Application.DTO.CommentDTO.DTO;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using Shouldly;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Exceptions;
using MediatR;
using Moq;
using Application.Contracts;
using Application.Features.NotificationFeaure.Requests.Commands;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class CreateCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
private CommentCreateDTO testComment;
public CreateCommentHandlerTest()
{

_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
_mediator = new Mock<IMediator>();

testComment = new CommentCreateDTO()
{
PostId = 1,
Content = "Test comment"
};
}

[Fact]
public async Task ValidCreateCommentTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );

// act
var result = await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None);

// assert
result.ShouldBeOfType<BaseResponse<CommentResponseDTO>>();
}

[Fact]
public async Task CreateCommentForNonExistentPostTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );
testComment.PostId = 100;

// act and assert
await Should.ThrowAsync<NotFoundException>(async () =>
await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None));
}
[Fact]
public async Task CreateCommentWithNoContentTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );
testComment.Content = "";

// act and arrange

await Should.ThrowAsync<ValidationException>(async () =>
await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Application.Contracts;
using Application.Exceptions;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Response;
using Application.Tests.Mocks;
using MediatR;
using Moq;
using Shouldly;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class DeleteCommentHandlerTest
{
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
public DeleteCommentHandlerTest()
{
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();
_mediator = new Mock<IMediator>();

}

[Fact]
public async Task DeleteCommentValidTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var handler = new CommentDeleteHandler(_mockUnitOfWork.Object, _mediator.Object);

var result = await handler.Handle(new CommentDeleteCommand() { Id = 1 }, CancellationToken.None);

result.ShouldBeOfType<BaseResponse<int>>();
}
[Fact]
public async Task DeleteCommentWithInValidIdTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var handler = new CommentDeleteHandler(_mockUnitOfWork.Object, _mediator.Object);

await Should.ThrowAsync<NotFoundException>(async () =>
await handler.Handle(new CommentDeleteCommand(), CancellationToken.None));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.Exceptions;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using MediatR;
using Moq;
using Shouldly;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class UpdateCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
CommentUpdateDTO testComment;
public UpdateCommentHandlerTest()
{

_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
_mediator = new Mock<IMediator>();

testComment = new CommentUpdateDTO()
{
Content = "Test comment"
};
}

[Fact]
public async Task UpdateCommentValidTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "Updated comment";
var result = await handler.Handle(new UpdateCommentCommand()
{
Id = 1,
userId = 1,
CommentData = testComment

}, CancellationToken.None);

result.ShouldBeOfType<BaseResponse<CommentResponseDTO>>();
}

[Fact]
public async Task UpdateCommentWithNoContentTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "";
await Should.ThrowAsync<ValidationException>(async () => await handler.Handle(new UpdateCommentCommand()
{
Id = 1,
userId = 1,
CommentData = testComment

}, CancellationToken.None));

}
[Fact]
public async Task UpdateNonExistentCommentTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "Updated comment";
await Should.ThrowAsync<NotFoundException>(async () => await handler.Handle(new UpdateCommentCommand()
{
Id = 100,
userId = 1,
CommentData = testComment

}, CancellationToken.None));

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.Exceptions;
using Application.Features.CommentFeatures.Handlers.Queries;
using Application.Features.CommentFeatures.Requests.Queries;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using Moq;
using Shouldly;
using Xunit;

namespace Application.Tests.Features.CommentFeatureTest.Queries
{
public class GetCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

public GetCommentHandlerTest()
{
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
}

[Fact]
public async Task GetValidCommentTest()
{
var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new GetCommentQueryHandler(_mockUnitOfWork.Object, _mapper);
var result = await handler.Handle(new GetSingleCommentQuery()
{
Id = 1
}, CancellationToken.None);

result.ShouldBeOfType<BaseResponse<CommentResponseDTO>>();
}
[Fact]
public async Task GetInValidCommentTest()
{
var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new GetCommentQueryHandler(_mockUnitOfWork.Object, _mapper);

await Should.ThrowAsync<BadRequestException>(async () => await handler.Handle(new GetSingleCommentQuery()
{
Id = 100
}, CancellationToken.None));
}
}
}
Loading

0 comments on commit c7f0aa7

Please sign in to comment.