File size: 2,753 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
using FluentAssertions;
using NSubstitute;
using SCM.Application.Common;
using SCM.Application.Controle;
using SCM.Application.Controle.Dtos;
using SCM.Domain.Controle;
using SCM.Shared.Exceptions;
using Xunit;

namespace SCM.UnitTests.Controle;

public class ExecutorServiceTests
{
    private readonly IExecutorRepository _repository = Substitute.For<IExecutorRepository>();
    private readonly IProducaoControleRepository _producaoRepository = Substitute.For<IProducaoControleRepository>();
    private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
    private readonly ExecutorService _sut;

    private static readonly Guid Obra = Guid.NewGuid();

    public ExecutorServiceTests() => _sut = new ExecutorService(_repository, _producaoRepository, _unitOfWork);

    [Fact]
    public async Task CreateAsync_DeveLancarNotFound_QuandoObraNaoExiste()
    {
        _repository.ObraExistsAsync(Obra, Arg.Any<CancellationToken>()).Returns(false);

        var act = () => _sut.CreateAsync(new CreateExecutorRequest(Obra, "EX-01", "Empreiteira"));

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

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

        var act = () => _sut.CreateAsync(new CreateExecutorRequest(Obra, "EX-01", "Empreiteira"));

        await act.Should().ThrowAsync<ConflictException>();
    }

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

        var response = await _sut.CreateAsync(new CreateExecutorRequest(Obra, "EX-99", "Empreiteira"));

        response.Codigo.Should().Be("EX-99");
        _unitOfWork.Received(1).Commit();
        await _repository.Received(1).AddAsync(Arg.Any<Executor>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task DeleteAsync_DeveExcluir_QuandoEncontrado()
    {
        var id = Guid.NewGuid();
        var executor = Executor.Create(Obra, "EX-01", "Empreiteira");
        executor.Id = id;
        _repository.GetByIdAsync(id, Arg.Any<CancellationToken>()).Returns(executor);

        await _sut.DeleteAsync(id);

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