Skip to content

Commit

Permalink
Merge branch 'main' into xabpe/2-Refactor-BuildingBlocks-to-clean-arc…
Browse files Browse the repository at this point in the history
…hitecture
  • Loading branch information
dstenroejl authored and AndersBallingPetersen committed Nov 25, 2024
2 parents c485e42 + b96fd70 commit a709d6c
Show file tree
Hide file tree
Showing 64 changed files with 955 additions and 264 deletions.
4 changes: 4 additions & 0 deletions docs/ProcessManager.Client/ReleaseNotes/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ProcessManager.Client Release Notes

## Version 0.11.0

- Extend framework to require 'OperatingIdentity' when initiating commands (start, schedule, cancel).

## Version 0.10.0

- Added 'RequestCalculatedDataClientV1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.AzureStorage" Version="7.0.0" />
<PackageReference Include="Azure.Storage.Blobs.Batch" Version="12.18.1" />
<PackageReference Include="Azure.Storage.Blobs.Batch" Version="12.20.0" />
<PackageReference Include="Energinet.DataHub.Core.App.Common" Version="13.2.0" />
<PackageReference Include="Energinet.DataHub.Core.Messaging" Version="6.1.0" />
<PackageReference Include="MediatR.Contracts" Version="2.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public ScenarioOrchestrationsAppFixture()
: base(
ScenarioAppFixturesConfiguration.Instance.DatabaseManager,
ScenarioAppFixturesConfiguration.Instance.TaskHubName,
ScenarioAppFixturesConfiguration.Instance.OrchestrationsAppPort)
ScenarioAppFixturesConfiguration.Instance.OrchestrationsAppPort,
disposeDatabase: true) // Quickfix: Only dispose database in one of the scenario fixtures
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public ScenarioProcessManagerAppFixture()
: base(
ScenarioAppFixturesConfiguration.Instance.DatabaseManager,
ScenarioAppFixturesConfiguration.Instance.TaskHubName,
ScenarioAppFixturesConfiguration.Instance.ProcessManagerAppPort)
ScenarioAppFixturesConfiguration.Instance.ProcessManagerAppPort,
disposeDatabase: false) // Quickfix: Only dispose database in one of the scenario fixtures
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ public async Task CalculationBrs023_WhenScheduledUsingClient_CanMonitorLifecycle
{
// TODO: Move to API test project
dynamic scheduleRequestDto = new ExpandoObject();
scheduleRequestDto.OperatingIdentity = new ExpandoObject();
scheduleRequestDto.OperatingIdentity.UserId = Guid.NewGuid();
scheduleRequestDto.OperatingIdentity.ActorId = Guid.NewGuid();
scheduleRequestDto.RunAt = "2024-11-01T06:19:10.0209567+01:00";
scheduleRequestDto.InputParameter = new ExpandoObject();
scheduleRequestDto.InputParameter.CalculationType = 0;
scheduleRequestDto.InputParameter.GridAreaCodes = new[] { "543" };
scheduleRequestDto.InputParameter.PeriodStartDate = "2024-10-29T15:19:10.0151351+01:00";
scheduleRequestDto.InputParameter.PeriodEndDate = "2024-10-29T16:19:10.0193962+01:00";
scheduleRequestDto.InputParameter.IsInternalCalculation = true;
scheduleRequestDto.InputParameter.UserId = Guid.NewGuid();

using var scheduleRequest = new HttpRequestMessage(
HttpMethod.Post,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ public async Task CalculationBrs023_WhenScheduledUsingClient_CanMonitorLifecycle
// Step 1: Schedule new calculation orchestration instance
var orchestrationInstanceId = await calculationClient
.ScheduleNewCalculationAsync(
new ClientTypes.Energinet.DataHub.ProcessManager.Api.Model.ScheduleOrchestrationInstanceDto<NotifyAggregatedMeasureDataInputV1>(
RunAt: DateTimeOffset.Parse("2024-11-01T06:19:10.0209567+01:00"),
InputParameter: new NotifyAggregatedMeasureDataInputV1(
new ClientTypes.Energinet.DataHub.ProcessManager.Api.Model.ScheduleOrchestrationInstanceCommand<NotifyAggregatedMeasureDataInputV1>(
operatingIdentity: new ClientTypes.Energinet.DataHub.ProcessManager.Api.Model.OrchestrationInstance.UserIdentityDto(
UserId: Guid.NewGuid(),
ActorId: Guid.NewGuid()),
runAt: DateTimeOffset.Parse("2024-11-01T06:19:10.0209567+01:00"),
inputParameter: new NotifyAggregatedMeasureDataInputV1(
CalculationTypes.BalanceFixing,
GridAreaCodes: new[] { "543" },
PeriodStartDate: DateTimeOffset.Parse("2024-10-29T15:19:10.0151351+01:00"),
PeriodEndDate: DateTimeOffset.Parse("2024-10-29T16:19:10.0193962+01:00"),
IsInternalCalculation: true,
UserId: Guid.NewGuid())),
IsInternalCalculation: true)),
CancellationToken.None);

// Step 2: Trigger the scheduler to queue the calculation orchestration instance
Expand Down
3 changes: 2 additions & 1 deletion source/ProcessManager.Client/IProcessManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.ProcessManager.Api.Model;
using Energinet.DataHub.ProcessManager.Api.Model.OrchestrationInstance;

namespace Energinet.DataHub.ProcessManager.Client;
Expand All @@ -25,7 +26,7 @@ public interface IProcessManagerClient
/// Cancel a scheduled orchestration instance.
/// </summary>
public Task CancelScheduledOrchestrationInstanceAsync(
Guid id,
CancelScheduledOrchestrationInstanceCommand command,
CancellationToken cancellationToken);

/// <summary>
Expand Down
15 changes: 12 additions & 3 deletions source/ProcessManager.Client/ProcessManager.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>Energinet.DataHub.ProcessManager.Client</PackageId>
<PackageVersion>0.10.0$(VersionSuffix)</PackageVersion>
<PackageVersion>0.11.0$(VersionSuffix)</PackageVersion>
<Title>DH3 Process Manager Client library</Title>
<Company>Energinet-DataHub</Company>
<Authors>Energinet-DataHub</Authors>
Expand Down Expand Up @@ -52,14 +52,23 @@
<Folder Include="Processes\BRS_023_027\V1\Model\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared\ProcessManager\Api\Model\CancelScheduledOrchestrationInstanceCommand.cs" Link="Model\CancelScheduledOrchestrationInstanceCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\IInputParameterDto.cs" Link="Model\IInputParameterDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\MessageCommand.cs" Link="Model\MessageCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstanceCommand.cs" Link="Model\OrchestrationInstanceCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstanceTypedDto.cs" Link="Model\OrchestrationInstanceTypedDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\ActorIdentityDto.cs" Link="Model\OrchestrationInstance\ActorIdentityDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\OperatingIdentityDto.cs" Link="Model\OrchestrationInstance\OperatingIdentityDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\OrchestrationInstanceDto.cs" Link="Model\OrchestrationInstance\OrchestrationInstanceDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\OrchestrationInstanceLifecycleStateDto.cs" Link="Model\OrchestrationInstance\OrchestrationInstanceLifecycleStateDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\OrchestrationInstanceLifecycleStates.cs" Link="Model\OrchestrationInstance\OrchestrationInstanceLifecycleStates.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\OrchestrationInstanceLifecycleStatesDto.cs" Link="Model\OrchestrationInstance\OrchestrationInstanceLifecycleStatesDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\StepInstanceDto.cs" Link="Model\OrchestrationInstance\StepInstanceDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\StepInstanceLifecycleStateDto.cs" Link="Model\OrchestrationInstance\StepInstanceLifecycleStateDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\StepInstanceLifecycleStates.cs" Link="Model\OrchestrationInstance\StepInstanceLifecycleStates.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\ScheduleOrchestrationInstanceDto.cs" Link="Model\ScheduleOrchestrationInstanceDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\OrchestrationInstance\UserIdentityDto.cs" Link="Model\OrchestrationInstance\UserIdentityDto.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\ScheduleOrchestrationInstanceCommand.cs" Link="Model\ScheduleOrchestrationInstanceCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\StartOrchestrationInstanceCommand.cs" Link="Model\StartOrchestrationInstanceCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Api\Model\UserCommand.cs" Link="Model\UserCommand.cs" />
<Compile Include="..\Shared\ProcessManager\Orchestrations\Processes\BRS_023_027\V1\Model\CalculationTypes.cs" Link="Processes\BRS_023_027\V1\Model\CalculationTypes.cs" />
<Compile Include="..\Shared\ProcessManager\Orchestrations\Processes\BRS_023_027\V1\Model\NotifyAggregatedMeasureDataInputV1.cs" Link="Processes\BRS_023_027\V1\Model\NotifyAggregatedMeasureDataInputV1.cs" />
<Compile Include="..\Shared\ProcessManager\Orchestrations\Processes\BRS_026\V1\Model\RequestCalculatedEnergyTimeSeriesInputV1.cs">
Expand Down
13 changes: 10 additions & 3 deletions source/ProcessManager.Client/ProcessManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System.Globalization;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using Energinet.DataHub.ProcessManager.Api.Model;
using Energinet.DataHub.ProcessManager.Api.Model.OrchestrationInstance;
using Energinet.DataHub.ProcessManager.Client.Extensions.DependencyInjection;

Expand All @@ -32,12 +34,17 @@ public ProcessManagerClient(IHttpClientFactory httpClientFactory)

/// <inheritdoc/>
public async Task CancelScheduledOrchestrationInstanceAsync(
Guid id,
CancelScheduledOrchestrationInstanceCommand command,
CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(
HttpMethod.Delete,
$"/api/processmanager/orchestrationinstance/{id}");
HttpMethod.Post,
"/api/processmanager/orchestrationinstance/cancel");
var json = JsonSerializer.Serialize(command);
request.Content = new StringContent(
json,
Encoding.UTF8,
"application/json");

using var actualResponse = await _httpClient
.SendAsync(request, cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface INotifyAggregatedMeasureDataClientV1
/// Schedule a BRS-023 or BRS-027 calculation and return its id.
/// </summary>
public Task<Guid> ScheduleNewCalculationAsync(
ScheduleOrchestrationInstanceDto<NotifyAggregatedMeasureDataInputV1> requestDto,
ScheduleOrchestrationInstanceCommand<NotifyAggregatedMeasureDataInputV1> command,
CancellationToken cancellationToken);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public NotifyAggregatedMeasureDataClientV1(IHttpClientFactory httpClientFactory)

/// <inheritdoc/>
public async Task<Guid> ScheduleNewCalculationAsync(
ScheduleOrchestrationInstanceDto<NotifyAggregatedMeasureDataInputV1> requestDto,
ScheduleOrchestrationInstanceCommand<NotifyAggregatedMeasureDataInputV1> command,
CancellationToken cancellationToken)
{
// TODO:
Expand All @@ -46,8 +46,9 @@ public async Task<Guid> ScheduleNewCalculationAsync(
using var request = new HttpRequestMessage(
HttpMethod.Post,
"/api/processmanager/orchestrationinstance/brs_023_027/1");
var json = JsonSerializer.Serialize(command);
request.Content = new StringContent(
JsonSerializer.Serialize(requestDto),
json,
Encoding.UTF8,
"application/json");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.ProcessManager.Api.Model;
using Energinet.DataHub.ProcessManager.Client.Processes.BRS_026_028.V1.Model;
using Energinet.DataHub.ProcessManager.Orchestrations.Processes.BRS_026.V1.Models;

Expand All @@ -25,10 +26,14 @@ public interface IRequestCalculatedDataClientV1
/// <summary>
/// Start a request for energy results
/// </summary>
public Task RequestCalculatedEnergyTimeSeriesAsync(RequestCalculatedDataInputV1<RequestCalculatedEnergyTimeSeriesInputV1> input, CancellationToken cancellationToken);
public Task RequestCalculatedEnergyTimeSeriesAsync(
MessageCommand<RequestCalculatedEnergyTimeSeriesInputV1> command,
CancellationToken cancellationToken);

/// <summary>
/// Start a request for wholesale results
/// </summary>
public Task RequestCalculatedWholesaleServicesAsync(RequestCalculatedDataInputV1<object> input, CancellationToken cancellationToken);
public Task RequestCalculatedWholesaleServicesAsync(
MessageCommand<RequestCalculatedWholesaleServicesInputV1> command,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.ProcessManager.Api.Model;

namespace Energinet.DataHub.ProcessManager.Client.Processes.BRS_026_028.V1.Model;

public record RequestCalculatedDataInputV1<TInput>(
string MessageId,
TInput Input)
where TInput : class;
public record RequestCalculatedWholesaleServicesInputV1()
: IInputParameterDto;
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Dynamic;
using System.Text.Json;
using Azure.Messaging.ServiceBus;
using Energinet.DataHub.ProcessManager.Api.Model;
using Energinet.DataHub.ProcessManager.Client.Extensions.Options;
using Energinet.DataHub.ProcessManager.Client.Processes.BRS_026_028.V1.Model;
using Energinet.DataHub.ProcessManager.Orchestrations.Contracts;
Expand All @@ -30,47 +30,47 @@ public class RequestCalculatedDataClientV1(
{
private readonly ServiceBusSender _serviceBusSender = serviceBusFactory.CreateClient(nameof(ProcessManagerServiceBusClientsOptions.TopicName));

public async Task RequestCalculatedEnergyTimeSeriesAsync(RequestCalculatedDataInputV1<RequestCalculatedEnergyTimeSeriesInputV1> input, CancellationToken cancellationToken)
public async Task RequestCalculatedEnergyTimeSeriesAsync(MessageCommand<RequestCalculatedEnergyTimeSeriesInputV1> command, CancellationToken cancellationToken)
{
var serviceBusMessage = CreateServiceBusMessage(
"BRS_026",
1,
input);
command);

await SendServiceBusMessage(
serviceBusMessage,
cancellationToken)
.ConfigureAwait(false);
}

public async Task RequestCalculatedWholesaleServicesAsync(RequestCalculatedDataInputV1<object> input, CancellationToken cancellationToken)
public async Task RequestCalculatedWholesaleServicesAsync(MessageCommand<RequestCalculatedWholesaleServicesInputV1> command, CancellationToken cancellationToken)
{
var serviceBusMessage = CreateServiceBusMessage(
"BRS_028",
1,
input);
command);

await SendServiceBusMessage(serviceBusMessage, cancellationToken)
.ConfigureAwait(false);
}

private ServiceBusMessage CreateServiceBusMessage<TInput>(
private ServiceBusMessage CreateServiceBusMessage<TInputParameterDto>(
string orchestrationName,
int orchestrationVersion,
RequestCalculatedDataInputV1<TInput> input)
where TInput : class
MessageCommand<TInputParameterDto> command)
where TInputParameterDto : IInputParameterDto
{
var message = new StartOrchestrationDto
{
OrchestrationName = orchestrationName,
OrchestrationVersion = orchestrationVersion,
JsonInput = JsonSerializer.Serialize(input.Input),
JsonInput = JsonSerializer.Serialize(command.InputParameter),
};

ServiceBusMessage serviceBusMessage = new(JsonFormatter.Default.Format(message))
{
Subject = orchestrationName,
MessageId = input.MessageId,
MessageId = command.MessageId,
ContentType = "application/json",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ public async Task Given_OrchestrationInstanceWithStepsAddedToDbContext_WhenRetri
.BeEquivalentTo(existingOrchestrationInstance);
}

[Fact]
public async Task Given_UserCanceledOrchestrationInstanceAddedToDbContext_WhenRetrievingFromDatabase_HasCorrectValues()
{
// Arrange
var userIdentity = new UserIdentity(new UserId(Guid.NewGuid()), new ActorId(Guid.NewGuid()));

var existingOrchestrationDescription = CreateOrchestrationDescription();
var existingOrchestrationInstance = CreateOrchestrationInstance(
existingOrchestrationDescription,
identity: userIdentity,
runAt: SystemClock.Instance.GetCurrentInstant());
existingOrchestrationInstance.Lifecycle.TransitionToUserCanceled(SystemClock.Instance, userIdentity);

await using (var writeDbContext = _fixture.DatabaseManager.CreateDbContext())
{
writeDbContext.OrchestrationDescriptions.Add(existingOrchestrationDescription);
writeDbContext.OrchestrationInstances.Add(existingOrchestrationInstance);
await writeDbContext.SaveChangesAsync();
}

// Act
await using var readDbContext = _fixture.DatabaseManager.CreateDbContext();
var orchestrationInstance = await readDbContext.OrchestrationInstances.FindAsync(existingOrchestrationInstance.Id);

// Assert
orchestrationInstance.Should()
.NotBeNull()
.And
.BeEquivalentTo(existingOrchestrationInstance);
}

private static OrchestrationDescription CreateOrchestrationDescription()
{
var orchestrationDescription = new OrchestrationDescription(
Expand All @@ -95,12 +126,19 @@ private static OrchestrationDescription CreateOrchestrationDescription()
return orchestrationDescription;
}

private static OrchestrationInstance CreateOrchestrationInstance(OrchestrationDescription orchestrationDescription)
private static OrchestrationInstance CreateOrchestrationInstance(OrchestrationDescription orchestrationDescription, OperatingIdentity? identity = default, Instant? runAt = default)
{
var operatingIdentity = identity
?? new UserIdentity(
new UserId(Guid.NewGuid()),
new ActorId(Guid.NewGuid()));

var orchestrationInstance = OrchestrationInstance.CreateFromDescription(
description: orchestrationDescription,
operatingIdentity,
orchestrationDescription,
skipStepsBySequence: [3],
clock: SystemClock.Instance);
clock: SystemClock.Instance,
runAt);

orchestrationInstance.ParameterValue.SetFromInstance(new TestOrchestrationParameter
{
Expand Down
Loading

0 comments on commit a709d6c

Please sign in to comment.