-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mikecarr/firmware-filters
adding firmware filtering based on soc
- Loading branch information
Showing
13 changed files
with
308 additions
and
82 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Moq; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Moq.Protected; | ||
using OpenIPC_Config.Services; | ||
using Xunit; | ||
using Assert = NUnit.Framework.Assert; | ||
|
||
public class GitHubServiceTests | ||
{ | ||
private readonly Mock<HttpMessageHandler> _httpMessageHandlerMock; | ||
private readonly HttpClient _httpClient; | ||
private readonly IMemoryCache _memoryCache; | ||
private readonly GitHubService _gitHubService; | ||
|
||
public GitHubServiceTests() | ||
{ | ||
_httpMessageHandlerMock = new Mock<HttpMessageHandler>(); | ||
_httpClient = new HttpClient(_httpMessageHandlerMock.Object); | ||
_memoryCache = new MemoryCache(new MemoryCacheOptions()); | ||
_gitHubService = new GitHubService(_memoryCache, _httpClient); | ||
} | ||
|
||
[Fact] | ||
public async Task GetGitHubDataAsync_ReturnsDataFromCache_WhenAvailable() | ||
{ | ||
// Arrange | ||
string url = "https://api.github.com/repos/example/repo"; | ||
string cachedData = "cached response"; | ||
_memoryCache.Set("GitHubData", cachedData, TimeSpan.FromMinutes(60)); | ||
|
||
// Act | ||
string result = await _gitHubService.GetGitHubDataAsync(url); | ||
|
||
// Assert | ||
Assert.That(cachedData, Is.EqualTo(result)); | ||
} | ||
|
||
[Fact] | ||
public async Task GetGitHubDataAsync_FetchesFromGitHub_WhenNotCached() | ||
{ | ||
// Arrange | ||
string url = "https://api.github.com/repos/example/repo"; | ||
string apiResponse = "api response"; | ||
// string cachedData = "cached response"; | ||
// _memoryCache.Set("GitHubData", cachedData, TimeSpan.FromMinutes(60)); | ||
|
||
_httpMessageHandlerMock | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = System.Net.HttpStatusCode.OK, | ||
Content = new StringContent(apiResponse) | ||
}); | ||
|
||
// Act | ||
string result = await _gitHubService.GetGitHubDataAsync(url); | ||
|
||
// Assert | ||
Assert.That(apiResponse, Is.EqualTo(result)); | ||
_memoryCache.TryGetValue("GitHubData", out string cachedResult); | ||
|
||
// Assert.True(_memoryCache.TryGetValue("GitHubData", out string cachedResult)); | ||
Assert.That(apiResponse, Is.EqualTo(cachedResult)); // Assert that the cached data matches the API response cachedResult); | ||
} | ||
|
||
[Fact] | ||
public async Task GetGitHubDataAsync_ReturnsNull_OnHttpRequestException() | ||
{ | ||
// Arrange | ||
string url = "https://api.github.com/repos/example/repo"; | ||
|
||
_httpMessageHandlerMock | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ThrowsAsync(new HttpRequestException("Network error")); | ||
|
||
// Act | ||
string result = await _gitHubService.GetGitHubDataAsync(url); | ||
|
||
// Assert | ||
Assert.Null(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
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
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,55 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Serilog; | ||
|
||
namespace OpenIPC_Config.Services; | ||
|
||
public class GitHubService | ||
{ | ||
private readonly IMemoryCache _cache; | ||
private readonly HttpClient _httpClient; | ||
private readonly string _cacheKey = "GitHubData"; // Unique key for your data | ||
private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(60); // Cache for 1 hour | ||
|
||
|
||
public GitHubService(IMemoryCache cache, HttpClient httpClient) | ||
{ | ||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); | ||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
} | ||
|
||
public async Task<string> GetGitHubDataAsync(string url) | ||
{ | ||
if (_cache.TryGetValue(_cacheKey, out string cachedData)) | ||
{ | ||
// Data found in cache | ||
Log.Information("Github API data retrieved from cache."); | ||
return cachedData; | ||
} | ||
|
||
// Data not in cache, fetch from GitHub API | ||
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; OpenIPC-Config/1.0)"); | ||
|
||
try | ||
{ | ||
var response = await _httpClient.GetStringAsync(url); | ||
|
||
// Store the data in the cache | ||
var cacheEntryOptions = new MemoryCacheEntryOptions() | ||
.SetAbsoluteExpiration(_cacheDuration) // Data expires after this time | ||
.SetPriority(CacheItemPriority.Normal); // Low priority for eviction | ||
|
||
_cache.Set(_cacheKey, response, cacheEntryOptions); | ||
Log.Information("Data retrieved from GitHub API and cached."); | ||
return response; | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
// Handle API errors gracefully (log, throw, etc.) | ||
Log.Error($"Error calling GitHub API: {ex.Message}"); | ||
return null; // Or throw the exception, depending on your needs | ||
} | ||
} | ||
} |
Oops, something went wrong.