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: Test of no input example #51

Merged
merged 22 commits into from
Dec 19, 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
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.14.5

- Internal refactoring.

## Version 0.14.4

- Add actor id to start orchestration messages.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ProcessManager.Orchestrations.Abstractions Release Notes

## Version 0.2.6

- Internal refactoring.

## Version 0.2.5

- Add actor id to start orchestration messages.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 Energinet.DataHub.ProcessManager.Abstractions.Api.Model.OrchestrationDescription;

namespace Energinet.DataHub.Example.Orchestrations.Abstractions.Processes.BRS_X01.NoInputExample.V1.Model;

public record Brs_X01_NoInputExample_V1()
: OrchestrationDescriptionUniqueNameDto("Brs_X01_NoInputExample_V1", 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 Energinet.DataHub.ProcessManager.Abstractions.Api.Model;
using Energinet.DataHub.ProcessManager.Abstractions.Api.Model.OrchestrationInstance;

namespace Energinet.DataHub.Example.Orchestrations.Abstractions.Processes.BRS_X01.NoInputExample.V1.Model;

/// <summary>
/// Command for starting a BRS-X01 no-input example.
/// Must be JSON serializable.
/// </summary>
public sealed record StartNoInputExampleComandV1
: StartOrchestrationInstanceCommand<UserIdentityDto>
{
/// <summary>
/// Construct command.
/// </summary>
/// <param name="operatingIdentity">Identity of the user executing the command.</param>
public StartNoInputExampleComandV1(
UserIdentityDto operatingIdentity)
: base(
operatingIdentity,
orchestrationDescriptionUniqueName: new Brs_X01_NoInputExample_V1())
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ public async Task ExampleOrchestration_WhenStarted_CanMonitorLifecycle()
Encoding.UTF8,
"application/json");

// Step 1: Start new example orchestration instance
// Step 1: Start new orchestration instance
using var response = await Fixture.ExampleOrchestrationsAppManager.AppHostManager
.HttpClient
.SendAsync(scheduleRequest);
response.EnsureSuccessStatusCode();

var calculationId = await response.Content
var orchestrationInstanceId = await response.Content
.ReadFromJsonAsync<Guid>();

// Step 2: Query until terminated with succeeded
var getRequest = new GetOrchestrationInstanceByIdQuery(
new UserIdentityDto(
Guid.NewGuid(),
Guid.NewGuid()),
calculationId);
orchestrationInstanceId);

