File size: 2,815 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using FluentAssertions;
using NSubstitute;
using SCM.Application.Common;
using SCM.Application.Qualidade;
using SCM.Application.Qualidade.Dtos;
using SCM.Domain.Qualidade;
using SCM.Shared.Exceptions;
using Xunit;

namespace SCM.UnitTests.Qualidade;

public class AgenteServiceTests
{
    private readonly IAgenteRepository _repository = Substitute.For<IAgenteRepository>();
    private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
    private readonly AgenteService _sut;

    public AgenteServiceTests() => _sut = new AgenteService(_repository, _unitOfWork);

    [Fact]
    public async Task CreateAsync_DeveLancarConflict_QuandoCodigoJaExiste()
    {
        _repository.CodigoExistsAsync("AG-1", null, Arg.Any<CancellationToken>()).Returns(true);

        var act = () => _sut.CreateAsync(new CreateAgenteRequest("AG-1", "Nome", AgenteTipo.CQ, null, null));

        await act.Should().ThrowAsync<ConflictException>();
        await _repository.DidNotReceive().AddAsync(Arg.Any<Agente>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido()
    {
        _repository.CodigoExistsAsync(Arg.Any<string>(), null, Arg.Any<CancellationToken>()).Returns(false);

        var response = await _sut.CreateAsync(new CreateAgenteRequest("AG-1", "Nome", AgenteTipo.Producao, null, null));

        response.Codigo.Should().Be("AG-1");
        response.Tipo.Should().Be("Producao");
        _unitOfWork.Received(1).BeginTransaction();
        _unitOfWork.Received(1).Commit();
        await _repository.Received(1).AddAsync(Arg.Any<Agente>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task DeleteAsync_DeveLancarConflict_QuandoTemProgramacao()
    {
        var id = Guid.NewGuid();
        _repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
            .Returns(new Agente { Id = id, Codigo = "AG-1", Nome = "Nome", Tipo = AgenteTipo.CQ });
        _repository.ContarProgramacoesAsync(id, Arg.Any<CancellationToken>()).Returns(4);

        var act = () => _sut.DeleteAsync(id);

        await act.Should().ThrowAsync<ConflictException>();
        await _repository.DidNotReceive().DeleteAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task DeleteAsync_DeveExcluir_QuandoSemProgramacao()
    {
        var id = Guid.NewGuid();
        _repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
            .Returns(new Agente { Id = id, Codigo = "AG-1", Nome = "Nome", Tipo = AgenteTipo.CQ });
        _repository.ContarProgramacoesAsync(id, Arg.Any<CancellationToken>()).Returns(0);

        await _sut.DeleteAsync(id);

        await _repository.Received(1).DeleteAsync(id, Arg.Any<CancellationToken>());
        _unitOfWork.Received(1).Commit();
    }
}