Untitled

 avatar
user_8078570
plain_text
a month ago
4.5 kB
4
Indexable
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Xunit;

public class SaveMethodTests
{
    private readonly Fixture fixture;

    public SaveMethodTests()
    {
        fixture = new Fixture(); // Using the existing fixture setup
    }

    [Fact]
    public async Task Save_ShouldReturn_Status403Forbidden_WhenUserIsNotOwner()
    {
        // Arrange
        var setupId = 1;
        var user = "testuser";
        var request = new NodeValuesRequest();
        var setup = new Setup { WhoStored = "anotheruser" };

        fixture.setupServiceMoq.Setup(s => s.FindSetupById(setupId, user))
                               .ReturnsAsync(setup);

        var service = new SetupService(fixture.setupValuesUOWMock.Object, fixture.configurationMock.Object);

        // Act
        var act = async () => await service.Save(setupId, request, user);

        // Assert
        await act.Should().ThrowAsync<InvalidSetupOwnership>()
                 .WithMessage("You are not the owner of this setup.");
    }

    [Fact]
    public async Task Save_ShouldReturn_Status404NotFound_OnInexistentSetup()
    {
        // Arrange
        var setupId = 1;
        var user = "testuser";
        var request = new NodeValuesRequest();

        fixture.setupServiceMoq.Setup(s => s.FindSetupById(setupId, user))
                               .Throws(new EntityNotFoundException("Setup", null));

        var service = new SetupService(fixture.setupValuesUOWMock.Object, fixture.configurationMock.Object);

        // Act
        var act = async () => await service.Save(setupId, request, user);

        // Assert
        await act.Should().ThrowAsync<EntityNotFoundException>();
        fixture.setupValuesUOWMock.Verify(u => u.RollbackAsync(It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task Save_ShouldReturn_Status500InternalServerError_OnDbException()
    {
        // Arrange
        var setupId = 1;
        var user = "testuser";
        var request = new NodeValuesRequest();

        fixture.setupValuesUOWMock.Setup(u => u.BeginTransactionAsync())
                                  .Throws(new DbException("Database error"));

        var service = new SetupService(fixture.setupValuesUOWMock.Object, fixture.configurationMock.Object);

        // Act
        var act = async () => await service.Save(setupId, request, user);

        // Assert
        await act.Should().ThrowAsync<DbException>()
                 .WithMessage("Database error");
        fixture.setupValuesUOWMock.Verify(u => u.RollbackAsync(It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task Save_ShouldReturn_Status200OK_WhenSaveIsSuccessful()
    {
        // Arrange
        var setupId = 1;
        var user = "testuser";
        var request = new NodeValuesRequest();
        var setup = new Setup { WhoStored = user, Version = "v5" };

        fixture.setupServiceMoq.Setup(s => s.FindSetupById(setupId, user))
                               .ReturnsAsync(setup);

        fixture.setupValuesUOWMock.Setup(u => u.SaveSetup(setup, user, It.IsAny<CancellationToken>()))
                                  .Returns(Task.CompletedTask);

        var service = new SetupService(fixture.setupValuesUOWMock.Object, fixture.configurationMock.Object);

        // Act
        var result = await service.Save(setupId, request, user);

        // Assert
        result.Should().NotBeNull();
        fixture.setupValuesUOWMock.Verify(u => u.CommitAsync(It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task Save_ShouldReturn_Status500InternalServerError_OnUnhandledException()
    {
        // Arrange
        var setupId = 1;
        var user = "testuser";
        var request = new NodeValuesRequest();

        fixture.setupServiceMoq.Setup(s => s.FindSetupById(setupId, user))
                               .Throws(new Exception("Unexpected error"));

        var service = new SetupService(fixture.setupValuesUOWMock.Object, fixture.configurationMock.Object);

        // Act
        var act = async () => await service.Save(setupId, request, user);

        // Assert
        await act.Should().ThrowAsync<DbException>()
                 .WithMessage("An unhandled error occured when trying to add/update SetupValues");
        fixture.setupValuesUOWMock.Verify(u => u.RollbackAsync(It.IsAny<CancellationToken>()), Times.Once);
    }
}
Leave a Comment