diff --git a/.github/workflows/createrelease.yml b/.github/workflows/createrelease.yml index dda58fa..a66501a 100644 --- a/.github/workflows/createrelease.yml +++ b/.github/workflows/createrelease.yml @@ -30,12 +30,12 @@ jobs: - name: Build Code run: dotnet build MessagingService.sln --configuration Release - #- name: Run Unit Tests - # run: | - # echo "ASPNETCORE_ENVIRONMENT are > ${ASPNETCORE_ENVIRONMENT}" - # dotnet test "MessagingService.BusinessLogic.Tests\MessagingService.BusinessLogic.Tests.csproj" - # dotnet test "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj" - # dotnet test "MessagingService.Tests\MessagingService.Tests.csproj" + - name: Run Unit Tests + run: | + echo "ASPNETCORE_ENVIRONMENT are > ${ASPNETCORE_ENVIRONMENT}" + dotnet test "MessagingService.BusinessLogic.Tests\MessagingService.BusinessLogic.Tests.csproj" + dotnet test "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj" + dotnet test "MessagingService.Tests\MessagingService.Tests.csproj" #- name: Build Docker Image # run: docker build . --file MessagingService/Dockerfile --tag stuartferguson/messagingservice:latest --tag stuartferguson/messagingservice:${{ steps.get_version.outputs.VERSION }} diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 89de793..c79d84d 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -27,12 +27,12 @@ jobs: - name: Build Code run: dotnet build MessagingService.sln --configuration Release - #- name: Run Unit Tests - # run: | - # echo "ASPNETCORE_ENVIRONMENT are > ${ASPNETCORE_ENVIRONMENT}" - # dotnet test "MessagingService.BusinessLogic.Tests\MessagingService.BusinessLogic.Tests.csproj" - # dotnet test "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj" - # dotnet test "MessagingService.Tests\MessagingService.Tests.csproj" + - name: Run Unit Tests + run: | + echo "ASPNETCORE_ENVIRONMENT are > ${ASPNETCORE_ENVIRONMENT}" + dotnet test "MessagingService.BusinessLogic.Tests\MessagingService.BusinessLogic.Tests.csproj" + dotnet test "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj" + dotnet test "MessagingService.Tests\MessagingService.Tests.csproj" #- name: Build Docker Image # run: docker build . --file MessagingService/Dockerfile --tag messagingservice:latest diff --git a/MessagingService.BusinessLogic.Tests/MessagingService.BusinessLogic.Tests.csproj b/MessagingService.BusinessLogic.Tests/MessagingService.BusinessLogic.Tests.csproj new file mode 100644 index 0000000..bd6a069 --- /dev/null +++ b/MessagingService.BusinessLogic.Tests/MessagingService.BusinessLogic.Tests.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp3.1 + None + false + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/MessagingService.BusinessLogic.Tests/RequestHandlers/EmailRequestHandlerTests.cs b/MessagingService.BusinessLogic.Tests/RequestHandlers/EmailRequestHandlerTests.cs new file mode 100644 index 0000000..6594cc2 --- /dev/null +++ b/MessagingService.BusinessLogic.Tests/RequestHandlers/EmailRequestHandlerTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MessagingService.BusinessLogic.Tests.RequestHandlers +{ + using System.Threading; + using BusinessLogic.RequestHandlers; + using BusinessLogic.Requests; + using BusinessLogic.Services; + using Moq; + using Services; + using Shouldly; + using Testing; + using Xunit; + + public class EmailRequestHandlerTests + { + [Fact] + public void TransactionRequestHandler_ProcessLogonTransactionRequest_IsHandled() + { + Mock emailDomainService = new Mock(); + EmailRequestHandler handler = new EmailRequestHandler(emailDomainService.Object); + + SendEmailRequest command = TestData.SendEmailRequest; + + Should.NotThrow(async () => + { + await handler.Handle(command, CancellationToken.None); + }); + + } + } +} diff --git a/MessagingService.BusinessLogic.Tests/Requests/RequestTests.cs b/MessagingService.BusinessLogic.Tests/Requests/RequestTests.cs new file mode 100644 index 0000000..e64a6e0 --- /dev/null +++ b/MessagingService.BusinessLogic.Tests/Requests/RequestTests.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MessagingService.BusinessLogic.Tests.Requests +{ + using BusinessLogic.Requests; + using Shouldly; + using Testing; + using Xunit; + + public class RequestTests + { + [Fact] + public void SendEmailRequest_CanBeCreated_IsCreated() + { + SendEmailRequest request = SendEmailRequest.Create(TestData.ConnectionIdentifier, + TestData.MessageId, + TestData.FromAddress, + TestData.ToAddresses, + TestData.Subject, + TestData.Body, + TestData.IsHtmlTrue); + + request.ShouldNotBeNull(); + request.ConnectionIdentifier.ShouldBe(TestData.ConnectionIdentifier); + request.MessageId.ShouldBe(TestData.MessageId); + request.FromAddress.ShouldBe(TestData.FromAddress); + request.ToAddresses.ShouldBe(TestData.ToAddresses); + request.Subject.ShouldBe(TestData.Subject); + request.Body.ShouldBe(TestData.Body); + request.IsHtml.ShouldBe(TestData.IsHtmlTrue); + } + } +} diff --git a/MessagingService.BusinessLogic.Tests/Services/EmailDomainServiceTests.cs b/MessagingService.BusinessLogic.Tests/Services/EmailDomainServiceTests.cs new file mode 100644 index 0000000..7fdbadf --- /dev/null +++ b/MessagingService.BusinessLogic.Tests/Services/EmailDomainServiceTests.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MessagingService.BusinessLogic.Tests.Services +{ + using System.Threading; + using System.Threading.Tasks; + using BusinessLogic.Services; + using BusinessLogic.Services.EmailServices; + using EmailMessageAggregate; + using Moq; + using Shared.DomainDrivenDesign.EventStore; + using Shared.EventStore.EventStore; + using Testing; + using Xunit; + + public class EmailDomainServiceTests + { + [Fact] + public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIsProcessed() + { + Mock> aggregateRepository = new Mock>(); + aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.GetEmptyEmailAggregate()); + Mock aggregateRepositoryManager = new Mock(); + aggregateRepositoryManager.Setup(a => a.GetAggregateRepository(It.IsAny())).Returns(aggregateRepository.Object); + Mock emailServiceProxy = new Mock(); + emailServiceProxy + .Setup(e => e.SendEmail(It.IsAny(), + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())).ReturnsAsync(TestData.SuccessfulEmailServiceProxyResponse); + //IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build(); + //ConfigurationReader.Initialise(configurationRoot); + + //Logger.Initialise(NullLogger.Instance); + + EmailDomainService emailDomainService = + new EmailDomainService(aggregateRepositoryManager.Object, emailServiceProxy.Object); + + await emailDomainService.SendEmailMessage(TestData.ConnectionIdentifier, + TestData.MessageId, + TestData.FromAddress, + TestData.ToAddresses, + TestData.Subject, + TestData.Body, + TestData.IsHtmlTrue, + CancellationToken.None); + } + + } +} diff --git a/MessagingService.BusinessLogic/Services/EmailServices/EmailServiceProxyResponse.cs b/MessagingService.BusinessLogic/Services/EmailServices/EmailServiceProxyResponse.cs index 516d308..69240d2 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/EmailServiceProxyResponse.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/EmailServiceProxyResponse.cs @@ -1,11 +1,13 @@ namespace MessagingService.BusinessLogic.Services.EmailServices { using System; + using System.Diagnostics.CodeAnalysis; using System.Net; /// /// /// + [ExcludeFromCodeCoverage] public class EmailServiceProxyResponse { /// diff --git a/MessagingService.BusinessLogic/Services/EmailServices/IntegrationTest/IntegrationTestEmailServiceProxy.cs b/MessagingService.BusinessLogic/Services/EmailServices/IntegrationTest/IntegrationTestEmailServiceProxy.cs index 8036e5d..1bdce22 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/IntegrationTest/IntegrationTestEmailServiceProxy.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/IntegrationTest/IntegrationTestEmailServiceProxy.cs @@ -7,9 +7,11 @@ namespace MessagingService.Service.Services.Email.IntegrationTest { + using System.Diagnostics.CodeAnalysis; using BusinessLogic.Requests; using BusinessLogic.Services.EmailServices; + [ExcludeFromCodeCoverage] public class IntegrationTestEmailServiceProxy : IEmailServiceProxy { /// diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailRequest.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailRequest.cs index 4fbc2c5..19b7ceb 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailRequest.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailRequest.cs @@ -3,6 +3,9 @@ namespace MessagingService.Service.Services.Email.Smtp2Go { + using System.Diagnostics.CodeAnalysis; + + [ExcludeFromCodeCoverage] public class Smtp2GoSendEmailRequest { /// diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponse.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponse.cs index a7af089..e54fa5d 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponse.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponse.cs @@ -3,6 +3,9 @@ namespace MessagingService.Service.Services.Email.Smtp2Go { + using System.Diagnostics.CodeAnalysis; + + [ExcludeFromCodeCoverage] public class Smtp2GoSendEmailResponse { /// diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponseData.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponseData.cs index 988ef28..90b0c51 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponseData.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoSendEmailResponseData.cs @@ -3,6 +3,9 @@ namespace MessagingService.Service.Services.Email.Smtp2Go { + using System.Diagnostics.CodeAnalysis; + + [ExcludeFromCodeCoverage] public class Smtp2GoSendEmailResponseData { /// diff --git a/MessagingService.DataTransferObjects/SendEmailRequest.cs b/MessagingService.DataTransferObjects/SendEmailRequest.cs index cec5f51..2114e48 100644 --- a/MessagingService.DataTransferObjects/SendEmailRequest.cs +++ b/MessagingService.DataTransferObjects/SendEmailRequest.cs @@ -2,7 +2,9 @@ { using System; using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; + [ExcludeFromCodeCoverage] public class SendEmailRequest { #region Properties diff --git a/MessagingService.DataTransferObjects/SendEmailResponse.cs b/MessagingService.DataTransferObjects/SendEmailResponse.cs index 6815675..91808b4 100644 --- a/MessagingService.DataTransferObjects/SendEmailResponse.cs +++ b/MessagingService.DataTransferObjects/SendEmailResponse.cs @@ -1,10 +1,12 @@ namespace MessagingService.DataTransferObjects { using System; + using System.Diagnostics.CodeAnalysis; /// /// /// + [ExcludeFromCodeCoverage] public class SendEmailResponse { #region Properties diff --git a/MessagingService.EmailAggregate.Tests/MessagingService.EmailAggregate.Tests.csproj b/MessagingService.EmailAggregate.Tests/MessagingService.EmailAggregate.Tests.csproj new file mode 100644 index 0000000..7836b29 --- /dev/null +++ b/MessagingService.EmailAggregate.Tests/MessagingService.EmailAggregate.Tests.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/MessagingService.EmailAggregate.Tests/UnitTest1.cs b/MessagingService.EmailAggregate.Tests/UnitTest1.cs new file mode 100644 index 0000000..6f46b39 --- /dev/null +++ b/MessagingService.EmailAggregate.Tests/UnitTest1.cs @@ -0,0 +1,46 @@ +using System; +using Xunit; + +namespace MessagingService.EmailAggregate.Tests +{ + using EmailMessageAggregate; + using Shouldly; + using Testing; + + public class EmailAggregateTests + { + [Fact] + public void EmailAggregate_CanBeCreated_IsCreated() + { + EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId); + + emailAggregate.MessageId.ShouldBe(TestData.MessageId); + } + + [Fact] + public void EmailAggregate_SendRequestToProvider_RequestSent() + { + EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId); + + emailAggregate.SendRequestToProvider(TestData.FromAddress, TestData.ToAddresses, TestData.Subject, TestData.Body, TestData.IsHtmlTrue); + + emailAggregate.FromAddress.ShouldBe(TestData.FromAddress); + emailAggregate.Subject.ShouldBe(TestData.Subject); + emailAggregate.Body.ShouldBe(TestData.Body); + emailAggregate.IsHtml.ShouldBe(TestData.IsHtmlTrue); + // TODO: Get Recipients + } + + [Fact] + public void EmailAggregate_ReceiveResponseFromProvider_ResponseReceived() + { + EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId); + + emailAggregate.SendRequestToProvider(TestData.FromAddress, TestData.ToAddresses, TestData.Subject, TestData.Body, TestData.IsHtmlTrue); + emailAggregate.ReceiveResponseFromProvider(TestData.ProviderRequestReference, TestData.ProviderEmailReference); + + emailAggregate.ProviderRequestReference.ShouldBe(TestData.ProviderRequestReference); + emailAggregate.ProviderEmailReference.ShouldBe(TestData.ProviderEmailReference); + } + } +} diff --git a/MessagingService.EmailMessageAggregate/EmailAggregate.cs b/MessagingService.EmailMessageAggregate/EmailAggregate.cs index 0a77798..85e09d8 100644 --- a/MessagingService.EmailMessageAggregate/EmailAggregate.cs +++ b/MessagingService.EmailMessageAggregate/EmailAggregate.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using EmailMessage.DomainEvents; - using Microsoft.EntityFrameworkCore.Migrations.Operations; using Shared.DomainDrivenDesign.EventSourcing; using Shared.DomainDrivenDesign.EventStore; using Shared.General; @@ -15,6 +14,15 @@ /// public class EmailAggregate : Aggregate { + #region Fields + + /// + /// The recipients + /// + private readonly List Recipients; + + #endregion + #region Constructors /// @@ -35,36 +43,94 @@ private EmailAggregate(Guid aggregateId) Guard.ThrowIfInvalidGuid(aggregateId, "Aggregate Id cannot be an Empty Guid"); this.AggregateId = aggregateId; + this.MessageId = aggregateId; this.Recipients = new List(); } #endregion - #region Methods + #region Properties /// - /// Messages the send to recipient failure. + /// Gets the body. /// - public void MessageSendToRecipientFailure() - { - } + /// + /// The body. + /// + public String Body { get; private set; } + + /// + /// Gets from address. + /// + /// + /// From address. + /// + public String FromAddress { get; private set; } /// - /// Messages the send to recipient successful. + /// Gets a value indicating whether this instance is HTML. /// - public void MessageSendToRecipientSuccessful() + /// + /// true if this instance is HTML; otherwise, false. + /// + public Boolean IsHtml { get; private set; } + + /// + /// Gets the message identifier. + /// + /// + /// The message identifier. + /// + public Guid MessageId { get; } + + /// + /// Gets the provider email reference. + /// + /// + /// The provider email reference. + /// + public String ProviderEmailReference { get; private set; } + + /// + /// Gets the provider request reference. + /// + /// + /// The provider request reference. + /// + public String ProviderRequestReference { get; private set; } + + /// + /// Gets the subject. + /// + /// + /// The subject. + /// + public String Subject { get; private set; } + + #endregion + + #region Methods + + /// + /// Creates the specified aggregate identifier. + /// + /// The aggregate identifier. + /// + public static EmailAggregate Create(Guid aggregateId) { + return new EmailAggregate(aggregateId); } /// - /// Receives the response from provider. + /// Messages the send to recipient failure. /// /// The provider request reference. /// The provider email reference. public void ReceiveResponseFromProvider(String providerRequestReference, String providerEmailReference) { - ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = ResponseReceivedFromProviderEvent.Create(this.AggregateId, providerRequestReference, providerEmailReference); + ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = + ResponseReceivedFromProviderEvent.Create(this.AggregateId, providerRequestReference, providerEmailReference); this.ApplyAndPend(responseReceivedFromProviderEvent); } @@ -88,16 +154,6 @@ public void SendRequestToProvider(String fromAddress, this.ApplyAndPend(requestSentToProviderEvent); } - public String ProviderRequestReference { get; private set; } - public String ProviderEmailReference { get; private set; } - public String FromAddress { get; private set; } - public String Subject { get; private set; } - public String Body { get; private set; } - public Boolean IsHtml { get; private set; } - - private List Recipients; - - /// /// Gets the metadata. /// @@ -117,6 +173,10 @@ protected override void PlayEvent(DomainEvent domainEvent) this.PlayEvent((dynamic)domainEvent); } + /// + /// Plays the event. + /// + /// The domain event. private void PlayEvent(RequestSentToProviderEvent domainEvent) { this.Body = domainEvent.Body; @@ -132,6 +192,10 @@ private void PlayEvent(RequestSentToProviderEvent domainEvent) } } + /// + /// Plays the event. + /// + /// The domain event. private void PlayEvent(ResponseReceivedFromProviderEvent domainEvent) { this.ProviderEmailReference = domainEvent.ProviderEmailReference; @@ -141,18 +205,38 @@ private void PlayEvent(ResponseReceivedFromProviderEvent domainEvent) #endregion } + /// + /// + /// internal class MessageRecipient { + #region Constructors + + #endregion + + #region Properties + + /// + /// Converts to address. + /// + /// + /// To address. + /// internal String ToAddress { get; private set; } - internal MessageRecipient() - { - - } + #endregion + + #region Methods + /// + /// Creates the specified to address. + /// + /// To address. internal void Create(String toAddress) { this.ToAddress = toAddress; } + + #endregion } } \ No newline at end of file diff --git a/MessagingService.Testing/MessagingService.Testing.csproj b/MessagingService.Testing/MessagingService.Testing.csproj new file mode 100644 index 0000000..7e8fb56 --- /dev/null +++ b/MessagingService.Testing/MessagingService.Testing.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + None + + + + + + + diff --git a/MessagingService.Testing/TestData.cs b/MessagingService.Testing/TestData.cs new file mode 100644 index 0000000..c858155 --- /dev/null +++ b/MessagingService.Testing/TestData.cs @@ -0,0 +1,64 @@ +namespace MessagingService.Testing +{ + using MessagingService.BusinessLogic.Requests; + using System; + using System.Collections.Generic; + using System.Net; + using BusinessLogic.Services.EmailServices; + using EmailMessageAggregate; + using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; + + public class TestData + { + public static Guid ConnectionIdentifier = Guid.Parse("AF987E74-B01F-456D-8182-ECCB82CBB5B1"); + + public static Guid MessageId = Guid.Parse("DB41ED46-0F4A-4995-B497-E62D0223F66B"); + + public static String FromAddress = "testfromemail@email.com"; + + public static List ToAddresses = new List + { + "testtoemail1@email.com", + "testtoemail2@email.com" + }; + + public static String Subject = "Test Subject"; + + public static String Body = "Test Body"; + + public static Boolean IsHtmlTrue = true; + + public static String EmailIdentifier = "36B1021A-3668-4C47-9949-8C8BF4AA041D"; + + public static HttpStatusCode ApiStatusCodeSuccess = HttpStatusCode.OK; + + public static String RequestIdentifier = "E0FEFE04-178D-492B-B5D2-1C22189D88B3"; + + public static String ProviderRequestReference = "ProviderRequestReference"; + + public static String ProviderEmailReference = "ProviderEmailReference"; + + public static EmailServiceProxyResponse SuccessfulEmailServiceProxyResponse => + new EmailServiceProxyResponse + { + EmailIdentifier = TestData.EmailIdentifier, + ApiStatusCode = TestData.ApiStatusCodeSuccess, + RequestIdentifier = TestData.RequestIdentifier + }; + + public static SendEmailRequest SendEmailRequest => SendEmailRequest.Create(TestData.ConnectionIdentifier, + TestData.MessageId, + TestData.FromAddress, + TestData.ToAddresses, + TestData.Subject, + TestData.Body, + TestData.IsHtmlTrue); + + public static EmailAggregate GetEmptyEmailAggregate() + { + EmailAggregate emailAggregate = new EmailAggregate(); + + return emailAggregate; + } + } +} \ No newline at end of file diff --git a/MessagingService.Tests/General/BootstrapperTests.cs b/MessagingService.Tests/General/BootstrapperTests.cs new file mode 100644 index 0000000..56cfb30 --- /dev/null +++ b/MessagingService.Tests/General/BootstrapperTests.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MessagingService.Tests.General +{ + using System.Diagnostics; + using System.Linq; + using Microsoft.AspNetCore.Hosting; + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using Xunit; + + public class BootstrapperTests + { + #region Methods + + /// + /// Verifies the bootstrapper is valid. + /// + [Fact] + public void VerifyBootstrapperIsValid() + { + Mock hostingEnvironment = new Mock(); + hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); + hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); + hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); + + IServiceCollection services = new ServiceCollection(); + Startup s = new Startup(hostingEnvironment.Object); + Startup.Configuration = this.SetupMemoryConfiguration(); + + s.ConfigureServices(services); + + this.AddTestRegistrations(services, hostingEnvironment.Object); + + services.AssertConfigurationIsValid(); + } + + private IConfigurationRoot SetupMemoryConfiguration() + { + Dictionary configuration = new Dictionary(); + + IConfigurationBuilder builder = new ConfigurationBuilder(); + + configuration.Add("EventStoreSettings:ConnectionString", "ConnectTo=tcp://admin:changeit@127.0.0.1:1112;VerboseLogging=true;"); + configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection"); + configuration.Add("EventStoreSettings:HttpPort", "2113"); + configuration.Add("AppSettings:UseConnectionStringConfig", "false"); + configuration.Add("AppSettings:ClientId", "clientId"); + configuration.Add("AppSettings:ClientSecret", "clientSecret"); + configuration.Add("AppSettings:EstateManagementApi", "http://localhost"); + configuration.Add("AppSettings:SecurityService", "http://localhost"); + + builder.AddInMemoryCollection(configuration); + + return builder.Build(); + } + + /// + /// Adds the test registrations. + /// + /// The services. + /// The hosting environment. + private void AddTestRegistrations(IServiceCollection services, + IWebHostEnvironment hostingEnvironment) + { + services.AddLogging(); + DiagnosticListener diagnosticSource = new DiagnosticListener(hostingEnvironment.ApplicationName); + services.AddSingleton(diagnosticSource); + services.AddSingleton(diagnosticSource); + services.AddSingleton(hostingEnvironment); + } + + #endregion + } + + public static class ServiceCollectionExtensions + { + public static void AssertConfigurationIsValid(this IServiceCollection serviceCollection, + List typesToIgnore = null) + { + ServiceProvider buildServiceProvider = serviceCollection.BuildServiceProvider(); + + List list = serviceCollection.Where(x => x.ServiceType.Namespace != null && x.ServiceType.Namespace.Contains("Vme")).ToList(); + + if (typesToIgnore != null) + { + list.RemoveAll(listItem => typesToIgnore.Contains(listItem.ServiceType)); + } + + foreach (ServiceDescriptor serviceDescriptor in list) + { + Type type = serviceDescriptor.ServiceType; + + //This throws an Exception if the type cannot be instantiated. + buildServiceProvider.GetService(type); + } + } + } +} diff --git a/MessagingService.Tests/MessagingService.Tests.csproj b/MessagingService.Tests/MessagingService.Tests.csproj new file mode 100644 index 0000000..4aec007 --- /dev/null +++ b/MessagingService.Tests/MessagingService.Tests.csproj @@ -0,0 +1,28 @@ + + + + netcoreapp3.1 + None + false + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/MessagingService.sln b/MessagingService.sln index 036858a..95a8a12 100644 --- a/MessagingService.sln +++ b/MessagingService.sln @@ -13,9 +13,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.DataTransf EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.BusinessLogic", "MessagingService.BusinessLogic\MessagingService.BusinessLogic.csproj", "{A372C9E9-C412-4A36-961F-A358FF8F700E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.EmailMessageAggregate", "MessagingService.EmailMessageAggregate\MessagingService.EmailMessageAggregate.csproj", "{0BD162FA-64F1-44D5-9C06-749400F84FA5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.EmailMessageAggregate", "MessagingService.EmailMessageAggregate\MessagingService.EmailMessageAggregate.csproj", "{0BD162FA-64F1-44D5-9C06-749400F84FA5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.EmailMessage.DomainEvents", "MessagingService.EmailMessage.DomainEvents\MessagingService.EmailMessage.DomainEvents.csproj", "{689A531D-86EB-4656-81B3-4C6E569A7E4B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.EmailMessage.DomainEvents", "MessagingService.EmailMessage.DomainEvents\MessagingService.EmailMessage.DomainEvents.csproj", "{689A531D-86EB-4656-81B3-4C6E569A7E4B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.Tests", "MessagingService.Tests\MessagingService.Tests.csproj", "{1617DC31-CE88-49ED-A865-CE896E8D861D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.Testing", "MessagingService.Testing\MessagingService.Testing.csproj", "{5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.BusinessLogic.Tests", "MessagingService.BusinessLogic.Tests\MessagingService.BusinessLogic.Tests.csproj", "{17A755D6-96EE-46EB-8850-9641B8F1A5E1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.EmailAggregate.Tests", "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj", "{2FBED4B6-3096-4AD1-8436-247A59E0CDC2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,6 +51,22 @@ Global {689A531D-86EB-4656-81B3-4C6E569A7E4B}.Debug|Any CPU.Build.0 = Debug|Any CPU {689A531D-86EB-4656-81B3-4C6E569A7E4B}.Release|Any CPU.ActiveCfg = Release|Any CPU {689A531D-86EB-4656-81B3-4C6E569A7E4B}.Release|Any CPU.Build.0 = Release|Any CPU + {1617DC31-CE88-49ED-A865-CE896E8D861D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1617DC31-CE88-49ED-A865-CE896E8D861D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1617DC31-CE88-49ED-A865-CE896E8D861D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1617DC31-CE88-49ED-A865-CE896E8D861D}.Release|Any CPU.Build.0 = Release|Any CPU + {5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C}.Release|Any CPU.Build.0 = Release|Any CPU + {17A755D6-96EE-46EB-8850-9641B8F1A5E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {17A755D6-96EE-46EB-8850-9641B8F1A5E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {17A755D6-96EE-46EB-8850-9641B8F1A5E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {17A755D6-96EE-46EB-8850-9641B8F1A5E1}.Release|Any CPU.Build.0 = Release|Any CPU + {2FBED4B6-3096-4AD1-8436-247A59E0CDC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FBED4B6-3096-4AD1-8436-247A59E0CDC2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2FBED4B6-3096-4AD1-8436-247A59E0CDC2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2FBED4B6-3096-4AD1-8436-247A59E0CDC2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -53,6 +77,10 @@ Global {A372C9E9-C412-4A36-961F-A358FF8F700E} = {BF2482A1-13C0-4305-B732-AB62EBD9429B} {0BD162FA-64F1-44D5-9C06-749400F84FA5} = {BF2482A1-13C0-4305-B732-AB62EBD9429B} {689A531D-86EB-4656-81B3-4C6E569A7E4B} = {BF2482A1-13C0-4305-B732-AB62EBD9429B} + {1617DC31-CE88-49ED-A865-CE896E8D861D} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203} + {5CEAAA6F-2B2C-4C1F-B812-65E5ACC9002C} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203} + {17A755D6-96EE-46EB-8850-9641B8F1A5E1} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203} + {2FBED4B6-3096-4AD1-8436-247A59E0CDC2} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1929C0FE-8CEB-4D0E-BD22-9E5E16E2B49F} diff --git a/MessagingService/Startup.cs b/MessagingService/Startup.cs index 013af96..b2cbd66 100644 --- a/MessagingService/Startup.cs +++ b/MessagingService/Startup.cs @@ -12,6 +12,7 @@ namespace MessagingService { + using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net.Http; using System.Reflection; @@ -43,6 +44,7 @@ namespace MessagingService using Swashbuckle.AspNetCore.SwaggerGen; using ILogger = EventStore.ClientAPI.ILogger; + [ExcludeFromCodeCoverage] public class Startup { public Startup(IWebHostEnvironment webHostEnvironment)