|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="ReqIFContentTestFixture.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2017-2025 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +namespace ReqIFSharp.Tests |
| 22 | +{ |
| 23 | + using System; |
| 24 | + using System.IO; |
| 25 | + using System.Linq; |
| 26 | + using System.Threading; |
| 27 | + using System.Threading.Tasks; |
| 28 | + using System.Xml; |
| 29 | + |
| 30 | + using Microsoft.Extensions.Logging; |
| 31 | + |
| 32 | + using NUnit.Framework; |
| 33 | + |
| 34 | + using ReqIFSharp; |
| 35 | + |
| 36 | + using Serilog; |
| 37 | + using Serilog.Events; |
| 38 | + |
| 39 | + [TestFixture] |
| 40 | + public class ReqIFContentTestFixture |
| 41 | + { |
| 42 | + private ILoggerFactory loggerFactory; |
| 43 | + |
| 44 | + private TestLogEventSink testLogEventSink; |
| 45 | + |
| 46 | + [OneTimeSetUp] |
| 47 | + public void OneTimeSetUp() |
| 48 | + { |
| 49 | + this.testLogEventSink = new TestLogEventSink(); |
| 50 | + |
| 51 | + Log.Logger = new LoggerConfiguration() |
| 52 | + .MinimumLevel.Verbose() |
| 53 | + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}") |
| 54 | + .WriteTo.Sink(this.testLogEventSink) |
| 55 | + .CreateLogger(); |
| 56 | + |
| 57 | + this.loggerFactory = LoggerFactory.Create(builder => |
| 58 | + { |
| 59 | + builder.AddSerilog(); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + [SetUp] |
| 64 | + public void SetUp() |
| 65 | + { |
| 66 | + this.testLogEventSink.Events.Clear(); |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void ReadXml_UnsupportedElement_EmitsLogWarning() |
| 71 | + { |
| 72 | + var xml = """ |
| 73 | + <UNSUPPORTED-ELEMENT /> |
| 74 | + """; |
| 75 | + |
| 76 | + var reqIfContent = new ReqIFContent(this.loggerFactory); |
| 77 | + |
| 78 | + using var reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { Async = false, IgnoreWhitespace = false }); |
| 79 | + |
| 80 | + reqIfContent.ReadXml(reader); |
| 81 | + |
| 82 | + var warningEvent = this.testLogEventSink.Events |
| 83 | + .Where(e => e.Level == LogEventLevel.Warning) |
| 84 | + .ToList().First(); |
| 85 | + |
| 86 | + Assert.That(warningEvent.Properties["LocalName"].ToString().Trim('"'), |
| 87 | + Is.EqualTo("UNSUPPORTED-ELEMENT")); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public async Task ReadXmlAsync_UnsupportedElement_EmitsLogWarning() |
| 92 | + { |
| 93 | + var xml = """ |
| 94 | + <UNSUPPORTED-ELEMENT /> |
| 95 | + """; |
| 96 | + |
| 97 | + var reqIfContent = new ReqIFContent(this.loggerFactory); |
| 98 | + |
| 99 | + using var reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { Async = true, IgnoreWhitespace = false }); |
| 100 | + |
| 101 | + await reqIfContent.ReadXmlAsync(reader, CancellationToken.None); |
| 102 | + |
| 103 | + var warningEvent = this.testLogEventSink.Events |
| 104 | + .Where(e => e.Level == LogEventLevel.Warning) |
| 105 | + .ToList().First(); |
| 106 | + |
| 107 | + Assert.That(warningEvent.Properties["LocalName"].ToString().Trim('"'), |
| 108 | + Is.EqualTo("UNSUPPORTED-ELEMENT")); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public async Task VErify_that_when_cancelled_ReadXmlAsync_throws_exception() |
| 113 | + { |
| 114 | + var xml = """ |
| 115 | + <REQ-IF-CONTENT /> |
| 116 | + """; |
| 117 | + |
| 118 | + var reqIfContent = new ReqIFContent(this.loggerFactory); |
| 119 | + |
| 120 | + using var reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { Async = true, IgnoreWhitespace = false }); |
| 121 | + |
| 122 | + var cts = new CancellationTokenSource(); |
| 123 | + |
| 124 | + await cts.CancelAsync(); |
| 125 | + |
| 126 | + await Assert.ThatAsync(() => reqIfContent.ReadXmlAsync(reader, cts.Token), |
| 127 | + Throws.TypeOf<OperationCanceledException>()); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments