<commit_before>﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikkerDigitalPost.Domene;
using SikkerDigitalPost.Klient;
using SikkerDigitalPost.Klient.Envelope;

namespace SikkerDigitalPost.Tester
{
    [TestClass]
    public class ManifestTester : TestBase
    {
        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            Initialiser();
        }

        private static bool _harFeilet;

        [TestMethod]
        public void ValidereManifestMotXsdValiderer()
        {
            var settings = new XmlReaderSettings();
            settings.XmlResolver = null;

            settings.Schemas.Add(Navnerom.Ns9, ManifestXsdPath());
            settings.Schemas.Add(Navnerom.Ns9, FellesXsdPath());

            settings.ValidationType = ValidationType.Schema;
            settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += ValidationEventHandler;

            try
            {
                var reader = XmlReader.Create(new MemoryStream(Manifest.Bytes), settings);
                var document = new XmlDocument();
                document.Load(reader);
                Assert.IsFalse(_harFeilet);
            }
            catch (Exception e)
            {
                var message = String.Format("Validering feilet: {0} Inndre feilmelding: {1}", e.Message,e.InnerException);
                Assert.Fail(message);
            }
        }

        private static void ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Warning)
                Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred. " + e.Message);
            else if (e.Severity == XmlSeverityType.Error)
                Console.WriteLine("\tValidation error: " + e.Message);
            _harFeilet = true;
        }

        private string ManifestXsdPath()
        {
            return XsdFil("sdp-manifest.xsd");
        }

        private string FellesXsdPath()
        {
            return XsdFil("sdp-felles.xsd");
        }

        private string XmlDsigCoreSchema()
        {
            return XsdFil(@"w3/xmldsig-core-schema.xsd");
            
        }

        private string XsdFil(string bareFilnavn)
        {
            return Path.Combine(TestDataMappe, "xsd", bareFilnavn);
        }
    }
}<commit_msg>ManifestTester bruker nå valideringsklassene i klienten<commit_after>﻿using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikkerDigitalPost.Klient;
using SikkerDigitalPost.Klient.XmlValidering;

namespace SikkerDigitalPost.Tester
{
    [TestClass]
    public class ManifestTester : TestBase
    {
        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            Initialiser();
        }

        private static bool _harFeilet;

        [TestMethod]
        public void ValidereManifestMotXsdValiderer()
        {
            var manifestXml = Manifest.Xml();

            var manifestValidering = new ManifestValidering();
            var validert = manifestValidering.ValiderDokumentMotXsd(manifestXml.OuterXml);
            Assert.IsTrue(validert);
            
            //Endre navn på hoveddokument til å være for kort
            var namespaceManager = new XmlNamespaceManager(manifestXml.NameTable);
            namespaceManager.AddNamespace("ns9", Navnerom.Ns9);
            namespaceManager.AddNamespace("ds", Navnerom.ds);

            var hoveddokumentNode = manifestXml.DocumentElement.SelectSingleNode("//ns9:hoveddokument", namespaceManager);
            hoveddokumentNode.Attributes["href"].Value = "abc";

            validert = manifestValidering.ValiderDokumentMotXsd(manifestXml.OuterXml);

            Assert.IsFalse(validert);
        }
    }
}
{'fraction_non_alphanumeric': 0.0845997973657548, 'fraction_numerical': 0.002026342451874367, 'mean_word_length': 2.2555647155812037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7379904', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1063, 'n_words': 202}