11using Microsoft . Extensions . Caching . Memory ;
2+ using Microsoft . Extensions . Logging ;
23using Microsoft . Extensions . Options ;
34using Nhs . Appointments . Core . ReferenceNumber . V2 ;
45
@@ -16,14 +17,15 @@ public class ProviderTests
1617
1718 private readonly Mock < IBookingReferenceDocumentStore > _bookingReferenceDocumentStore = new ( ) ;
1819 private readonly Mock < TimeProvider > _timeProvider = new ( ) ;
20+ private readonly Mock < ILogger < Provider > > _logger = new ( ) ;
1921 private readonly Mock < IOptions < ReferenceNumberOptions > > _options = new ( ) ;
2022
2123 private readonly IProvider _sut ;
2224
2325 public ProviderTests ( )
2426 {
2527 _options . Setup ( x => x . Value ) . Returns ( new ReferenceNumberOptions { HmacKeyVersion = 1 , HmacKey = HmacTestSecretKey } ) ;
26- _sut = new Provider ( _options . Object , _bookingReferenceDocumentStore . Object , new MemoryCache ( new MemoryCacheOptions ( ) ) , _timeProvider . Object ) ;
28+ _sut = new Provider ( _options . Object , _bookingReferenceDocumentStore . Object , new MemoryCache ( new MemoryCacheOptions ( ) ) , _logger . Object , _timeProvider . Object ) ;
2729 }
2830
2931 [ Fact ]
@@ -135,26 +137,60 @@ public async Task GetReferenceNumber_GeneratesCorrectlyFormattedNumber_MinSequen
135137 }
136138
137139 [ Fact ]
138- public async Task GetReferenceNumber_ChecksumDigitExists_MeansOnlyOneReferenceIsValid ( )
140+ public async Task GetReferenceNumber_LuhnChecksumDigitExists_MeansOnlyOneReferenceIsValid ( )
139141 {
140142 _timeProvider . Setup ( x => x . GetUtcNow ( ) ) . Returns ( new DateTime ( 2016 , 6 , 06 , 17 , 23 , 00 ) ) ;
141143 _bookingReferenceDocumentStore . Setup ( x => x . GetNextSequenceNumber ( ) ) . ReturnsAsync ( 76543789 ) ;
142144
143145 var result = await _sut . GetReferenceNumber ( ) ;
144146 result . Should ( ) . Be ( "4016-90927-6174" ) ;
145147 Assert . True ( _sut . IsValidBookingReference ( result ) ) ;
148+
149+ _logger . Verify ( x => x . Log (
150+ LogLevel . Warning ,
151+ It . IsAny < EventId > ( ) ,
152+ It . Is < It . IsAnyType > ( ( state , t ) =>
153+ state . ToString ( ) . Contains ( "Booking Reference '4016-90927-6174' does not pass the valid Luhn digit requirement." )
154+ ) ,
155+ It . IsAny < Exception > ( ) ,
156+ It . IsAny < Func < It . IsAnyType , Exception , string > > ( )
157+ ) , Times . Never
158+ ) ;
146159
147160 //the other 9 numbers with a different final digit should NOT be valid references
148161 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6171" ) ) ;
149162 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6172" ) ) ;
150163 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6173" ) ) ;
151-
152164 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6175" ) ) ;
153165 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6176" ) ) ;
154166 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6177" ) ) ;
155167 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6178" ) ) ;
156168 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6179" ) ) ;
157169 Assert . False ( _sut . IsValidBookingReference ( "4016-90927-6170" ) ) ;
170+
171+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6171" ) ;
172+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6172" ) ;
173+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6173" ) ;
174+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6175" ) ;
175+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6176" ) ;
176+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6177" ) ;
177+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6178" ) ;
178+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6179" ) ;
179+ VerifyLuhnDigitInvalidLogged ( "4016-90927-6170" ) ;
180+ }
181+
182+ private void VerifyLuhnDigitInvalidLogged ( string bookingReference )
183+ {
184+ _logger . Verify ( x => x . Log (
185+ LogLevel . Warning ,
186+ It . IsAny < EventId > ( ) ,
187+ It . Is < It . IsAnyType > ( ( state , t ) =>
188+ state . ToString ( ) . Contains ( $ "Booking Reference '{ bookingReference } ' does not pass the valid Luhn digit requirement.")
189+ ) ,
190+ It . IsAny < Exception > ( ) ,
191+ It . IsAny < Func < It . IsAnyType , Exception , string > > ( )
192+ ) , Times . Once
193+ ) ;
158194 }
159195
160196 [ Fact ]
0 commit comments