@@ -1,6 +1,8 @@ | |||||
using System; | using System; | ||||
using System.Data; | using System.Data; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using DotNetCore.CAP.Abstractions; | |||||
using DotNetCore.CAP.Models; | |||||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||
using Xunit; | using Xunit; | ||||
@@ -49,6 +51,30 @@ namespace DotNetCore.CAP.Test | |||||
Assert.NotNull(thingy); | Assert.NotNull(thingy); | ||||
} | } | ||||
[Fact] | |||||
public void CanOverrideContentSerialize() | |||||
{ | |||||
var services = new ServiceCollection(); | |||||
services.AddCap(x => { }).AddContentSerializer<MyContentSerializer>(); | |||||
var thingy = services.BuildServiceProvider() | |||||
.GetRequiredService<IContentSerializer>() as MyContentSerializer; | |||||
Assert.NotNull(thingy); | |||||
} | |||||
[Fact] | |||||
public void CanOverrideMessagePack() | |||||
{ | |||||
var services = new ServiceCollection(); | |||||
services.AddCap(x => { }).AddMessagePacker<MyMessagePacker>(); | |||||
var thingy = services.BuildServiceProvider() | |||||
.GetRequiredService<IMessagePacker>() as MyMessagePacker; | |||||
Assert.NotNull(thingy); | |||||
} | |||||
[Fact] | [Fact] | ||||
public void CanResolveCapOptions() | public void CanResolveCapOptions() | ||||
{ | { | ||||
@@ -59,6 +85,33 @@ namespace DotNetCore.CAP.Test | |||||
Assert.NotNull(capOptions); | Assert.NotNull(capOptions); | ||||
} | } | ||||
private class MyMessagePacker : IMessagePacker | |||||
{ | |||||
public string Pack(CapMessage obj) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public CapMessage UnPack(string packingMessage) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
} | |||||
private class MyContentSerializer : IContentSerializer | |||||
{ | |||||
public T DeSerialize<T>(string content) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public string Serialize<T>(T obj) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
} | |||||
private class MyProducerService : ICapPublisher | private class MyProducerService : ICapPublisher | ||||
{ | { | ||||
public void Publish<T>(string name, T contentObj, string callbackName = null) | public void Publish<T>(string name, T contentObj, string callbackName = null) | ||||
@@ -18,6 +18,7 @@ namespace DotNetCore.CAP.Test | |||||
var services = new ServiceCollection(); | var services = new ServiceCollection(); | ||||
services.AddLogging(); | services.AddLogging(); | ||||
services.AddSingleton<IContentSerializer, JsonContentSerializer>(); | services.AddSingleton<IContentSerializer, JsonContentSerializer>(); | ||||
services.AddSingleton<IMessagePacker, DefaultMessagePacker>(); | |||||
var provider = services.BuildServiceProvider(); | var provider = services.BuildServiceProvider(); | ||||
var logFactory = provider.GetRequiredService<ILoggerFactory>(); | var logFactory = provider.GetRequiredService<ILoggerFactory>(); | ||||
var mesagePacker = provider.GetRequiredService<IMessagePacker>(); | var mesagePacker = provider.GetRequiredService<IMessagePacker>(); | ||||
@@ -39,9 +40,7 @@ namespace DotNetCore.CAP.Test | |||||
}; | }; | ||||
var messageContext = new MessageContext(); | var messageContext = new MessageContext(); | ||||
var context = new ConsumerContext(description, messageContext); | |||||
var invoker = consumerInvokerFactory.CreateInvoker(context); | |||||
var invoker = consumerInvokerFactory.CreateInvoker(); | |||||
Assert.NotNull(invoker); | Assert.NotNull(invoker); | ||||
} | } | ||||
@@ -63,11 +62,11 @@ namespace DotNetCore.CAP.Test | |||||
var context = new ConsumerContext(description, messageContext); | var context = new ConsumerContext(description, messageContext); | ||||
var invoker = consumerInvokerFactory.CreateInvoker(context); | |||||
var invoker = consumerInvokerFactory.CreateInvoker(); | |||||
Assert.Throws<Exception>(() => | Assert.Throws<Exception>(() => | ||||
{ | { | ||||
invoker.InvokeAsync().GetAwaiter().GetResult(); | |||||
invoker.InvokeAsync(context).GetAwaiter().GetResult(); | |||||
}); | }); | ||||
} | } | ||||
} | } |
@@ -1,6 +1,7 @@ | |||||
using System; | using System; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using DotNetCore.CAP.Abstractions; | using DotNetCore.CAP.Abstractions; | ||||
using DotNetCore.CAP.Internal; | |||||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||
using Xunit; | using Xunit; | ||||
@@ -0,0 +1,62 @@ | |||||
using System; | |||||
using DotNetCore.CAP.Internal; | |||||
using DotNetCore.CAP.Models; | |||||
using Newtonsoft.Json; | |||||
using Xunit; | |||||
namespace DotNetCore.CAP.Test | |||||
{ | |||||
public class JsonContentSerializerTest | |||||
{ | |||||
[Fact] | |||||
public void CanSerialize() | |||||
{ | |||||
//Arrange | |||||
var fixtrue = Create(); | |||||
var message = new CapMessageDto | |||||
{ | |||||
Id = "1", | |||||
Content = "Content", | |||||
CallbackName = "Callback", | |||||
Timestamp = DateTime.Now | |||||
}; | |||||
//Act | |||||
var ret = fixtrue.Serialize(message); | |||||
//Assert | |||||
var expected = JsonConvert.SerializeObject(message); | |||||
Assert.NotNull(ret); | |||||
Assert.Equal(expected, ret); | |||||
} | |||||
[Fact] | |||||
public void CanDeSerialize() | |||||
{ | |||||
//Arrange | |||||
var fixtrue = Create(); | |||||
var message = new CapMessageDto | |||||
{ | |||||
Id = "1", | |||||
Content = "Content", | |||||
CallbackName = "Callback", | |||||
Timestamp = DateTime.Now | |||||
}; | |||||
var messageJson = JsonConvert.SerializeObject(message); | |||||
//Act | |||||
var ret = fixtrue.DeSerialize<CapMessageDto>(messageJson); | |||||
//Assert | |||||
Assert.NotNull(ret); | |||||
Assert.Equal(message.Id, ret.Id); | |||||
Assert.Equal(message.Content, ret.Content); | |||||
Assert.Equal(message.CallbackName, ret.CallbackName); | |||||
Assert.Equal(message.Timestamp, ret.Timestamp); | |||||
} | |||||
private JsonContentSerializer Create() => new JsonContentSerializer(); | |||||
} | |||||
} |
@@ -1,6 +1,8 @@ | |||||
using System; | using System; | ||||
using DotNetCore.CAP.Internal; | |||||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||
using Xunit; | using Xunit; | ||||
using Moq; | |||||
namespace DotNetCore.CAP.Test | namespace DotNetCore.CAP.Test | ||||
{ | { | ||||
@@ -13,6 +15,10 @@ namespace DotNetCore.CAP.Test | |||||
var services = new ServiceCollection(); | var services = new ServiceCollection(); | ||||
services.AddLogging(); | services.AddLogging(); | ||||
services.AddOptions(); | services.AddOptions(); | ||||
var callbackSender = Mock.Of<ICallbackMessageSender>(); | |||||
services.AddSingleton(callbackSender); | |||||
services.AddCap(x => { }); | services.AddCap(x => { }); | ||||
_provider = services.BuildServiceProvider(); | _provider = services.BuildServiceProvider(); | ||||
} | } | ||||