-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReservationChangeRequestTests.cs
116 lines (99 loc) · 4.54 KB
/
ReservationChangeRequestTests.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GroupFlights.Postsale.Domain.Changes.DomainService;
using GroupFlights.Postsale.Domain.Changes.Events;
using GroupFlights.Postsale.Domain.Changes.Repositories;
using GroupFlights.Postsale.Domain.Changes.Request;
using GroupFlights.Postsale.Domain.Exceptions;
using GroupFlights.Sales.Shared;
using GroupFlights.Sales.Shared.Changes;
using GroupFlights.Shared.Types;
using NSubstitute;
using Xunit;
namespace GroupFlights.Postsale.UnitTests.Changes;
public class ReservationChangeRequestTests
{
private ISalesApi _salesApi = Substitute.For<ISalesApi>();
private IReservationChangeRequestRepository _repository = Substitute.For<IReservationChangeRequestRepository>();
private ReservationChangeRequestDomainService _domainService;
[Fact]
public async Task ReservationChangeRequestDomainService_ForbidsCreatingChangeRequest_IfThereIsOtherActiveForThisUser()
{
_repository.ExistsActiveForGivenUser(Arg.Any<UserId>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(true);
var reservationId = Guid.NewGuid();
var currentTravelDate = DateTime.UtcNow.AddDays(10);
_salesApi.GetReservationForChange(Arg.Any<GetReservationForChangeQuery>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(new ReservationToChangeDto(
reservationId,
AirlineType.Traditional,
true,
new ReservationCostDto(new Money(250, Currency.PLN), new Money(50, Currency.PLN)),
new []
{
new FlightSegmentDto(
currentTravelDate,
new Airport("WAW", "Warsaw", "Warsaw Chopin", "Poland"),
new Airport("BCN", "Barcelona", "Barcelona Airport", "Spain"),
new FlightTimeDto(3, 15)
)
},
new [] { new RequiredPaymentDto(Guid.NewGuid(), new DeadlineDto(DateTime.UtcNow.AddDays(1), null, Guid.NewGuid())) },
new DeadlineDto(DateTime.UtcNow.AddDays(1), null, Guid.NewGuid())
));
_domainService = new(_salesApi, _repository);
await Assert.ThrowsAsync<OnlyOneActiveChangePerReservationIsAllowedException>(async () =>
{
await _domainService.CreateChangeRequest(
reservationId,
currentTravelDate.AddDays(10),
new UserId(Guid.NewGuid()));
});
}
[Fact]
public async Task ReservationChangeRequest_ChangesStatus_OnReject()
{
var changeRequest = await PrepareChangeRequest();
changeRequest.RejectChange();
var emittedEvent = changeRequest.DomainEvents
.OfType<ReservationChangeRequestFinalized>().SingleOrDefault();
Assert.Equal(expected: ReservationChangeRequest.CompletionStatus.ChangeRejectedByRequester,
emittedEvent?.CompletionStatus);
}
private async Task<ReservationChangeRequest> PrepareChangeRequest()
{
_repository.ExistsActiveForGivenUser(Arg.Any<UserId>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(false);
var reservationId = Guid.NewGuid();
var currentTravelDate = DateTime.UtcNow.AddDays(10);
_salesApi.GetReservationForChange(Arg.Any<GetReservationForChangeQuery>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(new ReservationToChangeDto(
reservationId,
AirlineType.Traditional,
true,
new ReservationCostDto(new Money(250, Currency.PLN), new Money(50, Currency.PLN)),
new[]
{
new FlightSegmentDto(
currentTravelDate,
new Airport("WAW", "Warsaw", "Warsaw Chopin", "Poland"),
new Airport("BCN", "Barcelona", "Barcelona Airport", "Spain"),
new FlightTimeDto(3, 15)
)
},
new[]
{
new RequiredPaymentDto(Guid.NewGuid(),
new DeadlineDto(DateTime.UtcNow.AddDays(1), null, Guid.NewGuid()))
},
new DeadlineDto(DateTime.UtcNow.AddDays(1), null, Guid.NewGuid())
));
_domainService = new(_salesApi, _repository);
return await _domainService.CreateChangeRequest(
reservationId,
currentTravelDate.AddDays(10),
new UserId(Guid.NewGuid()));
}
}