[FA-misc] Mass transit overhaul, needs testing and review
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.FileService.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Models.Configuration;
|
||||
using FictionArchive.Service.NovelService.Models.Enums;
|
||||
using FictionArchive.Service.NovelService.Models.Images;
|
||||
@@ -8,8 +7,9 @@ using FictionArchive.Service.NovelService.Models.Novels;
|
||||
using FictionArchive.Service.NovelService.Models.SourceAdapters;
|
||||
using FictionArchive.Service.NovelService.Services;
|
||||
using FictionArchive.Service.NovelService.Services.SourceAdapters;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
|
||||
using FluentAssertions;
|
||||
using MassTransit;
|
||||
using HtmlAgilityPack;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
@@ -72,7 +72,8 @@ public class NovelUpdateServiceTests
|
||||
private static NovelUpdateService CreateService(
|
||||
NovelServiceDbContext dbContext,
|
||||
ISourceAdapter adapter,
|
||||
IEventBus eventBus,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
ISendEndpointProvider sendEndpointProvider,
|
||||
string pendingImageUrl = "https://pending/placeholder.jpg")
|
||||
{
|
||||
var options = Options.Create(new NovelUpdateServiceConfiguration
|
||||
@@ -80,7 +81,7 @@ public class NovelUpdateServiceTests
|
||||
PendingImageUrl = pendingImageUrl
|
||||
});
|
||||
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, eventBus, options);
|
||||
return new NovelUpdateService(dbContext, NullLogger<NovelUpdateService>.Instance, new[] { adapter }, publishEndpoint, sendEndpointProvider, options);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -102,13 +103,15 @@ public class NovelUpdateServiceTests
|
||||
ImageData = new List<ImageData> { image1, image2 }
|
||||
}));
|
||||
|
||||
var publishedEvents = new List<FileUploadRequestCreatedEvent>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
eventBus.Publish(Arg.Do<FileUploadRequestCreatedEvent>(publishedEvents.Add)).Returns(Task.CompletedTask);
|
||||
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask);
|
||||
var publishedCommands = new List<UploadFileCommand>();
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||
var sendEndpoint = Substitute.For<ISendEndpoint>();
|
||||
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
|
||||
sendEndpoint.Send(Arg.Do<UploadFileCommand>(publishedCommands.Add), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
||||
|
||||
var pendingImageUrl = "https://pending/placeholder.jpg";
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingImageUrl);
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingImageUrl);
|
||||
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
|
||||
@@ -127,10 +130,10 @@ public class NovelUpdateServiceTests
|
||||
.Should()
|
||||
.BeEquivalentTo(updatedChapter.Images.Select(img => img.Id.ToString()));
|
||||
|
||||
publishedEvents.Should().HaveCount(2);
|
||||
publishedEvents.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
|
||||
publishedEvents.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
|
||||
publishedEvents.Should().OnlyContain(e => e.FilePath.StartsWith($"{novel.Id}/Images/Chapter-{updatedChapter.Id}/"));
|
||||
publishedCommands.Should().HaveCount(2);
|
||||
publishedCommands.Select(e => e.RequestId).Should().BeEquivalentTo(updatedChapter.Images.Select(i => i.Id));
|
||||
publishedCommands.Select(e => e.FileData).Should().BeEquivalentTo(new[] { image1.Data, image2.Data });
|
||||
publishedCommands.Should().OnlyContain(e => e.FilePath.StartsWith($"{novel.Id}/Images/Chapter-{updatedChapter.Id}/"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -151,11 +154,12 @@ public class NovelUpdateServiceTests
|
||||
ImageData = new List<ImageData> { image }
|
||||
}));
|
||||
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
eventBus.Publish(Arg.Any<FileUploadRequestCreatedEvent>()).Returns(Task.CompletedTask);
|
||||
eventBus.Publish(Arg.Any<object>(), Arg.Any<string>()).Returns(Task.CompletedTask);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||
var sendEndpoint = Substitute.For<ISendEndpoint>();
|
||||
sendEndpointProvider.GetSendEndpoint(Arg.Any<Uri>()).Returns(Task.FromResult(sendEndpoint));
|
||||
|
||||
var service = CreateService(dbContext, adapter, eventBus);
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
|
||||
|
||||
var updatedChapter = await service.PullChapterContents(novel.Id, volume.Id, chapter.Order);
|
||||
|
||||
@@ -186,8 +190,9 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/cover.jpg";
|
||||
|
||||
@@ -228,8 +233,9 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingUrl);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/image.jpg";
|
||||
|
||||
@@ -277,8 +283,9 @@ public class NovelUpdateServiceTests
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var adapter = Substitute.For<ISourceAdapter>();
|
||||
var eventBus = Substitute.For<IEventBus>();
|
||||
var service = CreateService(dbContext, adapter, eventBus, pendingUrl);
|
||||
var publishEndpoint = Substitute.For<IPublishEndpoint>();
|
||||
var sendEndpointProvider = Substitute.For<ISendEndpointProvider>();
|
||||
var service = CreateService(dbContext, adapter, publishEndpoint, sendEndpointProvider, pendingUrl);
|
||||
|
||||
var newUrl = "https://cdn.example.com/uploaded/img1.jpg";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user