-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1500fbb
commit 7326e86
Showing
21 changed files
with
231 additions
and
31 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
source/gpconnect-analytics.Functions.UnitTests/HttpClientExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Security.Authentication; | ||
using FluentAssertions; | ||
using gpconnect_analytics.Configuration.Infrastructure.HttpClient; | ||
|
||
namespace gpconnect_analytics.Functions.UnitTests; | ||
|
||
public class HttpClientExtensionsTests | ||
{ | ||
[Fact] | ||
public void ConfigureHttpClient_ShouldSetTimeoutAndAcceptHeader() | ||
{ | ||
// Arrange | ||
var options = new HttpClient(); | ||
|
||
// Act | ||
HttpClientExtensions.ConfigureHttpClient(options); | ||
|
||
// Assert | ||
options.Timeout.Should().Be(new TimeSpan(0, 0, 1, 0)); | ||
options.DefaultRequestHeaders.Accept.Should().ContainSingle(h => h.MediaType == "text/csv"); | ||
options.DefaultRequestHeaders.CacheControl?.NoCache.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void CreateHttpMessageHandler_ShouldReturnHandlerWithCorrectSslProtocols() | ||
{ | ||
// Act | ||
var handler = HttpClientExtensions.CreateHttpMessageHandler(); | ||
|
||
// Assert | ||
handler.Should().BeOfType<HttpClientHandler>(); // Verify type | ||
var httpClientHandler = (HttpClientHandler)handler; | ||
|
||
httpClientHandler.SslProtocols.Should().Be( | ||
SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, | ||
because: "the handler should support TLS 1.0, 1.1, 1.2, and 1.3" | ||
); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
source/gpconnect-analytics.Functions.UnitTests/Services/BatchServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Net; | ||
using Core.DTOs.Request; | ||
using Core.DTOs.Response.Configuration; | ||
using Core.DTOs.Response.Splunk; | ||
using Core.Helpers; | ||
using Core.Services.Interfaces; | ||
using Dapper; | ||
using FakeItEasy; | ||
using FluentAssertions; | ||
using function_app.Services; | ||
using function_app.Services.Interfaces; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace gpconnect_analytics.Functions.UnitTests.Services; | ||
|
||
public class BatchServiceTests | ||
{ | ||
[Fact] | ||
public async Task StartBatchDownloadForTodayAsync_ShouldProcessUrisAndReturnCount() | ||
{ | ||
// Arrange | ||
var mockConfigurationService = A.Fake<IConfigurationService>(); | ||
var mockImportService = A.Fake<IImportService>(); | ||
var mockSplunkService = A.Fake<ISplunkService>(); | ||
var mockLogger = A.Fake<ILogger<BatchService>>(); | ||
var mockDataService = A.Fake<IDataService>(); | ||
var mockExtractResponse = new ExtractResponse() | ||
{ | ||
ExtractResponseMessage = new HttpResponseMessage(HttpStatusCode.OK), | ||
ExtractResponseStream = Stream.Null, | ||
ExtractRequestDetails = new Extract(), | ||
FilePath = "test.csv", | ||
UriRequest = new UriRequest() | ||
}; | ||
|
||
A.CallTo(() => | ||
mockDataService.ExecuteStoredProcedure("Import.RemovePreviousDownload", A<DynamicParameters>.Ignored)) | ||
.Returns(Task.FromResult(1)); | ||
|
||
A.CallTo(() => mockSplunkService.DownloadCSVDateRangeAsync(A<FileType>._, A<UriRequest>._, true)) | ||
.Returns(mockExtractResponse); | ||
|
||
A.CallTo(() => mockImportService.AddObjectFileMessage(A<FileType>._, mockExtractResponse)) | ||
.Returns(Task.CompletedTask); | ||
|
||
var batchService = new BatchService( | ||
mockConfigurationService, | ||
mockImportService, | ||
mockSplunkService, | ||
mockLogger, | ||
mockDataService | ||
); | ||
|
||
var splunkQuery = "test query {latest}, {earliest}, {hour}"; | ||
|
||
var fileType = new FileType | ||
{ | ||
Enabled = true, | ||
FileTypeId = 1, | ||
FileTypeFilePrefix = "test", | ||
SplunkQuery = splunkQuery | ||
}; | ||
var uriList = new List<UriRequest> | ||
{ | ||
new() { Request = new Uri("https://example.com"), EarliestDate = DateTime.Now, LatestDate = DateTime.Now } | ||
}; | ||
|
||
A.CallTo(() => mockConfigurationService.GetFileType(A<FileTypes>._)) | ||
.Returns(Task.FromResult(fileType)); | ||
|
||
A.CallTo(() => mockConfigurationService.GetSplunkClientConfiguration()) | ||
.Returns(Task.FromResult(new SplunkClient | ||
{ HostName = "localhost", HostPort = 8080, BaseUrl = "api", QueryParameters = "test parameters {0}" })); | ||
|
||
// Act | ||
var result = await batchService.StartBatchDownloadForTodayAsync(FileTypes.asidlookup); | ||
|
||
// Assert | ||
const int hoursInDay = 24; | ||
result.Should().Be(hoursInDay); | ||
|
||
A.CallTo(() => mockConfigurationService.GetFileType(A<FileTypes>.That.Matches(x => x == FileTypes.asidlookup))) | ||
.MustHaveHappenedOnceExactly(); | ||
|
||
A.CallTo(() => | ||
mockDataService.ExecuteStoredProcedure("Import.RemovePreviousDownload", A<DynamicParameters>.Ignored)) | ||
.MustHaveHappenedOnceExactly(); | ||
|
||
A.CallTo(() => mockSplunkService.DownloadCSVDateRangeAsync(A<FileType>._, A<UriRequest>._, true)) | ||
.MustHaveHappened(24, Times.Exactly); // One for each hour | ||
|
||
A.CallTo(() => mockImportService.AddObjectFileMessage(A<FileType>._, A<ExtractResponse>._)) | ||
.MustHaveHappened(24, Times.Exactly); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ce/gpconnect-analytics.Functions.UnitTests/gpconnect-analytics.Functions.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>gpconnect_analytics.Functions.UnitTests</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageReference Include="FakeItEasy" Version="8.3.0" /> | ||
<PackageReference Include="FluentAssertions" Version="7.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/> | ||
<PackageReference Include="xunit" Version="2.5.3"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\gpconnect-analytics.Functions\gpconnect-analytics.Functions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
source/gpconnect-analytics.Functions/Configuration/EmailConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Data; | ||
using Core; | ||
using Core.DTOs.Response.Configuration; | ||
using Core.Helpers; | ||
using Dapper; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
public interface IEmailConfigurationProvider | ||
{ | ||
Email? GetEmailConfiguration(IConfiguration configuration); | ||
} | ||
|
||
public class EmailConfigurationProvider(IConnectionFactory connectionFactory) : IEmailConfigurationProvider | ||
{ | ||
public Email? GetEmailConfiguration(IConfiguration configuration) | ||
{ | ||
var connectionString = configuration.GetConnectionString(ConnectionStrings.GpConnectAnalytics) ?? | ||
throw new InvalidOperationException("connection string cannot be null at this point."); | ||
|
||
using var sqlConnection = connectionFactory.CreateConnection(connectionString); | ||
|
||
IEnumerable<Email?> result = sqlConnection.Query<Email>("[Configuration].[GetEmailConfiguration]", | ||
commandType: CommandType.StoredProcedure); | ||
|
||
return result.FirstOrDefault(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters