From bab7fe4c69a34aa6b196c20f9782eab8b4b698f2 Mon Sep 17 00:00:00 2001 From: Kristian Enge Date: Tue, 7 Jun 2016 16:53:51 +0200 Subject: [PATCH 1/4] Introduced ValidationRunner to hold the state of the validation --- .../Difi.Felles.Utility.Tester.csproj | 1 + .../Validation/TestGenerator.cs | 10 +++ .../Validation/ValidationMessagesTests.cs | 23 +----- .../Validation/ValidationRunnerTests.cs | 40 ++++++++++ .../Validation/XmlValidatorTests.cs | 76 +++++++++++++++---- .../Difi.Felles.Utility.csproj | 5 +- .../Validation/ValidationMessages.cs | 10 +-- .../Validation/ValidationRunner.cs | 60 +++++++++++++++ Difi.Felles.Utility/XmlValidator.cs | 57 +++----------- 9 files changed, 191 insertions(+), 91 deletions(-) create mode 100755 Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs create mode 100755 Difi.Felles.Utility/Validation/ValidationRunner.cs diff --git a/Difi.Felles.Utility.Tester/Difi.Felles.Utility.Tester.csproj b/Difi.Felles.Utility.Tester/Difi.Felles.Utility.Tester.csproj index e2ee753..d6b7cc9 100755 --- a/Difi.Felles.Utility.Tester/Difi.Felles.Utility.Tester.csproj +++ b/Difi.Felles.Utility.Tester/Difi.Felles.Utility.Tester.csproj @@ -77,6 +77,7 @@ + diff --git a/Difi.Felles.Utility.Tester/Validation/TestGenerator.cs b/Difi.Felles.Utility.Tester/Validation/TestGenerator.cs index 5c850dd..6598b73 100755 --- a/Difi.Felles.Utility.Tester/Validation/TestGenerator.cs +++ b/Difi.Felles.Utility.Tester/Validation/TestGenerator.cs @@ -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 @@ -60,5 +63,12 @@ public List 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; + } } } \ No newline at end of file diff --git a/Difi.Felles.Utility.Tester/Validation/ValidationMessagesTests.cs b/Difi.Felles.Utility.Tester/Validation/ValidationMessagesTests.cs index 72f4268..85f1e2e 100755 --- a/Difi.Felles.Utility.Tester/Validation/ValidationMessagesTests.cs +++ b/Difi.Felles.Utility.Tester/Validation/ValidationMessagesTests.cs @@ -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 {expectedError}; - messages.Add(XmlSeverityType.Error, expectedError); - CollectionAssert.AreEqual(expectedList, messages); - - messages.Reset(); - - CollectionAssert.AreEqual(new List(), messages); - Assert.AreEqual("", messages.ToString()); - Assert.IsFalse(messages.HasErrors); - Assert.IsFalse(messages.HasWarnings); - Assert.AreEqual(0, messages.Count); - } - } + } } \ No newline at end of file diff --git a/Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs b/Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs new file mode 100755 index 0000000..9b68d64 --- /dev/null +++ b/Difi.Felles.Utility.Tester/Validation/ValidationRunnerTests.cs @@ -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); + } + + } + } +} \ No newline at end of file diff --git a/Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs b/Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs index 675079f..5ea0815 100755 --- a/Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs +++ b/Difi.Felles.Utility.Tester/Validation/XmlValidatorTests.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Difi.Felles.Utility.Tester.Validation { @@ -8,16 +9,59 @@ 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 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 messagesList; + var validateResult = validator.Validate(validTestCouple.Input(), out messagesList); Assert.IsTrue(validateResult); - Assert.AreEqual(0, validator.ValidationMessages.Count); + Assert.AreEqual(0, messagesList.Count); } [TestMethod] @@ -25,10 +69,10 @@ public void ValidateWithInvalidContentShouldReturnFalseAndGiveValidationError() { var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple(); XmlValidator validator = new XmlValidatorTestImplementation(); + List 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); } @@ -37,10 +81,11 @@ public void ValidateWithInvalidSyntaxShouldReturnFalseAndGiveValidationError() { var invalidSyntaxTestRequest = new TestGenerator.InvalidSyntaxTestCouple(); XmlValidator validator = new XmlValidatorTestImplementation(); + List 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); } @@ -50,19 +95,20 @@ public void MultipleValidateShouldNotHoldValidationState() XmlValidator validator = new XmlValidatorTestImplementation(); var invalidContentTestCouple = new TestGenerator.InvalidContentTestCouple(); - var validateResult = validator.Validate(invalidContentTestCouple.Input()); + List 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())); } } } diff --git a/Difi.Felles.Utility/Difi.Felles.Utility.csproj b/Difi.Felles.Utility/Difi.Felles.Utility.csproj index 4aba5cd..5fb5b46 100755 --- a/Difi.Felles.Utility/Difi.Felles.Utility.csproj +++ b/Difi.Felles.Utility/Difi.Felles.Utility.csproj @@ -58,6 +58,7 @@ + @@ -80,7 +81,9 @@ - + + Designer +