var isTerminated = await Awaiter.TryWaitUntilConditionAsync(
async () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.Net.Http.Json;
using System.Text;
using System.Text.Json;
using Energinet.DataHub.Core.TestCommon;
using Energinet.DataHub.Example.Orchestrations.Abstractions.Processes.BRS_X01.NoInputExample.V1.Model;
using Energinet.DataHub.Example.Orchestrations.Tests.Fixtures;
using Energinet.DataHub.ProcessManager.Abstractions.Api.Model;
using Energinet.DataHub.ProcessManager.Abstractions.Api.Model.OrchestrationInstance;
using FluentAssertions;
using Xunit.Abstractions;

namespace Energinet.DataHub.Example.Orchestrations.Tests.Integration.Processes.BRS_X01.NoInputExample;

/// <summary>
/// Test case where we verify the Example.Orchestrations and Process Manager Api
/// can be used to start an example orchestration (with input parameter) and
RasmusGraabaek marked this conversation as resolved.
Show resolved Hide resolved
/// monitor its status during its lifetime.
/// </summary>
[Collection(nameof(ExampleOrchestrationsAppCollection))]
public class MonitorNoInputExampleUsingApiScenario : IAsyncLifetime
{
public MonitorNoInputExampleUsingApiScenario(
ExampleOrchestrationsAppFixture fixture,
ITestOutputHelper testOutputHelper)
{
Fixture = fixture;
Fixture.SetTestOutputHelper(testOutputHelper);
}

private ExampleOrchestrationsAppFixture Fixture { get; }

public Task InitializeAsync()
{
Fixture.ProcessManagerAppManager.AppHostManager.ClearHostLog();
Fixture.ExampleOrchestrationsAppManager.AppHostManager.ClearHostLog();

return Task.CompletedTask;
}

public Task DisposeAsync()
{
Fixture.ProcessManagerAppManager.SetTestOutputHelper(null!);
Fixture.ExampleOrchestrationsAppManager.SetTestOutputHelper(null!);

return Task.CompletedTask;
}

[Fact]
public async Task NoInputExampleOrchestration_WhenStarted_CanMonitorLifecycle()
{
var orchestration = new Brs_X01_NoInputExample_V1();

var command = new StartNoInputExampleComandV1(
RasmusGraabaek marked this conversation as resolved.
Show resolved Hide resolved
operatingIdentity: new UserIdentityDto(
Guid.NewGuid(),
Guid.NewGuid()));

using var scheduleRequest = new HttpRequestMessage(
HttpMethod.Post,
$"/api/orchestrationinstance/command/start");
scheduleRequest.Content = new StringContent(
JsonSerializer.Serialize(command),
Encoding.UTF8,
"application/json");

// Step 1: Start new orchestration instance
using var response = await Fixture.ProcessManagerAppManager.AppHostManager
.HttpClient
.SendAsync(scheduleRequest);
response.EnsureSuccessStatusCode();

var orchestrationInstanceId = await response.Content
.ReadFromJsonAsync<Guid>();

// Step 2: Query until terminated with succeeded
var getRequest = new GetOrchestrationInstanceByIdQuery(
new UserIdentityDto(
Guid.NewGuid(),
Guid.NewGuid()),
orchestrationInstanceId);

var isTerminated = await Awaiter.TryWaitUntilConditionAsync(
async () =>
{
using var queryRequest = new HttpRequestMessage(
HttpMethod.Post,
"/api/orchestrationinstance/query/id");
queryRequest.Content = new StringContent(
JsonSerializer.Serialize(getRequest),
Encoding.UTF8,
"application/json");

using var queryResponse = await Fixture.ProcessManagerAppManager.AppHostManager
.HttpClient
.SendAsync(queryRequest);
queryResponse.EnsureSuccessStatusCode();

var orchestrationInstance = await queryResponse.Content
.ReadFromJsonAsync<OrchestrationInstanceDto>();

return orchestrationInstance!.Lifecycle.State == OrchestrationInstanceLifecycleStates.Terminated;
},
timeLimit: TimeSpan.FromSeconds(40),
delay: TimeSpan.FromSeconds(2));

isTerminated.Should().BeTrue("because we expects the orchestration instance can complete within given wait time");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public async Task<OrchestrationExecutionPlan> Run(
orchestrationInstance.Lifecycle.TransitionToRunning(Clock);
await ProgressRepository.UnitOfWork.CommitAsync().ConfigureAwait(false);

// Orchestrations that have input have a custom start handler in which they can
// transition steps to skipped before starting the orchestration.
// We can extract that information and use it to plan the execution of the orchestrations.
var stepsSkippedBySequence = orchestrationInstance.Steps
.Where(step => step.IsSkipped())
.Select(step => step.Sequence)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Energinet.DataHub.ProcessManagement.Core.Application.Orchestration;
using Energinet.DataHub.ProcessManagement.Core.Domain.OrchestrationInstance;
using Microsoft.Azure.Functions.Worker;
using NodaTime;

namespace Energinet.DataHub.Example.Orchestrations.Processes.BRS_X01.NoInputExample.V1.Activities;

internal class FirstStepStartActivity_Brs_X01_NoInputExample_V1(
IClock clock,
IOrchestrationInstanceProgressRepository progressRepository)
: ProgressActivityBase(
clock,
progressRepository)
{
[Function(nameof(FirstStepStartActivity_Brs_X01_NoInputExample_V1))]
public async Task Run(
[ActivityTrigger] ActivityInput input)
{
var orchestrationInstance = await ProgressRepository
.GetAsync(input.OrchestrationInstanceId)
.ConfigureAwait(false);

var step = orchestrationInstance.Steps.Single(x => x.Sequence == Orchestration_Brs_X01_NoInputExample_V1.FirstStepSequence);
step.Lifecycle.TransitionToRunning(Clock);
await ProgressRepository.UnitOfWork.CommitAsync().ConfigureAwait(false);

await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}

public record ActivityInput(
OrchestrationInstanceId OrchestrationInstanceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Energinet.DataHub.ProcessManagement.Core.Application.Orchestration;
using Energinet.DataHub.ProcessManagement.Core.Domain.OrchestrationInstance;
using Microsoft.Azure.Functions.Worker;
using NodaTime;

namespace Energinet.DataHub.Example.Orchestrations.Processes.BRS_X01.NoInputExample.V1.Activities;

internal class FirstStepStopActivity_Brs_X01_NoInputExample_V1(
IClock clock,
IOrchestrationInstanceProgressRepository progressRepository)
: ProgressActivityBase(
clock,
progressRepository)
{
[Function(nameof(FirstStepStopActivity_Brs_X01_NoInputExample_V1))]
public async Task Run(
[ActivityTrigger] ActivityInput input)
{
var orchestrationInstance = await ProgressRepository
.GetAsync(input.OrchestrationInstanceId)
.ConfigureAwait(false);

var step = orchestrationInstance.Steps.Single(x => x.Sequence == Orchestration_Brs_X01_NoInputExample_V1.FirstStepSequence);
step.Lifecycle.TransitionToTerminated(Clock, OrchestrationStepTerminationStates.Succeeded);
await ProgressRepository.UnitOfWork.CommitAsync().ConfigureAwait(false);

await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}

public record ActivityInput(
OrchestrationInstanceId OrchestrationInstanceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 Energinet.DataHub.Example.Orchestrations.Processes.BRS_X01.NoInputExample.V1.Model;
using Energinet.DataHub.ProcessManagement.Core.Application.Orchestration;
using Energinet.DataHub.ProcessManagement.Core.Domain.OrchestrationInstance;
using Microsoft.Azure.Functions.Worker;
using NodaTime;

namespace Energinet.DataHub.Example.Orchestrations.Processes.BRS_X01.NoInputExample.V1.Activities;

internal class OrchestrationInitializeActivity_Brs_X01_NoInputExample_V1(
IClock clock,
IOrchestrationInstanceProgressRepository progressRepository)
: ProgressActivityBase(
clock,
progressRepository)
{
[Function(nameof(OrchestrationInitializeActivity_Brs_X01_NoInputExample_V1))]
public async Task<OrchestrationExecutionPlan> Run(
[ActivityTrigger] ActivityInput input)
{
var orchestrationInstance = await ProgressRepository
.GetAsync(input.OrchestrationInstanceId)
.ConfigureAwait(false);

orchestrationInstance.Lifecycle.TransitionToRunning(Clock);
await ProgressRepository.UnitOfWork.CommitAsync().ConfigureAwait(false);

// Orchestrations that have input have a custom start handler in which they can
// transition steps to skipped before starting the orchestration.
// Orchestrations without input doesn't have a custom start handler, so if they
// want to skip steps it must be done dynamically during the orchestration.
// Here we just use a random function to determine if skippable functions should be skipped or not.
var shouldSkip = Random.Shared.Next(0, 2) == 0;
var stepsSkippedBySequence = orchestrationInstance.Steps
.Where(step => step.Lifecycle.CanBeSkipped && shouldSkip)
.Select(step => step.Sequence)
.ToList();
return new OrchestrationExecutionPlan(stepsSkippedBySequence);
}

public record ActivityInput(
OrchestrationInstanceId OrchestrationInstanceId);
}
Loading
Loading