-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced ValidationRunner to hold the state of the validation
- Loading branch information
1 parent
dbfb1cd
commit bab7fe4
Showing
9 changed files
with
191 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Difi.Felles.Utility.Validation; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Difi.Felles.Utility.Tester.Validation | ||
{ | ||
[TestClass] | ||
public class ValidationRunnerTests | ||
{ | ||
[TestClass] | ||
public class ConstructorMethod : ValidationRunnerTests | ||
{ | ||
[TestMethod] | ||
public void SimpleInitialization() | ||
{ | ||
//Arrange | ||
var validationRunner = new ValidationRunner(TestGenerator.XmlSchemaSet()); | ||
|
||
//Act | ||
|
||
//Assert | ||
Assert.IsTrue(1 == 1); | ||
} | ||
|
||
[TestMethod] | ||
public void AddsValidationMessage() | ||
{ | ||
//Arrange | ||
var validationRunner = new ValidationRunner(TestGenerator.XmlSchemaSet()); | ||
var invalidTestCouple = new TestGenerator.InvalidContentTestCouple(); | ||
|
||
//Act | ||
validationRunner.Validate(invalidTestCouple.Input()); | ||
|
||
//Assert | ||
Assert.AreEqual(1, validationRunner.ValidationMessages.Count); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
|
||
namespace Difi.Felles.Utility.Validation | ||
{ | ||
public class ValidationRunner | ||
{ | ||
private const string ToleratedXsdIdErrorEnUs = "It is an error if there is a member of the attribute uses of a type definition with type xs:ID or derived from xs:ID and another attribute with type xs:ID matches an attribute wildcard."; | ||
private const string ToleratedXsdIdErrorNbNo = "Det er en feil hvis det finnes et medlem av attributtet som bruker en typedefinisjon med typen xs:ID eller avledet fra xs:ID og et annet attributt med typen xs:ID tilsvarer et attributtjokertegn."; | ||
private const string ToleratedPrefixListErrorEnUs = "The 'PrefixList' attribute is invalid - The value '' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:NMTOKENS' - The attribute value cannot be empty."; | ||
private const string ToleratedPrefixListErrorNbNo = "Attributtet PrefixList er ugyldig - Verdien er ugyldig i henhold til datatypen http://www.w3.org/2001/XMLSchema:NMTOKENS - Attributtverdien kan ikke være tom."; | ||
|
||
internal static readonly List<string> ToleratedErrors = new List<string> {ToleratedXsdIdErrorEnUs, ToleratedXsdIdErrorNbNo, ToleratedPrefixListErrorEnUs, ToleratedPrefixListErrorNbNo}; | ||
|
||
private readonly XmlSchemaSet _schemaSet; | ||
|
||
public ValidationRunner(XmlSchemaSet schemaSet) | ||
{ | ||
_schemaSet = schemaSet; | ||
} | ||
|
||
public ValidationMessages ValidationMessages { get; } = new ValidationMessages(); | ||
|
||
public bool Validate(string document) | ||
{ | ||
var settings = new XmlReaderSettings(); | ||
settings.Schemas.Add(_schemaSet); | ||
settings.ValidationType = ValidationType.Schema; | ||
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; | ||
settings.ValidationEventHandler += ValidationEventHandler; | ||
|
||
var xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(document)), settings); | ||
|
||
while (xmlReader.Read()) | ||
{ | ||
} | ||
|
||
return !ValidationMessages.HasErrors && !ValidationMessages.HasWarnings; | ||
} | ||
|
||
private void ValidationEventHandler(object sender, ValidationEventArgs e) | ||
{ | ||
if (IsToleratedError(e)) | ||
{ | ||
} | ||
else | ||
{ | ||
ValidationMessages.Add(e.Severity, e.Message); | ||
} | ||
} | ||
|
||
private static bool IsToleratedError(ValidationEventArgs e) | ||
{ | ||
return ToleratedErrors.Contains(e.Message); | ||
} | ||
} | ||
} |
Oops, something went wrong.