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
28 changes: 28 additions & 0 deletions MessagingService.Client/IMessagingServiceClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace MessagingService.Client
{
using System;
using System.Threading;
using System.Threading.Tasks;
using DataTransferObjects;

/// <summary>
///
/// </summary>
public interface IMessagingServiceClient
{
#region Methods

/// <summary>
/// Sends the email.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<SendEmailResponse> SendEmail(String accessToken,
SendEmailRequest request,
CancellationToken cancellationToken);

#endregion
}
}
20 changes: 20 additions & 0 deletions MessagingService.Client/MessagingService.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClientProxyBase" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MessagingService.DataTransferObjects\MessagingService.DataTransferObjects.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MessagingService.DataTransferObjects\MessagingService.DataTransferObjects.csproj" PrivateAssets="All" />
</ItemGroup>

</Project>
114 changes: 114 additions & 0 deletions MessagingService.Client/MessagingServiceClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
namespace MessagingService.Client
{
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ClientProxyBase;
using DataTransferObjects;
using Newtonsoft.Json;

/// <summary>
///
/// </summary>
/// <seealso cref="ClientProxyBase.ClientProxyBase" />
/// <seealso cref="MessagingService.Client.IMessagingServiceClient" />
public class MessagingServiceClient : ClientProxyBase, IMessagingServiceClient
{
#region Fields

/// <summary>
/// The base address
/// </summary>
private readonly String BaseAddress;

/// <summary>
/// The base address resolver
/// </summary>
private readonly Func<String, String> BaseAddressResolver;

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="MessagingServiceClient"/> class.
/// </summary>
/// <param name="baseAddressResolver">The base address resolver.</param>
/// <param name="httpClient">The HTTP client.</param>
public MessagingServiceClient(Func<String, String> baseAddressResolver,
HttpClient httpClient) : base(httpClient)
{
this.BaseAddressResolver = baseAddressResolver;

// Add the API version header
this.HttpClient.DefaultRequestHeaders.Add("api-version", "1.0");
}

#endregion

#region Methods

/// <summary>
/// Sends the email.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="sendEmailRequest">The send email request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<SendEmailResponse> SendEmail(String accessToken,
SendEmailRequest sendEmailRequest,
CancellationToken cancellationToken)
{
SendEmailResponse response = null;

String requestUri = this.BuildRequestUrl("/api/email/");

try
{
String requestSerialised = JsonConvert.SerializeObject(sendEmailRequest);

StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json");

// Add the access token to the client headers
this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.PostAsync(requestUri, httpContent, cancellationToken);

// Process the response
String content = await this.HandleResponse(httpResponse, cancellationToken);

// call was successful so now deserialise the body to the response object
response = JsonConvert.DeserializeObject<SendEmailResponse>(content);
}
catch(Exception ex)
{
// An exception has occurred, add some additional information to the message
Exception exception = new Exception("Error sending email message.", ex);

throw exception;
}

return response;
}

/// <summary>
/// Builds the request URL.
/// </summary>
/// <param name="route">The route.</param>
/// <returns></returns>
private String BuildRequestUrl(String route)
{
String baseAddress = this.BaseAddressResolver("MessagingServiceApi");

String requestUri = $"{baseAddress}{route}";

return requestUri;
}

#endregion
}
}
11 changes: 7 additions & 4 deletions MessagingService.IntegrationTests/Common/DockerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Client;
using Ductus.FluentDocker.Builders;
using Ductus.FluentDocker.Common;
using Ductus.FluentDocker.Model.Builders;
Expand All @@ -26,7 +27,10 @@ public class DockerHelper : global::Shared.IntegrationTesting.DockerHelper
/// </summary>
public ISecurityServiceClient SecurityServiceClient;

public HttpClient MessagingServiceClient;
/// <summary>
/// The messaging service client
/// </summary>
public IMessagingServiceClient MessagingServiceClient;

/// <summary>
/// The test identifier
Expand Down Expand Up @@ -166,12 +170,11 @@ public override async Task StartContainersForScenarioRun(String scenarioName)

