Skip to content

Commit

Permalink
Unit tests and tidy up (#334)
Browse files Browse the repository at this point in the history
* Fixing typo. RuleTemplateMessageRunner > RuleMessageTemplateRunner

* Added unit tests
  • Loading branch information
chrisjensenuk authored and Lee Wadhams committed Nov 2, 2018
1 parent 39ee012 commit 25a8ce8
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Employer/Employer.Web/Configuration/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void RegisterMapperDeps(IServiceCollection services)
{
services.AddTransient<DisplayVacancyViewModelMapper>();
services.AddTransient<ReviewFieldIndicatorMapper>();
services.AddScoped<IRuleMessageTemplateRunner, RuleTemplateMessageRunner>();
services.AddScoped<IRuleMessageTemplateRunner, RuleMessageTemplateRunner>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using Esfa.Recruit.Employer.Web.Mappings;
using Esfa.Recruit.Shared.Web.RuleTemplates;
using Esfa.Recruit.Vacancies.Client.Application.Rules;
using Esfa.Recruit.Vacancies.Client.Domain.Entities;
using FluentAssertions;
using Xunit;

namespace Esfa.Recruit.Employer.UnitTests.Employer.Web.Mappings
{
public class ReviewFieldIndicatorMapperTests
{
[Fact]
public void ShouldMap()
{
var shortDescriptionProfanityCheckRuleOutcome =
new RuleOutcome(
RuleId.ProfanityChecks,
100,
"Profanity 'drat' found in 'ShortDescription'",
"ShortDescription",
new List<RuleOutcome>(), "{\"Profanity\" : \"drat\",\"Occurrences\" : 1}");

var titleProfanityCheckRuleOutcome = new RuleOutcome(
RuleId.ProfanityChecks,
0,
"No profanities found in 'Title'",
"Title");

var profanityChecksRuleOutcome = new RuleOutcome(
RuleId.ProfanityChecks,
100,
"No profanities found in 'Title', Profanity 'drat' found in 'ShortDescription', etc",
"",
new List<RuleOutcome>
{
shortDescriptionProfanityCheckRuleOutcome,
titleProfanityCheckRuleOutcome
}
);

var sut = new ReviewFieldIndicatorMapper(new RuleMessageTemplateRunner());

var review = new VacancyReview
{
ManualQaFieldIndicators = new List<ManualQaFieldIndicator>
{
new ManualQaFieldIndicator
{
IsChangeRequested = false,
FieldIdentifier = VacancyReview.FieldIdentifiers.Title
},
new ManualQaFieldIndicator
{
IsChangeRequested = true,
FieldIdentifier = VacancyReview.FieldIdentifiers.ShortDescription
}
},
AutomatedQaOutcomeIndicators = new List<RuleOutcomeIndicator>
{
new RuleOutcomeIndicator
{
IsReferred = false,
RuleOutcomeId = titleProfanityCheckRuleOutcome.Id
},
new RuleOutcomeIndicator
{
IsReferred = true,
RuleOutcomeId = shortDescriptionProfanityCheckRuleOutcome.Id
}
},
AutomatedQaOutcome = new RuleSetOutcome {RuleOutcomes = new List<RuleOutcome> {profanityChecksRuleOutcome}}
};

var vm = sut.MapFromFieldIndicators(ReviewFieldMappingLookups.GetPreviewReviewFieldIndicators(), review).ToList();

vm.Count.Should().Be(1);

var shortDescription = vm.Single(v => v.ReviewFieldIdentifier == VacancyReview.FieldIdentifiers.ShortDescription);

shortDescription.ManualQaText.Should().NotBeNullOrEmpty();

shortDescription.AutoQaTexts.Count.Should().Be(1);
shortDescription.AutoQaTexts[0].Should().Be("Brief overview contains the phrase 'drat'");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Esfa.Recruit.Employer.Web.Services;
using Esfa.Recruit.Vacancies.Client.Infrastructure.Client;
using Esfa.Recruit.Vacancies.Client.Infrastructure.QueryStore.Projections.EditVacancyInfo;
using FluentAssertions;
using Moq;
using Xunit;

namespace Esfa.Recruit.Employer.UnitTests.Employer.Web.Services
{
public class LegalEntityAgreementServiceTests
{
const string EmployerAccountId = "ABCDEF";
const long LegalEntityId = 1234;

private Mock<IEmployerVacancyClient> _clientMock;

[Fact]
public void HasLegalEntityAgreementAsync_ShouldReturnFalseIfNoMatchingLegalEntity()
{
var sut = GetLegalEntityAgreementService(EmployerAccountId, 5678, true, 5678, true);

var result = sut.HasLegalEntityAgreementAsync(EmployerAccountId, LegalEntityId).Result;

result.Should().BeFalse();
_clientMock.Verify(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId), Times.Never);
}

[Fact]
public void HasLegalEntityAgreementAsync_ShouldNotCheckEmployerServiceWhenHasAgreement()
{
var sut = GetLegalEntityAgreementService(EmployerAccountId, LegalEntityId, true, LegalEntityId, true);

var result = sut.HasLegalEntityAgreementAsync(EmployerAccountId, LegalEntityId).Result;

result.Should().BeTrue();
_clientMock.Verify(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId), Times.Never);
}

[Fact] public void HasLegalEntityAgreementAsync_ShouldCheckEmployerServiceWhenHasNoAgreement()
{
var sut = GetLegalEntityAgreementService(EmployerAccountId, LegalEntityId, false, LegalEntityId, true);

var result = sut.HasLegalEntityAgreementAsync(EmployerAccountId, LegalEntityId).Result;

result.Should().BeTrue();
_clientMock.Verify(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId), Times.Once);
_clientMock.Verify(c => c.SetupEmployerAsync(EmployerAccountId), Times.Once);
}

[Fact] public void HasLegalEntityAgreementAsync_ShouldReturnFalseWhenEmployerServiceLegalEntityHasNoAgreement()
{
var sut = GetLegalEntityAgreementService(EmployerAccountId, LegalEntityId, false, LegalEntityId, false);

var result = sut.HasLegalEntityAgreementAsync(EmployerAccountId, LegalEntityId).Result;

result.Should().BeFalse();
_clientMock.Verify(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId), Times.Once);
_clientMock.Verify(c => c.SetupEmployerAsync(EmployerAccountId), Times.Never);
}

[Fact]
public void HasLegalEntityAgreementAsync_ShouldReturnFalseWhenEmployerServiceCantLocateLegalEntity()
{
var sut = GetLegalEntityAgreementService(EmployerAccountId, LegalEntityId, false, 5678, true);

var result = sut.HasLegalEntityAgreementAsync(EmployerAccountId, LegalEntityId).Result;

result.Should().BeFalse();
_clientMock.Verify(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId), Times.Once);
_clientMock.Verify(c => c.SetupEmployerAsync(EmployerAccountId), Times.Never);
}

private LegalEntityAgreementService GetLegalEntityAgreementService(string employerAccountId, long legalEntityId, bool hasLegalEntityAgreement, long employerServiceLegalEntityId, bool employerServiceHasLegalEntityAgreement)
{
_clientMock = new Mock<IEmployerVacancyClient>();
_clientMock.Setup(c => c.GetEditVacancyInfoAsync(employerAccountId)).Returns(Task.FromResult(
new EditVacancyInfo
{
LegalEntities = new List<LegalEntity>
{
new LegalEntity{LegalEntityId = legalEntityId, HasLegalEntityAgreement = hasLegalEntityAgreement}
}
}));

_clientMock.Setup(c => c.GetEmployerLegalEntitiesAsync(EmployerAccountId)).Returns(Task.FromResult(
new List<LegalEntity>
{
new LegalEntity
{
LegalEntityId = employerServiceLegalEntityId,
HasLegalEntityAgreement = employerServiceHasLegalEntityAgreement
}
}
.AsEnumerable()));

return new LegalEntityAgreementService(_clientMock.Object);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void ShouldRedirectToEmployer(string route, bool shouldRedirect)
[InlineData(RouteNames.ShortDescription_Post, false)]
[InlineData(RouteNames.Employer_Get, false)]
[InlineData(RouteNames.Employer_Post, false)]
[InlineData(RouteNames.LegalEntityAgreement_SoftStop_Get, false)]
[InlineData(RouteNames.Training_Get, false)]
[InlineData(RouteNames.Training_Post, false)]
[InlineData("any other route", true)]
Expand All @@ -95,6 +96,7 @@ public void ShouldRedirectToTraining(string route, bool shouldRedirect)
[InlineData(RouteNames.ShortDescription_Post, false)]
[InlineData(RouteNames.Employer_Get, false)]
[InlineData(RouteNames.Employer_Post, false)]
[InlineData(RouteNames.LegalEntityAgreement_SoftStop_Get, false)]
[InlineData(RouteNames.Training_Get, false)]
[InlineData(RouteNames.Training_Post, false)]
[InlineData(RouteNames.Wage_Get, false)]
Expand Down
2 changes: 1 addition & 1 deletion src/QA/QA.Web/Startup.ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IGeocodeImageService>(_ => new GoogleMapsGeocodeImageService(_configuration.GetValue<string>("GoogleMapsPrivateKey")));
services.AddScoped<ReviewMapper>();

services.AddScoped<IRuleMessageTemplateRunner, RuleTemplateMessageRunner>();
services.AddScoped<IRuleMessageTemplateRunner, RuleMessageTemplateRunner>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Esfa.Recruit.Shared.Web.RuleTemplates
{
public class RuleTemplateMessageRunner : IRuleMessageTemplateRunner
public class RuleMessageTemplateRunner : IRuleMessageTemplateRunner
{
public string ToText(RuleId ruleId, string data, string fieldName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Esfa.Recruit.Shared.Web.RuleTemplates;
using Esfa.Recruit.Vacancies.Client.Application.Rules;
using FluentAssertions;
using Xunit;

namespace Esfa.Recruit.Vacancies.Client.UnitTests.Shared.Web.RuleTemplates
{
public class RuleMessageTemplateRunnerTests
{
[Fact]
public void ShouldCreateProfanityCheckMessage()
{
var sut = new RuleMessageTemplateRunner();

var actual = sut.ToText(RuleId.ProfanityChecks, "{\"Profanity\":\"drat\",\"Occurrences\":1}", "Profanity field");
actual.Should().Be("Profanity field contains the phrase 'drat'");

actual = sut.ToText(RuleId.ProfanityChecks, "{\"Profanity\":\"drat\",\"Occurrences\":3}", "Profanity field");
actual.Should().Be("Profanity field contains the phrase 'drat' 3 times");
}

[Fact]
public void ShouldCreateBannedPhraseCheckMessage()
{
var sut = new RuleMessageTemplateRunner();

var actual = sut.ToText(RuleId.BannedPhraseChecks, "{\"BannedPhrase\":\"driving licence\",\"Occurrences\":1}", "Banned phrase field");
actual.Should().Be("Banned phrase field contains the phrase 'driving licence'");

actual = sut.ToText(RuleId.BannedPhraseChecks, "{\"BannedPhrase\":\"driving licence\",\"Occurrences\":3}", "Banned phrase field");

actual.Should().Be("Banned phrase field contains the phrase 'driving licence' 3 times");
}

[Fact]
public void ShouldCreateTitlePopularityCheckMessage()
{
var sut = new RuleMessageTemplateRunner();

var actual = sut.ToText(RuleId.TitlePopularity, "{\"TrainingCode\":\"CODE\",\"TrainingTitle\":\"TITLE\",\"TrainingType\":\"TYPE\"}", "Title field");

actual.Should().Be("The title field is not common for apprenticeships with the training TYPE CODE - TITLE.");
}
}
}

0 comments on commit 25a8ce8

Please sign in to comment.