Skip to content

Commit

Permalink
Rewrite (#4)
Browse files Browse the repository at this point in the history
* locator rewrite and tests
* add worker tests
* gateway tests
* Add tools for coverage
* updated empty options
* settings
* add test tasks
  • Loading branch information
melgish authored Jul 27, 2024
1 parent 8222fc4 commit 4ca609b
Show file tree
Hide file tree
Showing 45 changed files with 1,208 additions and 699 deletions.
6 changes: 6 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"commands": [
"dotnet-version"
]
},
"dotnet-reportgenerator-globaltool": {
"version": "5.3.8",
"commands": [
"reportgenerator"
]
}
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,5 @@ appsettings.local.json
appsettings.docker.json
compose.override.yaml
.mono
.env
.env
.coverage
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"appsettings",
"Arien",
"Enphase",
"Serilog"
"Serilog",
"reportgenerator",
"globaltool"
]
}
48 changes: 48 additions & 0 deletions .vscode/tasks.json
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": []
}
]
}
5 changes: 5 additions & 0 deletions Anar.Tests/Anar.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" Version="8.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="moq" Version="4.20.70" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.26" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
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);
}
}
21 changes: 0 additions & 21 deletions Anar.Tests/Services/Gateway/LocationTests.cs

This file was deleted.

42 changes: 0 additions & 42 deletions Anar.Tests/Services/Influx/DomainExtensionsTests.cs

This file was deleted.

15 changes: 15 additions & 0 deletions Anar.Tests/Services/Locator/LayoutDTOTests.cs
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);
}
}
15 changes: 15 additions & 0 deletions Anar.Tests/Services/Locator/LocationTests.cs
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);
}
}
Loading

0 comments on commit 4ca609b

Please sign in to comment.