Skip to content
Open
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
41 changes: 25 additions & 16 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,47 @@
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.3.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/MongoDB.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration.Tests", "Migratio
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionFiles", ".SolutionFiles", "{A21A1FF3-25C1-4FE1-994E-E16CDF98BB99}"
ProjectSection(SolutionItems) = preProject
..\Directory.Packages.props = ..\Directory.Packages.props
TestProject.props = TestProject.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Context.AllowedTypes.Tests", "Context.AllowedTypes.Tests\Context.AllowedTypes.Tests.csproj", "{C59B2068-C3F4-4900-A1CB-7BFD63F94095}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Outbox", "Outbox\Outbox.csproj", "{DA61731E-A164-4EFC-B40C-CFADA88EDF20}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Outbox.Tests", "Outbox.Tests\Outbox.Tests.csproj", "{47201075-0A32-474D-ABD6-5C7D2DEB30EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +91,14 @@ Global
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Release|Any CPU.Build.0 = Release|Any CPU
{DA61731E-A164-4EFC-B40C-CFADA88EDF20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA61731E-A164-4EFC-B40C-CFADA88EDF20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA61731E-A164-4EFC-B40C-CFADA88EDF20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA61731E-A164-4EFC-B40C-CFADA88EDF20}.Release|Any CPU.Build.0 = Release|Any CPU
{47201075-0A32-474D-ABD6-5C7D2DEB30EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47201075-0A32-474D-ABD6-5C7D2DEB30EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47201075-0A32-474D-ABD6-5C7D2DEB30EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47201075-0A32-474D-ABD6-5C7D2DEB30EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
57 changes: 57 additions & 0 deletions src/Outbox.Tests/Core/ConsumerProxyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using MongoDB.Extensions.Outbox.Core;
using MongoDB.Extensions.Outbox.Core.Exceptions;
using Xunit;

namespace MongoDB.Extensions.Outbox.Tests.Core
{
public class ConsumerProxyTests
{
[Fact]
public async Task ConsumeAsync_WithValidMessageType_ShouldAcceptConsumption()
{
//Arrange
var message = new DummyMessage();
CancellationToken token = new CancellationTokenSource().Token;

var consumer = new Mock<IConsumer<DummyMessage>>(MockBehavior.Strict);
consumer
.Setup(c => c.ConsumeAsync(message, token))
.Returns(Task.CompletedTask);

var proxy = new ConsumerProxy<DummyMessage>(consumer.Object);

//Act
await proxy.ConsumeAsync(message, token);

//Assert
consumer.VerifyAll();
}

[Fact]
public async Task ConsumeAsync_WithUnsupportedMessageType_ShouldThrowException()
{
//Arrange
var message = new object();
CancellationToken token = new CancellationTokenSource().Token;

var consumer = new Mock<IConsumer<DummyMessage>>(MockBehavior.Strict);

var proxy = new ConsumerProxy<DummyMessage>(consumer.Object);

//Act
Func<Task> tryConsume = async () => await proxy.ConsumeAsync(message, token);

//Assert
await tryConsume.Should().ThrowAsync<UnsupportedMessageTypeException>();
}

public class DummyMessage
{
}
}
}
74 changes: 74 additions & 0 deletions src/Outbox.Tests/Core/ConsumptionFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Extensions.Outbox.Core;
using Xunit;

namespace MongoDB.Extensions.Outbox.Tests.Core
{
public class ConsumptionFactoryTests
{
[Fact]
public void Create_ForSupportedMessage_ShouldReturnConsumptionWithProxy()
{
//Arrange
var proxy = new DummyProxy();

var services = new ServiceCollection();
IServiceProvider provider = services.BuildServiceProvider();

Func<IServiceProvider, DummyProxy> proxyFact = (IServiceProvider p) => proxy;

var factory = new ConsumptionFactory(
provider, new Dictionary<Type, Func<IServiceProvider, IConsumerProxy>>
{
{ typeof(DummyMessage), proxyFact }
});

//Act
IConsumption job = factory.Create(new DummyMessage());

//Assert
job.Should().BeOfType<Consumption<DummyMessage>>()
.Subject.ConsumerProxy.Should().Be(proxy);
}

[Fact]
public void Create_ForUnsupportedMessage_ShouldThrow()
{
//Arrange
var services = new ServiceCollection();
IServiceProvider provider = services.BuildServiceProvider();

Func<IServiceProvider, DummyProxy> proxyFact = (IServiceProvider p)
=> new DummyProxy();

var factory = new ConsumptionFactory(
provider, new Dictionary<Type, Func<IServiceProvider, IConsumerProxy>>
{
{ typeof(DummyMessage), proxyFact }
});

//Act
Func<IConsumption> tryGetConsumption = () => factory.Create(new object());

//Assert
tryGetConsumption.Should().Throw<KeyNotFoundException>();
}

public class DummyMessage
{
}

public class DummyProxy : IConsumerProxy
{
public Task ConsumeAsync(object message, CancellationToken token)
{
throw new NotImplementedException();
}
}
}
}
36 changes: 36 additions & 0 deletions src/Outbox.Tests/Core/ConsumptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Threading;
using System.Threading.Tasks;
using Moq;
using MongoDB.Extensions.Outbox.Core;
using Xunit;

namespace MongoDB.Extensions.Outbox.Tests.Core
{
public class ConsumptionTests
{
[Fact]
public async Task ConsumeAsync_WithMessage_ShouldForwardToProxy()
{
//Arrange
var message = new DummyMessage();
CancellationToken token = new CancellationTokenSource().Token;

var proxy = new Mock<IConsumerProxy<DummyMessage>>(MockBehavior.Strict);
proxy
.Setup(c => c.ConsumeAsync(message, token))
.Returns(Task.CompletedTask);

var consumption = new Consumption<DummyMessage>(proxy.Object, message);

//Act
await consumption.ConsumeAsync(token);

//Assert
proxy.VerifyAll();
}

public class DummyMessage
{
}
}
}
33 changes: 33 additions & 0 deletions src/Outbox.Tests/Core/OutboxServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MongoDB.Extensions.Outbox.Core;
using Xunit;

namespace MongoDB.Extensions.Outbox.Tests.Core
{
public class OutboxServiceCollectionExtensionsTests
{
[Fact]
public void AddTransactionalOutbox_WithOptions_ShouldRegisterServiceAndHostedService()
{
//Arrange
var services = new ServiceCollection();

var options = new OutboxOptions();

//Act
services.AddTransactionalOutbox(options);

//Assert
services.Should().ContainSingle(s => s.ServiceType== typeof(OutboxOptions))
.Which.Lifetime.Should().Be(ServiceLifetime.Singleton);
services.Should().ContainSingle(s => s.ServiceType== typeof(IOutboxService))
.Which.ImplementationType.Should().Be(typeof(OutboxService));
services.Should().ContainSingle(s => s.ServiceType== typeof(IMessageProcessor))
.Which.ImplementationType.Should().Be(typeof(MessageProcessor));
services.Should().ContainSingle(s => s.ServiceType== typeof(IHostedService))
.Which.ImplementationType.Should().Be(typeof(MessagesDeliveryService));
}
}
}
41 changes: 41 additions & 0 deletions src/Outbox.Tests/Core/OutboxServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using MongoDB.Extensions.Outbox.Core;
using Xunit;

namespace MongoDB.Extensions.Outbox.Tests.Core
{
public class OutboxServiceTests
{
[Fact]
public async Task AddMessagesAsync_WithPayloads_ShouldAddMessagesToRepository()
{
//Arrange
CancellationToken token = new CancellationTokenSource().Token;
var payloads = new object[] { new object() };

var transactionSection = new Mock<ITransactionSession>(MockBehavior.Strict);

var repository = new Mock<IOutboxRepository>(MockBehavior.Strict);
repository
.Setup(r => r.AddMessagesAsync(
transactionSection.Object,
It.Is<IReadOnlyList<OutboxMessage>>(l => l.Count == payloads.Count()
&& l.All(i => payloads.Contains(i.Payload))),
token))
.Returns(Task.CompletedTask);

var service = new OutboxService(repository.Object);

//Act
await service.AddMessagesAsync(transactionSection.Object, payloads, token);

//Assert
repository.VerifyAll();
}
}
}
17 changes: 17 additions & 0 deletions src/Outbox.Tests/Outbox.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />

<PropertyGroup>
<AssemblyName>MongoDB.Extensions.Outbox.Tests</AssemblyName>
<RootNamespace>MongoDB.Extensions.Outbox.Tests</RootNamespace>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\Outbox\Outbox.csproj" />
</ItemGroup>

</Project>
Loading