Skip to content

Commit

Permalink
Endrer navn på ValidationRunner til XmlValidationRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksander Aas Sjåfjell committed Jun 13, 2016
1 parent 8045c69 commit 95f4d77
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<Compile Include="Utilities\XmlUtility.cs" />
<Compile Include="Validation\TestGenerator.cs" />
<Compile Include="Validation\ValidationMessagesTests.cs" />
<Compile Include="Validation\ValidationRunnerTests.cs" />
<Compile Include="Validation\XmlValidationRunnerTests.cs" />
<Compile Include="Validation\XmlValidatorTestImplementation.cs" />
<Compile Include="Validation\XmlValidatorTests.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
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());

}
}

[TestClass]
public class ValidateMethod : ValidationMessagesTests
{
[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);
}
}
}
using Difi.Felles.Utility.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Difi.Felles.Utility.Tester.Validation
{
[TestClass]
public class XmlValidationRunnerTests
{
[TestClass]
public class ConstructorMethod : XmlValidationRunnerTests
{
[TestMethod]
public void SimpleInitialization()
{
//Arrange
var xmlSchemaSet = TestGenerator.XmlSchemaSet();

//Act
var validationRunner = new XmlValidationRunner(xmlSchemaSet);

//Assert
Assert.AreEqual(xmlSchemaSet, validationRunner.XmlSchemaSet);
}
}

[TestClass]
public class ValidateMethod : ValidationMessagesTests
{
[TestMethod]
public void AddsValidationMessage()
{
//Arrange
var validationRunner = new XmlValidationRunner(TestGenerator.XmlSchemaSet());
var invalidTestCouple = new TestGenerator.InvalidContentTestCouple();

//Act
validationRunner.Validate(invalidTestCouple.Input());

//Assert
Assert.AreEqual(1, validationRunner.ValidationMessages.Count);
}
}
}
}
13 changes: 4 additions & 9 deletions Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,37 @@ public class ValidateMethod : XmlValidatorTests
[TestMethod]
public void ValidateReturnsBoolAndOutsString()
{
//Arrange
var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
string validationMessage;
//Act

var status = validator.Validate(invalidContentTestCouple.Input(), out validationMessage);

//Assert
Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(validationMessage));
Assert.IsFalse(status);
}

[TestMethod]
public void ValidateReturnsBoolAndOutsListOfStrings()
{
//Arrange
var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
List<string> validationMessages;
//Act

var status = validator.Validate(invalidContentTestCouple.Input(), out validationMessages);

//Assert
Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(validationMessages.ToString()));
Assert.IsFalse(status);
}

[TestMethod]
public void ValidateReturnsBool()
{
//Arrange
var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
//Act

var status = validator.Validate(invalidContentTestCouple.Input());

//Assert
Assert.IsFalse(status);
}

Expand All @@ -58,6 +52,7 @@ public void ValidateWithCorrectXmlShouldReturnTrueAndGiveNoValidationMessages()
var validTestCouple = new TestGenerator.ValidTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
List<string> messagesList;

var validateResult = validator.Validate(validTestCouple.Input(), out messagesList);

Assert.IsTrue(validateResult);
Expand Down
2 changes: 1 addition & 1 deletion Difi.Felles.Utility/Difi.Felles.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<Compile Include="CertificateChainValidator.cs" />
<Compile Include="Utilities\NamespaceUtility.cs" />
<Compile Include="Validation\ValidationMessages.cs" />
<Compile Include="Validation\ValidationRunner.cs" />
<Compile Include="Validation\XmlValidationRunner.cs" />
<Compile Include="XmlValidator.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace Difi.Felles.Utility.Validation
{
internal 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;

internal ValidationRunner(XmlSchemaSet schemaSet)
{
_schemaSet = schemaSet;
}

internal ValidationMessages ValidationMessages { get; } = new ValidationMessages();

internal 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);
}
}
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace Difi.Felles.Utility.Validation
{
internal class XmlValidationRunner
{
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};

internal XmlSchemaSet XmlSchemaSet { get; }

internal XmlValidationRunner(XmlSchemaSet xmlSchemaSet)
{
XmlSchemaSet = xmlSchemaSet;
}

internal ValidationMessages ValidationMessages { get; } = new ValidationMessages();

internal bool Validate(string document)
{
var settings = new XmlReaderSettings();
settings.Schemas.Add(XmlSchemaSet);
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);
}
}
}
6 changes: 3 additions & 3 deletions Difi.Felles.Utility/XmlValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public abstract class XmlValidator

public bool Validate(string document)
{
return new ValidationRunner(_schemaSet).Validate(document);
return new XmlValidationRunner(_schemaSet).Validate(document);
}

public bool Validate(string document, out string validationMessage)
{
var validationRunner = new ValidationRunner(_schemaSet);
var validationRunner = new XmlValidationRunner(_schemaSet);
var status = validationRunner.Validate(document);
validationMessage = validationRunner.ValidationMessages.ToString();
return status;
}

public bool Validate(string document, out List<string> validationMessages)
{
var validationRunner = new ValidationRunner(_schemaSet);
var validationRunner = new XmlValidationRunner(_schemaSet);
var result = validationRunner.Validate(document);
validationMessages = validationRunner.ValidationMessages;
return result;
Expand Down

0 comments on commit 95f4d77

Please sign in to comment.