// Setup the base address resolvers
String SecurityServiceBaseAddressResolver(String api) => $"http://127.0.0.1:{this.SecurityServicePort}";
String MessagingServiceBaseAddressResolver(String api) => $"http://127.0.0.1:{this.MessagingServicePort}";

HttpClient httpClient = new HttpClient();
this.SecurityServiceClient = new SecurityServiceClient(SecurityServiceBaseAddressResolver, httpClient);

this.MessagingServiceClient = new HttpClient();
this.MessagingServiceClient.BaseAddress = new Uri($"http://127.0.0.1:{this.MessagingServicePort}");
this.MessagingServiceClient = new MessagingServiceClient(MessagingServiceBaseAddressResolver, httpClient);
}

/// <summary>
Expand Down
12 changes: 2 additions & 10 deletions MessagingService.IntegrationTests/Email/SendEmailSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,10 @@ private async Task SendEmail(TableRow tableRow)
Subject = subject,
ToAddresses = toAddresses.Split(",").ToList()
};

StringContent requestContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
this.TestingContext.DockerHelper.MessagingServiceClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.TestingContext.AccessToken);

HttpResponseMessage httpResponse = await this.TestingContext.DockerHelper.MessagingServiceClient.PostAsync("api/Email", requestContent, CancellationToken.None).ConfigureAwait(false);

httpResponse.StatusCode.ShouldBe(HttpStatusCode.Created);
String responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
SendEmailResponse sendEmailResponse = await this.TestingContext.DockerHelper.MessagingServiceClient.SendEmail(this.TestingContext.AccessToken, request, CancellationToken.None).ConfigureAwait(false);

responseContent.ShouldNotBeNullOrEmpty();
SendEmailResponse response = JsonConvert.DeserializeObject<SendEmailResponse>(responseContent);
response.MessageId.ShouldNotBe(Guid.Empty);
sendEmailResponse.MessageId.ShouldNotBe(Guid.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClientProxyBase" Version="0.0.15.3" />
<PackageReference Include="ClientProxyBase" Version="1.0.1" />
<PackageReference Include="Ductus.FluentDocker" Version="2.7.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="SecurityService.Client" Version="1.0.0" />
Expand All @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MessagingService.Client\MessagingService.Client.csproj" />
<ProjectReference Include="..\MessagingService.DataTransferObjects\MessagingService.DataTransferObjects.csproj" />
</ItemGroup>

Expand Down
9 changes: 8 additions & 1 deletion MessagingService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.BusinessLo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.EmailAggregate.Tests", "MessagingService.EmailAggregate.Tests\MessagingService.EmailAggregate.Tests.csproj", "{2FBED4B6-3096-4AD1-8436-247A59E0CDC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.IntegrationTests", "MessagingService.IntegrationTests\MessagingService.IntegrationTests.csproj", "{FEBD44D2-5B93-40D8-B59C-E5221570055D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagingService.IntegrationTests", "MessagingService.IntegrationTests\MessagingService.IntegrationTests.csproj", "{FEBD44D2-5B93-40D8-B59C-E5221570055D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagingService.Client", "MessagingService.Client\MessagingService.Client.csproj", "{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{FEBD44D2-5B93-40D8-B59C-E5221570055D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEBD44D2-5B93-40D8-B59C-E5221570055D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEBD44D2-5B93-40D8-B59C-E5221570055D}.Release|Any CPU.Build.0 = Release|Any CPU
{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -88,6 +94,7 @@ Global
{17A755D6-96EE-46EB-8850-9641B8F1A5E1} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203}
{2FBED4B6-3096-4AD1-8436-247A59E0CDC2} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203}
{FEBD44D2-5B93-40D8-B59C-E5221570055D} = {9AEE6ADE-DD45-4605-A933-E06CF0BA4203}
{E1F3FEA4-3358-429F-B3CF-FD7E15EDB993} = {BF2482A1-13C0-4305-B732-AB62EBD9429B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1929C0FE-8CEB-4D0E-BD22-9E5E16E2B49F}
Expand Down