|
| 1 | +using EstateManagementUI.BusinessLogic.Client; |
| 2 | +using EstateManagementUI.BusinessLogic.Models; |
| 3 | +using EstateManagementUI.BusinessLogic.RequestHandlers; |
| 4 | +using EstateManagementUI.BusinessLogic.Requests; |
| 5 | +using Moq; |
| 6 | +using Shouldly; |
| 7 | +using SimpleResults; |
| 8 | + |
| 9 | +namespace EstateManagementUI.BlazorServer.Tests.RequestHandlers; |
| 10 | + |
| 11 | +public class EstateRequestHandlerTests |
| 12 | +{ |
| 13 | + private readonly Mock<IApiClient> _mockApiClient; |
| 14 | + private readonly EstateRequestHandler _handler; |
| 15 | + |
| 16 | + public EstateRequestHandlerTests() |
| 17 | + { |
| 18 | + _mockApiClient = new Mock<IApiClient>(); |
| 19 | + _handler = new EstateRequestHandler(_mockApiClient.Object); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task Handle_GetEstateQuery_ReturnsSuccess_WhenApiClientSucceeds() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var estateId = Guid.NewGuid(); |
| 27 | + var query = new EstateQueries.GetEstateQuery(CorrelationIdHelper.New(), estateId); |
| 28 | + var estateModel = new EstateModels.EstateModel |
| 29 | + { |
| 30 | + EstateId = estateId, |
| 31 | + EstateName = "Test Estate" |
| 32 | + }; |
| 33 | + |
| 34 | + _mockApiClient |
| 35 | + .Setup(c => c.GetEstate(query, It.IsAny<CancellationToken>())) |
| 36 | + .ReturnsAsync(Result.Success(estateModel)); |
| 37 | + |
| 38 | + // Act |
| 39 | + var result = await _handler.Handle(query, CancellationToken.None); |
| 40 | + |
| 41 | + // Assert |
| 42 | + result.IsSuccess.ShouldBeTrue(); |
| 43 | + result.Data.ShouldNotBeNull(); |
| 44 | + result.Data!.EstateId.ShouldBe(estateId); |
| 45 | + result.Data.EstateName.ShouldBe("Test Estate"); |
| 46 | + |
| 47 | + _mockApiClient.Verify(c => c.GetEstate(query, It.IsAny<CancellationToken>()), Times.Once); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public async Task Handle_GetEstateQuery_ReturnsFailure_WhenApiClientFails() |
| 52 | + { |
| 53 | + // Arrange |
| 54 | + var query = new EstateQueries.GetEstateQuery(CorrelationIdHelper.New(), Guid.NewGuid()); |
| 55 | + |
| 56 | + _mockApiClient |
| 57 | + .Setup(c => c.GetEstate(query, It.IsAny<CancellationToken>())) |
| 58 | + .ReturnsAsync(Result.Failure("api error")); |
| 59 | + |
| 60 | + // Act |
| 61 | + var result = await _handler.Handle(query, CancellationToken.None); |
| 62 | + |
| 63 | + // Assert |
| 64 | + result.IsFailed.ShouldBeTrue(); |
| 65 | + |
| 66 | + _mockApiClient.Verify(c => c.GetEstate(query, It.IsAny<CancellationToken>()), Times.Once); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task Handle_AddOperatorToEstateCommand_ReturnsSuccess_WhenApiClientSucceeds() |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + var command = new EstateCommands.AddOperatorToEstateCommand(CorrelationIdHelper.New(), Guid.NewGuid(), Guid.NewGuid()); |
| 74 | + |
| 75 | + _mockApiClient |
| 76 | + .Setup(c => c.AddEstateOperator(command, It.IsAny<CancellationToken>())) |
| 77 | + .ReturnsAsync(Result.Success()); |
| 78 | + |
| 79 | + // Act |
| 80 | + var result = await _handler.Handle(command, CancellationToken.None); |
| 81 | + |
| 82 | + // Assert |
| 83 | + result.IsSuccess.ShouldBeTrue(); |
| 84 | + |
| 85 | + _mockApiClient.Verify(c => c.AddEstateOperator(command, It.IsAny<CancellationToken>()), Times.Once); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task Handle_AddOperatorToEstateCommand_ReturnsFailure_WhenApiClientFails() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var command = new EstateCommands.AddOperatorToEstateCommand(CorrelationIdHelper.New(), Guid.NewGuid(), Guid.NewGuid()); |
| 93 | + |
| 94 | + _mockApiClient |
| 95 | + .Setup(c => c.AddEstateOperator(command, It.IsAny<CancellationToken>())) |
| 96 | + .ReturnsAsync(Result.Failure("api error")); |
| 97 | + |
| 98 | + // Act |
| 99 | + var result = await _handler.Handle(command, CancellationToken.None); |
| 100 | + |
| 101 | + // Assert |
| 102 | + result.IsFailed.ShouldBeTrue(); |
| 103 | + |
| 104 | + _mockApiClient.Verify(c => c.AddEstateOperator(command, It.IsAny<CancellationToken>()), Times.Once); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public async Task Handle_RemoveOperatorFromEstateCommand_ReturnsSuccess_WhenApiClientSucceeds() |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + var command = new EstateCommands.RemoveOperatorFromEstateCommand(CorrelationIdHelper.New(), Guid.NewGuid(), Guid.NewGuid()); |
| 112 | + |
| 113 | + _mockApiClient |
| 114 | + .Setup(c => c.RemoveEstateOperator(command, It.IsAny<CancellationToken>())) |
| 115 | + .ReturnsAsync(Result.Success()); |
| 116 | + |
| 117 | + // Act |
| 118 | + var result = await _handler.Handle(command, CancellationToken.None); |
| 119 | + |
| 120 | + // Assert |
| 121 | + result.IsSuccess.ShouldBeTrue(); |
| 122 | + |
| 123 | + _mockApiClient.Verify(c => c.RemoveEstateOperator(command, It.IsAny<CancellationToken>()), Times.Once); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task Handle_RemoveOperatorFromEstateCommand_ReturnsFailure_WhenApiClientFails() |
| 128 | + { |
| 129 | + // Arrange |
| 130 | + var command = new EstateCommands.RemoveOperatorFromEstateCommand(CorrelationIdHelper.New(), Guid.NewGuid(), Guid.NewGuid()); |
| 131 | + |
| 132 | + _mockApiClient |
| 133 | + .Setup(c => c.RemoveEstateOperator(command, It.IsAny<CancellationToken>())) |
| 134 | + .ReturnsAsync(Result.Failure("api error")); |
| 135 | + |
| 136 | + // Act |
| 137 | + var result = await _handler.Handle(command, CancellationToken.None); |
| 138 | + |
| 139 | + // Assert |
| 140 | + result.IsFailed.ShouldBeTrue(); |
| 141 | + |
| 142 | + _mockApiClient.Verify(c => c.RemoveEstateOperator(command, It.IsAny<CancellationToken>()), Times.Once); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public async Task Handle_GetAssignedOperatorsQuery_ReturnsSuccess_WhenApiClientSucceeds() |
| 147 | + { |
| 148 | + // Arrange |
| 149 | + var estateId = Guid.NewGuid(); |
| 150 | + var query = new EstateQueries.GetAssignedOperatorsQuery(CorrelationIdHelper.New(), estateId); |
| 151 | + var operators = new List<OperatorModels.OperatorModel> |
| 152 | + { |
| 153 | + new() { OperatorId = Guid.NewGuid(), Name = "Operator 1" }, |
| 154 | + new() { OperatorId = Guid.NewGuid(), Name = "Operator 2" } |
| 155 | + }; |
| 156 | + |
| 157 | + _mockApiClient |
| 158 | + .Setup(c => c.GetEstateAssignedOperators(query, It.IsAny<CancellationToken>())) |
| 159 | + .ReturnsAsync(Result.Success(operators)); |
| 160 | + |
| 161 | + // Act |
| 162 | + var result = await _handler.Handle(query, CancellationToken.None); |
| 163 | + |
| 164 | + // Assert |
| 165 | + result.IsSuccess.ShouldBeTrue(); |
| 166 | + result.Data.ShouldNotBeNull(); |
| 167 | + result.Data!.Count.ShouldBe(2); |
| 168 | + result.Data[0].Name.ShouldBe("Operator 1"); |
| 169 | + result.Data[1].Name.ShouldBe("Operator 2"); |
| 170 | + |
| 171 | + _mockApiClient.Verify(c => c.GetEstateAssignedOperators(query, It.IsAny<CancellationToken>()), Times.Once); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public async Task Handle_GetAssignedOperatorsQuery_ReturnsFailure_WhenApiClientFails() |
| 176 | + { |
| 177 | + // Arrange |
| 178 | + var query = new EstateQueries.GetAssignedOperatorsQuery(CorrelationIdHelper.New(), Guid.NewGuid()); |
| 179 | + |
| 180 | + _mockApiClient |
| 181 | + .Setup(c => c.GetEstateAssignedOperators(query, It.IsAny<CancellationToken>())) |
| 182 | + .ReturnsAsync(Result.Failure("api error")); |
| 183 | + |
| 184 | + // Act |
| 185 | + var result = await _handler.Handle(query, CancellationToken.None); |
| 186 | + |
| 187 | + // Assert |
| 188 | + result.IsFailed.ShouldBeTrue(); |
| 189 | + |
| 190 | + _mockApiClient.Verify(c => c.GetEstateAssignedOperators(query, It.IsAny<CancellationToken>()), Times.Once); |
| 191 | + } |
| 192 | +} |
0 commit comments