122 lines
5.7 KiB
C#
122 lines
5.7 KiB
C#
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<HttpRequestMessage, string, HttpResponseMessage> respond) : HttpMessageHandler
|
|
{
|
|
public readonly List<(HttpRequestMessage Request, string Body)> Calls = [];
|
|
protected override async Task<HttpResponseMessage> 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<JopetrolGateway>.Instance);
|
|
|
|
private static string Answer(string text) =>
|
|
$"""<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="{JopetrolSoap.SoapNamespace}"><soap:Body><sendClaimandUpdateStatusTo7Response xmlns="{JopetrolSoap.ServiceNamespace}"><sendClaimandUpdateStatusTo7Result>{text}</sendClaimandUpdateStatusTo7Result></sendClaimandUpdateStatusTo7Response></soap:Body></soap:Envelope>""";
|
|
|
|
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", "<p>", "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("<p>", 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("<soap:Envelope xmlns:soap=\"" + JopetrolSoap.SoapNamespace + "\"><soap:Body><soap:Fault/></soap:Body></soap:Envelope>"));
|
|
}
|
|
|
|
[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("<tran:TransNumber>1234567</tran:TransNumber>", call.Body);
|
|
Assert.Contains("<tran:claimNo>20230100041</tran:claimNo>", call.Body);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Answer_without_a_result_element_is_an_empty_text()
|
|
{
|
|
var h = new FakeHandler((_, _) => Ok("<html><body>Server error</body></html>"));
|
|
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<InvalidOperationException>(() => 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<InvalidOperationException>(() => 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);
|
|
}
|
|
}
|