Skip to content
Open
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,121 @@
using AutoMapper;
using FluentAssertions;
using MediatR;
using Moq;
using Streetcode.BLL.DTO.AdditionalContent.Coordinates.Types;
using Streetcode.BLL.MediatR.AdditionalContent.Coordinate.Create;
using Streetcode.BLL.Mapping.AdditionalContent.Coordinates;
using Streetcode.DAL.Entities.AdditionalContent.Coordinates.Types;
using Streetcode.DAL.Repositories.Interfaces.Base;

Check warning on line 9 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Create/CreateCoordinateHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dr5JFclLl1HJ7oB&open=AZy44dr5JFclLl1HJ7oB&pullRequest=112
using Streetcode.Resources;
using Streetcode.Shared.Extensions;
using Xunit;

namespace Streetcode.XUnitTest.MediatR.AdditionalContent.Coordinate;

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

public CreateCoordinateHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();

var configuration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new StreetcodeCoordinateProfile());
// Ensure mapping from DTO to Entity is available if not in profile
cfg.CreateMap<StreetcodeCoordinateDTO, StreetcodeCoordinate>();
});

_mapper = configuration.CreateMapper();
}

[Fact]
public async Task Handle_ValidRequest_ReturnsSuccessAndCallsCreate()
{
// Arrange
var command = new CreateCoordinateCommand(
new StreetcodeCoordinateDTO
{
Latitude = 10,
Longtitude = 20
});

// FIXED: Changed Create to CreateAsync to match the Handler implementation
_mockRepo.Setup(r => r.StreetcodeCoordinateRepository.CreateAsync(

Check warning on line 47 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Create/CreateCoordinateHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy_iOBZNERQer0CxlMw&open=AZy_iOBZNERQer0CxlMw&pullRequest=112
It.IsAny<StreetcodeCoordinate>()))
.ReturnsAsync(new StreetcodeCoordinate());

_mockRepo.Setup(r => r.SaveChangesAsync())
.ReturnsAsync(1);

var handler = new CreateCoordinateHandler(
_mockRepo.Object,
_mapper);

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

// Assert
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(Unit.Value);

// FIXED: Verifying CreateAsync because that is what the Handler actually invokes
_mockRepo.Verify(r => r.StreetcodeCoordinateRepository.CreateAsync(

Check warning on line 68 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Create/CreateCoordinateHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy_iOBZNERQer0CxlMx&open=AZy_iOBZNERQer0CxlMx&pullRequest=112
It.IsAny<StreetcodeCoordinate>()), Times.Once);

Check warning on line 69 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Create/CreateCoordinateHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The parameters should begin on the line after the declaration, whenever the parameter span across multiple lines

See more on https://sonarcloud.io/project/issues?id=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy_iOBZNERQer0CxlMy&open=AZy_iOBZNERQer0CxlMy&pullRequest=112

_mockRepo.Verify(r => r.SaveChangesAsync(), Times.Once);
}

[Fact]
public async Task Handle_MapperReturnsNull_ReturnsFailure()
{
// Arrange
// Passing null ensures the mapper returns null or throws,
// triggering the safety check in the Handler.
var command = new CreateCoordinateCommand(null!);

var handler = new CreateCoordinateHandler(
_mockRepo.Object,
_mapper);

var expectedError = Messages.Error_ConvertNullToEntity.Format(
nameof(StreetcodeCoordinate));

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

// Assert
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle()
.Which.Message.Should().Be(expectedError);
}

