Skip to content

Commit

Permalink
fix: sonar cloud issues (#554)
Browse files Browse the repository at this point in the history
* chore: refactoring ValidateDates unit tests and making IsValidDate static

* fix: removing check for a exceptionQuery as it can never happen
  • Loading branch information
SamRobinson75684 authored Jan 15, 2025
1 parent 359d803 commit 72a7a71
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogLevel>(l => l == LogLevel.Error),
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("MobileNumberEffectiveFromDate")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
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);
}
}

0 comments on commit 72a7a71

Please sign in to comment.