|
| 1 | +namespace NHS.CohortManager.Tests.CohortDistributionServiceTests; |
| 2 | + |
| 3 | +using DataServices.Client; |
| 4 | +using Microsoft.Extensions.Logging.Abstractions; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using Model; |
| 7 | +using Model.Enums; |
| 8 | +using Moq; |
| 9 | +using NHS.CohortManager.CohortDistributionServices; |
| 10 | +using Common; |
| 11 | + |
| 12 | +[TestClass] |
| 13 | +public class AllocateServiceProviderTests |
| 14 | +{ |
| 15 | + private readonly DistributeParticipantActivities _distributionParticipantActivities; |
| 16 | + private readonly Mock<IAllocationConfigProvider> _allocationConfigProvider = new(); |
| 17 | + private readonly Mock<IDataServiceClient<CohortDistribution>> _cohortDistributionClient = new(); |
| 18 | + private readonly Mock<IDataServiceClient<ParticipantManagement>> _participantManagementClient = new(); |
| 19 | + private readonly Mock<IDataServiceClient<ParticipantDemographic>> _participantDemographicClient = new(); |
| 20 | + private readonly Mock<IHttpClientFunction> _httpClientFunction = new(); |
| 21 | + private readonly Mock<IOptions<DistributeParticipantConfig>> _config = new(); |
| 22 | + |
| 23 | + public AllocateServiceProviderTests() |
| 24 | + { |
| 25 | + DistributeParticipantConfig config = new() |
| 26 | + { |
| 27 | + LookupValidationURL = "LookupValidationURL", |
| 28 | + StaticValidationURL = "StaticValidationURL", |
| 29 | + TransformDataServiceURL = "TransformDataServiceURL", |
| 30 | + ParticipantManagementUrl = "ParticipantManagementUrl", |
| 31 | + CohortDistributionDataServiceUrl = "CohortDistributionDataServiceUrl", |
| 32 | + ParticipantDemographicDataServiceUrl = "ParticipantDemographicDataServiceUrl", |
| 33 | + CohortDistributionTopic = "cohort-distribution-topic", |
| 34 | + DistributeParticipantSubscription = "distribute-participant-sub", |
| 35 | + RemoveOldValidationRecordUrl = "RemoveOldValidationRecordUrl", |
| 36 | + SendServiceNowMessageURL = "SendServiceNowMessageURL" |
| 37 | + }; |
| 38 | + |
| 39 | + _config.Setup(x => x.Value).Returns(config); |
| 40 | + |
| 41 | + _distributionParticipantActivities = new( |
| 42 | + _cohortDistributionClient.Object, |
| 43 | + _participantManagementClient.Object, |
| 44 | + _participantDemographicClient.Object, |
| 45 | + _config.Object, |
| 46 | + NullLogger<DistributeParticipantActivities>.Instance, |
| 47 | + _httpClientFunction.Object, |
| 48 | + _allocationConfigProvider.Object |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public async Task AllocateServiceProvider_PostcodeIsNull_ReturnsBSSDefault() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var participant = new CohortDistributionParticipant { Postcode = null, ScreeningAcronym = "BSS" }; |
| 58 | + |
| 59 | + // Act |
| 60 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.AreEqual(EnumHelper.GetDisplayName(ServiceProvider.BSS), result); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public async Task AllocateServiceProvider_PostcodeIsEmpty_ReturnsBSSDefault() |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + var participant = new CohortDistributionParticipant { Postcode = "", ScreeningAcronym = "BSS" }; |
| 71 | + |
| 72 | + // Act |
| 73 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 74 | + |
| 75 | + // Assert |
| 76 | + Assert.AreEqual(EnumHelper.GetDisplayName(ServiceProvider.BSS), result); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public async Task AllocateServiceProvider_ScreeningAcronymIsNull_ReturnsBSSDefault() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = null }; |
| 84 | + |
| 85 | + // Act |
| 86 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 87 | + |
| 88 | + // Assert |
| 89 | + Assert.AreEqual(EnumHelper.GetDisplayName(ServiceProvider.BSS), result); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public async Task AllocateServiceProvider_ScreeningAcronymIsEmpty_ReturnsBSSDefault() |
| 94 | + { |
| 95 | + // Arrange |
| 96 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "" }; |
| 97 | + |
| 98 | + // Act |
| 99 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 100 | + |
| 101 | + // Assert |
| 102 | + Assert.AreEqual(EnumHelper.GetDisplayName(ServiceProvider.BSS), result); |
| 103 | + } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public async Task AllocateServiceProvider_MatchingPostcodeAndScreeningService_ReturnsConfiguredProvider() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "BSS" }; |
| 110 | + var configData = new AllocationConfigDataList |
| 111 | + { |
| 112 | + ConfigDataList = |
| 113 | + [ |
| 114 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "TestServiceProvider" } |
| 115 | + ] |
| 116 | + }; |
| 117 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 118 | + |
| 119 | + // Act |
| 120 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 121 | + |
| 122 | + // Assert |
| 123 | + Assert.AreEqual("TestServiceProvider", result); |
| 124 | + } |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public async Task AllocateServiceProvider_NoMatchingPostcode_ReturnsBSSelect() |
| 128 | + { |
| 129 | + // Arrange |
| 130 | + var participant = new CohortDistributionParticipant { Postcode = "ZZ1 2CD", ScreeningAcronym = "BSS" }; |
| 131 | + var configData = new AllocationConfigDataList |
| 132 | + { |
| 133 | + ConfigDataList = |
| 134 | + [ |
| 135 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "TestServiceProvider" } |
| 136 | + ] |
| 137 | + }; |
| 138 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 139 | + |
| 140 | + // Act |
| 141 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 142 | + |
| 143 | + // Assert |
| 144 | + Assert.AreEqual("BS SELECT", result); |
| 145 | + } |
| 146 | + |
| 147 | + [TestMethod] |
| 148 | + public async Task AllocateServiceProvider_NoMatchingScreeningService_ReturnsBSSelect() |
| 149 | + { |
| 150 | + // Arrange |
| 151 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "CSS" }; |
| 152 | + var configData = new AllocationConfigDataList |
| 153 | + { |
| 154 | + ConfigDataList = |
| 155 | + [ |
| 156 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "TestServiceProvider" } |
| 157 | + ] |
| 158 | + }; |
| 159 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 160 | + |
| 161 | + // Act |
| 162 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 163 | + |
| 164 | + // Assert |
| 165 | + Assert.AreEqual("BS SELECT", result); |
| 166 | + } |
| 167 | + |
| 168 | + [TestMethod] |
| 169 | + public async Task AllocateServiceProvider_MultipleMatches_ReturnsLongestPostcodeMatch() |
| 170 | + { |
| 171 | + // Arrange |
| 172 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "BSS" }; |
| 173 | + var configData = new AllocationConfigDataList |
| 174 | + { |
| 175 | + ConfigDataList = |
| 176 | + [ |
| 177 | + new AllocationConfigData { Postcode = "A", ScreeningService = "BSS", ServiceProvider = "Short Match" }, |
| 178 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "Longer Match" }, |
| 179 | + new AllocationConfigData { Postcode = "AB1", ScreeningService = "BSS", ServiceProvider = "Longest Match" } |
| 180 | + ] |
| 181 | + }; |
| 182 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 183 | + |
| 184 | + // Act |
| 185 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 186 | + |
| 187 | + // Assert |
| 188 | + Assert.AreEqual("Longest Match", result); |
| 189 | + } |
| 190 | + |
| 191 | + [TestMethod] |
| 192 | + public async Task AllocateServiceProvider_PostcodeMatchIsCaseInsensitive_ReturnsMatch() |
| 193 | + { |
| 194 | + // Arrange |
| 195 | + var participant = new CohortDistributionParticipant { Postcode = "ab1 2cd", ScreeningAcronym = "BSS" }; |
| 196 | + var configData = new AllocationConfigDataList |
| 197 | + { |
| 198 | + ConfigDataList = |
| 199 | + [ |
| 200 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "TestServiceProvider" } |
| 201 | + ] |
| 202 | + }; |
| 203 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 204 | + |
| 205 | + // Act |
| 206 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 207 | + |
| 208 | + // Assert |
| 209 | + Assert.AreEqual("TestServiceProvider", result); |
| 210 | + } |
| 211 | + |
| 212 | + [TestMethod] |
| 213 | + public async Task AllocateServiceProvider_ScreeningAcronymMatchIsCaseInsensitive_ReturnsMatch() |
| 214 | + { |
| 215 | + // Arrange |
| 216 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "bss" }; |
| 217 | + var configData = new AllocationConfigDataList |
| 218 | + { |
| 219 | + ConfigDataList = |
| 220 | + [ |
| 221 | + new AllocationConfigData { Postcode = "AB", ScreeningService = "BSS", ServiceProvider = "TestServiceProvider" } |
| 222 | + ] |
| 223 | + }; |
| 224 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 225 | + |
| 226 | + // Act |
| 227 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 228 | + |
| 229 | + // Assert |
| 230 | + Assert.AreEqual("TestServiceProvider", result); |
| 231 | + } |
| 232 | + |
| 233 | + [TestMethod] |
| 234 | + public async Task AllocateServiceProvider_EmptyConfigDataList_ReturnsBSSelect() |
| 235 | + { |
| 236 | + // Arrange |
| 237 | + var participant = new CohortDistributionParticipant { Postcode = "AB1 2CD", ScreeningAcronym = "BSS" }; |
| 238 | + var configData = new AllocationConfigDataList { ConfigDataList = [] }; |
| 239 | + _allocationConfigProvider.Setup(x => x.GetConfig()).Returns(configData); |
| 240 | + |
| 241 | + // Act |
| 242 | + var result = await _distributionParticipantActivities.AllocateServiceProvider(participant); |
| 243 | + |
| 244 | + // Assert |
| 245 | + Assert.AreEqual("BS SELECT", result); |
| 246 | + } |
| 247 | +} |
0 commit comments