Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update ActorConsolidationAuditLogRepository to have an object in new and old value #1050

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry]
ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry]
ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE())
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

namespace Energinet.DataHub.MarketParticipant.Infrastructure.Model;

public sealed class ActorConsolidationActorAndDate
{
public Guid ActorId { get; set; }
public DateTimeOffset ConsolidateAt { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public sealed class ActorConsolidationAuditLogEntryEntity
public string NewValue { get; set; } = null!;
public string OldValue { get; set; } = null!;
public DateTimeOffset Timestamp { get; set; }
public DateTimeOffset ConsolidateAt { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Energinet.DataHub.MarketParticipant.Domain.Model;
using Energinet.DataHub.MarketParticipant.Domain.Model.Users;
using Energinet.DataHub.MarketParticipant.Domain.Repositories;
using Energinet.DataHub.MarketParticipant.Infrastructure.Model;
using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model;
using Microsoft.EntityFrameworkCore;
using NodaTime.Extensions;
Expand Down Expand Up @@ -51,8 +53,8 @@ from entity in entities
entity.Timestamp.ToInstant(),
new AuditIdentity(entity.ChangedByUserId),
false,
entity.NewValue,
entity.OldValue);
GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.NewValue),
GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.OldValue));

static GridAreaAuditedChange Map(ActorConsolidationAuditLogField field)
{
Expand Down Expand Up @@ -82,8 +84,8 @@ from entity in entities
entity.Timestamp.ToInstant(),
new AuditIdentity(entity.ChangedByUserId),
false,
entity.NewValue,
entity.OldValue);
GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.NewValue),
GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.OldValue));

static ActorAuditedChange Map(ActorConsolidationAuditLogField field)
{
Expand Down Expand Up @@ -113,7 +115,8 @@ public Task AuditAsync(
ChangedByUserId = auditIdentity.Value,
Field = (int)Map(change),
NewValue = actorConsolidation.ActorToId.ToString(),
OldValue = actorConsolidation.ActorFromId.ToString()
OldValue = actorConsolidation.ActorFromId.ToString(),
ConsolidateAt = actorConsolidation.ConsolidateAt.ToDateTimeOffset()
};

_context.ActorConsolidationAuditLogEntries.Add(entity);
Expand All @@ -129,4 +132,13 @@ static ActorConsolidationAuditLogField Map(GridAreaAuditedChange change)
};
}
}

