using System;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Dpz.Core.Service.RepositoryService;
using Dpz.Core.Service.RepositoryServiceImpl;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace Dpz.Core.ServiceTest;
[TestFixture]
public class SteamGameTest : Basic
{
private readonly HttpClient _httpClient = new() { BaseAddress = new Uri("https://api.steampowered.com") };
[Test]
public async Task UpdateTest()
{
var mockLogger = new Mock<ILogger<SteamGameService>>();
var service = ServiceProvider.GetService<ISteamGameService>() ?? throw new Exception("ISteamGameService is null");
service.OnLogoDownloadComplete += async (stream, i) =>
{
await using var fileStream = new FileStream(@$"D:\localDevTemp\Stream\{Guid.NewGuid():N}", FileMode.Create,
FileAccess.Write);
var name = fileStream.Name;
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
await fileStream.DisposeAsync();
return name;
};
service.OnAchievementIconDownloadComplete += async (stream, _, _) =>
{
await using var fileStream = new FileStream(@$"D:\localDevTemp\Stream\{Guid.NewGuid():N}", FileMode.Create,
FileAccess.Write);
var name = fileStream.Name;
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
await fileStream.DisposeAsync();
return name;
};
service.OnAchievementIconGrayDownloadComplete += async (stream, _, _) =>
{
await using var fileStream = new FileStream(@$"D:\localDevTemp\Stream\{Guid.NewGuid():N}", FileMode.Create,
FileAccess.Write);
var name = fileStream.Name;
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
await fileStream.DisposeAsync();
return name;
};
await service.UpdateGamesAsync();
}
[Test]
public async Task GetGamesTest()
{
var service = ServiceProvider.GetService<ISteamGameService>() ?? throw new Exception("ISteamGameService is null");
var list = await service.GetGamesAsync();
Assert.IsNotNull(list);
Assert.IsNotEmpty(list);
await using var fileStream =
new FileStream(@"D:\work\temp data\steam_data.json", FileMode.Create, FileAccess.Write);
await JsonSerializer.SerializeAsync(fileStream, list,
options: new JsonSerializerOptions { WriteIndented = true });
await fileStream.FlushAsync();
}
}