@@ -15,6 +15,7 @@ namespace NHS.CohortManager.Tests.ParticipantManagementServiceTests;
1515using System . Runtime . CompilerServices ;
1616using Microsoft . Extensions . Options ;
1717using System . Text . Json ;
18+ using System . Text ;
1819
1920[ TestClass ]
2021public class UpdateBlockedFlagTests
@@ -44,6 +45,17 @@ public UpdateBlockedFlagTests()
4445 } ) ;
4546 _blockParticipantHandler = new BlockParticipantHandler ( _mockHandlerLogger . Object , _mockParticipantManagementClient . Object , _mockParticipantDemographicClient . Object , _mockHttpClient . Object , _mockConfig . Object ) ;
4647 _sut = new UpdateBlockedFlag ( _mockUpdateBlockedFlagLogger . Object , _mockCreateResponse . Object , _blockParticipantHandler ) ;
48+
49+ _mockCreateResponse . Setup ( x => x . CreateHttpResponseWithBodyAsync (
50+ It . IsAny < HttpStatusCode > ( ) ,
51+ It . IsAny < HttpRequestData > ( ) ,
52+ It . IsAny < string > ( ) ) )
53+ . Returns ( ( HttpStatusCode statusCode , HttpRequestData req , string responseBody ) =>
54+ {
55+ var response = req . CreateResponse ( statusCode ) ;
56+ response . WriteString ( responseBody ) ;
57+ return Task . FromResult ( response ) ;
58+ } ) ;
4759 }
4860
4961 [ TestMethod ]
@@ -66,6 +78,13 @@ public async Task BlockParticipant_ExistingParticipant_ReturnsSuccess()
6678 BlockedFlag = 0 ,
6779
6880 } ) ;
81+ _mockParticipantDemographicClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantDemographic , bool > > > ( ) ) )
82+ . ReturnsAsync ( new ParticipantDemographic
83+ {
84+ NhsNumber = 6427635034 ,
85+ FamilyName = "Jones" ,
86+ DateOfBirth = "19231012"
87+ } ) ;
6988
7089 _mockParticipantManagementClient . Setup ( x => x . Update ( It . IsAny < ParticipantManagement > ( ) ) )
7190 . ReturnsAsync ( true ) ;
@@ -81,7 +100,185 @@ public async Task BlockParticipant_ExistingParticipant_ReturnsSuccess()
81100
82101 //asset
83102 Assert . AreEqual ( HttpStatusCode . OK , result . StatusCode ) ;
103+ _mockHttpClient . Verify ( x => x . SendPost ( "NemsUnsubscribeUrl" , It . IsAny < Dictionary < string , string > > ( ) ) , Times . Once ) ;
104+ _mockParticipantManagementClient . Verify ( x => x . Update ( It . IsAny < ParticipantManagement > ( ) ) , Times . Once ) ;
105+ _mockHttpClient . VerifyNoOtherCalls ( ) ;
106+ }
107+ [ TestMethod ]
108+ public async Task BlockParticipant_NonExistentParticipant_ReturnsSuccess ( )
109+ {
110+ //arrange
111+ var requestBody = new BlockParticipantDto
112+ {
113+ NhsNumber = 6427635034 ,
114+ FamilyName = "Jones" ,
115+ DateOfBirth = "1923-10-12"
116+ } ;
117+
118+ _request = _setupRequest . Setup ( JsonSerializer . Serialize ( requestBody ) ) ;
119+
120+ _mockParticipantManagementClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) )
121+ . Returns ( Task . FromResult < ParticipantManagement > ( null ! ) ) ;
122+
123+
124+ var pdsDemoResponse = JsonSerializer . Serialize (
125+ new ParticipantDemographic
126+ {
127+ NhsNumber = 6427635034 ,
128+ FamilyName = "Jones" ,
129+ DateOfBirth = "19231012"
130+ } ) ;
131+
132+ _mockHttpClient . Setup ( x => x . SendGet ( "RetrievePdsDemographicUrl" , It . IsAny < Dictionary < string , string > > ( ) ) )
133+ . ReturnsAsync ( pdsDemoResponse ) ;
134+
135+ _mockParticipantManagementClient . Setup ( x => x . Add ( It . IsAny < ParticipantManagement > ( ) ) )
136+ . ReturnsAsync ( true ) ;
137+
138+
139+ //act
140+ var result = await _sut . BlockParticipant ( _request . Object ) ;
141+
142+ //asset
143+ Assert . AreEqual ( HttpStatusCode . OK , result . StatusCode ) ;
144+ _mockHttpClient . Verify ( x => x . SendGet ( "RetrievePdsDemographicUrl" , It . IsAny < Dictionary < string , string > > ( ) ) , Times . Once ) ;
145+ _mockParticipantManagementClient . Verify ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) ) ;
146+ _mockParticipantManagementClient . Verify ( x => x . Add ( It . IsAny < ParticipantManagement > ( ) ) , Times . Once ) ;
147+ _mockParticipantManagementClient . VerifyNoOtherCalls ( ) ;
148+ _mockHttpClient . VerifyNoOtherCalls ( ) ;
149+ }
150+ [ TestMethod ]
151+ public async Task BlockParticipant_InvalidNhsNumber_ReturnsFailure ( )
152+ {
153+ //arrange
154+ var requestBody = new BlockParticipantDto
155+ {
156+ NhsNumber = 6427635035 ,
157+ FamilyName = "Jones" ,
158+ DateOfBirth = "1923-10-12"
159+ } ;
160+
161+ _request = _setupRequest . Setup ( JsonSerializer . Serialize ( requestBody ) ) ;
162+
163+ //act
164+ var result = await _sut . BlockParticipant ( _request . Object ) ;
165+
166+ //asset
167+ Assert . AreEqual ( HttpStatusCode . BadRequest , result . StatusCode ) ;
168+ }
169+ [ TestMethod ]
170+ public async Task BlockParticipant_ParticipantAlreadyBlocked_ReturnsFailure ( )
171+ {
172+ //arrange
173+ var requestBody = new BlockParticipantDto
174+ {
175+ NhsNumber = 6427635034 ,
176+ FamilyName = "Jones" ,
177+ DateOfBirth = "1923-10-12"
178+ } ;
179+
180+ _request = _setupRequest . Setup ( JsonSerializer . Serialize ( requestBody ) ) ;
181+
182+ _mockParticipantManagementClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) )
183+ . ReturnsAsync ( new ParticipantManagement
184+ {
185+ NHSNumber = 6427635034 ,
186+ BlockedFlag = 1 ,
187+
188+ } ) ;
189+
190+ //act
191+ var result = await _sut . BlockParticipant ( _request . Object ) ;
192+
193+ //asset
194+ Assert . AreEqual ( HttpStatusCode . BadRequest , result . StatusCode ) ;
195+ _mockHttpClient . Verify ( x => x . SendPost ( "NemsUnsubscribeUrl" , It . IsAny < Dictionary < string , string > > ( ) ) , Times . Never ) ;
196+ _mockHttpClient . VerifyNoOtherCalls ( ) ;
197+ _mockParticipantManagementClient . Verify ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) , Times . Once ) ;
198+ _mockParticipantManagementClient . VerifyNoOtherCalls ( ) ;
199+
200+ }
201+ [ TestMethod ]
202+ public async Task BlockParticipant_ExistingParticipantFailsThreePointCheck_ReturnsSuccess ( )
203+ {
204+ //arrange
205+ var requestBody = new BlockParticipantDto
206+ {
207+ NhsNumber = 6427635034 ,
208+ FamilyName = "Jones" ,
209+ DateOfBirth = "1923-10-12"
210+ } ;
211+
212+ _request = _setupRequest . Setup ( JsonSerializer . Serialize ( requestBody ) ) ;
213+
214+ _mockParticipantManagementClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) )
215+ . ReturnsAsync ( new ParticipantManagement
216+ {
217+ NHSNumber = 6427635034 ,
218+ BlockedFlag = 0 ,
84219
220+ } ) ;
221+ _mockParticipantDemographicClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantDemographic , bool > > > ( ) ) )
222+ . ReturnsAsync ( new ParticipantDemographic
223+ {
224+ NhsNumber = 6427635034 ,
225+ FamilyName = "Davies" ,
226+ DateOfBirth = "19231012"
227+ } ) ;
228+
229+ //act
230+ var result = await _sut . BlockParticipant ( _request . Object ) ;
231+
232+ //asset
233+ Assert . AreEqual ( HttpStatusCode . BadRequest , result . StatusCode ) ;
234+ _mockHttpClient . Verify ( x => x . SendPost ( "NemsUnsubscribeUrl" , It . IsAny < Dictionary < string , string > > ( ) ) , Times . Never ) ;
235+ _mockHttpClient . VerifyNoOtherCalls ( ) ;
236+ _mockParticipantManagementClient . Verify ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) , Times . Once ) ;
237+ _mockParticipantManagementClient . VerifyNoOtherCalls ( ) ;
238+ _mockParticipantDemographicClient . Verify ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantDemographic , bool > > > ( ) ) , Times . Once ) ;
239+ _mockParticipantDemographicClient . VerifyNoOtherCalls ( ) ;
240+ }
241+ [ TestMethod ]
242+ public async Task BlockParticipant_NonExistentParticipantFailsThreePointCheck_ReturnsFailure ( )
243+ {
244+ //arrange
245+ var requestBody = new BlockParticipantDto
246+ {
247+ NhsNumber = 6427635034 ,
248+ FamilyName = "Jones" ,
249+ DateOfBirth = "1923-10-12"
250+ } ;
251+
252+ _request = _setupRequest . Setup ( JsonSerializer . Serialize ( requestBody ) ) ;
253+
254+ _mockParticipantManagementClient . Setup ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) )
255+ . Returns ( Task . FromResult < ParticipantManagement > ( null ! ) ) ;
256+
257+
258+ var pdsDemoResponse = JsonSerializer . Serialize (
259+ new ParticipantDemographic
260+ {
261+ NhsNumber = 6427635034 ,
262+ FamilyName = "Davies" ,
263+ DateOfBirth = "19231012"
264+ } ) ;
265+
266+ _mockHttpClient . Setup ( x => x . SendGet ( "RetrievePdsDemographicUrl" , It . IsAny < Dictionary < string , string > > ( ) ) )
267+ . ReturnsAsync ( pdsDemoResponse ) ;
268+
269+ _mockParticipantManagementClient . Setup ( x => x . Add ( It . IsAny < ParticipantManagement > ( ) ) )
270+ . ReturnsAsync ( true ) ;
271+
272+
273+ //act
274+ var result = await _sut . BlockParticipant ( _request . Object ) ;
275+
276+ //asset
277+ Assert . AreEqual ( HttpStatusCode . BadRequest , result . StatusCode ) ;
278+ _mockHttpClient . Verify ( x => x . SendGet ( "RetrievePdsDemographicUrl" , It . IsAny < Dictionary < string , string > > ( ) ) , Times . Once ) ;
279+ _mockParticipantManagementClient . Verify ( x => x . GetSingleByFilter ( It . IsAny < Expression < Func < ParticipantManagement , bool > > > ( ) ) ) ;
280+ _mockParticipantManagementClient . VerifyNoOtherCalls ( ) ;
281+ _mockHttpClient . VerifyNoOtherCalls ( ) ;
85282 }
86283
87284
0 commit comments