Skip to content

Commit

Permalink
Resolve SonarCloud warnings created by PR zarusz#239 / Issue zarusz#238
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Pringle <richardpringle@gmail.com>
  • Loading branch information
EtherZa committed Apr 9, 2024
1 parent 30d9177 commit 411572e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static MessageBusBuilder AddHybridSerializer(this MessageBusBuilder mbb,
var builder = new HybridSerializerOptionsBuilder();
registration(builder);

foreach (var action in builder.ServiceRegistrations)
foreach (var action in builder.GetServiceRegistrations())
{
mbb.PostConfigurationActions.Add(action);
}
Expand All @@ -50,8 +50,9 @@ public static MessageBusBuilder AddHybridSerializer(this MessageBusBuilder mbb,
throw new NotSupportedException($"Registering instances of {nameof(IMessageSerializer)} outside of {nameof(AddHybridSerializer)} is not supported.");
}

var defaultMessageSerializer = builder.DefaultSerializer != null ? (IMessageSerializer)svp.GetRequiredService(builder.DefaultSerializer) : null;
var typeRegistrations = builder.TypeRegistrations.ToDictionary(x => (IMessageSerializer)svp.GetRequiredService(x.Key), x => x.Value);
var defaultMessageSerializerType = builder.GetDefaultSerializer();
var defaultMessageSerializer = defaultMessageSerializerType != null ? (IMessageSerializer)svp.GetRequiredService(builder.GetDefaultSerializer()) : null;
var typeRegistrations = builder.GetTypeRegistrations().ToDictionary(x => (IMessageSerializer)svp.GetRequiredService(x.Key), x => x.Value);
return new HybridMessageSerializer(svp.GetRequiredService<ILogger<HybridMessageSerializer>>(), typeRegistrations, defaultMessageSerializer);
});

Expand All @@ -64,51 +65,42 @@ public sealed class HybridSerializerOptionsBuilder
{
private readonly List<SerializerConfiguration> _configurations = [];

public Type DefaultSerializer
public ISerializationBuilder AsDefault()
{
get
{
return _configurations
.OfType<DefaultSerializerConfiguration>()
.LastOrDefault(x => x.IsValid)
.Type;
}
var configuration = new DefaultSerializerConfiguration();
this._configurations.Add(configuration);
return configuration;
}

public IReadOnlyList<Action<IServiceCollection>> ServiceRegistrations
public ISerializationBuilder For(params Type[] types)
{
get
{
return _configurations
.Where(x => x.IsValid)
.Select(x => x.Action)
.ToList();
}
var configuration = new ForSerializerConfiguration(types);
this._configurations.Add(configuration);
return configuration;
}

public IReadOnlyDictionary<Type, Type[]> TypeRegistrations
public Type GetDefaultSerializer()
{
get
{
return _configurations
.OfType<ForSerializerConfiguration>()
.Where(x => x.IsValid)
.ToDictionary(x => x.Type, x => x.Types);
}
return _configurations
.OfType<DefaultSerializerConfiguration>()
.LastOrDefault(x => x.IsValid)?
.Type;
}

public ISerializationBuilder AsDefault()
public IReadOnlyList<Action<IServiceCollection>> GetServiceRegistrations()
{
var configuration = new DefaultSerializerConfiguration();
this._configurations.Add(configuration);
return configuration;
return _configurations
.Where(x => x.IsValid)
.Select(x => x.Action)
.ToList();
}

public ISerializationBuilder For(params Type[] types)
public IReadOnlyDictionary<Type, Type[]> GetTypeRegistrations()
{
var configuration = new ForSerializerConfiguration(types);
this._configurations.Add(configuration);
return configuration;
return _configurations
.OfType<ForSerializerConfiguration>()
.Where(x => x.IsValid)
.ToDictionary(x => x.Type, x => x.Types);
}

public abstract class SerializerConfiguration : ISerializationBuilder
Expand All @@ -125,12 +117,12 @@ public void RegisterSerializer<TMessageSerializer>(Action<IServiceCollection> se
}
}

