Skip to content

Commit

Permalink
Api test
Browse files Browse the repository at this point in the history
  • Loading branch information
dstenroejl committed Dec 18, 2024
1 parent 36e9a41 commit 5f5bd5a
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<Compile Include="..\ProcessManager.Core.Tests\Fixtures\ProcessManagerDatabaseManager.cs" Link="Fixtures\ProcessManagerDatabaseManager.cs" />
<Compile Include="..\ProcessManager.Tests\Fixtures\ProcessManagerAppManager.cs" Link="Fixtures\ProcessManagerAppManager.cs" />
</ItemGroup>

<ItemGroup>
Expand All @@ -27,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\ProcessManager.DatabaseMigration\ProcessManager.DatabaseMigration.csproj" />
<ProjectReference Include="..\Example.Orchestrations\Example.Orchestrations.csproj" />
<ProjectReference Include="..\ProcessManager\ProcessManager.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,83 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.Core.FunctionApp.TestCommon.Azurite;
using Energinet.DataHub.Core.FunctionApp.TestCommon.Configuration;
using Energinet.DataHub.ProcessManager.Core.Tests.Fixtures;
using Energinet.DataHub.ProcessManager.Tests.Fixtures;
using Xunit.Abstractions;

namespace Energinet.DataHub.Example.Orchestrations.Tests.Fixtures;

/// <summary>
/// Support testing Process Manager Orchestrations app using default fixture configuration.
/// Support testing the interactions between Example.Orchestrations and
/// Process Manager Api, by coordinating the startup of the dependent applications
/// Example.Orchestrations and ProcessManager (Api).
/// </summary>
public class ExampleOrchestrationsAppFixture
: IAsyncLifetime
public class ExampleOrchestrationsAppFixture : IAsyncLifetime
{
private const string TaskHubName = "ExampleOrchestrationsAppTest01";

public ExampleOrchestrationsAppFixture()
{
ExampleOrchestrationsAppManager = new ExampleOrchestrationsAppManager();
DatabaseManager = new ProcessManagerDatabaseManager("ExampleOrchestrationsAppTests");
AzuriteManager = new AzuriteManager(useOAuth: true);

IntegrationTestConfiguration = new IntegrationTestConfiguration();

ExampleOrchestrationsAppManager = new ExampleOrchestrationsAppManager(
DatabaseManager,
IntegrationTestConfiguration,
AzuriteManager,
taskHubName: TaskHubName,
appPort: 8113,
manageDatabase: false,
manageAzurite: false);

ProcessManagerAppManager = new ProcessManagerAppManager(
DatabaseManager,
IntegrationTestConfiguration,
AzuriteManager,
taskHubName: TaskHubName,
appPort: 8114,
manageDatabase: false,
manageAzurite: false);
}

public IntegrationTestConfiguration IntegrationTestConfiguration { get; }

public ExampleOrchestrationsAppManager ExampleOrchestrationsAppManager { get; }

public ProcessManagerAppManager ProcessManagerAppManager { get; }

private ProcessManagerDatabaseManager DatabaseManager { get; }

private AzuriteManager AzuriteManager { get; }

public async Task InitializeAsync()
{
AzuriteManager.CleanupAzuriteStorage();
AzuriteManager.StartAzurite();

await DatabaseManager.CreateDatabaseAsync();

await ExampleOrchestrationsAppManager.StartAsync();
await ProcessManagerAppManager.StartAsync();
}

public async Task DisposeAsync()
{
await ExampleOrchestrationsAppManager.DisposeAsync();
await ProcessManagerAppManager.DisposeAsync();

await DatabaseManager.DeleteDatabaseAsync();

AzuriteManager.Dispose();
}

public void SetTestOutputHelper(ITestOutputHelper? testOutputHelper)
{
ExampleOrchestrationsAppManager.SetTestOutputHelper(testOutputHelper);
ProcessManagerAppManager.SetTestOutputHelper(testOutputHelper);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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.Example.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.ProcessManager.Tests.Integration.Processes;

/// <summary>
/// Test case where we verify the Process Manager clients can be used to start a
/// calculation orchestration (with input parameter) and monitor its status during its lifetime.
/// </summary>
[Collection(nameof(ExampleOrchestrationsAppCollection))]
public class MonitorExampleUsingApiScenario : IAsyncLifetime
{
public MonitorExampleUsingApiScenario(
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 Example_WhenStarted_CanMonitorLifecycle()
{
var orchestration = new Brs_X01_Example_V1();
var input = new InputV1(false);

var command = new StartExampleCommandV1(
operatingIdentity: new UserIdentityDto(
Guid.NewGuid(),
Guid.NewGuid()),
input);

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

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

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

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

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 @@ -80,7 +80,9 @@ public async Task DisposeAsync()
{
await ExampleOrchestrationsAppManager.DisposeAsync();
await ProcessManagerAppManager.DisposeAsync();

await DatabaseManager.DeleteDatabaseAsync();

AzuriteManager.Dispose();
}

Expand Down

0 comments on commit 5f5bd5a

Please sign in to comment.