Skip to content

Commit

Permalink
fix(cms->db): Fix E2E tests to match changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwashbrook committed Oct 1, 2024
1 parent cf072cf commit 97b6eba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
7 changes: 7 additions & 0 deletions plan-technology-for-your-school.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.PlanTech.Infrastructure
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.PlanTech.Infrastructure.ServiceBus", "src\Dfe.PlanTech.Infrastructure.ServiceBus\Dfe.PlanTech.Infrastructure.ServiceBus.csproj", "{D12F6316-D1C1-4CB2-A6B4-9062E215DB00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.PlanTech.CmsDbMigrations.E2ETests", "tests\Dfe.PlanTech.CmsDbMigrations.E2ETests\Dfe.PlanTech.CmsDbMigrations.E2ETests.csproj", "{FAC9275D-BCA1-4646-B415-9B786B5C9901}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -121,6 +123,10 @@ Global
{D12F6316-D1C1-4CB2-A6B4-9062E215DB00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D12F6316-D1C1-4CB2-A6B4-9062E215DB00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D12F6316-D1C1-4CB2-A6B4-9062E215DB00}.Release|Any CPU.Build.0 = Release|Any CPU
{FAC9275D-BCA1-4646-B415-9B786B5C9901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAC9275D-BCA1-4646-B415-9B786B5C9901}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAC9275D-BCA1-4646-B415-9B786B5C9901}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAC9275D-BCA1-4646-B415-9B786B5C9901}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -142,6 +148,7 @@ Global
{199DF118-68E9-42D6-ABB2-47FCC9BFD515} = {33DA2E55-0921-4BEA-A7F4-96CC77F09928}
{6A2698E9-2AB0-4CDB-A070-FA2DB4CA9DD4} = {33DA2E55-0921-4BEA-A7F4-96CC77F09928}
{D12F6316-D1C1-4CB2-A6B4-9062E215DB00} = {20FD2D0C-81CA-4337-B00D-9CB0BB36FE46}
{FAC9275D-BCA1-4646-B415-9B786B5C9901} = {33DA2E55-0921-4BEA-A7F4-96CC77F09928}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2661FAF0-D2EA-4265-846E-E6AB213C854C}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dfe.PlanTech.Application.Persistence.Interfaces;
using Dfe.PlanTech.Application.Persistence.Mappings;
using Dfe.PlanTech.CmsDbMigrations.E2ETests.Generators;
using Dfe.PlanTech.CmsDbMigrations.E2ETests.Utilities;
using Dfe.PlanTech.Domain.Caching.Enums;
using Dfe.PlanTech.Domain.Caching.Interfaces;
using Dfe.PlanTech.Domain.Content.Models;
Expand Down Expand Up @@ -59,6 +60,7 @@ public EntityTests(int listSize = 50)
protected readonly ICacheClearer cacheClearer;
protected readonly IDatabaseHelper<ICmsDbContext> DatabaseHelper;
protected readonly ILogger<WebhookToDbCommand> WebhookLogger;

