Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using AutoMapper;

Check warning on line 1 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsiY&open=AZ4d6-h4G_SzeXAESsiY&pullRequest=49
using FluentAssertions;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using Streetcode.BLL.DTO.Team;

Check warning on line 5 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsic&open=AZ4d6-h4G_SzeXAESsic&pullRequest=49
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.MediatR.Team.GetAll;
using Streetcode.DAL.Entities.Team;

Check warning on line 8 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsif&open=AZ4d6-h4G_SzeXAESsif&pullRequest=49
using Streetcode.DAL.Repositories.Interfaces.Base;
using System.Linq.Expressions;
using Xunit;

namespace Streetcode.XUnitTest.MediatR.Team.GetAll;

public class GetAllMainTeamHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;
private readonly IMapper _mapper;

Check warning on line 18 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mapper' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsil&open=AZ4d6-h4G_SzeXAESsil&pullRequest=49
private readonly Mock<ILoggerService> _mockLogger;
private readonly GetAllMainTeamHandler _handler;

public GetAllMainTeamHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();
this._mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TeamMember, TeamMemberDTO>();
}).CreateMapper();
_mockLogger = new Mock<ILoggerService>();
_handler = new GetAllMainTeamHandler(_mockRepo.Object, _mapper, _mockLogger.Object);

Check warning on line 30 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsiu&open=AZ4d6-h4G_SzeXAESsiu&pullRequest=49
}

[Fact]
public async Task Handle_ReturnsOkResult_WhenMainTeamExists()
{
var mainTeamEntities = new List<TeamMember>
{
new TeamMember { Id = 1, FirstName = "John", LastName = "Doe", IsMain = true },
new TeamMember { Id = 2, FirstName = "Jane", LastName = "Smith", IsMain = true }

Check warning on line 39 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use trailing comma in multi-line initializers

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsiy&open=AZ4d6-h4G_SzeXAESsiy&pullRequest=49
};
var mainTeamDTOs = mainTeamEntities.Select(tm => this._mapper.Map<TeamMemberDTO>(tm)).ToList();

_mockRepo.Setup(
repo => repo.TeamRepository.GetAllAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)
).ReturnsAsync(mainTeamEntities);

var result = await _handler.Handle(new GetAllMainTeamQuery(), CancellationToken.None);

Check warning on line 50 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsi0&open=AZ4d6-h4G_SzeXAESsi0&pullRequest=49
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeEquivalentTo(mainTeamDTOs);
}

[Fact]
public async Task Handle_ReturnsFailResult_WhenNoMainTeamExists()
{
_mockRepo.Setup(

Check warning on line 58 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsi3&open=AZ4d6-h4G_SzeXAESsi3&pullRequest=49
repo => repo.TeamRepository.GetAllAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)
).ReturnsAsync((IEnumerable<TeamMember>)null);

Check warning on line 63 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Argument of type 'ISetup<IRepositoryWrapper, Task<IEnumerable<TeamMember>>>' cannot be used for parameter 'mock' of type 'IReturns<IRepositoryWrapper, Task<IEnumerable<TeamMember>?>>' in 'IReturnsResult<IRepositoryWrapper> ReturnsExtensions.ReturnsAsync<IRepositoryWrapper, IEnumerable<TeamMember>?>(IReturns<IRepositoryWrapper, Task<IEnumerable<TeamMember>?>> mock, IEnumerable<TeamMember>? value)' due to differences in the nullability of reference types.

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsiX&open=AZ4d6-h4G_SzeXAESsiX&pullRequest=49

Check warning on line 63 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should be on line of last parameter

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsi5&open=AZ4d6-h4G_SzeXAESsi5&pullRequest=49

Check warning on line 63 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Converting null literal or possible null value to non-nullable type.

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsiW&open=AZ4d6-h4G_SzeXAESsiW&pullRequest=49
var result = await _handler.Handle(new GetAllMainTeamQuery(), CancellationToken.None);

Check warning on line 64 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllMainTeamQueryTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-h4G_SzeXAESsi4&open=AZ4d6-h4G_SzeXAESsi4&pullRequest=49
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "Cannot find any team");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using AutoMapper;

Check warning on line 1 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsjq&open=AZ4d6-iNG_SzeXAESsjq&pullRequest=49
using FluentAssertions;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using Streetcode.BLL.DTO.Team;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.MediatR.Team.GetAll;
using Streetcode.DAL.Entities.Team;
using Streetcode.DAL.Repositories.Interfaces.Base;
using Streetcode.DAL.Repositories.Interfaces.Team;
using System.Linq.Expressions;
using Xunit;

Check warning on line 12 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj1&open=AZ4d6-iNG_SzeXAESsj1&pullRequest=49