public class ForSerializerConfiguration(Type[] types) : SerializerConfiguration, ISerializationBuilder
public class ForSerializerConfiguration(Type[] types) : SerializerConfiguration
{
public Type[] Types { get; } = types;
}

public class DefaultSerializerConfiguration : SerializerConfiguration, ISerializationBuilder
public class DefaultSerializerConfiguration : SerializerConfiguration
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace SlimMessageBus.Host.Serialization.Hybrid.Test;

using System;

using Microsoft.Extensions.DependencyInjection;

public class SerializationBuilderExtensionsTests
Expand All @@ -8,13 +10,16 @@ public class SerializationBuilderExtensionsTests

public SerializationBuilderExtensionsTests()
{
// arrange
var mockLogger = new Mock<ILogger<HybridMessageSerializer>>();

_services = new ServiceCollection();
_services.AddSingleton(mockLogger.Object);
}

// act
[Fact]
public void When_HybridMessageSerializerIsAdded_Then_RegisterAsIMessageSerializer()
{
// arrange
_services.AddSlimMessageBus(cfg =>
{
cfg.AddHybridSerializer(builder =>
Expand All @@ -32,20 +37,10 @@ public SerializationBuilderExtensionsTests()
.RegisterSerializer<SerializerThree>(services => services.AddSingleton<SerializerThree>());
});
});
}

[Fact]
public void When_IMessageSerializerRegistrationsAlreadyExist_Then_RemovePreviousRegistrations()
{
// assert
_services.Count(x => x.ServiceType == typeof(IMessageSerializer)).Should().Be(1);
}
var serviceProvider = _services.BuildServiceProvider();

[Fact]
public void When_HybridMessageSerializerIsAdded_Then_RegisterAsIMessageSerializer()
{
// act
var serviceProvider = _services.BuildServiceProvider();
var target = serviceProvider.GetServices<IMessageSerializer>().ToList();

// assert
Expand All @@ -56,8 +51,28 @@ public void When_HybridMessageSerializerIsAdded_Then_RegisterAsIMessageSerialize
[Fact]
public void When_HybridMessageSerializerIsAdded_Then_SerializersAndTypesShouldConfigured()
{
// act
// arrange
_services.AddSlimMessageBus(cfg =>
{
cfg.AddHybridSerializer(builder =>
{
builder
.AsDefault()
.RegisterSerializer<SerializerOne>(services => services.AddSingleton<SerializerOne>());

builder
.For(typeof(SampleTwo))
.RegisterSerializer<SerializerTwo>(services => services.AddSingleton<SerializerTwo>());

builder
.For(typeof(SampleThree))
.RegisterSerializer<SerializerThree>(services => services.AddSingleton<SerializerThree>());
});
});

var serviceProvider = _services.BuildServiceProvider();

// act
var target = serviceProvider.GetService<HybridMessageSerializer>();

// assert
Expand All @@ -67,6 +82,32 @@ public void When_HybridMessageSerializerIsAdded_Then_SerializersAndTypesShouldCo
target.SerializerByType.Should().ContainKey(typeof(SampleThree)).WhoseValue.Should().BeOfType<SerializerThree>();
}

[Fact]
public void When_IMessageSerializerRegistrationsAlreadyExist_Then_ThrowException()
{
// arrange
_services.AddSlimMessageBus(cfg =>
{
cfg.RegisterSerializer<SerializerOne>(services => services.AddSingleton<SerializerOne>());

cfg.AddHybridSerializer(builder =>
{
builder
.AsDefault()
.RegisterSerializer<SerializerTwo>(services => services.AddSingleton<SerializerTwo>());
});
});

var serviceProvider = _services.BuildServiceProvider();

// act
var act = () => serviceProvider.GetService<HybridMessageSerializer>();


// arrange
act.Should().Throw<NotSupportedException>();
}

public abstract class AbstractSerializer : IMessageSerializer
{
public object Deserialize(Type t, byte[] payload)
Expand Down

0 comments on commit 411572e

Please sign in to comment.