Skip to content

Commit

Permalink
Add SLCoreRuleMetaDataProvider (#5236)
Browse files Browse the repository at this point in the history
Fixes #5207
  • Loading branch information
1 parent 93a96bd commit 9534ec5
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 224 deletions.
12 changes: 6 additions & 6 deletions src/Education.UnitTests/EducationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void ShowRuleHelp_KnownRule_DocumentIsDisplayedInToolWindow()
var ruleId = new SonarCompositeRuleId("repoKey", "ruleKey");

var ruleInfo = Mock.Of<IRuleInfo>();
ruleMetaDataProvider.Setup(x => x.GetRuleInfoAsync(It.IsAny<SonarCompositeRuleId>(), It.IsAny<CancellationToken>())).ReturnsAsync(ruleInfo);
ruleMetaDataProvider.Setup(x => x.GetRuleInfoAsync(It.IsAny<SonarCompositeRuleId>())).ReturnsAsync(ruleInfo);

var flowDocument = Mock.Of<FlowDocument>();
var ruleHelpXamlBuilder = new Mock<IRuleHelpXamlBuilder>();
Expand All @@ -76,7 +76,7 @@ public void ShowRuleHelp_KnownRule_DocumentIsDisplayedInToolWindow()
// Act
testSubject.ShowRuleHelp(ruleId, null);

ruleMetaDataProvider.Verify(x => x.GetRuleInfoAsync(ruleId, CancellationToken.None), Times.Once);
ruleMetaDataProvider.Verify(x => x.GetRuleInfoAsync(ruleId), Times.Once);
ruleHelpXamlBuilder.Verify(x => x.Create(ruleInfo, /* todo */ null), Times.Once);
ruleDescriptionToolWindow.Verify(x => x.UpdateContent(flowDocument), Times.Once);
toolWindowService.Verify(x => x.Show(RuleHelpToolWindow.ToolWindowId), Times.Once);
Expand All @@ -95,7 +95,7 @@ public void ShowRuleHelp_FailedToDisplayRule_RuleIsShownInBrowser()
var ruleId = new SonarCompositeRuleId("repoKey", "ruleKey");

var ruleInfo = Mock.Of<IRuleInfo>();
ruleMetadataProvider.Setup(x => x.GetRuleInfoAsync(It.IsAny<SonarCompositeRuleId>(), It.IsAny<CancellationToken>())).ReturnsAsync(ruleInfo);
ruleMetadataProvider.Setup(x => x.GetRuleInfoAsync(It.IsAny<SonarCompositeRuleId>())).ReturnsAsync(ruleInfo);

ruleHelpXamlBuilder.Setup(x => x.Create(ruleInfo, /* todo */ null)).Throws(new Exception("some layout error"));

Expand All @@ -109,7 +109,7 @@ public void ShowRuleHelp_FailedToDisplayRule_RuleIsShownInBrowser()

testSubject.ShowRuleHelp(ruleId, /* todo */ null);

ruleMetadataProvider.Verify(x => x.GetRuleInfoAsync(ruleId, CancellationToken.None), Times.Once);
ruleMetadataProvider.Verify(x => x.GetRuleInfoAsync(ruleId), Times.Once);
showRuleInBrowser.Verify(x => x.ShowRuleDescription(ruleId), Times.Once);

// should have attempted to build the rule, but failed
Expand All @@ -126,7 +126,7 @@ public void ShowRuleHelp_UnknownRule_RuleIsShownInBrowser()
var showRuleInBrowser = new Mock<IShowRuleInBrowser>();

var unknownRule = new SonarCompositeRuleId("known", "xxx");
ruleMetadataProvider.Setup(x => x.GetRuleInfoAsync(unknownRule, It.IsAny<CancellationToken>())).ReturnsAsync((IRuleInfo)null);
ruleMetadataProvider.Setup(x => x.GetRuleInfoAsync(unknownRule)).ReturnsAsync((IRuleInfo)null);

var testSubject = CreateEducation(
toolWindowService.Object,
Expand All @@ -138,7 +138,7 @@ public void ShowRuleHelp_UnknownRule_RuleIsShownInBrowser()

testSubject.ShowRuleHelp(unknownRule, /* todo */ null);

ruleMetadataProvider.Verify(x => x.GetRuleInfoAsync(unknownRule, CancellationToken.None), Times.Once);
ruleMetadataProvider.Verify(x => x.GetRuleInfoAsync(unknownRule), Times.Once);
showRuleInBrowser.Verify(x => x.ShowRuleDescription(unknownRule), Times.Once);

// Should not have attempted to build the rule
Expand Down
80 changes: 3 additions & 77 deletions src/Education.UnitTests/Rule/RuleInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,120 +36,46 @@ public class RuleInfoTests
public void Ctor_NullCollectionsAreAllowed_SetToEmptyCollections()
{
var testSubject = new RuleInfo(
languageKey: null,
fullRuleKey: null,
description: null,
name: null,
RuleIssueSeverity.Unknown,
RuleIssueType.Unknown,
isActiveByDefault: false,
tags: null,
educationPrinciples: null,
htmlNote: null,
richRuleDescriptionDto: null,
cleanCodeAttribute: null,
defaultImpacts: null);

testSubject.Tags.Should().NotBeNull();
testSubject.EducationPrinciples.Should().NotBeNull();

testSubject.Tags.Should().HaveCount(0);
testSubject.EducationPrinciples.Should().HaveCount(0);

testSubject.DefaultImpacts.Should().NotBeNull();
}

[TestMethod]
public void WithCleanCodeTaxonomyDisabled_SetsCctPropertiesToNull()
{
var languageKey = "any";
var fullRuleKey = "any";
var description = "any";
var name = "any";
var ruleIssueSeverity = RuleIssueSeverity.Critical;
var ruleIssueType = RuleIssueType.Bug;
var isActiveByDefault = true;
IReadOnlyList<string> tags = new []{ "any"};
IReadOnlyList<string> educationPrinciples = Array.Empty<string>();
var htmlNote = "any";
var richDescription = new RuleSplitDescriptionDto("any", new List<RuleDescriptionTabDto>());
CleanCodeAttribute? cleanCodeAttribute = CleanCodeAttribute.Focused;
Dictionary<SoftwareQuality,SoftwareQualitySeverity> defaultImpacts = new Dictionary<SoftwareQuality, SoftwareQualitySeverity>();

var testSubject = new RuleInfo(
languageKey,
fullRuleKey,
description,
name,
ruleIssueSeverity,
ruleIssueType,
isActiveByDefault,
tags,
educationPrinciples,
htmlNote,
richDescription,
cleanCodeAttribute,
defaultImpacts);

var expected = new RuleInfo(
languageKey,
fullRuleKey,
description,
name,
ruleIssueSeverity,
ruleIssueType,
isActiveByDefault,
tags,
educationPrinciples,
htmlNote,
richDescription,
null,
null);

testSubject.WithCleanCodeTaxonomyDisabled().Should().BeEquivalentTo(expected);
}

[TestMethod]
public void Ctor_SetsProperties()
{
var richDescription = new RuleSplitDescriptionDto("any", new List<RuleDescriptionTabDto>());

var educationPrinciples = new[] { "defense_in_depth", "never_trust_user_input" };

var defaultImpacts = new Dictionary<SoftwareQuality, SoftwareQualitySeverity>();
defaultImpacts.Add(SoftwareQuality.Maintainability, SoftwareQualitySeverity.Medium);
defaultImpacts.Add(SoftwareQuality.Reliability, SoftwareQualitySeverity.Low);

var tags = new string[] { "convention", "bad-practice" };

var testSubject = new RuleInfo(
Language.CSharp.ServerLanguage.Key,
"xxx:S123",
"a description",
"the rule name",
RuleIssueSeverity.Blocker,
RuleIssueType.Vulnerability,
isActiveByDefault: true,
tags,
educationPrinciples,
"some user note",
richDescription,
CleanCodeAttribute.Respectful,
defaultImpacts);

testSubject.LanguageKey.Should().Be(Language.CSharp.ServerLanguage.Key);
testSubject.FullRuleKey.Should().Be("xxx:S123");
testSubject.Description.Should().Be("a description");
testSubject.Name.Should().Be("the rule name");
testSubject.Severity.Should().Be(RuleIssueSeverity.Blocker);
testSubject.IssueType.Should().Be(RuleIssueType.Vulnerability);
testSubject.IsActiveByDefault.Should().BeTrue();
testSubject.Tags.Should().BeEquivalentTo(tags);
testSubject.EducationPrinciples.Should().BeEquivalentTo(educationPrinciples);
testSubject.HtmlNote.Should().Be("some user note");
testSubject.RichRuleDescriptionDto.Should().BeSameAs(richDescription);
testSubject.CleanCodeAttribute.Should().Be(CleanCodeAttribute.Respectful);
testSubject.DefaultImpacts.Should().BeEquivalentTo(defaultImpacts);
}

}


}
Loading

0 comments on commit 9534ec5

Please sign in to comment.