Add component tests

This commit is contained in:
Sipke Schoorstra 2020-11-13 20:47:08 +01:00
parent 0c5bdf27a7
commit c75196f696
19 changed files with 113 additions and 75 deletions

View file

@ -17,7 +17,7 @@ namespace Elsa.Client.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddElsaClient(this IServiceCollection services, Action<ElsaClientOptions>? configure = default)
public static IServiceCollection AddElsaClient(this IServiceCollection services, Action<ElsaClientOptions>? configure = default, Func<HttpClient>? httpClientFactory = default)
{
if (configure != null)
services.Configure(configure);
@ -33,31 +33,27 @@ namespace Elsa.Client.Extensions
jsonSerializerSettings.Converters.Add(new JsonStringEnumConverter());
jsonSerializerSettings.Converters.Add(new TypeConverter());
jsonSerializerSettings.Converters.Add(new VersionOptionsConverter());
var refitSettings = new RefitSettings
{
ContentSerializer = new SystemTextJsonContentSerializer(jsonSerializerSettings)
};
services.AddRefitClient<IWorkflowDefinitionsApi>(refitSettings).ConfigureHttpClient((sp, client) =>
if (httpClientFactory == null)
{
var serverUrl = sp.GetRequiredService<IOptions<ElsaClientOptions>>().Value.ServerUrl;
client.BaseAddress = serverUrl;
}).AddHttpMessageHandler<Spy>().AddHttpMessageHandler();
services.AddRefitClient<IWorkflowDefinitionsApi>(refitSettings).ConfigureHttpClient((sp, client) =>
{
var serverUrl = sp.GetRequiredService<IOptions<ElsaClientOptions>>().Value.ServerUrl;
client.BaseAddress = serverUrl;
});
}
else
{
services.AddScoped(_ => RestService.For<IWorkflowDefinitionsApi>(httpClientFactory(), refitSettings));
}
services.AddTransient<Spy>();
return services
.AddTransient<IElsaClient, ElsaClient>();
.AddTransient<IElsaClient, ElsaClient>();
}
}
}
public class Spy : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
return response;
}
}
}
}

View file

@ -7,7 +7,7 @@ namespace ElsaDashboard.Blazor.Shared.Rpc
[ProtoContract]
public interface IWorkflowDefinitionService
{
ValueTask<ICollection<Test>> GetAsync(string workflowDefinitionId, VersionOptions version);
//ValueTask<ICollection<Test>> GetAsync(string workflowDefinitionId, VersionOptions version);
}
public record Test(int a, bool b);

View file

@ -1,44 +1,24 @@
using System;
using System.Net;
using System.Threading.Tasks;
using AutoFixture;
using Elsa.Activities.Console;
using Elsa.Client;
using Elsa.Client.Extensions;
using Elsa.Client.Models;
using Elsa.ComponentTests.Helpers;
using Elsa.Testing.Shared.AutoFixture;
using Elsa.Testing.Shared.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions
{
[Collection(ComponentTestsCollection.Name)]
public class PostTests : IDisposable
public class PostTests : ElsaClientTestBase
{
private readonly IFixture _fixture;
private readonly TemporaryFolder _tempFolder;
private readonly ElsaHostApplicationFactory _hostApplicationFactory;
private readonly IElsaClient _elsaClient;
public PostTests(ElsaHostApplicationFactory hostApplicationFactory)
public PostTests(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory)
{
_fixture = new Fixture().Customize(new NodaTimeCustomization());
_tempFolder = new TemporaryFolder();
_hostApplicationFactory = hostApplicationFactory;
hostApplicationFactory.SetDbConnectionString($@"Data Source={_tempFolder.Folder}elsa.db;Cache=Shared");
var httpClient = hostApplicationFactory.CreateClient();
var services = new ServiceCollection().AddElsaClient(options => options.ServerUrl = httpClient.BaseAddress!).BuildServiceProvider();
_elsaClient = services.GetRequiredService<IElsaClient>();
}
[Fact(DisplayName = "Posting a new workflow definition returns HTTP 201.")]
public async Task Post01()
{
var request = CreateWorkflowDefinitionRequest();
var workflowDefinition = await _elsaClient.WorkflowDefinitions.PostAsync(request);
var workflowDefinition = await ElsaClient.WorkflowDefinitions.PostAsync(request);
Assert.Equal(request.Name, workflowDefinition.Name);
}
@ -46,7 +26,7 @@ namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions
{
var writeLine = new ActivityDefinition
{
ActivityId = _fixture.Create<string>(),
ActivityId = Fixture.Create<string>(),
Type = nameof(WriteLine),
Properties = new ActivityDefinitionProperties
{
@ -54,20 +34,14 @@ namespace Elsa.ComponentTests.Clients.RestClient.WorkflowDefinitions
}
};
var readLine = new ActivityDefinition { ActivityId = _fixture.Create<string>(), Type = nameof(ReadLine) };
var activities = new[] { writeLine, readLine };
var connections = new[] { new ConnectionDefinition(writeLine.ActivityId, readLine.ActivityId, OutcomeNames.Done) };
var readLine = new ActivityDefinition {ActivityId = Fixture.Create<string>(), Type = nameof(ReadLine)};
var activities = new[] {writeLine, readLine};
var connections = new[] {new ConnectionDefinition(writeLine.ActivityId, readLine.ActivityId, OutcomeNames.Done)};
return _fixture.Build<PostWorkflowDefinitionRequest>()
return Fixture.Build<PostWorkflowDefinitionRequest>()
.With(x => x.Activities, activities)
.With(x => x.Connections, connections)
.Create();
}
public void Dispose()
{
_hostApplicationFactory.Dispose();
_tempFolder.Dispose();
}
}
}

