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
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@

namespace TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers;

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using EventHandling;
using MessagingService.Client;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using SecurityService.Client;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EntityFramework;
using Shared.EventStore.Aggregate;
using Shared.General;
using Shared.Logger;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Testing;
using Xunit;

Expand All @@ -35,124 +37,99 @@

public class VoucherDomainEventHandlerTests
{
private Mock<Shared.EntityFramework.IDbContextFactory<EstateManagementContext>> GetMockDbContextFactory()
private readonly Mock<IAggregateService> AggregateService;
private readonly Mock<IDbContextResolver<EstateManagementContext>> DbContextFactory;

Check notice on line 41 in TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs#L41

Remove the field 'DbContextFactory' and declare it as a local variable in the relevant methods.
private readonly EstateManagementContext Context;
private readonly Mock<ISecurityServiceClient> SecurityServiceClient;
private readonly Mock<IMessagingServiceClient> MessagingServiceClient;

Check notice on line 44 in TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs#L44

Remove the field 'MessagingServiceClient' and declare it as a local variable in the relevant methods.
private readonly VoucherDomainEventHandler VoucherDomainEventHandler;

private EstateManagementContext GetContext(String databaseName)
{
return new Mock<Shared.EntityFramework.IDbContextFactory<EstateManagementContext>>();
EstateManagementContext context = null;

Check warning on line 49 in TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs#L49

Remove the unused local variable 'context'.
DbContextOptionsBuilder<EstateManagementContext> builder = new DbContextOptionsBuilder<EstateManagementContext>().UseInMemoryDatabase(databaseName).ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));
return new EstateManagementContext(builder.Options);
}

private async Task<EstateManagementContext> GetContext(String databaseName, TestDatabaseType databaseType = TestDatabaseType.InMemory)
{
EstateManagementContext context = null;
if (databaseType == TestDatabaseType.InMemory)
{
DbContextOptionsBuilder<EstateManagementContext> builder = new DbContextOptionsBuilder<EstateManagementContext>()
.UseInMemoryDatabase(databaseName)
.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));
context = new EstateManagementContext(builder.Options);
}
else
public VoucherDomainEventHandlerTests() {
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
ConfigurationReader.Initialise(configurationRoot);
Logger.Initialise(NullLogger.Instance);

SecurityServiceClient = new Mock<ISecurityServiceClient>();
MessagingServiceClient = new Mock<IMessagingServiceClient>();

DirectoryInfo path = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
MockFileSystem fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
throw new NotSupportedException($"Database type [{databaseType}] not supported");
}
{ $"{path}/VoucherMessages/VoucherEmail.html", new MockFileData("Transaction Number: [TransactionNumber]") },
{ $"{path}/VoucherMessages/VoucherSMS.txt", new MockFileData("Transaction Number: [TransactionNumber]") }
});

this.AggregateService = new Mock<IAggregateService>();
this.DbContextFactory = new Mock<IDbContextResolver<EstateManagementContext>>();
this.Context = this.GetContext(Guid.NewGuid().ToString("N"));
var services = new ServiceCollection();
services.AddTransient<EstateManagementContext>(_ => this.Context);
var serviceProvider = services.BuildServiceProvider();
var scope = serviceProvider.CreateScope();
this.DbContextFactory.Setup(d => d.Resolve(It.IsAny<String>(), It.IsAny<String>())).Returns(new ResolvedDbContext<EstateManagementContext>(scope));

VoucherDomainEventHandler = new VoucherDomainEventHandler(this.SecurityServiceClient.Object,
this.AggregateService.Object,
this.DbContextFactory.Object,
this.MessagingServiceClient.Object,
fileSystem);

return context;
}

[Fact]
public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithEmailAddress_IsHandled()
{
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
ConfigurationReader.Initialise(configurationRoot);
Logger.Initialise(NullLogger.Instance);
this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));

Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));

Mock<IAggregateService> aggregateService = new();
aggregateService.Setup(t => t.Get<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
this.AggregateService.Setup(t => t.Get<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientEmail()));

EstateManagementContext context = await this.GetContext(Guid.NewGuid().ToString("N"), TestDatabaseType.InMemory);
context.Transactions.Add(new Database.Entities.Transaction()
this.Context.Transactions.Add(new Database.Entities.Transaction()

Check notice on line 94 in TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs#L94

Remove these redundant parentheses.
{
TransactionId = TestData.TransactionId,
MerchantId = TestData.MerchantId,
ContractId = TestData.ContractId
});
context.Contracts.Add(new Contract
this.Context.Contracts.Add(new Contract
{
ContractId = TestData.ContractId,
EstateId = TestData.EstateId,
Description = TestData.OperatorIdentifier
});
await context.SaveChangesAsync(CancellationToken.None);

var dbContextFactory = this.GetMockDbContextFactory();
dbContextFactory.Setup(d => d.GetContext(It.IsAny<Guid>(),It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(context);

Mock<IMessagingServiceClient> messagingServiceClient = new Mock<IMessagingServiceClient>();

DirectoryInfo path = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
MockFileSystem fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ $"{path}/VoucherMessages/VoucherEmail.html", new MockFileData("Transaction Number: [TransactionNumber]") }
});

VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler(securityServiceClient.Object,
aggregateService.Object,
dbContextFactory.Object,
messagingServiceClient.Object,
fileSystem);
await this.Context.SaveChangesAsync(CancellationToken.None);

await voucherDomainEventHandler.Handle(TestData.VoucherIssuedEvent, CancellationToken.None);
await this.VoucherDomainEventHandler.Handle(TestData.VoucherIssuedEvent, CancellationToken.None);
}

[Fact]
public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithRecipientMobile_IsHandled()
{
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
ConfigurationReader.Initialise(configurationRoot);
Logger.Initialise(NullLogger.Instance);

Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));
this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));

Mock<IAggregateService> aggregateService = new ();
aggregateService.Setup(t => t.Get<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
this.AggregateService.Setup(t => t.Get<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile()));

EstateManagementContext context = await this.GetContext(Guid.NewGuid().ToString("N"), TestDatabaseType.InMemory);
context.Transactions.Add(new Database.Entities.Transaction()
this.Context.Transactions.Add(new Database.Entities.Transaction()
{
TransactionId = TestData.TransactionId,
MerchantId = TestData.MerchantId,
ContractId = TestData.ContractId
});
context.Contracts.Add(new Contract
this.Context.Contracts.Add(new Contract
{
ContractId = TestData.ContractId,
EstateId = TestData.EstateId,
Description = TestData.OperatorIdentifier
});
await context.SaveChangesAsync(CancellationToken.None);

Mock<Shared.EntityFramework.IDbContextFactory<EstateManagementContext>> dbContextFactory = this.GetMockDbContextFactory();
dbContextFactory.Setup(d => d.GetContext(It.IsAny<Guid>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(context);

Mock<IMessagingServiceClient> messagingServiceClient = new Mock<IMessagingServiceClient>();

DirectoryInfo path = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
MockFileSystem fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ $"{path}/VoucherMessages/VoucherSMS.txt", new MockFileData("Transaction Number: [TransactionNumber]") }
});

VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler(securityServiceClient.Object,
aggregateService.Object,
dbContextFactory.Object,
messagingServiceClient.Object,
fileSystem);
await this.Context.SaveChangesAsync(CancellationToken.None);

await voucherDomainEventHandler.Handle(TestData.VoucherIssuedEvent, CancellationToken.None);
await VoucherDomainEventHandler.Handle(TestData.VoucherIssuedEvent, CancellationToken.None);
}
}
Loading
Loading