From 72a7a7136b99240ec3f45737e2edf6cb2529da2d Mon Sep 17 00:00:00 2001 From: SamRobinson75684 <166817125+SamRobinson75684@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:22:15 +0000 Subject: [PATCH] fix: sonar cloud issues (#554) * chore: refactoring ValidateDates unit tests and making IsValidDate static * fix: removing check for a exceptionQuery as it can never happen --- .../ProcessFileClasses/ValidateDates.cs | 19 ++- .../GetValidationExceptions.cs | 2 +- .../ValidateDates/ValidateDatesTests.cs | 111 +++++++++++------- 3 files changed, 78 insertions(+), 54 deletions(-) diff --git a/application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/ProcessFileClasses/ValidateDates.cs b/application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/ProcessFileClasses/ValidateDates.cs index 17545d416..d1ade51dd 100644 --- a/application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/ProcessFileClasses/ValidateDates.cs +++ b/application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/ProcessFileClasses/ValidateDates.cs @@ -17,44 +17,39 @@ public bool ValidateAllDates(Participant participant) { if (!IsValidDate(participant.CurrentPostingEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.CurrentPostingEffectiveFromDate)); + _logger.LogWarning("Invalid {CurrentPostingEffectiveFromDate} found in participant data", nameof(participant.CurrentPostingEffectiveFromDate)); return false; } if (!IsValidDate(participant.EmailAddressEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.EmailAddressEffectiveFromDate)); + _logger.LogWarning("Invalid {EmailAddressEffectiveFromDate} found in participant data", nameof(participant.EmailAddressEffectiveFromDate)); return false; } if (!IsValidDate(participant.MobileNumberEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.MobileNumberEffectiveFromDate)); + _logger.LogWarning("Invalid {MobileNumberEffectiveFromDate} found in participant data", nameof(participant.MobileNumberEffectiveFromDate)); return false; } if (!IsValidDate(participant.UsualAddressEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.UsualAddressEffectiveFromDate)); + _logger.LogWarning("Invalid {UsualAddressEffectiveFromDate} found in participant data", nameof(participant.UsualAddressEffectiveFromDate)); return false; } if (!IsValidDate(participant.TelephoneNumberEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.TelephoneNumberEffectiveFromDate)); + _logger.LogWarning("Invalid {TelephoneNumberEffectiveFromDate} found in participant data", nameof(participant.TelephoneNumberEffectiveFromDate)); return false; } if (!IsValidDate(participant.PrimaryCareProviderEffectiveFromDate)) { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.PrimaryCareProviderEffectiveFromDate)); - return false; - } - if (!IsValidDate(participant.CurrentPostingEffectiveFromDate)) - { - _logger.LogWarning("Invalid {datename} found in participant data", nameof(participant.CurrentPostingEffectiveFromDate)); + _logger.LogWarning("Invalid {PrimaryCareProviderEffectiveFromDate} found in participant data", nameof(participant.PrimaryCareProviderEffectiveFromDate)); return false; } return true; } - private bool IsValidDate(string? date) + private static bool IsValidDate(string? date) { if (date == null) { diff --git a/application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs b/application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs index 291ad4c0a..eeabf937e 100644 --- a/application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs +++ b/application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs @@ -58,7 +58,7 @@ public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] H var exceptionQuery = _validationData.GetAllExceptions(todayOnly).AsQueryable(); - if (exceptionQuery == null || exceptionQuery.Count() == 0) + if (exceptionQuery.Count() == 0) { return _createResponse.CreateHttpResponse(HttpStatusCode.NoContent, req); } diff --git a/tests/UnitTests/CaasIntegrationTests/ValidateDates/ValidateDatesTests.cs b/tests/UnitTests/CaasIntegrationTests/ValidateDates/ValidateDatesTests.cs index 9227cdac7..412632bbb 100644 --- a/tests/UnitTests/CaasIntegrationTests/ValidateDates/ValidateDatesTests.cs +++ b/tests/UnitTests/CaasIntegrationTests/ValidateDates/ValidateDatesTests.cs @@ -150,85 +150,114 @@ public void ValidateDates_ShouldReturnFalse_WhenUsualAddressEffectiveFromDateIsI } [TestMethod] - public void IsValidDate_ShouldReturnTrue_WhenDateIsNull() + public void ValidateDates_ShouldReturnTrue_WhenDatesAreNull() { // Arrange - string? date = null; - - - var method = _validateDates.GetType().GetMethod("IsValidDate", BindingFlags.Instance | BindingFlags.NonPublic); - var arguments = new object[] { date }; + var participant = new Participant + { + //all valid date formats + CurrentPostingEffectiveFromDate = null, + EmailAddressEffectiveFromDate = null, + MobileNumberEffectiveFromDate = null, + UsualAddressEffectiveFromDate = null, + TelephoneNumberEffectiveFromDate = null, + PrimaryCareProviderEffectiveFromDate = null + }; - // Act - var res = (bool)method.Invoke(_validateDates, arguments); + var res = _validateDates.ValidateAllDates(participant); // Assert Assert.IsTrue(res); + _loggerMock.Verify(x => x.Log(It.Is(l => l == LogLevel.Error), + It.IsAny(), + It.Is((v, t) => v.ToString().Contains("MobileNumberEffectiveFromDate")), + It.IsAny(), + It.IsAny>()), + Times.Never); } + [TestMethod] - public void IsValidDate_ShouldReturnTrue_WhenDateIsEmpty() + public void ValidateDates_ShouldReturnFalse_TelephoneNumberEffectiveFromDateIsInValid() { // Arrange - string date = string.Empty; - - var method = _validateDates.GetType().GetMethod("IsValidDate", BindingFlags.Instance | BindingFlags.NonPublic); - var arguments = new object[] { date }; - - // Act - + var participant = new Participant + { + //all valid date formats + CurrentPostingEffectiveFromDate = null, + EmailAddressEffectiveFromDate = null, + MobileNumberEffectiveFromDate = null, + UsualAddressEffectiveFromDate = null, + TelephoneNumberEffectiveFromDate = "12345678975657", + PrimaryCareProviderEffectiveFromDate = null + }; - var res = (bool)method.Invoke(_validateDates, arguments); + var res = _validateDates.ValidateAllDates(participant); // Assert - Assert.IsTrue(res); + Assert.IsFalse(res); } [TestMethod] - public void IsValidDate_ShouldReturnTrue_WhenDateLengthIs8() + public void ValidateDates_ShouldReturnFalse_MobileNumberEffectiveFromDateIsInValid() { // Arrange - string date = "20230101"; // Valid date format with 8 characters - - var method = _validateDates.GetType().GetMethod("IsValidDate", BindingFlags.Instance | BindingFlags.NonPublic); - var arguments = new object[] { date }; + var participant = new Participant + { + //all valid date formats + CurrentPostingEffectiveFromDate = null, + EmailAddressEffectiveFromDate = null, + MobileNumberEffectiveFromDate = "12345678975657", + UsualAddressEffectiveFromDate = null, + TelephoneNumberEffectiveFromDate = null, + PrimaryCareProviderEffectiveFromDate = null + }; - // Act - var res = (bool)method.Invoke(_validateDates, arguments); + var res = _validateDates.ValidateAllDates(participant); // Assert - Assert.IsTrue(res); + Assert.IsFalse(res); } [TestMethod] - public void IsValidDate_ShouldReturnFalse_WhenDateLengthIsGreaterThan8() + public void ValidateDates_ShouldReturnFalse_PrimaryCareProviderEffectiveFromDateIsInvalid() { // Arrange - string date = "20230101234"; // Invalid date with more than 8 characters - - var method = _validateDates.GetType().GetMethod("IsValidDate", BindingFlags.Instance | BindingFlags.NonPublic); - var arguments = new object[] { date }; + var participant = new Participant + { + //all valid date formats + CurrentPostingEffectiveFromDate = null, + EmailAddressEffectiveFromDate = null, + MobileNumberEffectiveFromDate = null, + UsualAddressEffectiveFromDate = null, + TelephoneNumberEffectiveFromDate = null, + PrimaryCareProviderEffectiveFromDate = "12345678975657" + }; - // Act - var res = (bool)method.Invoke(_validateDates, arguments); + var res = _validateDates.ValidateAllDates(participant); // Assert Assert.IsFalse(res); } [TestMethod] - public void IsValidDate_ShouldReturnTrue_WhenDateLengthIsLessThan8() + public void ValidateDates_ShouldReturnFalse_CurrentPostingEffectiveFromDateIsInValid() { // Arrange - string date = "202301"; // Valid date with less than 8 characters - - var method = _validateDates.GetType().GetMethod("IsValidDate", BindingFlags.Instance | BindingFlags.NonPublic); - var arguments = new object[] { date }; + var participant = new Participant + { + //all valid date formats + CurrentPostingEffectiveFromDate = "12345678975657", + EmailAddressEffectiveFromDate = null, + MobileNumberEffectiveFromDate = null, + UsualAddressEffectiveFromDate = null, + TelephoneNumberEffectiveFromDate = null, + PrimaryCareProviderEffectiveFromDate = null + }; - // Act - var res = (bool)method.Invoke(_validateDates, arguments); + var res = _validateDates.ValidateAllDates(participant); // Assert - Assert.IsTrue(res); + Assert.IsFalse(res); } }