[Fact]
public async Task Should_Publish_Entities()
{
Expand All @@ -80,7 +82,7 @@ public async Task Should_Update_Db_On_Publish()

foreach (var entity in updatedEntities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
ValidateDbMatches(entity.Updated, dbEntity, published: true);
Expand All @@ -101,7 +103,7 @@ public async Task Should_Ignore_Draft_Changes_When_UsePreview_False()
{
var newEntity = EntityGenerator.CopyWithDifferentValues(entity);

await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity), entity.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Sys.Id);
ValidateDbMatches(entity, dbEntity, published: true);
Expand Down Expand Up @@ -141,7 +143,7 @@ public async Task Should_Save_Changes_When_Draft_Content_Allowed()

foreach (var entity in updatedEntities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
ValidateDbMatches(entity.Updated, dbEntity, published: true);
Expand All @@ -167,7 +169,7 @@ public async Task Should_Republish_Content()

foreach (var entity in updatedEntities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
ValidateDbMatches(entity.Updated, dbEntity, published: true);
Expand All @@ -188,7 +190,7 @@ public async Task Should_Archive_Content()

foreach (var entity in updatedEntities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
ValidateDbMatches(entity.Updated, dbEntity, published: true, archived: true);
Expand All @@ -209,11 +211,11 @@ public async Task Should_Unarchive_Content()
{
Db.ChangeTracker.Clear();

await WebhookToDbCommand.ProcessMessage(CmsEvent.ARCHIVE.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(CmsEvent.ARCHIVE.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

Db.ChangeTracker.Clear();

await WebhookToDbCommand.ProcessMessage(CmsEvent.UNARCHIVE.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Original.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(CmsEvent.UNARCHIVE.ToString(), CreateWebhookBody(entity.Updated), entity.Original.Sys.Id, default);

Db.ChangeTracker.Clear();

Expand Down Expand Up @@ -252,7 +254,7 @@ public async Task Should_Allow_Save_When_NotExisting_In_Db()

foreach (var entity in entities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity), entity.Sys.Id, default);

var dbEntity = await GetDbEntityById(entity.Sys.Id);
ValidateDbMatches(entity, dbEntity, published: false);
Expand All @@ -275,7 +277,7 @@ public async Task Should_Allow_AutoSave_When_NotExisting_In_Db()

foreach (var entity in entities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity), entity.Sys.Id, default);

Db.ChangeTracker.Clear();

Expand Down Expand Up @@ -306,7 +308,7 @@ public async Task Should_Allow_Save_When_Exists_But_Not_Published()
Db.ChangeTracker.Clear();

//Update with SAVE event
await WebhookToDbCommand.ProcessMessage(CmsEvent.SAVE.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Updated.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(CmsEvent.SAVE.ToString(), CreateWebhookBody(entity.Updated), entity.Updated.Sys.Id, default);

//Assert changed
var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
Expand Down Expand Up @@ -334,7 +336,7 @@ public async Task Should_Allow_AutoSave_When_Exists_But_Not_Published()
Db.ChangeTracker.Clear();

//Update with SAVE event
await WebhookToDbCommand.ProcessMessage(CmsEvent.AUTO_SAVE.ToString(), JsonSerializer.Serialize(entity.Updated), entity.Updated.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(CmsEvent.AUTO_SAVE.ToString(), CreateWebhookBody(entity.Updated), entity.Updated.Sys.Id, default);

//Assert changed
var dbEntity = await GetDbEntityById(entity.Original.Sys.Id);
Expand All @@ -348,7 +350,7 @@ public async Task Should_Allow_AutoSave_When_Exists_But_Not_Published()

private async Task UpdateEntityStatus(TEntity entity, CmsEvent cmsEvent)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity), entity.Sys.Id, default);
Db.ChangeTracker.Clear();
}

Expand All @@ -375,7 +377,7 @@ private async Task CreateAndPublishEntities(Func<TEntity, Task>? afterEntitySave

foreach (var entity in entities)
{
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), JsonSerializer.Serialize(entity), entity.Sys.Id, default);
await WebhookToDbCommand.ProcessMessage(cmsEvent.ToString(), CreateWebhookBody(entity), entity.Sys.Id, default);

Db.ChangeTracker.Clear();

Expand All @@ -393,9 +395,7 @@ protected WebhookToDbCommand CreateWebhookToDbCommand(bool usePreviewContent = f

protected virtual Task<TDbEntity?> GetDbEntityById(TEntity entity) => GetDbEntityById(entity.Sys.Id);

protected virtual Task<TDbEntity?> GetDbEntityById(string id) => GetDbEntitiesQuery()
.AsNoTracking()
.FirstOrDefaultAsync(dbEntity => dbEntity.Id == id);
protected virtual Task<TDbEntity?> GetDbEntityById(string id) => GetDbEntitiesQuery().AsNoTracking().FirstOrDefaultAsync(dbEntity => dbEntity.Id == id);

protected virtual void ValidateEntityState(TDbEntity dbEntity, bool published, bool archived, bool deleted)
{
Expand All @@ -418,4 +418,10 @@ private static IMessageRetryHandler CreateMessageRetryHandlerSub()
return sub;
}

private string CreateWebhookBody(TEntity entity)
{
var dictionary = CreateEntityValuesDictionary(entity);

return EntityToPayload.ConvertEntityToPayload(entity, dictionary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ public TContentComponent CopyWithDifferentValues(TContentComponent contentCompon

public IEnumerable<UpdatedEntity<TContentComponent>> CopyWithDifferentValues(IEnumerable<TContentComponent> entities)
=> entities.Select(originalEntity => new UpdatedEntity<TContentComponent>(originalEntity, CopyWithDifferentValues(originalEntity)));

protected TContent CreateCopyOfEntityWithJustSys<TContent>(TContent content)
where TContent : ContentComponent, new()
=> new() { Sys = content.Sys };
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public static class GeneratorHelpers
public static void GenerateSys<TEntity>(this BaseGenerator<TEntity> baseGenerator)
where TEntity : ContentComponent
{
baseGenerator.RuleFor(answer => answer.Sys, faker => new SystemDetails() { Id = faker.Random.AlphaNumeric(22) });
baseGenerator.RuleFor(content => content.Sys, faker => new SystemDetails() { Id = faker.Random.AlphaNumeric(22) });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ public static IEnumerable<object> ToEntityIds(this IEnumerable<ContentComponent>
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

public static ServiceBusReceivedMessage CreateServiceBusMessage<T>(T entity, Dictionary<string, object?> fields, CmsEvent cmsEvent, ILogger logger)
where T : ContentComponent
{
var payload = ConvertEntityToPayload(entity, fields);

logger.LogInformation("Created JSON payload {Payload} from entity {Entity}", payload, JsonSerializer.Serialize(entity));

var subject = "ContentManagement.Entry." + cmsEvent.ToString().ToLower();
var serviceBusMessage = new ServiceBusMessage(payload) { Subject = subject };

var receivedMessage = ServiceBusReceivedMessage.FromAmqpMessage(serviceBusMessage.GetRawAmqpMessage(), BinaryData.FromBytes(Encoding.UTF8.GetBytes("")));
return receivedMessage;
}

public static string ConvertEntityToPayload<T>(T entity, Dictionary<string, object?> fields)
where T : ContentComponent
{
Expand Down

0 comments on commit 97b6eba

Please sign in to comment.