Skip to content

Commit 973ed23

Browse files
committed
Block Participant Tests
1 parent d32e4df commit 973ed23

2 files changed

Lines changed: 199 additions & 2 deletions

File tree

application/CohortManager/src/Functions/ParticipantManagementServices/UpdateBlockedFlag/UpdateBlockedFlag.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public async Task<HttpResponseData> BlockParticipant([HttpTrigger(AuthorizationL
4141
_logger.LogInformation("Block Participant Called");
4242
try
4343
{
44-
var blockParticipantDTO = await req.ReadFromJsonAsync<BlockParticipantDto>();
45-
44+
var blockParticipantDTOJson = req.ReadAsString();
45+
var blockParticipantDTO = JsonSerializer.Deserialize<BlockParticipantDto>(blockParticipantDTOJson);
4646
if (blockParticipantDTO == null)
4747
{
4848
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Unable to parse request");

tests/UnitTests/ParticipantManagementServicesTests/UpdateBlockedFlagTests/UpdateBlockedFlagTests.cs

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace NHS.CohortManager.Tests.ParticipantManagementServiceTests;
1515
using System.Runtime.CompilerServices;
1616
using Microsoft.Extensions.Options;
1717
using System.Text.Json;
18+
using System.Text;
1819

1920
[TestClass]
2021
public 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

Comments
 (0)