private static string GetSerializedActorConsolidationActorAndDate(DateTimeOffset consolidateAt, string actorId)
{
return JsonSerializer.Serialize(new ActorConsolidationActorAndDate
{
ActorId = Guid.Parse(actorId),
ConsolidateAt = consolidateAt
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Energinet.DataHub.Core.App.Common.Abstractions.Users;
using Energinet.DataHub.MarketParticipant.Application.Commands.GridAreas;
Expand All @@ -23,6 +24,8 @@
using Energinet.DataHub.MarketParticipant.Domain.Model;
using Energinet.DataHub.MarketParticipant.Domain.Model.Users;
using Energinet.DataHub.MarketParticipant.Domain.Repositories;
using Energinet.DataHub.MarketParticipant.Infrastructure.Model;
using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model;
using Energinet.DataHub.MarketParticipant.IntegrationTests.Common;
using Energinet.DataHub.MarketParticipant.IntegrationTests.Fixtures;
using MediatR;
Expand Down Expand Up @@ -74,6 +77,7 @@
{
var expectedFrom = new ActorId(Guid.NewGuid());
var expectedTo = new ActorId(Guid.NewGuid());
var consolidateAt = SystemClock.Instance.GetCurrentInstant();

return TestAuditOfGridAreaChangeAsync(
response =>
Expand All @@ -83,8 +87,12 @@
.Where(log => log.AuditIdentityId != KnownAuditIdentityProvider.TestFramework.IdentityId.Value)
.Single(log => log.Change == GridAreaAuditedChange.ConsolidationRequested && !string.IsNullOrEmpty(log.CurrentValue));

Assert.Equal(expectedTo.ToString(), expectedLog.CurrentValue);
Assert.Equal(expectedFrom.ToString(), expectedLog.PreviousValue);
var expectedCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(expectedLog.CurrentValue);

Check warning on line 90 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
var expectedPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(expectedLog.PreviousValue);

Check warning on line 91 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
Assert.Equal(expectedTo.Value, expectedCurrentValue!.ActorId);
Assert.Equal(consolidateAt.ToDateTimeOffset(), expectedCurrentValue.ConsolidateAt);
Assert.Equal(expectedFrom.Value, expectedPreviousValue!.ActorId);
Assert.Equal(consolidateAt.ToDateTimeOffset(), expectedPreviousValue.ConsolidateAt);
},
async (gridArea, sp) =>
{
Expand All @@ -94,7 +102,7 @@
var actorConsolidation = new ActorConsolidation(
expectedFrom,
expectedTo,
SystemClock.Instance.GetCurrentInstant());
consolidateAt);

await actorConsolidationAuditLogRepository.AuditAsync(
new AuditIdentity(frontendUser.CurrentUser.UserId),
Expand All @@ -118,8 +126,10 @@
.Where(log => log.AuditIdentityId != KnownAuditIdentityProvider.TestFramework.IdentityId.Value)
.Single(log => log.Change == GridAreaAuditedChange.ConsolidationCompleted && !string.IsNullOrEmpty(log.CurrentValue));

Assert.Equal(expectedTo.ToString(), expectedLog.CurrentValue);
Assert.Equal(expectedFrom.ToString(), expectedLog.PreviousValue);
var expectedCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(expectedLog.CurrentValue);

Check warning on line 129 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
var expectedPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(expectedLog.PreviousValue);

Check warning on line 130 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
Assert.Equal(expectedTo.ToString(), expectedCurrentValue.ActorId.ToString());

Check warning on line 131 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Dereference of a possibly null reference.
Assert.Equal(expectedFrom.ToString(), expectedPreviousValue.ActorId.ToString());

Check warning on line 132 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Dereference of a possibly null reference.
},
async (gridArea, sp) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Energinet.DataHub.MarketParticipant.Domain.Model;
using Energinet.DataHub.MarketParticipant.Domain.Model.Users;
using Energinet.DataHub.MarketParticipant.Infrastructure.Model;
using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model;
using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Repositories;
using Energinet.DataHub.MarketParticipant.IntegrationTests.Common;
using Energinet.DataHub.MarketParticipant.IntegrationTests.Fixtures;
Expand Down Expand Up @@ -65,10 +68,13 @@
var actual = (await target.GetAsync(gridAreaId)).Single();

// Assert
var actualCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.CurrentValue);

Check warning on line 71 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
var actualPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.PreviousValue);

Check warning on line 72 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
Assert.Equal(auditIdentity, actual.AuditIdentity);
Assert.Equal(GridAreaAuditedChange.ConsolidationRequested, actual.Change);
Assert.Equal(actorConsolidation.ActorFromId.ToString(), actual.PreviousValue);
Assert.Equal(actorConsolidation.ActorToId.ToString(), actual.CurrentValue);
Assert.Equal(actorConsolidation.ActorFromId.ToString(), actualPreviousValue!.ActorId.ToString());
Assert.Equal(actorConsolidation.ActorToId.ToString(), actualCurrentValue!.ActorId.ToString());
Assert.Equal(actorConsolidation.ConsolidateAt.ToDateTimeOffset(), actualCurrentValue.ConsolidateAt);
}

[Fact]
Expand Down Expand Up @@ -99,9 +105,12 @@
var actual = (await target.GetAsync(actorConsolidation.ActorFromId)).Single();

// Assert
var actualCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.CurrentValue);

Check warning on line 108 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
var actualPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.PreviousValue);

Check warning on line 109 in source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / ci_dotnet / dotnet_ci_build / create_prerelease

Possible null reference argument for parameter 'json' in 'ActorConsolidationActorAndDate? JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(string json, JsonSerializerOptions? options = null)'.
Assert.Equal(auditIdentity, actual.AuditIdentity);
Assert.Equal(ActorAuditedChange.ConsolidationRequested, actual.Change);
Assert.Equal(actorConsolidation.ActorFromId.ToString(), actual.PreviousValue);
Assert.Equal(actorConsolidation.ActorToId.ToString(), actual.CurrentValue);
Assert.Equal(actorConsolidation.ActorFromId.ToString(), actualPreviousValue!.ActorId.ToString());
Assert.Equal(actorConsolidation.ActorToId.ToString(), actualCurrentValue!.ActorId.ToString());
Assert.Equal(actorConsolidation.ConsolidateAt.ToDateTimeOffset(), actualCurrentValue.ConsolidateAt);
}
}
Loading