using System.Net; using System.Text; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using NTP.Infrastructure.Integrations; namespace NTP.Tests.Refinery; public class JopetrolGatewayTests { private sealed class FakeHandler(Func respond) : HttpMessageHandler { public readonly List<(HttpRequestMessage Request, string Body)> Calls = []; protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var body = request.Content is null ? "" : await request.Content.ReadAsStringAsync(cancellationToken); Calls.Add((request, body)); return respond(request, body); } } private sealed class FakeFactory(HttpMessageHandler handler) : IHttpClientFactory { public HttpClient CreateClient(string name) => new(handler, disposeHandler: false); } private static JopetrolGateway Gateway(FakeHandler h, bool enabled = true, string user = "test-user", string password = "test-pass", string url = "https://jopetrol.test/ws.asmx") => new(new FakeFactory(h), Options.Create(new JopetrolOptions { Enabled = enabled, Url = url, UserName = user, Password = password, TimeoutSeconds = 5 }), NullLogger.Instance); private static string Answer(string text) => $"""{text}"""; private static HttpResponseMessage Ok(string xml) => new(HttpStatusCode.OK) { Content = new StringContent(xml, Encoding.UTF8, "text/xml") }; [Fact] public void Envelope_carries_the_header_credentials_and_the_two_arguments() { var xml = JopetrolSoap.BuildEnvelope("test-user", "test-pass", "1234567", "20230100041"); var doc = System.Xml.Linq.XDocument.Parse(xml); // well formed var tran = System.Xml.Linq.XNamespace.Get(JopetrolSoap.ServiceNamespace); Assert.Equal("test-user", doc.Descendants(tran + "UserName").Single().Value); Assert.Equal("test-pass", doc.Descendants(tran + "Password").Single().Value); Assert.Equal("1234567", doc.Descendants(tran + "TransNumber").Single().Value); Assert.Equal("20230100041", doc.Descendants(tran + "claimNo").Single().Value); Assert.NotNull(doc.Descendants(tran + "sendClaimandUpdateStatusTo7").SingleOrDefault()); } [Fact] public void Envelope_escapes_xml_in_the_values() { var xml = JopetrolSoap.BuildEnvelope("a&b", "

", "1", "2"); var tran = System.Xml.Linq.XNamespace.Get(JopetrolSoap.ServiceNamespace); var doc = System.Xml.Linq.XDocument.Parse(xml); Assert.Equal("a&b", doc.Descendants(tran + "UserName").Single().Value); Assert.Equal("

", doc.Descendants(tran + "Password").Single().Value); } [Fact] public void Answer_is_read_by_local_name() { Assert.Equal("Status Updated successfully To 7", JopetrolSoap.ParseResult(Answer("Status Updated successfully To 7"))); Assert.Null(JopetrolSoap.ParseResult("")); } [Fact] public async Task Disabled_integration_returns_null_and_calls_nothing() { var h = new FakeHandler((_, _) => Ok(Answer("never"))); var result = await Gateway(h, enabled: false).SendClaimAndUpdateStatusTo7Async("1", "2"); Assert.Null(result); Assert.Empty(h.Calls); } [Fact] public async Task Enabled_integration_posts_the_envelope_and_returns_the_result_text() { var h = new FakeHandler((_, _) => Ok(Answer("Status Updated successfully To 7"))); var result = await Gateway(h).SendClaimAndUpdateStatusTo7Async("1234567", "20230100041"); Assert.Equal("Status Updated successfully To 7", result); var call = Assert.Single(h.Calls); Assert.Equal(HttpMethod.Post, call.Request.Method); Assert.Equal("https://jopetrol.test/ws.asmx", call.Request.RequestUri!.ToString()); Assert.StartsWith("text/xml", call.Request.Content!.Headers.ContentType!.MediaType); Assert.Contains("1234567", call.Body); Assert.Contains("20230100041", call.Body); } [Fact] public async Task Answer_without_a_result_element_is_an_empty_text() { var h = new FakeHandler((_, _) => Ok("Server error")); Assert.Equal("", await Gateway(h).SendClaimAndUpdateStatusTo7Async("1", "2")); } [Fact] public async Task Unreadable_answer_is_an_error() { var h = new FakeHandler((_, _) => Ok("not xml at all <<")); await Assert.ThrowsAsync(() => Gateway(h).SendClaimAndUpdateStatusTo7Async("1", "2")); } [Fact] public async Task Enabled_without_credentials_is_refused_before_any_call() { var h = new FakeHandler((_, _) => Ok(Answer("x"))); await Assert.ThrowsAsync(() => Gateway(h, user: "", password: "").SendClaimAndUpdateStatusTo7Async("1", "2")); Assert.Empty(h.Calls); } [Fact] public void Options_are_off_by_default() { var o = new JopetrolOptions(); Assert.False(o.Enabled); Assert.Equal("", o.UserName); Assert.Equal("", o.Password); Assert.Equal(30, o.TimeoutSeconds); } }