[Fact]
public async Task Handle_SaveFails_ReturnsFailure()
{
// Arrange
var command = new CreateCoordinateCommand(new StreetcodeCoordinateDTO());

_mockRepo.Setup(r => r.StreetcodeCoordinateRepository.CreateAsync(It.IsAny<StreetcodeCoordinate>()))
.ReturnsAsync(new StreetcodeCoordinate());

// Simulate database save failure (returning 0 rows affected)
_mockRepo.Setup(r => r.SaveChangesAsync())
.ReturnsAsync(0);

var handler = new CreateCoordinateHandler(_mockRepo.Object, _mapper);

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

// Assert
result.IsFailed.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Linq.Expressions;
using FluentAssertions;
using FluentResults;

Check warning on line 3 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Delete/DeleteCoordinateHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dsTJFclLl1HJ7o3&open=AZy44dsTJFclLl1HJ7o3&pullRequest=112
using MediatR;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using Streetcode.BLL.MediatR.AdditionalContent.Coordinate.Delete;
using Streetcode.DAL.Entities.AdditionalContent.Coordinates.Types;
using Streetcode.DAL.Repositories.Interfaces.Base;
using Xunit;

namespace Streetcode.XUnit.BLL.MediatR.AdditionalContent.Coordinate;

public class DeleteCoordinateHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepositoryWrapper;

public DeleteCoordinateHandlerTests()
{
_mockRepositoryWrapper = new Mock<IRepositoryWrapper>();
}

[Fact]
public async Task Handle_CoordinateNotFound_ReturnsFailResult()
{
// Arrange
int testId = 1;

_mockRepositoryWrapper.Setup(repo => repo.StreetcodeCoordinateRepository
.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<StreetcodeCoordinate, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeCoordinate>, IIncludableQueryable<StreetcodeCoordinate, object>>>(),
It.IsAny<bool>()))
.ReturnsAsync((StreetcodeCoordinate?)null);

var handler = new DeleteCoordinateHandler(_mockRepositoryWrapper.Object);
var command = new DeleteCoordinateCommand(testId);

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

// Assert
result.IsFailed.Should().BeTrue();
_mockRepositoryWrapper.Verify(r => r.StreetcodeCoordinateRepository.Delete(It.IsAny<StreetcodeCoordinate>()), Times.Never);
}

[Fact]
public async Task Handle_CoordinateExists_DeletesAndReturnsOkResult()
{
// Arrange
int testId = 1;
var coordinate = new StreetcodeCoordinate { Id = testId };

_mockRepositoryWrapper.Setup(repo => repo.StreetcodeCoordinateRepository
.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<StreetcodeCoordinate, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeCoordinate>, IIncludableQueryable<StreetcodeCoordinate, object>>>(),
It.IsAny<bool>()))
.ReturnsAsync(coordinate);

_mockRepositoryWrapper.Setup(repo => repo.SaveChangesAsync())

Check warning on line 61 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/Delete/DeleteCoordinateHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dsTJFclLl1HJ7pG&open=AZy44dsTJFclLl1HJ7pG&pullRequest=112
.ReturnsAsync(1);

var handler = new DeleteCoordinateHandler(_mockRepositoryWrapper.Object);
var command = new DeleteCoordinateCommand(testId);

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

// Assert
result.IsSuccess.Should().BeTrue();
_mockRepositoryWrapper.Verify(r => r.StreetcodeCoordinateRepository.Delete(coordinate), Times.Once);
_mockRepositoryWrapper.Verify(r => r.SaveChangesAsync(), Times.Once);
}

[Fact]
public async Task Handle_SaveChangesAsyncFails_ReturnsFailResult()
{
// Arrange
int testId = 1;
var coordinate = new StreetcodeCoordinate { Id = testId };

_mockRepositoryWrapper.Setup(repo => repo.StreetcodeCoordinateRepository
.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<StreetcodeCoordinate, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeCoordinate>, IIncludableQueryable<StreetcodeCoordinate, object>>>(),
It.IsAny<bool>()))
.ReturnsAsync(coordinate);

_mockRepositoryWrapper.Setup(repo => repo.SaveChangesAsync())
.ReturnsAsync(0);

var handler = new DeleteCoordinateHandler(_mockRepositoryWrapper.Object);
var command = new DeleteCoordinateCommand(testId);

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