namespace Streetcode.XUnitTest.MediatR.Team.GetAll;

public class GetAllTeamHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;
private readonly Mock<ITeamRepository> _mockTeamRepo;

Check warning on line 19 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mockTeamRepo' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj4&open=AZ4d6-iNG_SzeXAESsj4&pullRequest=49
private readonly IMapper _mapper;

Check warning on line 20 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mapper' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj5&open=AZ4d6-iNG_SzeXAESsj5&pullRequest=49
private readonly Mock<ILoggerService> _mockLogger;
private readonly GetAllTeamHandler _handler;

Check warning on line 22 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_handler' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj7&open=AZ4d6-iNG_SzeXAESsj7&pullRequest=49

public GetAllTeamHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();
_mockTeamRepo = new Mock<ITeamRepository>();

this._mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TeamMember, TeamMemberDTO>();
}).CreateMapper();

_mockLogger = new Mock<ILoggerService>();

Check warning on line 34 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj-&open=AZ4d6-iNG_SzeXAESsj-&pullRequest=49

_mockRepo.Setup(x => x.TeamRepository).Returns(_mockTeamRepo.Object);

Check warning on line 36 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESsj_&open=AZ4d6-iNG_SzeXAESsj_&pullRequest=49
_handler = new GetAllTeamHandler(_mockRepo.Object, _mapper, _mockLogger.Object);
}

[Fact]
public async Task Handle_ReturnsOkResult_WhenTeamExists()
{
var teamEntities = new List<TeamMember>
{
new TeamMember { Id = 1, FirstName = "John", LastName = "Doe" },
new TeamMember { Id = 2, FirstName = "Jane", LastName = "Smith" }
};
var teamDTOs = new List<TeamMemberDTO>
{
this._mapper.Map<TeamMemberDTO>(teamEntities.ElementAt(0)),
this._mapper.Map<TeamMemberDTO>(teamEntities.ElementAt(1)),
};

_mockTeamRepo.Setup(
repo => repo.GetAllAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)
).ReturnsAsync(teamEntities);

Check warning on line 59 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should be on line of last parameter

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESskR&open=AZ4d6-iNG_SzeXAESskR&pullRequest=49

var query = new GetAllTeamQuery();

var result = await _handler.Handle(query, CancellationToken.None);

result.IsSuccess.Should().BeTrue();
result.Value.Should().BeEquivalentTo(teamDTOs);
}

[Fact]
public async Task Handle_ReturnsFailResult_WhenNoTeamExists()
{
_mockTeamRepo.Setup(
repo => repo.GetAllAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)

Check warning on line 76 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetAllTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should not be preceded by a space.

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iNG_SzeXAESskH&open=AZ4d6-iNG_SzeXAESskH&pullRequest=49
).ReturnsAsync((IEnumerable<TeamMember>)null);
var query = new GetAllTeamQuery();

var result = await _handler.Handle(query, CancellationToken.None);

result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "Cannot find any team");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using AutoMapper;
using FluentAssertions;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using Streetcode.BLL.DTO.Team;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.MediatR.Team.GetAll;
using Streetcode.BLL.MediatR.Team.GetById;
using Streetcode.DAL.Entities.Team;

Check warning on line 9 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjE&open=AZ4d6-iDG_SzeXAESsjE&pullRequest=49
using Streetcode.DAL.Repositories.Interfaces.Base;
using Streetcode.DAL.Repositories.Interfaces.Team;
using System.Linq.Expressions;

Check warning on line 12 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive for 'System.Linq.Expressions' should appear before directive for 'Streetcode.DAL.Repositories.Interfaces.Team'

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjJ&open=AZ4d6-iDG_SzeXAESsjJ&pullRequest=49
using Xunit;

namespace Streetcode.XUnitTest.BLL.MediatR.Team
{
public class GetByIdTeamHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;

Check warning on line 19 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mockRepo' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjK&open=AZ4d6-iDG_SzeXAESsjK&pullRequest=49
private readonly Mock<ITeamRepository> _mockTeamRepo;
private readonly IMapper _mapper;
private readonly Mock<ILoggerService> _mockLogger;
private readonly GetByIdTeamHandler _handler;

Check warning on line 23 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_handler' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjO&open=AZ4d6-iDG_SzeXAESsjO&pullRequest=49
public GetByIdTeamHandlerTests()

Check warning on line 24 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Elements should be separated by blank line

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjP&open=AZ4d6-iDG_SzeXAESsjP&pullRequest=49
{
_mockRepo = new Mock<IRepositoryWrapper>();

Check warning on line 26 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjX&open=AZ4d6-iDG_SzeXAESsjX&pullRequest=49
_mockTeamRepo = new Mock<ITeamRepository>();
this._mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TeamMember, TeamMemberDTO>();
}).CreateMapper();
_mockLogger = new Mock<ILoggerService>();
_mockRepo.Setup(x => x.TeamRepository).Returns(_mockTeamRepo.Object);
_handler = new GetByIdTeamHandler(_mockRepo.Object, _mapper, _mockLogger.Object);

