-
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.
* locator rewrite and tests * add worker tests * gateway tests * Add tools for coverage * updated empty options * settings * add test tasks
- Loading branch information
Showing
45 changed files
with
1,208 additions
and
699 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -489,4 +489,5 @@ appsettings.local.json | |
appsettings.docker.json | ||
compose.override.yaml | ||
.mono | ||
.env | ||
.env | ||
.coverage |
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 |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
"appsettings", | ||
"Arien", | ||
"Enphase", | ||
"Serilog" | ||
"Serilog", | ||
"reportgenerator", | ||
"globaltool" | ||
] | ||
} |
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,48 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "dotnet", | ||
"task": "build", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": false | ||
}, | ||
"problemMatcher": ["$msCompile"], | ||
"label": "Build" | ||
}, | ||
{ | ||
"label": "Run Tests with Coverage", | ||
"type": "shell", | ||
"command": "dotnet", | ||
"args": [ | ||
"test", | ||
"/p:CollectCoverage=true", | ||
"/p:CoverletOutputFormat=opencover", | ||
"/p:CoverletOutput=../.coverage/" | ||
], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": false | ||
}, | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"label": "Generate Coverage Report", | ||
"type": "shell", | ||
"command": "dotnet", | ||
"args": [ | ||
"tool", | ||
"run", | ||
"reportgenerator", | ||
"-reports:.coverage/coverage.opencover.xml", | ||
"-targetdir:.coverage/report" | ||
], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": false | ||
}, | ||
"problemMatcher": [] | ||
} | ||
] | ||
} |
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
198 changes: 96 additions & 102 deletions
198
...ts/Services/Gateway/GatewayClientTests.cs → Anar.Tests/Services/Gateway/GatewayTests.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 |
---|---|---|
@@ -1,103 +1,97 @@ | ||
using Anar.Services; | ||
|
||
using Microsoft.Extensions.Logging.Testing; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Moq; | ||
using Moq.Protected; | ||
|
||
using System.Net; | ||
|
||
namespace Anar.Tests.Services; | ||
|
||
public class GatewayClientTests | ||
{ | ||
private readonly FakeLogger<GatewayClient> _fakeLogger; | ||
private readonly HttpClient _httpClient; | ||
private readonly Mock<IOptions<GatewayOptions>> _mockOptions; | ||
private readonly Mock<HttpMessageHandler> _mockHandler; | ||
private readonly GatewayOptions _options; | ||
|
||
public GatewayClientTests() | ||
{ | ||
_options = new GatewayOptions | ||
{ | ||
Uri = new Uri("http://localhost/"), | ||
Token = "token", | ||
Interval = TimeSpan.FromSeconds(10), | ||
Layout = [ | ||
new Location { SerialNumber = "12345", Azimuth = 0, ArrayName = "Array 1" }, | ||
] | ||
}; | ||
|
||
_fakeLogger = new FakeLogger<GatewayClient>(); | ||
_mockOptions = new Mock<IOptions<GatewayOptions>>(); | ||
_mockHandler = new Mock<HttpMessageHandler>(); | ||
_httpClient = new HttpClient(_mockHandler.Object) { | ||
BaseAddress = _options.Uri, | ||
}; | ||
|
||
_mockOptions.Setup(o => o.Value).Returns(_options); | ||
} | ||
|
||
private GatewayClient CreateGatewayClient() | ||
{ | ||
return new GatewayClient(_fakeLogger, _mockOptions.Object, _httpClient); | ||
} | ||
|
||
[Fact] | ||
public void Interval_ShouldReturnOptionsValue() | ||
{ | ||
var client = CreateGatewayClient(); | ||
|
||
Assert.Equal(_options.Interval, client.Interval); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Success_ReturnsInverters() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = new StringContent("[{\"SerialNumber\": \"12345\"}]") | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Single(result); | ||
Assert.Equal("12345", result.First().SerialNumber); | ||
Assert.NotNull(result.First().Location); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Success_ReturnsEmpty() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.NotFound | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Empty(result); | ||
Assert.Matches("get inverters", _fakeLogger.LatestRecord.Message); | ||
} | ||
using System.Net; | ||
|
||
using Anar.Services.Gateway; | ||
using Anar.Services.Locator; | ||
|
||
using Microsoft.Extensions.Logging.Testing; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Moq; | ||
using Moq.Protected; | ||
|
||
using MyGateway = Anar.Services.Gateway.Gateway; | ||
|
||
namespace Anar.Tests.Services.Gateway; | ||
|
||
public class GatewayClientTests | ||
{ | ||
private readonly FakeLogger<MyGateway> _fakeLogger; | ||
private readonly HttpClient _httpClient; | ||
private readonly Mock<IOptions<GatewayOptions>> _mockOptions; | ||
private readonly Mock<HttpMessageHandler> _mockHandler; | ||
private readonly Mock<ILocator> _mockLocator; | ||
private readonly GatewayOptions _options; | ||
|
||
public GatewayClientTests() | ||
{ | ||
_options = new GatewayOptions | ||
{ | ||
Uri = new Uri("http://localhost/"), | ||
Token = "token", | ||
Thumbprint = "thumbprint", | ||
}; | ||
|
||
_fakeLogger = new FakeLogger<MyGateway>(); | ||
_mockOptions = new Mock<IOptions<GatewayOptions>>(); | ||
_mockHandler = new Mock<HttpMessageHandler>(); | ||
_mockLocator = new Mock<ILocator>(); | ||
_httpClient = new HttpClient(_mockHandler.Object) | ||
{ | ||
BaseAddress = _options.Uri, | ||
}; | ||
|
||
_mockOptions.Setup(o => o.Value).Returns(_options); | ||
} | ||
|
||
private MyGateway CreateGatewayClient() | ||
{ | ||
return new MyGateway(_httpClient, _mockOptions.Object, _fakeLogger); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Success_ReturnsInverters() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = new StringContent("[{\"SerialNumber\": \"12345\"}]") | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Single(result); | ||
Assert.Equal("12345", result.First().SerialNumber); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Error_ReturnsEmpty() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.NotFound | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Empty(result); | ||
Assert.Matches("get inverters", _fakeLogger.LatestRecord.Message); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
namespace Anar.Services.Locator.Tests; | ||
|
||
public class LayoutDTOTests | ||
{ | ||
[Fact] | ||
public void ToLocations_ConvertsAllLocations() | ||
{ | ||
var source = TestData.LayoutFileData; | ||
|
||
var result = source.ToLocations().ToList(); | ||
|
||
// Assert | ||
Assert.Equal(TestData.LayoutFileLocations, result); | ||
} | ||
} |
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,15 @@ | ||
namespace Anar.Services.Locator.Tests; | ||
|
||
public class LocationTests | ||
{ | ||
[Fact] | ||
public void Facing_ReturnsExpectedDirection() | ||
{ | ||
// Assert | ||
Assert.Equal("north", new Location("SN123", "Array 1", 0).Facing); | ||
Assert.Equal("south", new Location("SN123", "Array 1", 180).Facing); | ||
Assert.Equal("east", new Location("SN123", "Array 1", 90).Facing); | ||
Assert.Equal("west", new Location("SN123", "Array 1", 270).Facing); | ||
Assert.Equal("unknown", new Location("SN123", "Array 1", 15).Facing); | ||
} | ||
} |
Oops, something went wrong.