<commit_before>﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using CAPNet.Models;

using Xunit;

namespace CAPNet.Tests
{
    public class XmlCreatorTests
    {
        private const string Sender = "<EMAIL>";

        private static readonly DateTimeOffset Sent = new DateTimeOffset(
            2014,
            6,
            10,
            10,
            35,
            23,
            512,
            TimeSpan.FromHours(-3));

        private static readonly Alert Alert = new Alert
        {
            Identifier = Guid.NewGuid().ToString(),
            Sender = Sender,
            Sent = Sent,
            Status = Status.Test,
            MessageType = MessageType.Alert,
            Scope = Scope.Private

        };

        [Fact]
        public void XmlNodeReturnedHasCAP12Namespace()
        {
            XElement xmlElement = XmlCreator.Create(Alert);

            Assert.Equal("urn:oasis:names:tc:emergency:cap:1.2", xmlElement.Name.NamespaceName);
        }

        [Fact]
        public void XmlNodeReturnedHasSender()
        {
            var alertElement = XmlCreator.Create(Alert);

            var senderElement = alertElement.Element(XmlCreator.CAP12Namespace + "sender");

            Assert.NotNull(senderElement);
            Assert.Equal(Sender, senderElement.Value);
        }

        [Fact]
        public void XmlNodeReturnedHasStatus()
        {
            var alertElement = XmlCreator.Create(Alert);

            var statusElement = alertElement.Element(XmlCreator.CAP12Namespace + "status");

            Assert.NotNull(statusElement);
            Assert.Equal("Test", statusElement.Value);
        }

        [Fact]
        public void XmlNodeReturnedHasSentTime()
        {
            var alertElement = XmlCreator.Create(Alert);

            var sentElement = alertElement.Element(XmlCreator.CAP12Namespace + "sent");

            Assert.NotNull(sentElement);
            Assert.Equal("2014-06-10T10:35:23-03:00", sentElement.Value);
        }

        [Fact]
        public void MultipleAlertXmlIsSerializedCorrectly()
        {
            var orangeAlertElement = XmlCreator.Create(CreateMultipleAlertXmlAlert());

            var document = new XDocument(orangeAlertElement);

            string alertAsString;
            var writerSettings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, NewLineChars = "\r\n" };
            using (var memoryStream = new MemoryStream())
            {
                var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
                using (var writer = XmlWriter.Create(streamWriter, writerSettings))
                {
                    document.Save(writer);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                alertAsString = new StreamReader(memoryStream, Encoding.UTF8).ReadToEnd();
            }

            Assert.Equal(Xml.MultipleAlertXml, alertAsString);

        }

        private static Alert CreateMultipleAlertXmlAlert()
        {
            var orangeAlert = new Alert
            {
                //<?xml version = "1.0" encoding = "UTF-8"?>
                //<alert xmlns = "urn:oasis:names:tc:emergency:cap:1.2">
                //  <identifier>43b080713727</identifier>
                Identifier = "43b080713727",<commit_msg>Add printfs in test for debugging<commit_after>﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using CAPNet.Models;

using Xunit;

namespace CAPNet.Tests
{
    public class XmlCreatorTests
    {
        private const string Sender = "<EMAIL>";

        private static readonly DateTimeOffset Sent = new DateTimeOffset(
            2014,
            6,
            10,
            10,
            35,
            23,
            512,
            TimeSpan.FromHours(-3));

        private static readonly Alert Alert = new Alert
        {
            Identifier = Guid.NewGuid().ToString(),
            Sender = Sender,
            Sent = Sent,
            Status = Status.Test,
            MessageType = MessageType.Alert,
            Scope = Scope.Private

        };

        [Fact]
        public void XmlNodeReturnedHasCAP12Namespace()
        {
            XElement xmlElement = XmlCreator.Create(Alert);

            Assert.Equal("urn:oasis:names:tc:emergency:cap:1.2", xmlElement.Name.NamespaceName);
        }

        [Fact]
        public void XmlNodeReturnedHasSender()
        {
            var alertElement = XmlCreator.Create(Alert);

            var senderElement = alertElement.Element(XmlCreator.CAP12Namespace + "sender");

            Assert.NotNull(senderElement);
            Assert.Equal(Sender, senderElement.Value);
        }

        [Fact]
        public void XmlNodeReturnedHasStatus()
        {
            var alertElement = XmlCreator.Create(Alert);

            var statusElement = alertElement.Element(XmlCreator.CAP12Namespace + "status");

            Assert.NotNull(statusElement);
            Assert.Equal("Test", statusElement.Value);
        }

        [Fact]
        public void XmlNodeReturnedHasSentTime()
        {
            var alertElement = XmlCreator.Create(Alert);

            var sentElement = alertElement.Element(XmlCreator.CAP12Namespace + "sent");

            Assert.NotNull(sentElement);
            Assert.Equal("2014-06-10T10:35:23-03:00", sentElement.Value);
        }

        [Fact]
        public void MultipleAlertXmlIsSerializedCorrectly()
        {
            var orangeAlertElement = XmlCreator.Create(CreateMultipleAlertXmlAlert());

            var document = new XDocument(orangeAlertElement);

            string alertAsString;
            var writerSettings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, NewLineChars = "\r\n" };
            using (var memoryStream = new MemoryStream())
            {
                var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
                using (var writer = XmlWriter.Create(streamWriter, writerSettings))
                {
                    document.Save(writer);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                alertAsString = new StreamReader(memoryStream, Encoding.UTF8).ReadToEnd();
            }

            Console.WriteLine("Expected char at 37 is {0}",
                Convert.ToInt32(
                    Xml.MultipleAlertXml.ToCharArray()[37]));
            Console.WriteLine("Actual char at 37 is {0}",
                Convert.ToInt32(
                    alertAsString.ToCharArray()[37]));

            Console.WriteLine("Expected char at 38 is {0}",
                Convert.ToInt32(
                    Xml.MultipleAlertXml.ToCharArray()[38]));
            Console.WriteLine("Actual char at 38 is {0}",
                Convert.ToInt32(
                    alertAsString.ToCharArray()[38]));

            Console.WriteLine("Expected char at 39 is {0}",
                Convert.ToInt32(
                    Xml.MultipleAlertXml.ToCharArray()[39]));
            Console.WriteLine("Actual char at 39 is {0}",
                Convert.ToInt32(
                    alertAsString.ToCharArray()[39]));

            Assert.Equal(Xml.MultipleAlertXml, alertAsString);

        }

        private static Alert CreateMultipleAlertXmlAlert()
        {
            var orangeAlert = new Alert
            {
                //<?xml version = "1.0" encoding = "UTF-8"?>
                //<alert xmlns = "urn:oasis:names:tc:emergency:cap:1.2">
                //  <identifier>43b080713727</identifier>
                Identifier = "43b080713727",
{'fraction_non_alphanumeric': 0.09586755646817248, 'fraction_numerical': 0.024897330595482547, 'mean_word_length': 1.8441605839416058, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3500769', 'n_tokens_mistral': 2197, 'n_tokens_neox': 1951, 'n_words': 410}