// Assert
result.IsFailed.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using AutoMapper;

Check warning on line 1 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/GetByStreetcodeId/GetCoordinatesByStreetcodeIdHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dnMJFclLl1HJ7nT&open=AZy44dnMJFclLl1HJ7nT&pullRequest=112
using FluentAssertions;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using Streetcode.BLL.DTO.AdditionalContent.Coordinates.Types;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.BLL.Mapping.AdditionalContent.Coordinates;
using Streetcode.BLL.MediatR.AdditionalContent.Coordinate.GetByStreetcodeId;
using Streetcode.DAL.Entities.AdditionalContent.Coordinates.Types;
using Streetcode.DAL.Entities.Streetcode;
using Streetcode.DAL.Repositories.Interfaces.Base;
using Streetcode.Resources;
using Streetcode.Shared.Extensions;
using System.Linq.Expressions;

Check warning on line 14 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/GetByStreetcodeId/GetCoordinatesByStreetcodeIdHandlerTests.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=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dnMJFclLl1HJ7nh&open=AZy44dnMJFclLl1HJ7nh&pullRequest=112
using Xunit;

namespace Streetcode.XUnitTest.MediatR.AdditionalContent.Coordinate;

public class GetCoordinatesByStreetcodeIdHandlerTests
{
private readonly Mock<IRepositoryWrapper> _mockRepo;
private readonly Mock<ILoggerService> _mockLogger;

Check warning on line 22 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/GetByStreetcodeId/GetCoordinatesByStreetcodeIdHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Field '_mockLogger' should not begin with an underscore

See more on https://sonarcloud.io/project/issues?id=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy44dnMJFclLl1HJ7nl&open=AZy44dnMJFclLl1HJ7nl&pullRequest=112
private readonly IMapper _mapper;

public GetCoordinatesByStreetcodeIdHandlerTests()
{
_mockRepo = new Mock<IRepositoryWrapper>();
_mockLogger = new Mock<ILoggerService>();

var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new StreetcodeCoordinateProfile());
});
_mapper = config.CreateMapper();
}

[Fact]
public async Task Handle_StreetcodeExists_ReturnsCorrectDataAndType()
{
// Arrange
int streetcodeId = 1;
var query = new GetCoordinatesByStreetcodeIdQuery(streetcodeId);
var coordinates = new List<StreetcodeCoordinate>
{
new() { Id = 1, StreetcodeId = streetcodeId, Latitude = 1.1m, Longtitude = 2.2m }
};

_mockRepo.Setup(r => r.StreetcodeRepository.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<StreetcodeContent, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeContent>, IIncludableQueryable<StreetcodeContent, object>>>(),
It.IsAny<bool>()))
.ReturnsAsync(new StreetcodeContent { Id = streetcodeId });

_mockRepo.Setup(r => r.StreetcodeCoordinateRepository.GetAllAsync(
It.IsAny<Expression<Func<StreetcodeCoordinate, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeCoordinate>, IIncludableQueryable<StreetcodeCoordinate, object>>>(),
It.IsAny<bool>()))
.ReturnsAsync(coordinates);

var handler = new GetCoordinatesByStreetcodeIdHandler(_mockRepo.Object, _mapper, _mockLogger.Object);

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

// Assert
result.IsSuccess.Should().BeTrue();
result.Value.Should().NotBeNull();
}

}

Check warning on line 70 in Streetcode/Streetcode.XUnitTest/MediatR/AdditionalContent/Cordinate/GetByStreetcodeId/GetCoordinatesByStreetcodeIdHandlerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A closing brace should not be preceded by a blank line.

See more on https://sonarcloud.io/project/issues?id=project-studying-dotnet_Streetcode-Server-January-2026&issues=AZy_iN6pNERQer0CxlMm&open=AZy_iN6pNERQer0CxlMm&pullRequest=112
Loading
Loading