417 lines
20 KiB
C#
417 lines
20 KiB
C#
using System.Text.Json;
|
|
using System.Xml.Linq;
|
|
using NTP.Application.Abstractions;
|
|
using NTP.Application.ExternalIntegration;
|
|
using NTP.Application.Refinery.Sync;
|
|
using NTP.Infrastructure.Integrations;
|
|
|
|
namespace NTP.Tests.Refinery;
|
|
|
|
public class JopetrolTripParserTests
|
|
{
|
|
private static XElement Node(string status = "2", string extra = "") => XElement.Parse($"""
|
|
<Table>
|
|
<NF55DOCO> 1234567 </NF55DOCO><NFTRDJ>121012</NFTRDJ><NFAN8>5001</NFAN8><NF55HEAD>12345</NF55HEAD><NF55TRAIL>6789</NF55TRAIL>
|
|
<NF55DRNID>9871234567</NF55DRNID><NF55STATUS>{status}</NF55STATUS><NFUSER>JDE</NFUSER><NFUPMJ>121013</NFUPMJ>
|
|
<NFLOAD>121012</NFLOAD><NFWGTR>39980</NFWGTR><NFBFWT>15000</NFBFWT><NFAFWT>54980</NFAFWT><NFTEMP>2550</NFTEMP><NFY55WQ>500</NFY55WQ>
|
|
<NFY55WFLAG>0</NFY55WFLAG>{extra}
|
|
</Table>
|
|
""");
|
|
|
|
[Theory]
|
|
[InlineData("121012", 2021, 1, 12)]
|
|
[InlineData("121365", 2021, 12, 31)]
|
|
[InlineData("120060", 2020, 2, 29)] // leap year
|
|
public void Jde_date_is_century_year_day_of_year(string text, int y, int m, int d) =>
|
|
Assert.Equal(new DateTime(y, m, d), JopetrolTripParser.JdeDate(text));
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("12101")]
|
|
[InlineData("121400")]
|
|
[InlineData("abcdef")]
|
|
public void Bad_jde_dates_are_null(string text) => Assert.Null(JopetrolTripParser.JdeDate(text));
|
|
|
|
[Fact]
|
|
public void Values_are_converted_the_way_legacy_did()
|
|
{
|
|
var t = JopetrolTripParser.Parse(Node());
|
|
Assert.Equal("1234567", t.Pn);
|
|
Assert.Equal(new DateTime(2021, 1, 12), t.DispatchDate);
|
|
Assert.Equal(39.98m, t.NetLoadingWeight); // kg / 1000
|
|
Assert.Equal(15m, t.EmptyWeight);
|
|
Assert.Equal(25.5m, t.Temperature); // / 100
|
|
Assert.Equal(0.5m, t.WaterAmount);
|
|
Assert.Equal(1, t.WaterFlag); // NFY55WFLAG = 0: water that does not accompany the load
|
|
Assert.Equal(2, t.TripStatus);
|
|
Assert.Equal("0", t.MaterialNo); // missing optional tags
|
|
Assert.Null(t.SedimentAmount);
|
|
}
|
|
|
|
[Fact]
|
|
public void Water_flag_is_zero_when_the_tag_is_missing_or_not_zero()
|
|
{
|
|
Assert.Equal(0, JopetrolTripParser.Parse(XElement.Parse(Node().ToString().Replace("<NFY55WFLAG>0</NFY55WFLAG>", ""))).WaterFlag);
|
|
Assert.Equal(0, JopetrolTripParser.Parse(XElement.Parse(Node().ToString().Replace("<NFY55WFLAG>0</NFY55WFLAG>", "<NFY55WFLAG>1</NFY55WFLAG>"))).WaterFlag);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_row_without_a_required_tag_is_refused()
|
|
{
|
|
var ex = Assert.Throws<FormatException>(() => JopetrolTripParser.Parse(XElement.Parse("<Table><NF55DOCO>1</NF55DOCO></Table>")));
|
|
Assert.Contains("NFTRDJ", ex.Message);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0, true, "NEW")]
|
|
[InlineData(1, true, "RESD")]
|
|
[InlineData(3, true, "LOADED")]
|
|
[InlineData(6, true, "DISCHARGED")]
|
|
[InlineData(0, false, "KEEP")] // an update does not move a trip to NEW / RESD by itself
|
|
[InlineData(1, false, "KEEP")]
|
|
[InlineData(4, false, "ARRIVE_LOCATION")]
|
|
[InlineData(5, false, "DISCHARGING")]
|
|
public void Status_code_follows_the_service_status(int status, bool insert, string expected) =>
|
|
Assert.Equal(expected, JopetrolTripParser.StatusCodeOf(status, "KEEP", insert));
|
|
}
|
|
|
|
public class JopetrolSoapTests
|
|
{
|
|
[Fact]
|
|
public void Query_answer_rows_are_the_children_of_NewDataSet()
|
|
{
|
|
const string xml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><QueryOnTransByStatusResponse><QueryOnTransByStatusResult><NewDataSet xmlns=\"x\"><Table><NF55DOCO>1</NF55DOCO></Table><Table><NF55DOCO>2</NF55DOCO></Table></NewDataSet></QueryOnTransByStatusResult></QueryOnTransByStatusResponse></soap:Body></soap:Envelope>";
|
|
Assert.Equal(2, JopetrolSoap.ParseDataSetRows(xml).Count);
|
|
Assert.Empty(JopetrolSoap.ParseDataSetRows("<a><b/></a>"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_answers_are_read_by_operation_name_or_any_result()
|
|
{
|
|
Assert.Equal("Status Updated successfully To -2", JopetrolSoap.ParseOperationResult("<r><UpdateStatusToMinus2Result>Status Updated successfully To -2</UpdateStatusToMinus2Result></r>", "UpdateStatusToMinus2"));
|
|
Assert.Equal("ok", JopetrolSoap.ParseOperationResult("<r><UpdateStatusTo1Result>ok</UpdateStatusTo1Result></r>", "UpdateStatusToMinus2")); // legacy read UpdateStatusTo1Result for both
|
|
Assert.Null(JopetrolSoap.ParseOperationResult("<r><x/></r>", "UpdateStatusTo1"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Envelopes_escape_what_they_carry()
|
|
{
|
|
var e = JopetrolSoap.BuildUpdateStatusToMinus2("u", "p<&>", "12<3");
|
|
Assert.Contains("p<&>", e);
|
|
Assert.Contains("12<3", e);
|
|
Assert.Contains("<tran:StatusNo>4</tran:StatusNo>", JopetrolSoap.BuildQueryByStatus("u", "p", 4));
|
|
}
|
|
}
|
|
|
|
public class RefineryOilSyncServiceTests
|
|
{
|
|
private sealed class FakeUnit : IUnitOfWork
|
|
{
|
|
public Task BeginAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task CommitAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task RollbackAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public Task<T> ExecuteInTransactionAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken ct = default) => work(ct);
|
|
}
|
|
|
|
private sealed class FakeMail : IMailService
|
|
{
|
|
public List<(string[] To, string Subject)> Sent = [];
|
|
public Task<bool> SendHtmlAsync(IReadOnlyList<string> to, string subject, string htmlBody, CancellationToken ct = default) { Sent.Add((to.ToArray(), subject)); return Task.FromResult(true); }
|
|
}
|
|
|
|
private sealed class FakeSettings(bool writeBack) : IRefineryJobSettingsService
|
|
{
|
|
public RefineryJobOptions Value { get; } = new() { JopetrolWriteBack = writeBack, CancelledTripRecipients = ["a@x.com"] };
|
|
}
|
|
|
|
private sealed class FakeGateway : IJopetrolTripsGateway
|
|
{
|
|
public bool IsReady { get; set; } = true;
|
|
public Dictionary<int, List<XElement>> Rows = [];
|
|
public string? To1Answer = "Status Updated successfully To 1";
|
|
public string? ToMinus2Answer = "Status Updated successfully To -2";
|
|
public List<string> Calls = [];
|
|
public Task<IReadOnlyList<XElement>> QueryOnTransByStatusAsync(int status, CancellationToken ct = default) =>
|
|
Task.FromResult<IReadOnlyList<XElement>>(Rows.TryGetValue(status, out var r) ? r : []);
|
|
public Task<string?> UpdateStatusTo1Async(string pn, CancellationToken ct = default) { Calls.Add("to1:" + pn); return Task.FromResult(To1Answer); }
|
|
public Task<string?> UpdateStatusToMinus2Async(string pn, CancellationToken ct = default) { Calls.Add("m2:" + pn); return Task.FromResult(ToMinus2Answer); }
|
|
}
|
|
|
|
private sealed class FakeRepo : IRefineryOilSyncRepository
|
|
{
|
|
public Dictionary<string, ExistingTrip> Trips = [];
|
|
public List<string> Writes = [];
|
|
public Task<ExistingTrip?> FindByPnAsync(string pn, CancellationToken ct = default) => Task.FromResult(Trips.GetValueOrDefault(pn));
|
|
public Task<bool> InsertAsync(JopetrolTripRecord t, string statusCode, string createdBy, CancellationToken ct = default)
|
|
{
|
|
Writes.Add($"insert:{t.Pn}:{statusCode}:{createdBy}");
|
|
Trips[t.Pn] = new ExistingTrip { Id = Trips.Count + 1, Pn = t.Pn, StatusCode = statusCode, TripStatus = t.TripStatus.ToString() };
|
|
return Task.FromResult(true);
|
|
}
|
|
public Task UpdateAsync(ExistingTrip old, JopetrolTripRecord t, string statusCode, int tripStatus, string updatedBy, CancellationToken ct = default)
|
|
{
|
|
Writes.Add($"update:{old.Pn}:{statusCode}:{tripStatus}");
|
|
old.StatusCode = statusCode; old.TripStatus = tripStatus.ToString();
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private static XElement Node(string pn, int status) => XElement.Parse($"""
|
|
<Table><NF55DOCO>{pn}</NF55DOCO><NFTRDJ>121012</NFTRDJ><NFAN8>5</NFAN8><NF55HEAD>1</NF55HEAD><NF55TRAIL>2</NF55TRAIL><NF55DRNID>3</NF55DRNID>
|
|
<NF55STATUS>{status}</NF55STATUS><NFUSER>U</NFUSER><NFUPMJ>121013</NFUPMJ></Table>
|
|
""");
|
|
|
|
private static (RefineryOilSyncService Service, FakeRepo Repo, FakeGateway Gateway, FakeMail Mail) Make(bool writeBack = true)
|
|
{
|
|
var repo = new FakeRepo(); var gw = new FakeGateway(); var mail = new FakeMail();
|
|
return (new RefineryOilSyncService(gw, repo, new FakeUnit(), mail, new FakeSettings(writeBack)), repo, gw, mail);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Does_nothing_when_jopetrol_is_not_ready()
|
|
{
|
|
var (s, repo, gw, _) = Make();
|
|
gw.IsReady = false;
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(0, r.Total);
|
|
Assert.Contains("جوبترول", r.Note);
|
|
Assert.Empty(repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task New_trips_are_created_with_the_status_of_their_service_status()
|
|
{
|
|
var (s, repo, gw, _) = Make();
|
|
gw.Rows[3] = [Node("111", 3)];
|
|
gw.Rows[6] = [Node("222", 6)];
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(2, r.Inserted);
|
|
Assert.Equal(["insert:111:LOADED:16186973", "insert:222:DISCHARGED:16186973"], repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_trip_that_moved_on_is_updated_unless_it_is_in_the_payment_phase()
|
|
{
|
|
var (s, repo, gw, _) = Make();
|
|
repo.Trips["1"] = new ExistingTrip { Id = 1, Pn = "1", StatusCode = "LOADED", TripStatus = "3" };
|
|
repo.Trips["2"] = new ExistingTrip { Id = 2, Pn = "2", StatusCode = "EXCHANGED", TripStatus = "5" };
|
|
repo.Trips["3"] = new ExistingTrip { Id = 3, Pn = "3", StatusCode = "LOADED", TripStatus = "3" };
|
|
gw.Rows[4] = [Node("1", 4)];
|
|
gw.Rows[6] = [Node("2", 6)];
|
|
gw.Rows[3] = [Node("3", 3)]; // same status: nothing to do
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(1, r.Updated);
|
|
Assert.Equal(["update:1:ARRIVE_LOCATION:4"], repo.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Status_0_is_released_at_jopetrol_and_then_here_only_when_jopetrol_confirms()
|
|
{
|
|
var (s, repo, gw, _) = Make();
|
|
repo.Trips["9"] = new ExistingTrip { Id = 9, Pn = "9", StatusCode = "NEW", TripStatus = "0" };
|
|
gw.Rows[0] = [Node("9", 0)];
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(1, r.Released);
|
|
Assert.Equal(["update:9:RESD:1"], repo.Writes);
|
|
Assert.Equal(["to1:9"], gw.Calls);
|
|
|
|
var (s2, repo2, gw2, _) = Make();
|
|
repo2.Trips["9"] = new ExistingTrip { Id = 9, Pn = "9", StatusCode = "NEW", TripStatus = "0" };
|
|
gw2.Rows[0] = [Node("9", 0)];
|
|
gw2.To1Answer = "Error";
|
|
Assert.Equal(0, (await s2.RunAsync(default)).Released);
|
|
Assert.Empty(repo2.Writes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Cancel_request_marks_the_trip_asks_jopetrol_and_mails_on_success()
|
|
{
|
|
var (s, repo, gw, mail) = Make();
|
|
repo.Trips["7"] = new ExistingTrip { Id = 7, Pn = "7", StatusCode = "RESD", TripStatus = "1" };
|
|
gw.Rows[-1] = [Node("7", -1)];
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(1, r.Cancelled);
|
|
Assert.Equal(["update:7:RVKD:-1", "update:7:RVKD:-2"], repo.Writes);
|
|
Assert.Equal(["m2:7"], gw.Calls);
|
|
Assert.Contains("7", mail.Sent.Single().Subject);
|
|
Assert.Equal(["a@x.com"], mail.Sent.Single().To);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Without_write_back_nothing_is_changed_at_jopetrol_and_status_0_and_minus_1_are_left_alone()
|
|
{
|
|
var (s, repo, gw, mail) = Make(writeBack: false);
|
|
repo.Trips["7"] = new ExistingTrip { Id = 7, Pn = "7", StatusCode = "RESD", TripStatus = "1" };
|
|
repo.Trips["8"] = new ExistingTrip { Id = 8, Pn = "8", StatusCode = "NEW", TripStatus = "0" };
|
|
gw.Rows[-1] = [Node("7", -1)];
|
|
gw.Rows[0] = [Node("8", 0)];
|
|
await s.RunAsync(default);
|
|
Assert.Empty(gw.Calls);
|
|
Assert.Empty(repo.Writes);
|
|
Assert.Empty(mail.Sent);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_cancel_request_for_an_unknown_trip_is_ignored_and_a_bad_row_is_counted_not_fatal()
|
|
{
|
|
var (s, repo, gw, _) = Make();
|
|
gw.Rows[-1] = [Node("404", -1)];
|
|
gw.Rows[5] = [XElement.Parse("<Table><NF55DOCO>1</NF55DOCO></Table>"), Node("5", 5)];
|
|
var r = await s.RunAsync(default);
|
|
Assert.Equal(1, r.Errors);
|
|
Assert.Equal(1, r.Inserted);
|
|
Assert.Equal(["insert:5:DISCHARGING:16186973"], repo.Writes);
|
|
Assert.Contains("NFTRDJ", r.Note);
|
|
}
|
|
}
|
|
|
|
public class RefineryDailyReportTests
|
|
{
|
|
[Fact]
|
|
public void Body_has_both_totals_in_dinars_the_title_and_the_year()
|
|
{
|
|
var html = RefineryDailyReportHtml.Build(1234.5m, 0m, new DateTime(2026, 11, 24), "https://x/en.png", "https://x/ar.png");
|
|
Assert.Contains("1,234.500 دينار", html);
|
|
Assert.Contains("0.000 دينار", html);
|
|
Assert.Contains("التقرير اليومي لعطاء الصرف", html);
|
|
Assert.Contains("Monday, November 23, 2026", html); // yesterday, as legacy now()-1
|
|
Assert.Contains("2026", html);
|
|
Assert.Contains("https://x/ar.png", html);
|
|
}
|
|
|
|
private sealed class Repo : IRefineryDailyReportRepository
|
|
{
|
|
public Task<(decimal First, decimal Second)> GetTotalsAsync(CancellationToken ct = default) => Task.FromResult((5m, 6m));
|
|
}
|
|
|
|
private sealed class Mail : IMailService
|
|
{
|
|
public string[] To = []; public string Subject = ""; public string Body = "";
|
|
public Task<bool> SendHtmlAsync(IReadOnlyList<string> to, string subject, string htmlBody, CancellationToken ct = default) { To = to.ToArray(); Subject = subject; Body = htmlBody; return Task.FromResult(true); }
|
|
}
|
|
|
|
private sealed class Settings : IRefineryJobSettingsService
|
|
{
|
|
public RefineryJobOptions Value { get; } = new() { DailyReportRecipients = ["fin@x.com", "ops@x.com"] };
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Service_mails_the_report_to_the_configured_receivers()
|
|
{
|
|
var mail = new Mail();
|
|
Assert.True(await new RefineryDailyReportService(new Repo(), mail, new Settings()).SendAsync(default));
|
|
Assert.Equal("التقرير اليومي لعطاء الصرف", mail.Subject);
|
|
Assert.Equal(["fin@x.com", "ops@x.com"], mail.To);
|
|
Assert.Contains("5.000 دينار", mail.Body);
|
|
Assert.Contains("6.000 دينار", mail.Body);
|
|
}
|
|
}
|
|
|
|
public class JoSilosTests
|
|
{
|
|
private sealed class Settings(bool enabled) : IJoSilosSettingsService
|
|
{
|
|
public JoSilosOptions Value { get; } = new() { Enabled = enabled, SilosUrl = "http://old", AqabaSilosUrl = "http://new" };
|
|
}
|
|
|
|
private sealed class Repo : IJoSilosRepository
|
|
{
|
|
public List<JoSilosPermitRow> Old = [];
|
|
public List<AqabaSilosPermitRow> New = [];
|
|
public List<string> Log = [];
|
|
public Task<IReadOnlyList<JoSilosPermitRow>> GetPermitsForSilosAsync(int max, CancellationToken ct = default) => Task.FromResult<IReadOnlyList<JoSilosPermitRow>>(Old);
|
|
public Task<IReadOnlyList<AqabaSilosPermitRow>> GetPermitsForAqabaSilosAsync(int max, CancellationToken ct = default) => Task.FromResult<IReadOnlyList<AqabaSilosPermitRow>>(New);
|
|
public Task<long> AddQueueLogAsync(string type, string operationType, long permitId, string permitStatusCode, int currentLeg, long eventId, string queueData, CancellationToken ct = default)
|
|
{ Log.Add($"add:{type}:{permitId}:{permitStatusCode}"); return Task.FromResult(100L + Log.Count); }
|
|
public Task MarkProcessedAsync(long logId, string result, CancellationToken ct = default) { Log.Add($"ok:{logId}:{result}"); return Task.CompletedTask; }
|
|
public Task SetResultAsync(long logId, string result, CancellationToken ct = default) { Log.Add($"fail:{logId}:{result}"); return Task.CompletedTask; }
|
|
}
|
|
|
|
private sealed class Gateway(Func<string, string> answer) : IJoSilosGatewayService
|
|
{
|
|
public List<(string Url, string Json)> Posts = [];
|
|
public Task<string> PostJsonAsync(string url, string json, CancellationToken ct = default) { Posts.Add((url, json)); return Task.FromResult(answer(url)); }
|
|
}
|
|
|
|
[Fact]
|
|
public void Old_service_body_keeps_the_legacy_fields_and_escapes_text()
|
|
{
|
|
using var doc = JsonDocument.Parse(JoSilosPayloads.Silos(new JoSilosPermitRow
|
|
{
|
|
RequestNo = "P1", RequestDate = "2026-11-24", ToLocation = "عمّان \"الشمال\"", ItemCode = "10", ItemName = "قمح", FirstPlateNo = "12345", SecondPlateNo = "678",
|
|
DriverName = "سائق", DriverNatId = "9871234567", DriverLicenseNo = "0", DriverMobileNo = "0791234567", TrailerType = "تريلا", CarrierId = "55", CarrierName = "شركة", TimeStamp = "2026-11-24 10:00:00",
|
|
}));
|
|
var r = doc.RootElement;
|
|
Assert.Equal(" Aqaba", r.GetProperty("fromLocation").GetString());
|
|
Assert.Equal("عمّان \"الشمال\"", r.GetProperty("toLocation").GetString());
|
|
Assert.Equal("2026-11-24", r.GetProperty("versionDate").GetString());
|
|
Assert.True(r.GetProperty("isEnsured").GetBoolean());
|
|
Assert.Equal(JsonValueKind.Null, r.GetProperty("driverLicenseExpiryDate").ValueKind);
|
|
Assert.Equal("nafith", r.GetProperty("createdBy").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public void New_service_body_has_upper_case_keys_numbers_and_the_log_id_last()
|
|
{
|
|
var json = JoSilosPayloads.AqabaSilos(new AqabaSilosPermitRow { PermitId = 5, Pn = "T1", ReferenceNo = "R1", LocationId = 77 }, 123);
|
|
using var doc = JsonDocument.Parse(json);
|
|
var r = doc.RootElement;
|
|
Assert.Equal("AQABASILOS", r.GetProperty("TYPE").GetString());
|
|
Assert.Equal(5, r.GetProperty("PERMIT_ID").GetInt64());
|
|
Assert.Equal(10184, r.GetProperty("DESTINATION").GetInt32());
|
|
Assert.Equal("RESD", r.GetProperty("PERMIT_STATUS_CODE").GetString());
|
|
Assert.Equal(123, r.GetProperty("LOGID").GetInt64());
|
|
Assert.EndsWith("\"LOGID\":123}", json);
|
|
Assert.DoesNotContain("LOGID", JoSilosPayloads.AqabaSilos(new AqabaSilosPermitRow(), null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Off_by_default_nothing_is_sent()
|
|
{
|
|
var repo = new Repo { Old = [new() { Id = 1 }] };
|
|
var gw = new Gateway(_ => "RECEIVED");
|
|
var r = await new JoSilosService(repo, gw, new Settings(false)).RunAsync(default);
|
|
Assert.Equal(0, r.Total);
|
|
Assert.Empty(gw.Posts);
|
|
Assert.Empty(repo.Log);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Each_service_has_its_own_success_answer_and_failures_stay_pending_with_the_answer()
|
|
{
|
|
gwCalls = 0;
|
|
var repo = new Repo
|
|
{
|
|
Old = [new() { Id = 1, RequestNo = "A" }, new() { Id = 2, RequestNo = "B" }],
|
|
New = [new() { PermitId = 3, Pn = "C" }, new() { PermitId = 4, Pn = "D" }],
|
|
};
|
|
var gw = new Gateway(url => url == "http://old" ? (gwCalls++ == 0 ? "RECEIVED" : "ERR") : (gwCalls++ == 2 ? "\"RECEIVED\"" : "boom"));
|
|
var r = await new JoSilosService(repo, gw, new Settings(true)).RunAsync(default);
|
|
Assert.Equal(1, r.SilosSent);
|
|
Assert.Equal(1, r.SilosFailed);
|
|
Assert.Equal(1, r.AqabaSent);
|
|
Assert.Equal(1, r.AqabaFailed);
|
|
Assert.Contains("ok:101:RECEIVED", repo.Log);
|
|
Assert.Contains(repo.Log, l => l.StartsWith("fail:") && l.EndsWith(":ERR"));
|
|
Assert.Contains(repo.Log, l => l.EndsWith(":\"RECEIVED\"") && l.StartsWith("ok:"));
|
|
Assert.Contains("add:JOSILOS:1:CLOSE", repo.Log);
|
|
Assert.Contains("add:AQABASILOS:3:RESD", repo.Log);
|
|
Assert.Contains("\"LOGID\":", gw.Posts[2].Json);
|
|
}
|
|
|
|
private static int gwCalls;
|
|
|
|
[Fact]
|
|
public async Task A_network_failure_on_one_permit_does_not_stop_the_others()
|
|
{
|
|
gwCalls = 0;
|
|
var repo = new Repo { Old = [new() { Id = 1, RequestNo = "A" }, new() { Id = 2, RequestNo = "B" }] };
|
|
var n = 0;
|
|
var gw = new Gateway(_ => ++n == 1 ? throw new HttpRequestException("down") : "RECEIVED");
|
|
var r = await new JoSilosService(repo, gw, new Settings(true)).RunAsync(default);
|
|
Assert.Equal(1, r.SilosSent);
|
|
Assert.Equal(1, r.SilosFailed);
|
|
Assert.Contains("down", r.Note);
|
|
}
|
|
}
|