From ba559d0477a67f0751b8a2a08663dec359326c58 Mon Sep 17 00:00:00 2001 From: ambosch Date: Tue, 8 Oct 2024 13:55:42 +0200 Subject: [PATCH] Mock LiceCount weekly Post to Mattilsynet --- .gitignore | 2 ++ api/AquaApi.Tests/AquaApi.Tests.csproj | 27 +++++++++++++++ api/AquaApi.Tests/MattilsynetServiceTests.cs | 18 ++++++++++ api/AquaApi/Application/MattilsynetService.cs | 18 ++++++++++ api/AquaApi/AquaApi.csproj | 7 ---- api/AquaApi/Domain/LiceCount.cs | 10 ++++++ .../Infrastructure/IMattilsynetClient.cs | 8 +++++ .../Infrastructure/MockMattilsynetClient.cs | 13 +++++++ api/AquaApi/MattilsynetService.cs | 34 ------------------- api/AquaApi/tests/MattilsynetServiceTests.cs | 30 ---------------- dataplatform-aquaculture-template.sln | 7 ++++ 11 files changed, 103 insertions(+), 71 deletions(-) create mode 100644 api/AquaApi.Tests/AquaApi.Tests.csproj create mode 100644 api/AquaApi.Tests/MattilsynetServiceTests.cs create mode 100644 api/AquaApi/Application/MattilsynetService.cs create mode 100644 api/AquaApi/Domain/LiceCount.cs create mode 100644 api/AquaApi/Infrastructure/IMattilsynetClient.cs create mode 100644 api/AquaApi/Infrastructure/MockMattilsynetClient.cs delete mode 100644 api/AquaApi/MattilsynetService.cs delete mode 100644 api/AquaApi/tests/MattilsynetServiceTests.cs diff --git a/.gitignore b/.gitignore index 2d195c0..8c3d2f1 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,8 @@ override.tf.json *_override.tf *_override.tf.json +**.DotSettings.user + # Ignore transient lock info files created by terraform apply .terraform.tfstate.lock.info diff --git a/api/AquaApi.Tests/AquaApi.Tests.csproj b/api/AquaApi.Tests/AquaApi.Tests.csproj new file mode 100644 index 0000000..da49919 --- /dev/null +++ b/api/AquaApi.Tests/AquaApi.Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/api/AquaApi.Tests/MattilsynetServiceTests.cs b/api/AquaApi.Tests/MattilsynetServiceTests.cs new file mode 100644 index 0000000..95150ef --- /dev/null +++ b/api/AquaApi.Tests/MattilsynetServiceTests.cs @@ -0,0 +1,18 @@ +using AquaApi.Application; + +namespace AquaApi.Tests; + +public class MattilsynetServiceTests +{ + [Fact] + public async Task PostLiceCount_ShouldReturnSuccess() + { + // Act + var client = new MockMattilsynetClient(); + var service = new MattilsynetService(client); + var result = await service.PostLiceCount(); + + // Assert + Assert.True(result); + } +} \ No newline at end of file diff --git a/api/AquaApi/Application/MattilsynetService.cs b/api/AquaApi/Application/MattilsynetService.cs new file mode 100644 index 0000000..090573e --- /dev/null +++ b/api/AquaApi/Application/MattilsynetService.cs @@ -0,0 +1,18 @@ +using AquaApi.Domain; + +namespace AquaApi.Application; + +public class MattilsynetService(IMattilsynetClient client) +{ + public async Task PostLiceCount() + { + var liceCount = new LiceCount + { + VoksneHunnlus = 11, + BevegeligeLus = 4, + FastsittendeLus = 1, + }; + var response = await client.PostLiceCount(liceCount); + return response.IsSuccessStatusCode; + } +} \ No newline at end of file diff --git a/api/AquaApi/AquaApi.csproj b/api/AquaApi/AquaApi.csproj index 076f097..a5d7b8c 100644 --- a/api/AquaApi/AquaApi.csproj +++ b/api/AquaApi/AquaApi.csproj @@ -11,14 +11,7 @@ - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/api/AquaApi/Domain/LiceCount.cs b/api/AquaApi/Domain/LiceCount.cs new file mode 100644 index 0000000..adfe603 --- /dev/null +++ b/api/AquaApi/Domain/LiceCount.cs @@ -0,0 +1,10 @@ +namespace AquaApi.Domain; + +public class LiceCount +{ + public double VoksneHunnlus { get; set; } + public double BevegeligeLus { get; set; } + public double FastsittendeLus { get; set; } + + +} \ No newline at end of file diff --git a/api/AquaApi/Infrastructure/IMattilsynetClient.cs b/api/AquaApi/Infrastructure/IMattilsynetClient.cs new file mode 100644 index 0000000..9c7daab --- /dev/null +++ b/api/AquaApi/Infrastructure/IMattilsynetClient.cs @@ -0,0 +1,8 @@ +using AquaApi.Domain; + +namespace AquaApi; + +public interface IMattilsynetClient +{ + Task PostLiceCount(LiceCount liceCount); +} \ No newline at end of file diff --git a/api/AquaApi/Infrastructure/MockMattilsynetClient.cs b/api/AquaApi/Infrastructure/MockMattilsynetClient.cs new file mode 100644 index 0000000..f5e4bb0 --- /dev/null +++ b/api/AquaApi/Infrastructure/MockMattilsynetClient.cs @@ -0,0 +1,13 @@ +using System.Net; +using AquaApi.Domain; + +namespace AquaApi; + +public class MockMattilsynetClient : IMattilsynetClient +{ + public async Task PostLiceCount(LiceCount liceCount) + { + await Task.Delay(200); + return new HttpResponseMessage(HttpStatusCode.Created); + } +} \ No newline at end of file diff --git a/api/AquaApi/MattilsynetService.cs b/api/AquaApi/MattilsynetService.cs deleted file mode 100644 index 686a6e3..0000000 --- a/api/AquaApi/MattilsynetService.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Net.Http; -using System.Text; -using System.Text.Json; -class MattilsynetService -{ - public static async Task PostLiceCount() - { - using var httpClient = new HttpClient(); - - string api_url = "https://example.com/";//"https://lakselusrapportering.fisk-dev.mattilsynet.io/api/v1_2/lakselusrapportering"; - - var jsonPayload = new - { - lusetelling = new - { - voksneHunnlus = 11, - bevegeligeLus = 4, - fastsittendeLus = 1, - } - }; - - string jsonString = JsonSerializer.Serialize(jsonPayload); - - HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json"); - - - HttpResponseMessage response = await httpClient.PostAsync(api_url, content); - - return response; - - - } -} \ No newline at end of file diff --git a/api/AquaApi/tests/MattilsynetServiceTests.cs b/api/AquaApi/tests/MattilsynetServiceTests.cs deleted file mode 100644 index cee723c..0000000 --- a/api/AquaApi/tests/MattilsynetServiceTests.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Xunit; -using Moq; -using System.Net; -using System.Threading.Tasks; - - -public class MattilsynetServiceTests -{ - [Fact] - public async Task PostLiceCount_ShouldReturnSuccess() - { - // // Arrange - // var mockHttpClient = new Mock(); - // mockHttpClient.Setup(m => m.GetAsync(It.IsAny())).ReturnsAsync(new HttpResponseMessage - // { - // StatusCode = HttpStatusCode.OK, - // Content = new StringContent("{\"status\": \"success\"}") - // }); - - // var service = new MattilsynetService(); - - // Act - var result = await MattilsynetService.PostLiceCount(); - - // Assert - Assert.True(result.IsSuccessStatusCode); - Assert.Contains("POST request successful!", await result.Content.ReadAsStringAsync()); - } - -} \ No newline at end of file diff --git a/dataplatform-aquaculture-template.sln b/dataplatform-aquaculture-template.sln index 5bf1763..02c1a37 100644 --- a/dataplatform-aquaculture-template.sln +++ b/dataplatform-aquaculture-template.sln @@ -7,6 +7,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{A6ED9EDF-D59 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AquaApi", "api\AquaApi\AquaApi.csproj", "{35950192-252F-44A8-B710-63E470A67118}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AquaApi.Tests", "api\AquaApi.Tests\AquaApi.Tests.csproj", "{A0796727-D372-4CE1-B11C-3C864835EE4B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,12 +19,17 @@ Global {35950192-252F-44A8-B710-63E470A67118}.Debug|Any CPU.Build.0 = Debug|Any CPU {35950192-252F-44A8-B710-63E470A67118}.Release|Any CPU.ActiveCfg = Release|Any CPU {35950192-252F-44A8-B710-63E470A67118}.Release|Any CPU.Build.0 = Release|Any CPU + {A0796727-D372-4CE1-B11C-3C864835EE4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0796727-D372-4CE1-B11C-3C864835EE4B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0796727-D372-4CE1-B11C-3C864835EE4B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0796727-D372-4CE1-B11C-3C864835EE4B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {35950192-252F-44A8-B710-63E470A67118} = {A6ED9EDF-D599-47B8-A6B2-2844E3FEBF9C} + {A0796727-D372-4CE1-B11C-3C864835EE4B} = {A6ED9EDF-D599-47B8-A6B2-2844E3FEBF9C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {00A64A12-39CC-40EA-996D-79EA6539A35B}