feat: begin working on database maintainer
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Faker;
|
||||
|
||||
class OfacFileServiceOptionsFaker : Faker<OfacFileServiceOptions>
|
||||
{
|
||||
public OfacFileServiceOptionsFaker()
|
||||
{
|
||||
RuleFor(x => x.Url, f => f.Internet.Url());
|
||||
RuleFor(x => x.SdnFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.AddressFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.AltNamesFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.CommentsFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.ConPrimaryNameFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.ConAddressesFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.ConAltNamesFileName, f => f.System.FileName("csv"));
|
||||
RuleFor(x => x.ConCommentsFileName, f => f.System.FileName("csv"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||
|
||||
public class DatabaseMaintainerTests : DatabaseTest
|
||||
{
|
||||
private readonly MockHttpMessageHandler _mockHttp = new();
|
||||
private readonly OfacFileServiceOptionsFaker _ofacFileServiceOptionsFaker = new();
|
||||
private readonly OfacFileServiceOptions _ofacFileServiceOptions;
|
||||
private readonly DatabaseMaintainer _databaseMaintainer;
|
||||
private static MemoryStream CreateCsvStream(string csv) => new(Encoding.UTF8.GetBytes(csv));
|
||||
|
||||
public DatabaseMaintainerTests()
|
||||
{
|
||||
_ofacFileServiceOptions = _ofacFileServiceOptionsFaker.Generate();
|
||||
|
||||
var ofacFileService = new OfacFileService(
|
||||
_mockHttp.ToHttpClient(),
|
||||
_loggerFactory.CreateLogger<OfacFileService>(),
|
||||
_ofacFileServiceOptions
|
||||
);
|
||||
|
||||
var uow = new EfUnitOfWork(_context, _loggerFactory);
|
||||
var logger = _loggerFactory.CreateLogger<DatabaseMaintainer>();
|
||||
_databaseMaintainer = new DatabaseMaintainer(uow, ofacFileService, logger);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildSdnTableAsync_WhenCalled_ItShouldAddSdnCsvRecordsToDatabase()
|
||||
{
|
||||
var testCsv = """
|
||||
6906,"AL-IRAQI, Abd al-Hadi","individual","SDGT",-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,"DOB 1961; POB Mosul, Iraq; nationality Iraq; Gender Male."
|
||||
6907,"SHIHATA, Thirwat Salah","individual","SDGT",-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,"DOB 29 Jun 1960; POB Egypt."
|
||||
6908,"AHMAD, Tariq Anwar al-Sayyid","individual","SDGT",-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,"DOB 15 Mar 1963; POB Alexandria, Egypt."
|
||||
""";
|
||||
|
||||
var testStream = CreateCsvStream(testCsv);
|
||||
|
||||
_mockHttp
|
||||
.When(_ofacFileServiceOptions.GetSdnFileUri().ToString())
|
||||
.Respond("text/csv", testStream);
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
_timeProviderMock
|
||||
.Setup(x => x.GetUtcNow())
|
||||
.Returns(now);
|
||||
|
||||
await _databaseMaintainer.BuildSdnTableAsync();
|
||||
|
||||
var sdns = await _context.Set<Sdn>().ToListAsync();
|
||||
|
||||
sdns.Should().HaveCount(3);
|
||||
|
||||
sdns[0].Should().BeEquivalentTo(new Sdn()
|
||||
{
|
||||
Id = 6906,
|
||||
Name = "AL-IRAQI, Abd al-Hadi",
|
||||
Type = "individual",
|
||||
Program = "SDGT",
|
||||
Remarks = "DOB 1961; POB Mosul, Iraq; nationality Iraq; Gender Male.",
|
||||
CreatedAt = now.DateTime,
|
||||
UpdatedAt = now.DateTime
|
||||
});
|
||||
|
||||
sdns[1].Should().BeEquivalentTo(new Sdn()
|
||||
{
|
||||
Id = 6907,
|
||||
Name = "SHIHATA, Thirwat Salah",
|
||||
Type = "individual",
|
||||
Program = "SDGT",
|
||||
Remarks = "DOB 29 Jun 1960; POB Egypt.",
|
||||
CreatedAt = now.DateTime,
|
||||
UpdatedAt = now.DateTime
|
||||
});
|
||||
|
||||
sdns[2].Should().BeEquivalentTo(new Sdn()
|
||||
{
|
||||
Id = 6908,
|
||||
Name = "AHMAD, Tariq Anwar al-Sayyid",
|
||||
Type = "individual",
|
||||
Program = "SDGT",
|
||||
Remarks = "DOB 15 Mar 1963; POB Alexandria, Egypt.",
|
||||
CreatedAt = now.DateTime,
|
||||
UpdatedAt = now.DateTime
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,35 +2,22 @@ namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class OfacFileServiceTests
|
||||
{
|
||||
private const string Url = "https://example.com";
|
||||
private const string TestCsv = "Name,Address\nJohn Doe,123 Main St\n";
|
||||
private static Stream TestStream => new MemoryStream(Encoding.UTF8.GetBytes(TestCsv));
|
||||
private readonly MockHttpMessageHandler _mockHttp = new();
|
||||
private readonly Mock<ILogger<OfacFileService>> _mockLogger = new();
|
||||
private readonly Mock<IOptionsSnapshot<OfacFileServiceOptions>> _mockOptions = new();
|
||||
private readonly OfacFileServiceOptionsFaker _optionsFaker = new();
|
||||
private readonly OfacFileServiceOptions _options;
|
||||
private readonly OfacFileService _service;
|
||||
|
||||
public OfacFileServiceTests()
|
||||
{
|
||||
_mockOptions
|
||||
.Setup(x => x.Value)
|
||||
.Returns(new OfacFileServiceOptions
|
||||
{
|
||||
Url = Url,
|
||||
SdnFileName = "sdn.csv",
|
||||
AddressFileName = "address.csv",
|
||||
AltNamesFileName = "alt_names.csv",
|
||||
CommentsFileName = "comments.csv",
|
||||
ConPrimaryNameFileName = "con_primary_name.csv",
|
||||
ConAddressesFileName = "con_addresses.csv",
|
||||
ConAltNamesFileName = "con_alt_names.csv",
|
||||
ConCommentsFileName = "con_comments.csv"
|
||||
});
|
||||
_options = _optionsFaker.Generate();
|
||||
|
||||
_service = new OfacFileService(
|
||||
_mockHttp.ToHttpClient(),
|
||||
_mockLogger.Object,
|
||||
_mockOptions.Object
|
||||
_options
|
||||
);
|
||||
}
|
||||
|
||||
@@ -46,7 +33,7 @@ public class OfacFileServiceTests
|
||||
var act = () => new OfacFileService(
|
||||
null!,
|
||||
_mockLogger.Object,
|
||||
_mockOptions.Object
|
||||
_options
|
||||
);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
@@ -58,7 +45,7 @@ public class OfacFileServiceTests
|
||||
var act = () => new OfacFileService(
|
||||
_mockHttp.ToHttpClient(),
|
||||
null!,
|
||||
_mockOptions.Object
|
||||
_options
|
||||
);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
@@ -79,12 +66,10 @@ public class OfacFileServiceTests
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithNullOptionsValue_ThrowsArgumentNullException()
|
||||
{
|
||||
_mockOptions.Setup(x => x.Value).Returns(() => null!);
|
||||
|
||||
var act = () => new OfacFileService(
|
||||
_mockHttp.ToHttpClient(),
|
||||
_mockLogger.Object,
|
||||
_mockOptions.Object
|
||||
null!
|
||||
);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
@@ -94,7 +79,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetSdnFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/sdn.csv")
|
||||
.When(_options.GetSdnFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetSdnFileAsync();
|
||||
@@ -107,7 +92,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetSdnFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/sdn.csv")
|
||||
.When(_options.GetSdnFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetSdnFileAsync();
|
||||
@@ -119,7 +104,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetSdnFileAsync_WhenExceptionThrown_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/sdn.csv")
|
||||
.When(_options.GetSdnFileUri().ToString())
|
||||
.Throw(new Exception("Test exception"));
|
||||
|
||||
var result = await _service.GetSdnFileAsync();
|
||||
@@ -131,7 +116,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetAddressFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/address.csv")
|
||||
.When(_options.GetAddressFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetAddressFileAsync();
|
||||
@@ -144,7 +129,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetAddressFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/address.csv")
|
||||
.When(_options.GetAddressFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetAddressFileAsync();
|
||||
@@ -156,7 +141,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetAltNamesFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/alt_names.csv")
|
||||
.When(_options.GetAltNamesFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetAltNamesFileAsync();
|
||||
@@ -169,7 +154,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetAltNamesFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/alt_names.csv")
|
||||
.When(_options.GetAltNamesFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetAltNamesFileAsync();
|
||||
@@ -181,7 +166,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetCommentsFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/comments.csv")
|
||||
.When(_options.GetCommentsFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetCommentsFileAsync();
|
||||
@@ -194,7 +179,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetCommentsFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/comments.csv")
|
||||
.When(_options.GetCommentsFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetCommentsFileAsync();
|
||||
@@ -206,7 +191,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConPrimaryNameFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_primary_name.csv")
|
||||
.When(_options.GetConPrimaryNameFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetConPrimaryNameFileAsync();
|
||||
@@ -219,7 +204,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConPrimaryNameFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_primary_name.csv")
|
||||
.When(_options.GetConPrimaryNameFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetConPrimaryNameFileAsync();
|
||||
@@ -231,7 +216,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConAddressesFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_addresses.csv")
|
||||
.When(_options.GetConAddressesFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetConAddressesFileAsync();
|
||||
@@ -244,7 +229,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConAddressesFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_addresses.csv")
|
||||
.When(_options.GetConAddressesFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetConAddressesFileAsync();
|
||||
@@ -256,7 +241,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConAltNamesFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_alt_names.csv")
|
||||
.When(_options.GetConAltNamesFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetConAltNamesFileAsync();
|
||||
@@ -269,7 +254,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConAltNamesFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_alt_names.csv")
|
||||
.When(_options.GetConAltNamesFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetConAltNamesFileAsync();
|
||||
@@ -281,7 +266,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConCommentsFileAsync_WhenCalled_ItShouldReturnStream()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_comments.csv")
|
||||
.When(_options.GetConCommentsFileUri().ToString())
|
||||
.Respond("text/csv", TestStream);
|
||||
|
||||
var result = await _service.GetConCommentsFileAsync();
|
||||
@@ -294,7 +279,7 @@ public class OfacFileServiceTests
|
||||
public async Task GetConCommentsFileAsync_WhenRequestFails_ItShouldReturnFailure()
|
||||
{
|
||||
_mockHttp
|
||||
.When("https://example.com/con_comments.csv")
|
||||
.When(_options.GetConCommentsFileUri().ToString())
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var result = await _service.GetConCommentsFileAsync();
|
||||
|
||||
Reference in New Issue
Block a user