Check warning on line 34 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjd&open=AZ4d6-iDG_SzeXAESsjd&pullRequest=49
}


[Fact]
public async Task Handle_ReturnsOkResult_WhenTeamExists()
{
var teamEntity = new TeamMember { Id = 1, FirstName = "John", LastName = "Doe" };
var teamDTO = this._mapper.Map<TeamMemberDTO>(teamEntity);
_mockTeamRepo.Setup(
repo => repo.GetSingleOrDefaultAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)

Check warning on line 47 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should not be preceded by a space.

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjS&open=AZ4d6-iDG_SzeXAESsjS&pullRequest=49
).ReturnsAsync(teamEntity);
var result = await _handler.Handle(new GetByIdTeamQuery(1), CancellationToken.None);
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeEquivalentTo(teamDTO);
}

[Fact]
public async Task Handle_ReturnsFailResult_WhenTeamDoesNotExist()
{
_mockTeamRepo.Setup(
repo => repo.GetSingleOrDefaultAsync(
It.IsAny<Expression<Func<TeamMember, bool>>>(),
It.IsAny<Func<IQueryable<TeamMember>, IIncludableQueryable<TeamMember, object>>>()
)
).ReturnsAsync((TeamMember)null);

Check warning on line 62 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/GetByIdTeamHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should be on line of last parameter

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-iDG_SzeXAESsjm&open=AZ4d6-iDG_SzeXAESsjm&pullRequest=49
var result = await _handler.Handle(new GetByIdTeamQuery(1), CancellationToken.None);
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message.Contains("Cannot find"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using AutoMapper;
using FluentAssertions;
using Moq;
using Streetcode.BLL.DTO.Team;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.MediatR.Team.Create;
using Streetcode.DAL.Entities.Team;
using Streetcode.DAL.Repositories.Interfaces.Base;

Check warning on line 8 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsh7&open=AZ4d6-htG_SzeXAESsh7&pullRequest=49
using Streetcode.DAL.Repositories.Interfaces.Team;
using Xunit;

namespace Streetcode.XUnitTest.BLL.MediatR.Team.Position
{
public class CreatePositionHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;
private readonly Mock<ITeamRepository> _mockTeamRepo;
private readonly IMapper _mapper;
private readonly Mock<ILoggerService> _mockLogger;
private readonly CreatePositionHandler _handler;

public CreatePositionHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();
_mockTeamRepo = new Mock<ITeamRepository>();
this._mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Positions, PositionDTO>();
cfg.CreateMap<PositionDTO, Positions>();
}).CreateMapper();
_mockLogger = new Mock<ILoggerService>();

Check warning on line 31 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiG&open=AZ4d6-htG_SzeXAESsiG&pullRequest=49
_mockRepo.Setup(x => x.TeamRepository).Returns(_mockTeamRepo.Object);
_handler = new CreatePositionHandler(_mapper, _mockRepo.Object, _mockLogger.Object);

Check warning on line 33 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiJ&open=AZ4d6-htG_SzeXAESsiJ&pullRequest=49
}

[Fact]
public async Task Handle_ShouldCreatePosition_WhenDataIsValid()
{
var positionDTO = new PositionDTO { Position = "Developer" };
var positionEntity = new Positions { Id = 1, Position = "Developer" };

_mockRepo.Setup(repo => repo.PositionRepository.CreateAsync(It.IsAny<Positions>()))

Check warning on line 42 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiN&open=AZ4d6-htG_SzeXAESsiN&pullRequest=49
.ReturnsAsync(positionEntity);

_mockRepo.Setup(repo => repo.SaveChanges()).Returns(1);

var result = await _handler.Handle(new CreatePositionQuery(positionDTO), CancellationToken.None);

Check warning on line 47 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiP&open=AZ4d6-htG_SzeXAESsiP&pullRequest=49

result.IsSuccess.Should().BeTrue();
result.Value.Should().NotBeNull();
result.Value.Position.Should().Be(positionDTO.Position);

_mockRepo.Verify(repo => repo.SaveChanges(), Times.Once);
}

