Skip to content

Commit

Permalink
MessageDescriptor can be created from types with no namespace (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kralizek committed Jan 22, 2019
1 parent c1e1c06 commit d300e95
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Nybus.Abstractions/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public TEvent Event
[AttributeUsage(AttributeTargets.Class)]
public class MessageAttribute : Attribute
{
public MessageAttribute(string name, string @namespace)
public MessageAttribute(string name, string @namespace = null)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Namespace = @namespace ?? throw new ArgumentNullException(nameof(@namespace));
Namespace = @namespace ?? MessageDescriptor.NamespaceFallback;
}

public string Name { get; }
Expand Down
4 changes: 3 additions & 1 deletion src/Nybus.Abstractions/Utils/IMessageDescriptorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ bool RegisterEventType<TEvent>()

public class MessageDescriptor
{
public const string NamespaceFallback = "$";

public static MessageDescriptor CreateFromType(Type type)
{
if (type == null)
Expand Down Expand Up @@ -73,7 +75,7 @@ public static bool TryParse(string descriptorName, out MessageDescriptor descrip
public MessageDescriptor(string name, string @namespace)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Namespace = @namespace ?? throw new ArgumentNullException(nameof(@namespace));
Namespace = @namespace ?? NamespaceFallback;
}

public string Name { get; }
Expand Down
5 changes: 5 additions & 0 deletions tests/TestUtils/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ public class AttributeTestCommand : ICommand
public string Message { get; set; }
}
}

public class NoNamespaceCommand : ICommand
{
public string Message { get; set; }
}
5 changes: 5 additions & 0 deletions tests/TestUtils/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public class AttributeTestEvent : IEvent
{
public string Message { get; set; }
}
}

public class NoNamespaceEvent : IEvent
{
public string Message { get; set; }
}
23 changes: 23 additions & 0 deletions tests/Tests.Nybus.Abstractions/Utils/MessageDescriptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ public void MessageDescriptor_can_be_created_from_type(Type type)
Assert.That(messageDescriptor.Namespace, Is.EqualTo(type.Namespace));
}

[Test]
[InlineAutoMoqData(typeof(NoNamespaceCommand))]
[InlineAutoMoqData(typeof(NoNamespaceEvent))]
public void MessageDescriptor_can_be_created_from_type_with_no_namespace(Type type)
{
Assume.That(type.Namespace, Is.Null);

var messageDescriptor = new MessageDescriptor(type);

Assert.That(messageDescriptor.Name, Is.EqualTo(type.Name));
Assert.That(messageDescriptor.Namespace, Is.EqualTo(MessageDescriptor.NamespaceFallback));
}

[Test, AutoMoqData]
public void MessageDescriptor_can_be_created_from_MessageAttribute(MessageAttribute attribute)
{
Expand All @@ -47,6 +60,16 @@ public void MessageDescriptor_can_be_created_from_MessageAttribute(MessageAttrib
Assert.That(messageDescriptor.Namespace, Is.EqualTo(attribute.Namespace));
}

[Test, AutoMoqData]
public void MessageDescriptor_can_be_created_from_MessageAttribute_with_no_namespace(string name)
{
var attribute = new MessageAttribute(name);
var messageDescriptor = new MessageDescriptor(attribute);

Assert.That(messageDescriptor.Name, Is.EqualTo(attribute.Name));
Assert.That(messageDescriptor.Namespace, Is.EqualTo(MessageDescriptor.NamespaceFallback));
}

[Test, CustomAutoMoqData]
public void TryParse_return_false_if_null()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Nybus;

namespace Tests
{
[TestFixture]
public class NoNamespaceMessageTests
{
[Test, AutoMoqData]
public async Task Host_can_loopback_commands(ServiceCollection services, NoNamespaceCommand testCommand)
{
var commandReceived = Mock.Of<CommandReceivedAsync<NoNamespaceCommand>>();

services.AddLogging(l => l.AddDebug());

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.SubscribeToCommand(commandReceived);
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.InvokeCommandAsync(testCommand);

await host.StopAsync();

Mock.Get(commandReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<ICommandContext<NoNamespaceCommand>>()), Times.Once);
}

[Test, AutoMoqData]
public async Task Host_can_loopback_events(ServiceCollection services, NoNamespaceEvent testEvent)
{
var eventReceived = Mock.Of<EventReceivedAsync<NoNamespaceEvent>>();

services.AddLogging(l => l.AddDebug());

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.SubscribeToEvent(eventReceived);
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.RaiseEventAsync(testEvent);

await host.StopAsync();

Mock.Get(eventReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<IEventContext<NoNamespaceEvent>>()), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System;
using System.Threading.Tasks;
using FakeRabbitMQ;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Nybus;
using Nybus.Configuration;
using static Tests.TestUtils;

namespace Tests
{
[TestFixture]
public class NoNamespaceMessageTests
{
[Test, AutoMoqData]
public async Task Host_can_loopback_commands(FakeServer server, NoNamespaceCommand testCommand)
{
var commandReceived = Mock.Of<CommandReceivedAsync<NoNamespaceCommand>>();

var host = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
nybus.SubscribeToCommand(commandReceived);
});

await host.StartAsync();

await host.Bus.InvokeCommandAsync(testCommand);

await host.StopAsync();

Mock.Get(commandReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<ICommandContext<NoNamespaceCommand>>()));
}

[Test, AutoMoqData]
public async Task Host_can_loopback_events(FakeServer server, NoNamespaceEvent testEvent)
{
var eventReceived = Mock.Of<EventReceivedAsync<NoNamespaceEvent>>();

var host = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
nybus.SubscribeToEvent(eventReceived);
});

await host.StartAsync();

await host.Bus.RaiseEventAsync(testEvent);

await host.StopAsync();

Mock.Get(eventReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<IEventContext<NoNamespaceEvent>>()));
}

[Test, AutoMoqData]
public async Task Hosts_can_exchange_commands(FakeServer server, NoNamespaceCommand testCommand)
{
var sender = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
});

var commandReceived = Mock.Of<CommandReceivedAsync<NoNamespaceCommand>>();

var receiver = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
nybus.SubscribeToCommand(commandReceived);
});

await sender.StartAsync();

await receiver.StartAsync();

await sender.Bus.InvokeCommandAsync(testCommand);

await receiver.StopAsync();

await sender.StopAsync();

Mock.Get(commandReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<ICommandContext<NoNamespaceCommand>>()));
}

[Test, AutoMoqData]
public async Task Hosts_can_exchange_events(FakeServer server, NoNamespaceEvent testEvent)
{
var sender = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
});

var eventReceived = Mock.Of<EventReceivedAsync<NoNamespaceEvent>>();

var receiver = CreateNybusHost(nybus =>
{
nybus.UseRabbitMqBusEngine(rabbitMq =>
{
rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
});
nybus.SubscribeToEvent(eventReceived);
});

await sender.StartAsync();

await receiver.StartAsync();

await sender.Bus.RaiseEventAsync(testEvent);

await receiver.StopAsync();

await sender.StopAsync();

Mock.Get(eventReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<IEventContext<NoNamespaceEvent>>()));
}
}
}

0 comments on commit d300e95

Please sign in to comment.