Skip to content
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="CloudNative.CloudEvents.Protobuf" Version="2.8.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="Grpc.Tools" Version="2.76.0" />
<PackageVersion Include="IdGen.DependencyInjection" Version="3.0.7" />
<PackageVersion Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.2.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="10.0.102" />
Expand Down
9 changes: 8 additions & 1 deletion Source/Messaging/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Fossa.Messaging;

using CloudNative.CloudEvents;
using IdGen;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -14,15 +15,21 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="services">Input Service Collection.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="applicationName">The application name.</param>
/// <param name="componentNames">The component names.</param>
/// <returns>Populated Service Collection.</returns>
public static IServiceCollection AddMessaging(
this IServiceCollection services,
IConfiguration configuration)
IConfiguration configuration,
string applicationName,
Seq<string> componentNames)
{
_ = services.AddSingleton<MessageMap>();
_ = services.AddSingleton<IMessagePublisher, MessagePublisher>();
_ = services.AddSingleton<IProducerProvider, ProducerProvider>();
_ = services.AddSingleton<CloudEventFormatter, CustomProtobufEventFormatter>();
_ = services.AddSingleton<IServiceIdentityProvider>(sp =>
new ServiceIdentityProvider(sp.GetRequiredService<IdGenerator>(), applicationName, componentNames));

_ = services.Configure<MessagingOptions>(configuration.GetRequiredSection("Messaging"));

Expand Down
29 changes: 29 additions & 0 deletions Source/Messaging/ServiceIdentityProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Fossa.Messaging;

using IdGen;
using TIKSN.Identity;

/// <summary>
/// Service Identity Provider.
/// </summary>
public class ServiceIdentityProvider : IServiceIdentityProvider
{
private readonly ServiceIdentity serviceIdentity;

/// <summary>
/// Initializes a new instance of the <see cref="ServiceIdentityProvider"/> class.
/// </summary>
/// <param name="idGenerator">The id generator.</param>
/// <param name="applicationName">The application name.</param>
/// <param name="componentNames">The component names.</param>
public ServiceIdentityProvider(IdGenerator idGenerator, string applicationName, Seq<string> componentNames)
{
ArgumentNullException.ThrowIfNull(idGenerator);
ArgumentNullException.ThrowIfNull(applicationName);

this.serviceIdentity = new ServiceIdentity(applicationName, componentNames, ServiceInstanceId.Create(idGenerator.Id));
}

/// <inheritdoc/>
public ServiceIdentity GetIdentity() => this.serviceIdentity;
}
1 change: 1 addition & 0 deletions Tests/Messaging.Test/Messaging.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="IdGen.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
</ItemGroup>

Expand Down
18 changes: 4 additions & 14 deletions Tests/Messaging.Test/ServiceIdentityProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ namespace Fossa.Messaging.Test;

using Autofac;
using Autofac.Extensions.DependencyInjection;
using IdGen.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Time.Testing;
using NSubstitute;
using TIKSN.DependencyInjection;
using TIKSN.Identity;
using Xunit;

public class ServiceIdentityProviderTests
Expand Down Expand Up @@ -50,23 +49,14 @@ public ServiceIdentityProviderTests()
})
.Build();
var services = new ServiceCollection();
_ = services.AddMessaging(configuration);
_ = services.AddFrameworkCore();

_ = services.AddMessaging(configuration, "Fossa", Seq("Messaging", "Test"));
_ = services.AddFrameworkCore();
_ = services.AddIdGen(9);
var fakeTimeProvider = new FakeTimeProvider(
new DateTimeOffset(2022, 9, 24, 0, 0, 0, TimeSpan.Zero));
_ = services.AddSingleton<TimeProvider>(fakeTimeProvider);

var serviceIdentityProvider = Substitute.For<IServiceIdentityProvider>();
_ = serviceIdentityProvider
.GetIdentity().Returns(
new ServiceIdentity(
applicationName: "Fossa",
componentNames: Seq("Messaging", "Test"),
instanceId: ServiceInstanceId.Create(Ulid.NewUlid())));

_ = services.AddSingleton(serviceIdentityProvider);

ContainerBuilder containerBuilder = new();
_ = containerBuilder.RegisterModule<CoreModule>();
containerBuilder.Populate(services);
Expand Down
Loading