[Fact]
public async Task Handle_ShouldReturnFailure_WhenSaveChangesThrowsException()
{

var positionDTO = new PositionDTO { Position = "Developer" };
var positionEntity = new Positions { Id = 1, Position = "Developer" };
_mockRepo.Setup(repo => repo.PositionRepository.CreateAsync(It.IsAny<Positions>()))

Check warning on line 62 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix local calls with this

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiS&open=AZ4d6-htG_SzeXAESsiS&pullRequest=49
.ReturnsAsync(positionEntity);

Check warning on line 64 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Code should not contain trailing whitespace

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiA&open=AZ4d6-htG_SzeXAESsiA&pullRequest=49
_mockRepo.Setup(repo => repo.SaveChanges()).Throws(new Exception("Database error"));

var result = await _handler.Handle(new CreatePositionQuery(positionDTO), CancellationToken.None);

Check warning on line 68 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/CreatePositionHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Code should not contain trailing whitespace

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-htG_SzeXAESsiC&open=AZ4d6-htG_SzeXAESsiC&pullRequest=49
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "Database error");

_mockRepo.Verify(repo => repo.SaveChanges(), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using AutoMapper;
using FluentAssertions;
using Microsoft.EntityFrameworkCore.Query;
using Moq;

Check warning on line 4 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/GetAllPositionsHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive should appear within a namespace declaration

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-hiG_SzeXAESshH&open=AZ4d6-hiG_SzeXAESshH&pullRequest=49
using Streetcode.BLL.DTO.Team;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.MediatR.Team.Position.GetAll;
using Streetcode.DAL.Entities.Team;
using Streetcode.DAL.Repositories.Interfaces.Base;
using Streetcode.DAL.Repositories.Interfaces.Team;
using System.Linq.Expressions;

Check warning on line 11 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/GetAllPositionsHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Using directive for 'System.Linq.Expressions' should appear before directive for 'Streetcode.DAL.Repositories.Interfaces.Team'

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-hiG_SzeXAESshQ&open=AZ4d6-hiG_SzeXAESshQ&pullRequest=49
using Xunit;

namespace Streetcode.XUnitTest.BLL.MediatR.Team.Position
{
public class GetAllPositionsHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;
private readonly Mock<IPositionRepository> _mockPositionRepo;
private readonly IMapper _mapper;

Check warning on line 20 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/GetAllPositionsHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mapper' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-hiG_SzeXAESshT&open=AZ4d6-hiG_SzeXAESshT&pullRequest=49
private readonly Mock<ILoggerService> _mockLogger;
private readonly GetAllPositionsHandler _handler;

Check warning on line 22 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/GetAllPositionsHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_handler' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-hiG_SzeXAESshV&open=AZ4d6-hiG_SzeXAESshV&pullRequest=49

public GetAllPositionsHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();
_mockPositionRepo = new Mock<IPositionRepository>();

_mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Positions, PositionDTO>();
}).CreateMapper();

_mockLogger = new Mock<ILoggerService>();

_mockRepo.Setup(x => x.PositionRepository).Returns(_mockPositionRepo.Object);

_handler = new GetAllPositionsHandler(_mockRepo.Object, _mapper, _mockLogger.Object);
}

[Fact]
public async Task Handle_ShouldReturnPositions_WhenPositionsExist()
{
var positions = new List<Positions>
{
new Positions { Id = 1, Position = "Position 1" },
new Positions { Id = 2, Position = "Position 2" }
};

_mockPositionRepo.Setup(repo => repo.GetAllAsync(
It.IsAny<Expression<Func<Positions, bool>>>(),
It.IsAny<Func<IQueryable<Positions>, IIncludableQueryable<Positions, object>>>()
)).ReturnsAsync(positions);

Check warning on line 53 in Streetcode/Streetcode.XUnitTest/BLL/MediatR/Team/Position/GetAllPositionsHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Closing parenthesis should be on line of last parameter

See more on https://sonarcloud.io/project/issues?id=ita-social-projects_StreetCode&issues=AZ4d6-hiG_SzeXAESshr&open=AZ4d6-hiG_SzeXAESshr&pullRequest=49

var result = await _handler.Handle(new GetAllPositionsQuery(), CancellationToken.None);

result.IsSuccess.Should().BeTrue();
result.Value.Should().HaveCount(2);
result.Value.Should().BeEquivalentTo(_mapper.Map<IEnumerable<PositionDTO>>(positions));
}

[Fact]
public async Task Handle_ShouldReturnFailure_WhenNoPositionsExist()
{
_mockPositionRepo.Setup(repo => repo.GetAllAsync(
It.IsAny<Expression<Func<Positions, bool>>>(),
It.IsAny<Func<IQueryable<Positions>, IIncludableQueryable<Positions, object>>>()
)).ReturnsAsync((IEnumerable<Positions>)null);
var result = await _handler.Handle(new GetAllPositionsQuery(), CancellationToken.None);
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "Cannot find any positions");
}
}
}
Loading
Loading