Skip to content

Commit

Permalink
Merge pull request #9 from difi/threadsafeValidate
Browse files Browse the repository at this point in the history
Threadsafe validate
  • Loading branch information
kristianenge committed Jun 8, 2016
2 parents dbfb1cd + b10cf64 commit 8045c69
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +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\XmlValidatorTestImplementation.cs" />
<Compile Include="Validation\XmlValidatorTests.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Difi.Felles.Utility.Tester/Validation/TestGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using ApiClientShared;

namespace Difi.Felles.Utility.Tester.Validation
Expand Down Expand Up @@ -60,5 +63,12 @@ public List<string> ExpectedValidationMessages
}
}
}

public static XmlSchemaSet XmlSchemaSet()
{
var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add("http://tempuri.org/po.xsd", XmlReader.Create(new MemoryStream(ResourceUtility.ReadAllBytes(true,"Xsd.Sample.xsd"))));
return xmlSchemaSet;
}
}
}
23 changes: 1 addition & 22 deletions Difi.Felles.Utility.Tester/Validation/ValidationMessagesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ public void OutputsCorrectly()
Assert.AreSame(expectedError, messages.ToString());
}
}

[TestClass]
public class ResetMethod : ValidationMessagesTests
{
[TestMethod]
public void ResetsState()
{
var messages = new ValidationMessages();
const string expectedError = "Error";
var expectedList = new List<string> {expectedError};
messages.Add(XmlSeverityType.Error, expectedError);
CollectionAssert.AreEqual(expectedList, messages);

messages.Reset();

CollectionAssert.AreEqual(new List<string>(), messages);
Assert.AreEqual("", messages.ToString());
Assert.IsFalse(messages.HasErrors);
Assert.IsFalse(messages.HasWarnings);
Assert.AreEqual(0, messages.Count);
}
}

}
}
39 changes: 39 additions & 0 deletions Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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);
}
}
}
}
76 changes: 61 additions & 15 deletions Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Difi.Felles.Utility.Tester.Validation
{
Expand All @@ -8,27 +9,70 @@ public class XmlValidatorTests
[TestClass]
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);
}

[TestMethod]
public void ValidateWithCorrectXmlShouldReturnTrueAndGiveNoValidationMessages()
{
var validTestCouple = new TestGenerator.ValidTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();

var validateResult = validator.Validate(validTestCouple.Input());
List<string> messagesList;
var validateResult = validator.Validate(validTestCouple.Input(), out messagesList);

Assert.IsTrue(validateResult);
Assert.AreEqual(0, validator.ValidationMessages.Count);
Assert.AreEqual(0, messagesList.Count);
}

[TestMethod]
public void ValidateWithInvalidContentShouldReturnFalseAndGiveValidationError()
{
var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
List<string> messagesList;
var validateResult = validator.Validate(invalidContentTestCouple.Input(), out messagesList);

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

Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(validator.ValidationMessages.ToString()));
Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(messagesList.ToString()));
Assert.IsFalse(validateResult);
}

Expand All @@ -37,10 +81,11 @@ public void ValidateWithInvalidSyntaxShouldReturnFalseAndGiveValidationError()
{
var invalidSyntaxTestRequest = new TestGenerator.InvalidSyntaxTestCouple();
XmlValidator validator = new XmlValidatorTestImplementation();
List<string> messagesList;

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

Assert.IsTrue(invalidSyntaxTestRequest.ExpectedValidationMessages.Contains(validator.ValidationMessages.ToString()));
Assert.IsTrue(invalidSyntaxTestRequest.ExpectedValidationMessages.Contains(messagesList.ToString()));
Assert.IsFalse(validateResult);
}

Expand All @@ -50,19 +95,20 @@ public void MultipleValidateShouldNotHoldValidationState()
XmlValidator validator = new XmlValidatorTestImplementation();

var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple();
var validateResult = validator.Validate(invalidContentTestCouple.Input());
List<string> messagesList;
var validateResult = validator.Validate(invalidContentTestCouple.Input(), out messagesList);
Assert.IsFalse(validateResult);
Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(validator.ValidationMessages.ToString()));
Assert.IsTrue(invalidContentTestCouple.ExpectedValidationMessages.Contains(messagesList.ToString()));

var validTestCouple = new TestGenerator.ValidTestCouple();
validateResult = validator.Validate(validTestCouple.Input());
validateResult = validator.Validate(validTestCouple.Input(), out messagesList);
Assert.IsTrue(validateResult);
Assert.AreEqual(0, validator.ValidationMessages.Count);
Assert.AreEqual(0, messagesList.Count);

var invalidSyntaxTestCouple = new TestGenerator.InvalidSyntaxTestCouple();
validateResult = validator.Validate(invalidSyntaxTestCouple.Input());
validateResult = validator.Validate(invalidSyntaxTestCouple.Input(), out messagesList);
Assert.IsFalse(validateResult);
Assert.IsTrue(invalidSyntaxTestCouple.ExpectedValidationMessages.Contains(validator.ValidationMessages.ToString()));
Assert.IsTrue(invalidSyntaxTestCouple.ExpectedValidationMessages.Contains(messagesList.ToString()));
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Difi.Felles.Utility/Difi.Felles.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="CertificateChainValidator.cs" />
<Compile Include="Utilities\NamespaceUtility.cs" />
<Compile Include="Validation\ValidationMessages.cs" />
<Compile Include="Validation\ValidationRunner.cs" />
<Compile Include="XmlValidator.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -80,7 +81,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
10 changes: 2 additions & 8 deletions Difi.Felles.Utility/Validation/ValidationMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Difi.Felles.Utility.Validation
{
public class ValidationMessages : List<string>
{
public bool HasErrors { get; private set; }
public bool HasErrors { get; private set; } = false;

public bool HasWarnings { get; private set; }
public bool HasWarnings { get; private set; } = false;

internal void Add(XmlSeverityType severity, string message)
{
Expand All @@ -32,11 +32,5 @@ public override string ToString()
{
return Count <= 0 ? "" : this.Aggregate((current, variable) => current + Environment.NewLine + variable);
}

internal void Reset()
{
Clear();
HasWarnings = HasErrors = false;
}
}
}
60 changes: 60 additions & 0 deletions Difi.Felles.Utility/Validation/ValidationRunner.cs
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
{
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);
}
}
}
Loading

0 comments on commit 8045c69

Please sign in to comment.