View file

@ -63,7 +63,7 @@ namespace Elsa.ComponentTests.Endpoints.WorkflowDefinitions
public void Dispose()
{
_hostApplicationFactory.Dispose();
_httpClient.Dispose();
_tempFolder.Dispose();
}
}

View file

@ -0,0 +1,18 @@
using Elsa.Client;
using Elsa.Client.Extensions;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.ComponentTests.Helpers
{
public abstract class ElsaClientTestBase : TestBase
{
protected ElsaClientTestBase(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory)
{
var services = new ServiceCollection().AddElsaClient(options => options.ServerUrl = HttpClient.BaseAddress!, () => HttpClient).BuildServiceProvider();
ElsaClient = services.GetRequiredService<IElsaClient>();
}
protected IElsaClient ElsaClient { get; }
}
}

View file

@ -0,0 +1,21 @@
using System.Net.Http;
namespace Elsa.ComponentTests.Helpers
{
public abstract class TestBase : WorkflowsComponentTestBase
{
protected TestBase(ElsaHostApplicationFactory hostApplicationFactory) : base(hostApplicationFactory)
{
HttpClient = hostApplicationFactory.CreateClient();
}
protected HttpClient HttpClient { get; }
public override void Dispose()
{
HttpClient.Dispose();
base.Dispose();
}
}
}

View file

@ -0,0 +1,29 @@
using System;
using AutoFixture;
using Elsa.Testing.Shared.AutoFixture;
using Elsa.Testing.Shared.Helpers;
namespace Elsa.ComponentTests.Helpers
{
public abstract class WorkflowsComponentTestBase : IDisposable
{
protected WorkflowsComponentTestBase(ElsaHostApplicationFactory hostApplicationFactory)
{
HostApplicationFactory = hostApplicationFactory;
Fixture = new Fixture().Customize(new NodaTimeCustomization());
TempFolder = new TemporaryFolder();
hostApplicationFactory.SetDbConnectionString($@"Data Source={TempFolder.Folder}elsa.db;Cache=Shared");
}
protected ElsaHostApplicationFactory HostApplicationFactory { get; }
protected IFixture Fixture { get; }
protected TemporaryFolder TempFolder { get; }
public virtual void Dispose()
{
TempFolder.Dispose();
}
}
}

View file

@ -10,7 +10,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.StorageProvider
{
public class StorageWorkflowProviderTests : WorkflowsTestBase
public class StorageWorkflowProviderTests : WorkflowsUnitTestBase
{
public StorageWorkflowProviderTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -6,7 +6,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class BasicWorkflowTests : WorkflowsTestBase
public class BasicWorkflowTests : WorkflowsUnitTestBase
{
public BasicWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -7,7 +7,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class FinishWorkflowTests : WorkflowsTestBase
public class FinishWorkflowTests : WorkflowsUnitTestBase
{
private readonly Fixture _fixture;

View file

@ -7,7 +7,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class ForEachWorkflowTests : WorkflowsTestBase
public class ForEachWorkflowTests : WorkflowsUnitTestBase
{
public ForEachWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -6,7 +6,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class ForLoopWorkflowTests : WorkflowsTestBase
public class ForLoopWorkflowTests : WorkflowsUnitTestBase
{
public ForLoopWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -11,7 +11,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class ForkJoinWorkflowTests : WorkflowsTestBase
public class ForkJoinWorkflowTests : WorkflowsUnitTestBase
{
public ForkJoinWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -6,7 +6,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class IfElseWorkflowTests : WorkflowsTestBase
public class IfElseWorkflowTests : WorkflowsUnitTestBase
{
public IfElseWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -7,7 +7,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class ParallelForEachWorkflowTests : WorkflowsTestBase
public class ParallelForEachWorkflowTests : WorkflowsUnitTestBase
{
public ParallelForEachWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -6,7 +6,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class SwitchWorkflowTests : WorkflowsTestBase
public class SwitchWorkflowTests : WorkflowsUnitTestBase
{
public SwitchWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -6,7 +6,7 @@ using Xunit.Abstractions;
namespace Elsa.Core.IntegrationTests.Workflows
{
public class WhileWorkflowTests : WorkflowsTestBase
public class WhileWorkflowTests : WorkflowsUnitTestBase
{
public WhileWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{

View file

@ -19,11 +19,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.14.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="NodaTime" Version="3.0.3" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
<PackageReference Include="AutoFixture" Version="4.14.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="NodaTime" Version="3.0.3" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
</ItemGroup>
<ItemGroup>
@ -34,7 +34,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\core\Elsa\Elsa.csproj" />
<ProjectReference Include="..\..\..\src\core\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -11,11 +11,11 @@ using YesSql.Provider.Sqlite;
namespace Elsa.Testing.Shared.Helpers
{
public abstract class WorkflowsTestBase : IAsyncLifetime, IDisposable
public abstract class WorkflowsUnitTestBase : IAsyncLifetime, IDisposable
{
private readonly TemporaryFolder _tempFolder;
protected WorkflowsTestBase(ITestOutputHelper testOutputHelper, Action<IServiceCollection>? configureServices = default)
protected WorkflowsUnitTestBase(ITestOutputHelper testOutputHelper, Action<IServiceCollection>? configureServices = default)
{
_tempFolder = new TemporaryFolder();
TestOutputHelper = testOutputHelper;