88
99namespace Nhs . Appointments . Core . UnitTests
1010{
11- public class BookingWriteServiceTests
11+ public class BookingWriteServiceTests_BookingReferenceV2Disabled ( ) : BookingWriteServiceTests ( false )
1212 {
13- private const string MockSite = "some-site" ;
14- private readonly Mock < IAvailabilityCreatedEventStore > _availabilityCreatedEventStore = new ( ) ;
15- private readonly Mock < IAvailabilityStore > _availabilityStore = new ( ) ;
13+
14+ }
15+
16+ public class BookingWriteServiceTests_BookingReferenceV2Enabled ( ) : BookingWriteServiceTests ( true )
17+ {
18+ [ Fact ]
19+ public async Task RescheduleBooking_Transition_Initial_V1_Reference_To_Reschedule_V2_Reference ( )
20+ {
21+ //setup first booking to use V1 reference
22+ _featureToggleHelper . Setup ( x => x . IsFeatureEnabled ( Flags . BookingReferenceV2 ) ) . ReturnsAsync ( false ) ;
23+
24+ ContactItem [ ] contactDetails =
25+ [ new ContactItem { Type = ContactItemType . Email , Value = "test@tempuri.org" } ] ;
26+
27+ var initialBooking = new Booking
28+ {
29+ Site = MockSite ,
30+ Service = "TSERV" ,
31+ From = new DateTime ( 2077 , 1 , 1 , 10 , 0 , 0 , 0 ) ,
32+ Duration = 10 ,
33+ ContactDetails = null ,
34+ Status = AppointmentStatus . Provisional
35+ } ;
36+
37+ _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
38+
39+ _bookingAvailabilityStateService . Setup ( x => x . GetAvailableSlots ( MockSite ,
40+ new DateTime ( 2077 , 1 , 1 , 10 , 0 , 0 , 0 ) ,
41+ new DateTime ( 2077 , 1 , 1 , 10 , 10 , 0 , 0 ) ) )
42+ . ReturnsAsync ( [
43+ new SessionInstance ( new DateTime ( 2077 , 1 , 1 , 10 , 0 , 0 , 0 ) ,
44+ new DateTime ( 2077 , 1 , 1 , 10 , 10 , 0 , 0 ) ) { Services = [ "TSERV" ] }
45+ ] ) ;
46+
47+ _bookingAvailabilityStateService . Setup ( x => x . GetAvailableSlots ( MockSite ,
48+ new DateTime ( 2077 , 1 , 1 , 11 , 0 , 0 , 0 ) ,
49+ new DateTime ( 2077 , 1 , 1 , 11 , 10 , 0 , 0 ) ) )
50+ . ReturnsAsync ( [
51+ new SessionInstance ( new DateTime ( 2077 , 1 , 1 , 11 , 0 , 0 , 0 ) ,
52+ new DateTime ( 2077 , 1 , 1 , 11 , 10 , 0 , 0 ) ) { Services = [ "TSERV" ] }
53+ ] ) ;
54+
55+ _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "14-90-002345" ) ;
56+
57+ _bookingsDocumentStore
58+ . Setup ( x => x . ConfirmProvisional ( It . IsAny < string > ( ) , It . IsAny < IEnumerable < ContactItem > > ( ) ,
59+ It . IsAny < string > ( ) , It . IsAny < CancellationReason > ( ) ) )
60+ . ReturnsAsync ( BookingConfirmationResult . Success ) ;
61+
62+ var initialBookingResult = await _sut . MakeBooking ( initialBooking ) ;
63+
64+ initialBookingResult . Reference . Should ( ) . Be ( "14-90-002345" ) ;
65+
66+ await _sut . ConfirmProvisionalBooking ( initialBookingResult . Reference , contactDetails , "" ) ;
67+
68+ var rescheduledBooking = new Booking
69+ {
70+ Site = MockSite ,
71+ Service = "TSERV" ,
72+ From = new DateTime ( 2077 , 1 , 1 , 11 , 0 , 0 , 0 ) ,
73+ Duration = 10 ,
74+ ContactDetails = null ,
75+ Status = AppointmentStatus . Provisional
76+ } ;
77+
78+ //the feature flag flips in-between reschedule operation
79+ _featureToggleHelper . Setup ( x => x . IsFeatureEnabled ( Flags . BookingReferenceV2 ) ) . ReturnsAsync ( true ) ;
80+
81+ _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "3825-69268-6774" ) ;
82+
83+ _bookingsDocumentStore . Setup ( x => x . GetByReferenceOrDefaultAsync ( "14-90-002345" ) ) . ReturnsAsync ( new Booking
84+ {
85+ Reference = "14-90-002345" ,
86+ Site = MockSite ,
87+ Service = "TSERV" ,
88+ From = new DateTime ( 2077 , 1 , 1 , 10 , 0 , 0 , 0 ) ,
89+ Duration = 10 ,
90+ ContactDetails = contactDetails ,
91+ Status = AppointmentStatus . Booked ,
92+ AttendeeDetails = new AttendeeDetails ( )
93+ } ) ;
94+ _bookingsDocumentStore . Setup ( x => x . GetByReferenceOrDefaultAsync ( "3825-69268-6774" ) ) . ReturnsAsync ( new Booking
95+ {
96+ Reference = "3825-69268-6774" ,
97+ Site = MockSite ,
98+ Service = "TSERV" ,
99+ From = new DateTime ( 2077 , 1 , 1 , 11 , 0 , 0 , 0 ) ,
100+ Duration = 10 ,
101+ ContactDetails = contactDetails ,
102+ Status = AppointmentStatus . Booked ,
103+ AttendeeDetails = new AttendeeDetails ( )
104+ } ) ;
105+
106+ await _sut . MakeBooking ( rescheduledBooking ) ;
107+
108+ var rescheduleResult = await _sut . ConfirmProvisionalBooking ( rescheduledBooking . Reference ,
109+ contactDetails , initialBookingResult . Reference ) ;
110+
111+ rescheduleResult . Should ( ) . Be ( BookingConfirmationResult . Success ) ;
112+
113+ initialBookingResult . Reference . Should ( ) . Be ( "14-90-002345" ) ;
114+ rescheduledBooking . Reference . Should ( ) . Be ( "3825-69268-6774" ) ;
16115
17- private readonly Mock < IBookingAvailabilityStateService > _bookingAvailabilityStateService = new ( ) ;
116+ _bookingsDocumentStore . Verify ( x =>
117+ x . ConfirmProvisional ( rescheduledBooking . Reference , contactDetails , initialBookingResult . Reference ,
118+ CancellationReason . RescheduledByCitizen ) ,
119+ Times . Once ) ;
120+ }
121+ }
122+
123+ public abstract class BookingWriteServiceTests
124+ {
125+ protected const string MockSite = "some-site" ;
126+ protected readonly Mock < IAvailabilityCreatedEventStore > _availabilityCreatedEventStore = new ( ) ;
127+ protected readonly Mock < IAvailabilityStore > _availabilityStore = new ( ) ;
18128
19- private readonly Mock < IBookingQueryService > _bookingQueryService = new ( ) ;
20- private readonly Mock < IBookingsDocumentStore > _bookingsDocumentStore = new ( ) ;
21- private readonly Mock < IMessageBus > _messageBus = new ( ) ;
22- private readonly Mock < IReferenceNumberProvider > _referenceNumberProviderV1 = new ( ) ;
23- private readonly Mock < IProvider > _referenceNumberProviderV2 = new ( ) ;
129+ protected readonly Mock < IBookingAvailabilityStateService > _bookingAvailabilityStateService = new ( ) ;
130+
131+ protected readonly Mock < IBookingQueryService > _bookingQueryService = new ( ) ;
132+ protected readonly Mock < IBookingsDocumentStore > _bookingsDocumentStore = new ( ) ;
133+ protected readonly Mock < IMessageBus > _messageBus = new ( ) ;
134+ protected readonly Mock < IReferenceNumberProvider > _referenceNumberProviderV1 = new ( ) ;
135+ protected readonly Mock < IProvider > _referenceNumberProviderV2 = new ( ) ;
136+
137+ protected readonly Mock < IFeatureToggleHelper > _featureToggleHelper = new ( ) ;
24138
25- private readonly Mock < IFeatureToggleHelper > _featureToggleHelper = new ( ) ;
139+ protected readonly Mock < ISiteLeaseManager > _siteLeaseManager = new ( ) ;
140+ protected BookingWriteService _sut ;
26141
27- private readonly Mock < ISiteLeaseManager > _siteLeaseManager = new ( ) ;
28- private BookingWriteService _sut ;
142+ protected readonly bool _bookingReferenceV2Enabled ;
29143
30- public BookingWriteServiceTests ( )
144+ protected BookingWriteServiceTests ( bool bookingReferenceV2Enabled )
31145 {
146+ _bookingReferenceV2Enabled = bookingReferenceV2Enabled ;
147+
148+ _featureToggleHelper . Setup ( x => x . IsFeatureEnabled ( Flags . BookingReferenceV2 ) ) . ReturnsAsync ( _bookingReferenceV2Enabled ) ;
149+
32150 _sut = new BookingWriteService (
33151 _bookingsDocumentStore . Object ,
34152 _bookingQueryService . Object ,
@@ -58,10 +176,9 @@ public async Task MakeBooking_AcquiresLock_WhenBooking()
58176 } ;
59177
60178 _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
61-
62- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
63- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
64179
180+ MockReferenceProvider ( "TEST1" ) ;
181+
65182 var bookingQueryService = new BookingQueryService ( _bookingsDocumentStore . Object , TimeProvider . System ) ;
66183 var availabilityQueryService =
67184 new AvailabilityQueryService ( _availabilityStore . Object , _availabilityCreatedEventStore . Object ) ;
@@ -83,6 +200,18 @@ public async Task MakeBooking_AcquiresLock_WhenBooking()
83200 task . IsCompleted . Should ( ) . BeTrue ( ) ;
84201 }
85202
203+ protected void MockReferenceProvider ( string reference )
204+ {
205+ if ( _bookingReferenceV2Enabled )
206+ {
207+ _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( reference ) ;
208+ }
209+ else
210+ {
211+ _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( reference ) ;
212+ }
213+ }
214+
86215 [ Fact ]
87216 public async Task MakeBooking_RaisesNotificationEvent_WhenBooking ( )
88217 {
@@ -108,9 +237,7 @@ public async Task MakeBooking_RaisesNotificationEvent_WhenBooking()
108237 _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
109238
110239 MockAvailability ( availability ) ;
111-
112- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
113- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
240+ MockReferenceProvider ( "TEST1" ) ;
114241
115242 var result = await _sut . MakeBooking ( booking ) ;
116243
@@ -149,9 +276,7 @@ public async Task MakeBooking_DoesNotRaiseNotificationEvent_WhenProvisional()
149276 _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
150277
151278 MockAvailability ( availability ) ;
152-
153- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
154- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
279+ MockReferenceProvider ( "TEST1" ) ;
155280
156281 var result = await _sut . MakeBooking ( booking ) ;
157282
@@ -161,18 +286,6 @@ public async Task MakeBooking_DoesNotRaiseNotificationEvent_WhenProvisional()
161286 [ Fact ]
162287 public async Task RescheduleBooking_IsSuccessful ( )
163288 {
164- var availability = new [ ]
165- {
166- new SessionInstance ( new DateTime ( 2077 , 1 , 1 , 10 , 0 , 0 , 0 ) , new DateTime ( 2077 , 1 , 1 , 10 , 10 , 0 , 0 ) )
167- {
168- Services = [ "TSERV" ]
169- } ,
170- new SessionInstance ( new DateTime ( 2077 , 1 , 1 , 11 , 0 , 0 , 0 ) , new DateTime ( 2077 , 1 , 1 , 11 , 10 , 0 , 0 ) )
171- {
172- Services = [ "TSERV" ]
173- } ,
174- } ;
175-
176289 ContactItem [ ] contactDetails =
177290 [ new ContactItem { Type = ContactItemType . Email , Value = "test@tempuri.org" } ] ;
178291
@@ -204,8 +317,7 @@ public async Task RescheduleBooking_IsSuccessful()
204317 new DateTime ( 2077 , 1 , 1 , 11 , 10 , 0 , 0 ) ) { Services = [ "TSERV" ] }
205318 ] ) ;
206319
207- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
208- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
320+ MockReferenceProvider ( "TEST1" ) ;
209321
210322 _bookingsDocumentStore
211323 . Setup ( x => x . ConfirmProvisional ( It . IsAny < string > ( ) , It . IsAny < IEnumerable < ContactItem > > ( ) ,
@@ -237,9 +349,6 @@ public async Task RescheduleBooking_IsSuccessful()
237349 var initialBookingResult = await _sut . MakeBooking ( initialBooking ) ;
238350 await _sut . ConfirmProvisionalBooking ( initialBookingResult . Reference , contactDetails , "" ) ;
239351
240- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
241- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST2" ) ;
242-
243352 var rescheduledBooking = new Booking
244353 {
245354 Site = MockSite ,
@@ -249,7 +358,10 @@ public async Task RescheduleBooking_IsSuccessful()
249358 ContactDetails = null ,
250359 Status = AppointmentStatus . Provisional
251360 } ;
252- var rescheduledBookingResult = await _sut . MakeBooking ( rescheduledBooking ) ;
361+
362+ MockReferenceProvider ( "TEST2" ) ;
363+
364+ await _sut . MakeBooking ( rescheduledBooking ) ;
253365
254366 var rescheduleResult = await _sut . ConfirmProvisionalBooking ( rescheduledBooking . Reference ,
255367 contactDetails , initialBookingResult . Reference ) ;
@@ -298,8 +410,7 @@ public async Task RescheduleBooking_RaisesNotificationEvent()
298410 new DateTime ( 2077 , 1 , 1 , 11 , 10 , 0 , 0 ) ) { Services = [ "TSERV" ] }
299411 ] ) ;
300412
301- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
302- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
413+ MockReferenceProvider ( "TEST1" ) ;
303414
304415 _bookingsDocumentStore
305416 . Setup ( x => x . ConfirmProvisional ( It . IsAny < string > ( ) , It . IsAny < IEnumerable < ContactItem > > ( ) ,
@@ -330,8 +441,7 @@ public async Task RescheduleBooking_RaisesNotificationEvent()
330441 var initialBookingResult = await _sut . MakeBooking ( initialBooking ) ;
331442 await _sut . ConfirmProvisionalBooking ( initialBookingResult . Reference , contactDetails , "" ) ;
332443
333- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
334- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST2" ) ;
444+ MockReferenceProvider ( "TEST2" ) ;
335445
336446 var rescheduledBooking = new Booking
337447 {
@@ -382,9 +492,7 @@ public async Task MakeBooking_ReturnsSuccess_WhenSlotIsAvailable()
382492 _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
383493
384494 MockAvailability ( availability ) ;
385-
386- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
387- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
495+ MockReferenceProvider ( "TEST1" ) ;
388496
389497 var result = await _sut . MakeBooking ( booking ) ;
390498 result . Success . Should ( ) . BeTrue ( ) ;
@@ -416,9 +524,7 @@ public async Task MakeBooking_FlagsBookingForReminder_WhenBooking()
416524 _siteLeaseManager . Setup ( x => x . Acquire ( It . IsAny < string > ( ) ) ) . Returns ( new FakeLeaseContext ( ) ) ;
417525
418526 MockAvailability ( availability ) ;
419-
420- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
421- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
527+ MockReferenceProvider ( "TEST1" ) ;
422528
423529 booking . ReminderSent = true ; // make sure we're not just testing the default value of False
424530
@@ -455,8 +561,7 @@ public async Task MakeBooking_ReturnsNonSuccess_WhenSlotIsNotAvailable()
455561 new DateTime ( 2077 , 1 , 1 , 13 , 10 , 0 , 0 ) ) { Services = [ "TDIFFSERV" ] }
456562 ] ) ;
457563
458- _referenceNumberProviderV1 . Setup ( x => x . GetReferenceNumber ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( "TEST1" ) ;
459- _referenceNumberProviderV2 . Setup ( x => x . GetReferenceNumber ( ) ) . ReturnsAsync ( "TEST1" ) ;
564+ MockReferenceProvider ( "TEST1" ) ;
460565
461566 var result = await _sut . MakeBooking ( booking ) ;
462567 result . Success . Should ( ) . BeFalse ( ) ;
0 commit comments