From 1760efc485123408692638a2885d99939854a7b6 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:22:48 +0200 Subject: [PATCH 01/14] feat: improve the overall unit test rate changes: - unit tests added --- .../Exceptions/SteamSdkExceptionTests.cs | 23 +++++++++++++++++++ .../Models/AboutModelTests.cs | 6 +++-- .../Models/AchievementModelTests.cs | 18 ++++++++++----- .../Models/GameDetailModelTests.cs | 8 +++---- .../Models/GameModelTests.cs | 6 ++--- 5 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs diff --git a/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs b/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs new file mode 100644 index 0000000..106050c --- /dev/null +++ b/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs @@ -0,0 +1,23 @@ +using BB84.SAU.Domain.Exceptions; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Domain.Tests.Exceptions; + +[TestClass] +public sealed class SteamSdkExceptionTests +{ + [TestMethod] + [TestCategory("Constructor")] + public void SteamSdkExceptionTest() + { + SteamSdkException? exception; + string message = "Unit Test Exception!"; + + exception = new(message); + + Assert.IsNotNull(exception); + Assert.AreEqual(message, exception.Message); + Assert.IsNull(exception.InnerException); + } +} diff --git a/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs index 521c95e..f06b9e3 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Domain.Models; -using BB84.SAU.Domain.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Domain.Tests.Models; @@ -21,5 +21,7 @@ public void AboutModelTest() Assert.IsNotNull(model.Comments); Assert.IsNotNull(model.Company); Assert.IsNotNull(model.Copyright); + Assert.IsNotNull(model.FrameworkName); + Assert.IsNotNull(model.Repository); } } diff --git a/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs index e7f6af0..4de5c64 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Domain.Models; -using BB84.SAU.Domain.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Domain.Tests.Models; @@ -16,8 +16,8 @@ public void AchievementModelTest() string name = "UnitTest"; string? description = "UnitTestDescription"; bool hidden = false; - string icon = "http://Test"; - string iconGray = "http://Test"; + string icon = "http://Test/icon"; + string iconGray = "http://Test/iconGray"; model = new(id, name, description, hidden, icon, iconGray); @@ -28,8 +28,10 @@ public void AchievementModelTest() Assert.AreEqual(hidden, model.Hidden); Assert.AreEqual(icon, model.Icon); Assert.AreEqual(iconGray, model.IconGray); + Assert.AreEqual(iconGray, model.ImageUrl); Assert.AreEqual(false, model.Unlocked); Assert.IsNull(model.UnlockedTime); + Assert.IsNull(model.LastUpdate); } [TestMethod] @@ -41,10 +43,11 @@ public void AchievementModelSetValuesTest() string name = "UnitTest"; string? description = "UnitTestDescription"; bool hidden = false; - string icon = "http://Test"; - string iconGray = "http://Test"; + string icon = "http://Test/icon"; + string iconGray = "http://Test/iconGray"; bool unlocked = true; DateTime unlockedDate = DateTime.MinValue; + DateTime lastUpdate = DateTime.MinValue; model.Id = id; model.Name = name; @@ -54,6 +57,7 @@ public void AchievementModelSetValuesTest() model.IconGray = iconGray; model.Unlocked = unlocked; model.UnlockedTime = unlockedDate; + model.LastUpdate = lastUpdate; Assert.AreEqual(id, model.Id); Assert.AreEqual(name, model.Name); @@ -61,7 +65,9 @@ public void AchievementModelSetValuesTest() Assert.AreEqual(hidden, model.Hidden); Assert.AreEqual(icon, model.Icon); Assert.AreEqual(iconGray, model.IconGray); + Assert.AreEqual(icon, model.ImageUrl); Assert.AreEqual(unlocked, model.Unlocked); Assert.AreEqual(unlockedDate, model.UnlockedTime); + Assert.AreEqual(lastUpdate, model.LastUpdate); } } diff --git a/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs index 199d07f..817bec0 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Domain.Models; -using BB84.SAU.Domain.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Domain.Tests.Models; @@ -8,7 +8,7 @@ namespace BB84.SAU.Domain.Tests.Models; public sealed class GameDetailModelTests { [TestMethod] - [TestCategory("UnitTest")] + [TestCategory("Constructor")] public void GameDetailModelTest() { GameDetailModel? model; @@ -31,7 +31,7 @@ public void GameDetailModelTest() [TestMethod] public void GameDetailModelSetValuesTest() { - GameDetailModel? model = new(0, string.Empty, string.Empty, string.Empty); + GameDetailModel? model = new(); int id = 1; string title = "UnitTest"; string description = "UnitTestDescription"; diff --git a/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs index fc10827..d7aa479 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Domain.Models; -using BB84.SAU.Domain.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Domain.Tests.Models; @@ -28,7 +28,7 @@ public void GameModelSetValuesTest() GameModel model = new(0, string.Empty); int id = 1; string title = "UnitTest"; - + model.Id = id; model.Title = title; From aa5945e55309e2ddd6a6e4b713c517be5b66cffa Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:27:32 +0200 Subject: [PATCH 02/14] feat: improve the overall unit test rate --- src/BB84.SAU.Application/Provider/DateTimeProvider.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/BB84.SAU.Application/Provider/DateTimeProvider.cs b/src/BB84.SAU.Application/Provider/DateTimeProvider.cs index 617bea8..b47db92 100644 --- a/src/BB84.SAU.Application/Provider/DateTimeProvider.cs +++ b/src/BB84.SAU.Application/Provider/DateTimeProvider.cs @@ -1,10 +1,13 @@ -using BB84.SAU.Application.Interfaces.Application.Provider; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Application.Interfaces.Application.Provider; namespace BB84.SAU.Application.Provider; /// /// The date time provider class. /// +[ExcludeFromCodeCoverage(Justification = "Wrapper for DateTime")] internal sealed class DateTimeProvider : IDateTimeProvider { public DateTime Now => DateTime.Now; From 11eb2fec6d7865496da90f0e3ddca94e1b92e8f7 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:44:24 +0200 Subject: [PATCH 03/14] feat: unit tests added --- .../Services/INotificationService.cs | 19 ++++- .../Services/NotificationService.cs | 16 +++- .../Windows/MainWindow.xaml.cs | 2 +- .../ApplicationTestBase.cs | 26 ++++++ .../Services/NotificationServiceTests.cs | 21 +++++ .../ViewModels/MainViewModelTests.cs | 24 ++++++ .../ViewModels/SettingsViewModelTests.cs | 31 ++++++++ .../ViewModels/UserDataViewModelTests.cs | 79 +++++++++++++++++++ 8 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs create mode 100644 tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs diff --git a/src/BB84.SAU.Application/Interfaces/Application/Services/INotificationService.cs b/src/BB84.SAU.Application/Interfaces/Application/Services/INotificationService.cs index 3d1315e..7b7f44b 100644 --- a/src/BB84.SAU.Application/Interfaces/Application/Services/INotificationService.cs +++ b/src/BB84.SAU.Application/Interfaces/Application/Services/INotificationService.cs @@ -8,9 +8,14 @@ namespace BB84.SAU.Application.Interfaces.Application.Services; public interface INotificationService { /// - /// The event handler for message receiving. + /// The event handler for asynchronous message receiving. /// - event AsyncNotificationEventHandler? MessageReceived; + event AsyncNotificationEventHandler? AsyncNotificationReceived; + + /// + /// The event handler for synchronous message receiving. + /// + event NotificationEventHandler? NotificationReceived; /// /// Sends a to all the subscribers. @@ -20,10 +25,18 @@ public interface INotificationService } /// -/// Represents the method that will handle an notification event when the event provides message data. +/// Represents the method that will handle an asynchronous notification event when the event provides message data. /// /// The source of the event. /// An that contains the event message. /// [SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "I want it that way.")] public delegate Task AsyncNotificationEventHandler(object sender, string message); + +/// +/// Represents the method that will handle an synchronous notification event when the event provides message data. +/// +/// The source of the event. +/// An that contains the event message. +[SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "I want it that way.")] +public delegate void NotificationEventHandler(object sender, string message); diff --git a/src/BB84.SAU.Application/Services/NotificationService.cs b/src/BB84.SAU.Application/Services/NotificationService.cs index 0693105..12d0425 100644 --- a/src/BB84.SAU.Application/Services/NotificationService.cs +++ b/src/BB84.SAU.Application/Services/NotificationService.cs @@ -1,4 +1,5 @@ -using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.Notifications.Extensions; +using BB84.SAU.Application.Interfaces.Application.Services; namespace BB84.SAU.Application.Services; @@ -7,8 +8,17 @@ namespace BB84.SAU.Application.Services; /// internal sealed class NotificationService : INotificationService { - public event AsyncNotificationEventHandler? MessageReceived; + /// + public event AsyncNotificationEventHandler? AsyncNotificationReceived; + /// + public event NotificationEventHandler? NotificationReceived; + + /// public void Send(string message) - => MessageReceived?.Invoke(this, message); + { + AsyncNotificationReceived?.Invoke(this, message) + .FireAndForgetSafeAsync(); + NotificationReceived?.Invoke(this, message); + } } diff --git a/src/BB84.SAU.Presentation/Windows/MainWindow.xaml.cs b/src/BB84.SAU.Presentation/Windows/MainWindow.xaml.cs index 706d12b..62f51e8 100644 --- a/src/BB84.SAU.Presentation/Windows/MainWindow.xaml.cs +++ b/src/BB84.SAU.Presentation/Windows/MainWindow.xaml.cs @@ -29,7 +29,7 @@ public MainWindow(INotificationService notificationService, AboutViewModel about InitializeComponent(); _notificationService = notificationService; - _notificationService.MessageReceived += async (s, e) => await OnMessageReceived(e); + _notificationService.AsyncNotificationReceived += async (s, e) => await OnMessageReceived(e); _aboutViewModel = aboutViewModel; DataContext = _mainViewModel = mainViewModel; diff --git a/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs b/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs new file mode 100644 index 0000000..6e2822c --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Application.Tests; + +[TestClass] +public abstract class ApplicationTestBase +{ + public sealed class ViewModelTestAttribute : TestMethodAttribute + { + public override TestResult[] Execute(ITestMethod testMethod) + { + if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) + return Invoke(testMethod); + + TestResult[] result = []; + Thread thread = new(() => result = Invoke(testMethod)); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + thread.Join(); + return result; + } + + private static TestResult[] Invoke(ITestMethod testMethod) + => [testMethod.Invoke(null)]; + } +} diff --git a/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs new file mode 100644 index 0000000..9099a39 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs @@ -0,0 +1,21 @@ +using BB84.SAU.Application.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Application.Tests.Services; + +[TestClass] +public sealed class NotificationServiceTests +{ + [TestMethod] + public void SendTest() + { + bool notificationReceived = false; + NotificationService service = new(); + service.NotificationReceived += (s, e) => notificationReceived = true; + + service.Send("Unit test message."); + + Assert.IsTrue(notificationReceived); + } +} diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs new file mode 100644 index 0000000..3e0816e --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs @@ -0,0 +1,24 @@ +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.ViewModels; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.ViewModels; + +[TestClass] +public sealed class MainViewModelTests +{ + [TestMethod] + [TestCategory("Constructor")] + public void MainViewModelTest() + { + Mock navigationServiceMock = new(); + + MainViewModel viewModel = new(navigationServiceMock.Object); + + Assert.IsNotNull(viewModel); + Assert.AreEqual(navigationServiceMock.Object, viewModel.NavigationService); + } +} diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs new file mode 100644 index 0000000..0f90c29 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs @@ -0,0 +1,31 @@ +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.ViewModels; +using BB84.SAU.Domain.Settings; + +using Microsoft.Extensions.Options; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.ViewModels; + +[TestClass] +public sealed class SettingsViewModelTests +{ + [TestMethod] + [TestCategory("Constructor")] + public void SettingsViewModelConstructorTest() + { + long id = long.MaxValue; + string apiKey = Guid.NewGuid().ToString(); + Mock navigationServiceMock = new(); + Mock> optionsMock = new(); + _ = optionsMock.Setup(x => x.Value).Returns(new SteamSettings() { Id = id, ApiKey = apiKey }); + + SettingsViewModel viewModel = new(optionsMock.Object, navigationServiceMock.Object); + + Assert.IsNotNull(viewModel); + Assert.AreEqual(optionsMock.Object.Value, viewModel.Model); + Assert.AreEqual(navigationServiceMock.Object, viewModel.NavigationService); + } +} diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs new file mode 100644 index 0000000..236f028 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs @@ -0,0 +1,79 @@ +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.ViewModels; +using BB84.SAU.Domain.Models; +using BB84.SAU.Domain.Settings; + +using Microsoft.Extensions.Options; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.ViewModels; + +[TestClass] +public sealed class UserDataViewModelTests : ApplicationTestBase +{ + private const string TestImageUrl = @"https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Telefunken_FuBK_test_pattern.svg/320px-Telefunken_FuBK_test_pattern.svg.png"; + + [TestMethod] + [TestCategory("Constructor")] + public void UserDataViewModelTest() + { + Mock steamWebApiServiceMock = new(); + Mock> optionsMock = new(); + _ = optionsMock.Setup(x => x.Value).Returns(new SteamSettings()); + UserDataModel userDataModel = new(); + + UserDataViewModel viewModel = new(steamWebApiServiceMock.Object, optionsMock.Object, userDataModel); + + Assert.IsNotNull(viewModel); + Assert.IsNull(viewModel.Image); + Assert.AreEqual(userDataModel, viewModel.Model); + Assert.IsFalse(viewModel.IsUserDataLoading); + Assert.IsFalse(viewModel.IsUserDataGridVisible); + Assert.IsTrue(viewModel.IsUserDataLoadGridVisible); + Assert.IsTrue(viewModel.LoadUserDataCommand.CanExecute()); + } + + [ViewModelTest] + public async Task LoadUserDataCommandTest() + { + long id = long.MaxValue; + string apiKey = Guid.NewGuid().ToString(); + UserDataModel userDataModel = + new("UnitTest", TestImageUrl, "UnitTestUrl", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue); + Mock steamWebApiServiceMock = new(); + _ = steamWebApiServiceMock.Setup(x => x.GetUserProfile(id, apiKey, default).Result).Returns(userDataModel); + Mock> optionsMock = new(); + _ = optionsMock.Setup(x => x.Value).Returns(new SteamSettings() { Id = id, ApiKey = apiKey }); + UserDataViewModel viewModel = new(steamWebApiServiceMock.Object, optionsMock.Object, new()); + + await viewModel.LoadUserDataCommand.ExecuteAsync() + .ConfigureAwait(false); + + Assert.IsNotNull(viewModel.Image); + Assert.IsTrue(viewModel.IsUserDataGridVisible); + Assert.IsFalse(viewModel.IsUserDataLoadGridVisible); + } + + [ViewModelTest] + public async Task LoadUserDataCommandReturnNullTest() + { + long id = long.MaxValue; + string apiKey = Guid.NewGuid().ToString(); + UserDataModel userDataModel = + new("UnitTest", TestImageUrl, "UnitTestUrl", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue); + Mock steamWebApiServiceMock = new(); + _ = steamWebApiServiceMock.Setup(x => x.GetUserProfile(id, apiKey, default).Result); + Mock> optionsMock = new(); + _ = optionsMock.Setup(x => x.Value).Returns(new SteamSettings() { Id = id, ApiKey = apiKey }); + UserDataViewModel viewModel = new(steamWebApiServiceMock.Object, optionsMock.Object, new()); + + await viewModel.LoadUserDataCommand.ExecuteAsync() + .ConfigureAwait(false); + + Assert.IsNull(viewModel.Image); + Assert.IsFalse(viewModel.IsUserDataGridVisible); + Assert.IsTrue(viewModel.IsUserDataLoadGridVisible); + } +} From eceae069e6dc7499ce34b47a11ae3bbb43439eb1 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:03:18 +0200 Subject: [PATCH 04/14] fix: LogbookViewModel changes: - changed notification handling from async to sync --- src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs b/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs index f205bd5..4f68122 100644 --- a/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs +++ b/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs @@ -20,7 +20,7 @@ public sealed class LogbookViewModel : ViewModelBase public LogbookViewModel(INotificationService notificationService) { _notificationService = notificationService; - _notificationService.MessageReceived += async (s, e) => await OnMessageReceived(e); + _notificationService.NotificationReceived += (s, e) => OnMessageReceived(e); LogbookEntries = []; } @@ -29,6 +29,6 @@ public LogbookViewModel(INotificationService notificationService) /// public ObservableCollection LogbookEntries { get; } - private async Task OnMessageReceived(string e) - => await Task.Factory.StartNew(() => LogbookEntries.Add(new(e))); + private void OnMessageReceived(string e) + => LogbookEntries.Add(new(e)); } From 7f16cf43e681bd4520b7be29c072641baf43f936 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:13:53 +0200 Subject: [PATCH 05/14] feat: improve the overall unit test rate changes: - logbook view model tests modified --- .../ViewModels/LogbookViewModel.cs | 6 +++--- .../ViewModels/LogbookViewModelTests.cs | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs b/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs index 4f68122..409972b 100644 --- a/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs +++ b/src/BB84.SAU.Application/ViewModels/LogbookViewModel.cs @@ -20,7 +20,7 @@ public sealed class LogbookViewModel : ViewModelBase public LogbookViewModel(INotificationService notificationService) { _notificationService = notificationService; - _notificationService.NotificationReceived += (s, e) => OnMessageReceived(e); + _notificationService.NotificationReceived += (sender, message) => OnNotificationReceived(message); LogbookEntries = []; } @@ -29,6 +29,6 @@ public LogbookViewModel(INotificationService notificationService) /// public ObservableCollection LogbookEntries { get; } - private void OnMessageReceived(string e) - => LogbookEntries.Add(new(e)); + private void OnNotificationReceived(string message) + => LogbookEntries.Add(new(message)); } diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs index d1d0a24..4483fd4 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs @@ -1,4 +1,5 @@ using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.Services; using BB84.SAU.Application.ViewModels; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,13 +12,14 @@ namespace BB84.SAU.Application.Tests.ViewModels; public sealed class LogbookViewModelTests { [TestMethod] - public void LogbookViewModelTest() + public void LogbookViewModelNotificationTest() { - Mock notificationServiceMock = new(); + string message = "Unit Test Message"; + NotificationService notificationService = new(); + LogbookViewModel viewModel = new(notificationService); - LogbookViewModel viewModel = new(notificationServiceMock.Object); + notificationService.Send(message); - Assert.IsNotNull(viewModel); - Assert.AreEqual(default, viewModel.LogbookEntries.Count); + Assert.AreEqual(message, viewModel.LogbookEntries.First().Message); } } From 86b3f3d8d28346842836731abd96177f688f4a09 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:31:10 +0200 Subject: [PATCH 06/14] feat: improve the overall unit test rate changes: - steam api service tests added --- .../ViewModels/GamesViewModel.cs | 20 ++--- .../Services/SteamApiService.cs | 3 +- .../ViewModels/GamesViewModelTests.cs | 55 ++++++++++++++ .../SteamApiServiceTests.GetAchievement.cs | 68 +++++++++++++++++ .../SteamApiServiceTests.Initialize.cs | 62 ++++++++++++++++ .../SteamApiServiceTests.RequestStats.cs | 74 +++++++++++++++++++ .../SteamApiServiceTests.ResetAchievement.cs | 61 +++++++++++++++ .../Services/SteamApiServiceTests.Shutdown.cs | 37 ++++++++++ .../SteamApiServiceTests.StoreStats.cs | 59 +++++++++++++++ .../SteamApiServiceTests.UnlockAchievement.cs | 59 +++++++++++++++ .../Services/SteamApiServiceTests.cs | 52 +++++++++++++ 11 files changed, 537 insertions(+), 13 deletions(-) create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs create mode 100644 tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs diff --git a/src/BB84.SAU.Application/ViewModels/GamesViewModel.cs b/src/BB84.SAU.Application/ViewModels/GamesViewModel.cs index 5e9017a..95009f0 100644 --- a/src/BB84.SAU.Application/ViewModels/GamesViewModel.cs +++ b/src/BB84.SAU.Application/ViewModels/GamesViewModel.cs @@ -4,14 +4,13 @@ using BB84.Notifications.Attributes; using BB84.Notifications.Commands; using BB84.Notifications.Interfaces.Commands; - -using Microsoft.Extensions.Options; - using BB84.SAU.Application.Interfaces.Application.Services; using BB84.SAU.Application.ViewModels.Base; using BB84.SAU.Domain.Models; using BB84.SAU.Domain.Settings; +using Microsoft.Extensions.Options; + namespace BB84.SAU.Application.ViewModels; /// @@ -94,8 +93,8 @@ public IAsyncActionCommand LoadGamesCommand /// /// The command to select the game. /// - public IActionCommand SelectGameCommand - => new ActionCommand(SelectGame, CanSelectGame); + public IActionCommand SelectGameCommand + => new ActionCommand(SelectGame, CanSelectGame); /// /// Indicates if the select game button is visible. @@ -106,16 +105,13 @@ public bool IsSelectButtonVisible private bool CanLoadGames() => Model.LastUpdate is not null && GamesAreLoading.IsFalse(); - private bool CanSelectGame(GameDetailModel? model) + private bool CanSelectGame(GameDetailModel model) => model is not null && model.LastUpdate is not null; - private void SelectGame(GameDetailModel? model) + private void SelectGame(GameDetailModel model) { - if (model is not null) - { - _achievementsViewModel.Model = model; - _navigationService.NavigateTo(); - } + _achievementsViewModel.Model = model; + _navigationService.NavigateTo(); } private async Task LoadGames() diff --git a/src/BB84.SAU.Infrastructure/Services/SteamApiService.cs b/src/BB84.SAU.Infrastructure/Services/SteamApiService.cs index 8827b85..69b2686 100644 --- a/src/BB84.SAU.Infrastructure/Services/SteamApiService.cs +++ b/src/BB84.SAU.Infrastructure/Services/SteamApiService.cs @@ -76,9 +76,10 @@ public bool RequestCurrentStats() throw new SteamSdkException("Stats have not been requested!"); bool result = steamWorksProvider.GetAchievementAndUnlockTime(name, out bool achieved, out uint unlockTime); + DateTime? dateTime = achieved ? DateTimeOffset.FromUnixTimeSeconds(unlockTime).LocalDateTime - : default; + : null; return (achieved, dateTime); } diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs new file mode 100644 index 0000000..32b67f0 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs @@ -0,0 +1,55 @@ +using BB84.SAU.Application.Interfaces.Application.Provider; +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.Interfaces.Infrastructure.Services; +using BB84.SAU.Application.ViewModels; +using BB84.SAU.Domain.Models; +using BB84.SAU.Domain.Settings; + +using Microsoft.Extensions.Options; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.ViewModels; + +[TestClass] +public sealed partial class GamesViewModelTests : ApplicationTestBase +{ + private Mock _dateTimeProviderMock = new(); + private Mock _navigationServiceMock = new(); + private Mock _steamApiServiceMock = new(); + private Mock _steamWebServiceMock = new(); + private Mock> _optionsMock = new(); + + [TestMethod] + [TestCategory("Constructor")] + public void GamesViewModelTest() + { + GamesViewModel gamesViewModel = CreateViewModelMock(); + + Assert.IsNotNull(gamesViewModel); + Assert.IsNotNull(gamesViewModel.Model); + Assert.IsNull(gamesViewModel.SelectedGame); + Assert.IsNull(gamesViewModel.GameImage); + Assert.IsFalse(gamesViewModel.LoadGamesCommand.CanExecute()); + Assert.IsFalse(gamesViewModel.GamesAreLoading); + Assert.IsFalse(gamesViewModel.IsGameVisible); + Assert.IsFalse(gamesViewModel.IsSelectButtonVisible); + } + + private GamesViewModel CreateViewModelMock() + { + _dateTimeProviderMock = new(); + _navigationServiceMock = new(); + _steamApiServiceMock = new(); + _steamWebServiceMock = new(); + _optionsMock = new(); + + AchievementsViewModel achievementsViewModel = + new(_steamApiServiceMock.Object, _steamWebServiceMock.Object, _optionsMock.Object, _dateTimeProviderMock.Object); + + UserDataModel userDataModel = new(); + + return new(_navigationServiceMock.Object, _steamWebServiceMock.Object, _optionsMock.Object, achievementsViewModel, userDataModel); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs new file mode 100644 index 0000000..d4a901e --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs @@ -0,0 +1,68 @@ +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void GetAchievementShouldReturnFalseWhenStatsNotRequested() + { + SteamApiService service = CreateMockedService(); + + (bool achieved, DateTime? unlockTime) = service.GetAchievement(AchievementName); + + Assert.IsFalse(achieved); + Assert.IsNull(unlockTime); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + Assert.AreEqual(0, _steamWorksProviderMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void GetAchievementShouldReturnFalseWhenAchievementNotUnlocked() + { + uint unlockTime = 0; + bool achieved = false; + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.GetAchievementAndUnlockTime(AchievementName, out achieved, out unlockTime)).Returns(true); + service.Init(AppId); + service.RequestCurrentStats(); + + (bool Achieved, DateTime? UnlockTime) result = service.GetAchievement(AchievementName); + + Assert.IsFalse(result.Achieved); + Assert.IsNull(result.UnlockTime); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + Assert.AreEqual(3, _steamWorksProviderMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void GetAchievementShouldReturnValuesWhenSteamWorksProviderReturnedValues() + { + uint unlockTime = 946681200; + bool achieved = true; + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.GetAchievementAndUnlockTime(AchievementName, out achieved, out unlockTime)); + + service.Init(AppId); + service.RequestCurrentStats(); + + (bool Achieved, DateTime? UnlockTime) result = service.GetAchievement(AchievementName); + + Assert.IsTrue(result.Achieved); + Assert.AreEqual(new DateTime(2000, 1, 1), result.UnlockTime); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + Assert.AreEqual(3, _steamWorksProviderMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs new file mode 100644 index 0000000..ce636a8 --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs @@ -0,0 +1,62 @@ +using BB84.SAU.Domain.Exceptions; +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void InitializeShouldReturnTrueWhenSteamWorksProviderReturnedTrue() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + + int appId = 123456; + bool result = service.Init(appId); + + Assert.IsTrue(result); + Assert.IsTrue(service.Initialized); + Assert.AreEqual(appId, service.AppId); + Assert.AreEqual(1, _fileProviderMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void InitializeShouldReturnFalseWhenSteamWorksProviderReturnedFalse() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(false); + + int appId = 123456; + bool result = service.Init(appId); + + Assert.IsFalse(result); + Assert.IsFalse(service.Initialized); + Assert.IsNull(service.AppId); + Assert.AreEqual(1, _fileProviderMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void InitializeShouldReturnFalseWhenExceptionGetsThrown() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Throws(new SteamSdkException("Failure")); + + int appId = 123456; + bool result = service.Init(appId); + + Assert.IsFalse(result); + Assert.IsFalse(service.Initialized); + Assert.IsNull(service.AppId); + Assert.IsTrue(_loggerServiceMock.Invocations.Count == 1); + Assert.IsTrue(_notificationServiceMock.Invocations.Count == 1); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs new file mode 100644 index 0000000..cd9dfc6 --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs @@ -0,0 +1,74 @@ +using BB84.SAU.Domain.Exceptions; +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void RequestStatsShouldReturnFalseWhenNotInitialized() + { + SteamApiService service = CreateMockedService(); + + bool result = service.RequestCurrentStats(); + + Assert.IsFalse(result); + Assert.IsFalse(service.StatsRequested); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void RequestStatsShouldReturnFalseWhenSteamWorksProviderReturnedFalse() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(false); + service.Init(AppId); + + bool result = service.RequestCurrentStats(); + + Assert.IsFalse(result); + Assert.IsFalse(service.StatsRequested); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void RequestStatsShouldReturnTrueWhenSteamWorksProviderReturnedTrue() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + service.Init(AppId); + + bool result = service.RequestCurrentStats(); + + Assert.IsTrue(result); + Assert.IsTrue(service.StatsRequested); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void RequestStatsShouldReturnFalseWhenExceptionGetThrown() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Throws(new SteamSdkException("Failure")); + service.Init(AppId); + + bool result = service.RequestCurrentStats(); + + Assert.IsFalse(result); + Assert.IsFalse(service.StatsRequested); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs new file mode 100644 index 0000000..201a0cb --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs @@ -0,0 +1,61 @@ +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void ResetAchievementShouldReturnFalseWhenStatsNotRequested() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + service.Init(1); + + bool result = service.ResetAchievement(AchievementName); + + Assert.IsFalse(result); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void ResetAchievementShouldReturnFalseWhenNotFound() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.ClearAchievement(AchievementName)).Returns(false); + + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.ResetAchievement(AchievementName); + + Assert.IsFalse(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void ResetAchievementShouldReturnTrueWhenFound() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.ClearAchievement(AchievementName)).Returns(true); + + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.ResetAchievement(AchievementName); + + Assert.IsTrue(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs new file mode 100644 index 0000000..cf8cbf5 --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs @@ -0,0 +1,37 @@ +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void ShutdownShouldThrowExceptionWhenNotInitialized() + { + SteamApiService service = CreateMockedService(); + + service.Shutdown(); + + Assert.AreEqual(0, _fileProviderMock.Invocations.Count); + Assert.AreEqual(0, _steamWorksProviderMock.Invocations.Count); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void ShutdownShouldSuccedWhenInitialized() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + service.Init(1); + + service.Shutdown(); + + Assert.IsNull(service.AppId); + Assert.AreEqual(2, _fileProviderMock.Invocations.Count); + Assert.AreEqual(2, _steamWorksProviderMock.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs new file mode 100644 index 0000000..615e3aa --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs @@ -0,0 +1,59 @@ +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void StoreStatsShouldReturnFalseWhenStatsNotRequested() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + service.Init(1); + + bool result = service.StoreStats(); + + Assert.IsFalse(result); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void StoreStatsShouldReturnFalseWhenStoreStatsReturnedFalse() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.StoreStats()).Returns(false); + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.StoreStats(); + + Assert.IsFalse(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void StoreStatsShouldReturnTrueWhenStoreStatsReturnedTrue() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.StoreStats()).Returns(true); + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.StoreStats(); + + Assert.IsTrue(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs new file mode 100644 index 0000000..302e6bc --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs @@ -0,0 +1,59 @@ +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +public sealed partial class SteamApiServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public void UnlockAchievementShouldReturnFalseWhenStatsNotRequested() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + service.Init(1); + + bool result = service.UnlockAchievement(AchievementName); + + Assert.IsFalse(result); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void UnlockAchievementShouldReturnFalseWhenAchievementNotUnlocked() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.SetAchievement(AchievementName)).Returns(false); + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.UnlockAchievement(AchievementName); + + Assert.IsFalse(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public void UnlockAchievementShouldReturnTrueWhenAchievementUnlocked() + { + SteamApiService service = CreateMockedService(); + _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); + _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); + _steamWorksProviderMock.Setup(x => x.SetAchievement(AchievementName)).Returns(true); + service.Init(1); + service.RequestCurrentStats(); + + bool result = service.UnlockAchievement(AchievementName); + + Assert.IsTrue(result); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs new file mode 100644 index 0000000..e170c4a --- /dev/null +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs @@ -0,0 +1,52 @@ +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.Interfaces.Infrastructure.Services; +using BB84.SAU.Infrastructure.Interfaces.Provider; +using BB84.SAU.Infrastructure.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Infrastructure.Tests.Services; + +[TestClass] +public sealed partial class SteamApiServiceTests +{ + private const int AppId = 1; + private const string AchievementName = "TestAchievement"; + private Mock> _loggerServiceMock = new(); + private Mock _notificationServiceMock = new(); + private Mock _steamWorksProviderMock = new(); + private Mock _fileProviderMock = new(); + + [TestMethod] + [TestCategory("Constructor")] + public void SteamApiServiceTest() + { + SteamApiService service = CreateMockedService(); + + Assert.IsNotNull(service); + Assert.IsNull(service.AppId); + Assert.IsFalse(service.Initialized); + Assert.IsFalse(service.StatsRequested); + } + + /// + /// Creates a new instance of the class with mocked dependencies. + /// + /// The new instance with mocked dependencies. + private SteamApiService CreateMockedService() + { + _loggerServiceMock = new(); + _steamWorksProviderMock = new(); + _fileProviderMock = new(); + _notificationServiceMock = new(); + + return new SteamApiService( + _loggerServiceMock.Object, + _notificationServiceMock.Object, + _steamWorksProviderMock.Object, + _fileProviderMock.Object + ); + } +} From 6d71f19054c9d62f6d3adcbf96228e36eae9fbc8 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Fri, 28 Jun 2024 15:40:13 +0200 Subject: [PATCH 07/14] feat: improve the overall unit test rate changes: - games view model tests added --- .../GamesViewModelTests.LoadGamesCommand.cs | 45 +++++++++++++++++++ .../GamesViewModelTests.SelectGameCommand.cs | 35 +++++++++++++++ .../ViewModels/GamesViewModelTests.cs | 2 + 3 files changed, 82 insertions(+) create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs create mode 100644 tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs new file mode 100644 index 0000000..e716d06 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs @@ -0,0 +1,45 @@ +using BB84.SAU.Application.ViewModels; +using BB84.SAU.Domain.Models; +using BB84.SAU.Domain.Settings; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.ViewModels; + +public sealed partial class GamesViewModelTests +{ + [TestMethod] + [TestCategory("Commands")] + public async Task LoadGamesCommandShouldLoadGamesWhenLastUpdateIsSet() + { + int appId = 1; + string appTitle = "Fancy"; + GameDetailModel game = new(appId, appTitle); + List games = [new(appId, appTitle)]; + GamesViewModel viewModel = CreateViewModelMock(); + _optionsMock.Setup(x => x.Value).Returns(SteamSettings); + _steamWebServiceMock.Setup(x => x.GetGames(SteamSettings.Id, SteamSettings.ApiKey, default)).ReturnsAsync(games); + _steamWebServiceMock.Setup(x => x.GetGameDetails(appId, default)).ReturnsAsync(game); + viewModel.Model.LastUpdate = DateTime.MinValue; + + await viewModel.LoadGamesCommand.ExecuteAsync() + .ConfigureAwait(false); + + Assert.IsFalse(viewModel.GamesAreLoading); + } + + [TestMethod] + [TestCategory("Commands")] + public async Task LoadGamesCommandShouldNotLoadGamesWhenLastUpdateIsNull() + { + GamesViewModel viewModel = CreateViewModelMock(); + viewModel.Model.LastUpdate = null; + + await viewModel.LoadGamesCommand.ExecuteAsync() + .ConfigureAwait(false); + + Assert.IsFalse(viewModel.GamesAreLoading); + } +} diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs new file mode 100644 index 0000000..ff0f332 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs @@ -0,0 +1,35 @@ +using BB84.SAU.Application.ViewModels; +using BB84.SAU.Domain.Models; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BB84.SAU.Application.Tests.ViewModels; + +public sealed partial class GamesViewModelTests +{ + [TestMethod] + [TestCategory("Commands")] + public void SelectGameCommandShouldNotSetSelectedGameWhenLastUpdateIsNull() + { + GamesViewModel viewModel = CreateViewModelMock(); + GameDetailModel game = new(1, "Fancy"); + + viewModel.SelectGameCommand.Execute(game); + + Assert.AreNotEqual(game, viewModel.SelectedGame); + } + + [ViewModelTest] + [TestCategory("Commands")] + public void SelectGameCommandShouldSetSelectedGameWhenLastUpdateIsSet() + { + string imageUrl = @"https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Telefunken_FuBK_test_pattern.svg/320px-Telefunken_FuBK_test_pattern.svg.png"; + GamesViewModel viewModel = CreateViewModelMock(); + viewModel.SelectedGame = new(1, "Fancy", string.Empty, imageUrl, DateTime.MinValue); + + viewModel.SelectGameCommand.Execute(viewModel.SelectedGame); + + Assert.IsTrue(viewModel.IsGameVisible); + Assert.IsTrue(viewModel.IsSelectButtonVisible); + } +} diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs index 32b67f0..536bb80 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs @@ -15,6 +15,7 @@ namespace BB84.SAU.Application.Tests.ViewModels; [TestClass] public sealed partial class GamesViewModelTests : ApplicationTestBase { + private static readonly SteamSettings SteamSettings = new() { Id = 0, ApiKey = "TestKey" }; private Mock _dateTimeProviderMock = new(); private Mock _navigationServiceMock = new(); private Mock _steamApiServiceMock = new(); @@ -44,6 +45,7 @@ private GamesViewModel CreateViewModelMock() _steamApiServiceMock = new(); _steamWebServiceMock = new(); _optionsMock = new(); + _optionsMock.Setup(x => x.Value).Returns(SteamSettings); AchievementsViewModel achievementsViewModel = new(_steamApiServiceMock.Object, _steamWebServiceMock.Object, _optionsMock.Object, _dateTimeProviderMock.Object); From 4153514a1fce6f3f9acb5d99c128ff9ac1d5f99f Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Fri, 28 Jun 2024 16:42:24 +0200 Subject: [PATCH 08/14] feat: improve the overall unit test rate changes: - steam web service unit tests started --- .../Services/SteamWebService.cs | 57 ++++++++-------- .../SteamWebServiceTests.GetAchievments.cs | 66 +++++++++++++++++++ .../Services/SteamWebServiceTests.cs | 52 +++++++++++++++ 3 files changed, 146 insertions(+), 29 deletions(-) create mode 100644 tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs create mode 100644 tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs diff --git a/src/BB84.SAU.Application/Services/SteamWebService.cs b/src/BB84.SAU.Application/Services/SteamWebService.cs index 5a40a9c..e8c4de3 100644 --- a/src/BB84.SAU.Application/Services/SteamWebService.cs +++ b/src/BB84.SAU.Application/Services/SteamWebService.cs @@ -2,14 +2,13 @@ using System.Text.Json; using BB84.Extensions; - -using Microsoft.Extensions.Logging; - using BB84.SAU.Application.Interfaces.Application.Provider; using BB84.SAU.Application.Interfaces.Application.Services; using BB84.SAU.Application.Interfaces.Infrastructure.Services; using BB84.SAU.Domain.Models; +using Microsoft.Extensions.Logging; + using HC = BB84.SAU.Application.Common.ApplicationConstants.HttpClients; namespace BB84.SAU.Application.Services; @@ -23,10 +22,6 @@ namespace BB84.SAU.Application.Services; /// The http client factory instance to use. internal sealed class SteamWebService(ILoggerService loggerService, INotificationService notificationService, IDateTimeProvider dateTimeProvider, IHttpClientFactory httpClientFactory) : ISteamWebService { - private readonly ILoggerService _loggerService = loggerService; - private readonly INotificationService _notificationService = notificationService; - private readonly IHttpClientFactory _httpClientFactory = httpClientFactory; - private static readonly Action LogException = LoggerMessage.Define(LogLevel.Error, 0, "Exception occured."); @@ -38,13 +33,14 @@ public async Task> GetAchievements(int appId, stri List achievements = []; try { - HttpClient httpClient = _httpClientFactory.CreateClient(nameof(HC.SteamPowered)); + HttpClient httpClient = httpClientFactory.CreateClient(nameof(HC.SteamPowered)); string requestUri = HC.SteamPowered.GetSchemaForGame.FormatInvariant(apiKey, appId); using HttpResponseMessage responseMessage = await httpClient.GetAsync(requestUri, cancellationToken); if (!responseMessage.IsSuccessStatusCode) { - _loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + notificationService.Send($"{responseMessage.StatusCode} - {responseMessage.ReasonPhrase}"); return achievements; } @@ -73,14 +69,14 @@ public async Task> GetAchievements(int appId, stri achievements.Add(new(id, name, description, hidden, icon, iconGray)); } - _notificationService.Send($"Loaded {achievements.Count} achievements."); + notificationService.Send($"Loaded {achievements.Count} achievements."); return achievements; } catch (Exception ex) { - _loggerService.Log(LogExceptionWithParams, appId, ex); - _notificationService.Send(ex.Message); + loggerService.Log(LogExceptionWithParams, appId, ex); + notificationService.Send(ex.Message); return achievements; } } @@ -90,17 +86,18 @@ public async Task> GetGames(long steamId, string apiKey, List games = []; try { - HttpClient httpClient = _httpClientFactory.CreateClient(nameof(HC.SteamPowered)); + HttpClient httpClient = httpClientFactory.CreateClient(nameof(HC.SteamPowered)); string requestUri = HC.SteamPowered.GetOwnedGames.FormatInvariant(apiKey, steamId); - using HttpResponseMessage gamesResponseMessage = await httpClient.GetAsync(requestUri, cancellationToken); + using HttpResponseMessage responseMessage = await httpClient.GetAsync(requestUri, cancellationToken); - if (!gamesResponseMessage.IsSuccessStatusCode) + if (!responseMessage.IsSuccessStatusCode) { - _loggerService.Log(LogExceptionWithParams, gamesResponseMessage.StatusCode, null); + loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + notificationService.Send($"{responseMessage.StatusCode} - {responseMessage.ReasonPhrase}"); return games; } - string jsonContent = await gamesResponseMessage.Content.ReadAsStringAsync(cancellationToken) + string jsonContent = await responseMessage.Content.ReadAsStringAsync(cancellationToken) .ConfigureAwait(false); JsonElement.ArrayEnumerator jsonArray = JsonSerializer.Deserialize(jsonContent) @@ -108,7 +105,7 @@ public async Task> GetGames(long steamId, string apiKey, .GetProperty("games") .EnumerateArray(); - foreach (var jsonElement in jsonArray) + foreach (JsonElement jsonElement in jsonArray) { int id = jsonElement.GetProperty("appid").GetInt32(); string title = jsonElement.GetProperty("name").ToString(); @@ -116,14 +113,14 @@ public async Task> GetGames(long steamId, string apiKey, games.Add(new(id, title)); } - _notificationService.Send($"Loaded {games.Count} games."); + notificationService.Send($"Loaded {games.Count} games."); return games; } catch (Exception ex) { - _loggerService.Log(LogException, ex); - _notificationService.Send(ex.Message); + loggerService.Log(LogException, ex); + notificationService.Send(ex.Message); return games; } } @@ -132,13 +129,14 @@ public async Task> GetGames(long steamId, string apiKey, { try { - HttpClient httpClient = _httpClientFactory.CreateClient(nameof(HC.SteamStore)); + HttpClient httpClient = httpClientFactory.CreateClient(nameof(HC.SteamStore)); string requestUri = HC.SteamStore.GetAppDetails.FormatInvariant(appId); using HttpResponseMessage responseMessage = await httpClient.GetAsync(requestUri, cancellationToken); if (!responseMessage.IsSuccessStatusCode) { - _loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + notificationService.Send($"{responseMessage.StatusCode} - {responseMessage.ReasonPhrase}"); return default; } @@ -164,8 +162,8 @@ public async Task> GetGames(long steamId, string apiKey, } catch (Exception ex) { - _loggerService.Log(LogExceptionWithParams, appId, ex); - _notificationService.Send(ex.Message); + loggerService.Log(LogExceptionWithParams, appId, ex); + notificationService.Send(ex.Message); return default; } } @@ -174,13 +172,14 @@ public async Task> GetGames(long steamId, string apiKey, { try { - HttpClient httpClient = _httpClientFactory.CreateClient(nameof(HC.SteamPowered)); + HttpClient httpClient = httpClientFactory.CreateClient(nameof(HC.SteamPowered)); string requestUri = HC.SteamPowered.GetPlayerSummaries.FormatInvariant(apiKey, steamId); using HttpResponseMessage responseMessage = await httpClient.GetAsync(requestUri, cancellationToken); if (!responseMessage.IsSuccessStatusCode) { - _loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + loggerService.Log(LogExceptionWithParams, responseMessage.StatusCode, null); + notificationService.Send($"{responseMessage.StatusCode} - {responseMessage.ReasonPhrase}"); return default; } @@ -205,8 +204,8 @@ public async Task> GetGames(long steamId, string apiKey, } catch (Exception ex) { - _loggerService.Log(LogException, ex); - _notificationService.Send(ex.Message); + loggerService.Log(LogException, ex); + notificationService.Send(ex.Message); return default; } } diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs new file mode 100644 index 0000000..f850f8d --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs @@ -0,0 +1,66 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; + +using BB84.SAU.Application.Services; +using BB84.SAU.Domain.Models; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.Services; + +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] +public sealed partial class SteamWebServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public async Task GetAchievementsShouldReturnEmptyResultWhenExceptionGetsThrown() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())).Throws(); + + IEnumerable result = await service.GetAchievements(APPID, APIKEY) + .ConfigureAwait(false); + + Assert.IsFalse(result.Any()); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetAchievementsShouldReturnEmptyResultWhenNotSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.NotFound)); + + IEnumerable result = await service.GetAchievements(APPID, APIKEY) + .ConfigureAwait(false); + + Assert.IsFalse(result.Any()); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetAchievementsShouldReturnOneResultWhenSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.OK, AchievmentsResponse)); + + IEnumerable result = await service.GetAchievements(APPID, APIKEY) + .ConfigureAwait(false); + + Assert.IsTrue(result.Any()); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs new file mode 100644 index 0000000..aed6efe --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs @@ -0,0 +1,52 @@ +using System.Net; +using System.Text; + +using BB84.SAU.Application.Interfaces.Application.Provider; +using BB84.SAU.Application.Interfaces.Application.Services; +using BB84.SAU.Application.Interfaces.Infrastructure.Services; +using BB84.SAU.Application.Services; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; +using Moq.Protected; + +namespace BB84.SAU.Application.Tests.Services; + +[TestClass] +public sealed partial class SteamWebServiceTests +{ + private const int APPID = 1; + private const string APIKEY = "UnitTestKey"; + private const string AchievmentsResponse = @"{""game"":{""gameName"":""MyTestGame"",""gameVersion"":""1"",""availableGameStats"":{""achievements"":[{""name"":""Achievement"",""defaultvalue"":0,""displayName"":""Achievement"",""hidden"":0,""description"":""Achievement"",""icon"":""https://www.unittest.dev/1.jpg"",""icongray"":""https://www.unittest.dev/2.jpg""}]}}}"; + + private Mock> _loggerServiceMock = new(); + private Mock _notificationServiceMock = new(); + private Mock _dateTimeProviderMock = new(); + private Mock _httpClientFactoryMock = new(); + private Mock _httpMessageHandler = new(); + + private SteamWebService CreateMockedInstance() + { + _loggerServiceMock = new(); + _notificationServiceMock = new(); + _dateTimeProviderMock = new(); + _httpClientFactoryMock = new(); + + return new(_loggerServiceMock.Object, _notificationServiceMock.Object, _dateTimeProviderMock.Object, _httpClientFactoryMock.Object); + } + + private HttpClient CreateMockedClient(HttpStatusCode statusCode, string? content = null) + { + _httpMessageHandler = new(MockBehavior.Strict); + HttpResponseMessage responseMessage = new(statusCode) + { + Content = new StringContent(content ?? string.Empty, Encoding.UTF8, "application/json") + }; + _httpMessageHandler.Protected() + .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .ReturnsAsync(responseMessage) + .Verifiable(); + return new(_httpMessageHandler.Object) { BaseAddress = new Uri("http://www.unittest.dev") }; + } +} From efaec6a6c74264886a242bb938676ecc3292ece4 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:10:06 +0200 Subject: [PATCH 09/14] fix: resolving merge problems --- .../SteamApiServiceTests.GetAchievement.cs | 14 ++++++++------ .../SteamApiServiceTests.Initialize.cs | 16 ++++++++-------- .../SteamApiServiceTests.RequestStats.cs | 19 +++++++++++-------- .../SteamApiServiceTests.ResetAchievement.cs | 17 +++++++++-------- .../Services/SteamApiServiceTests.Shutdown.cs | 7 +++++-- .../SteamApiServiceTests.StoreStats.cs | 15 +++++++++------ .../SteamApiServiceTests.UnlockAchievement.cs | 15 +++++++++------ 7 files changed, 59 insertions(+), 44 deletions(-) diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs index d4a901e..1cf2269 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs @@ -1,9 +1,12 @@ -using BB84.SAU.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -31,8 +34,8 @@ public void GetAchievementShouldReturnFalseWhenAchievementNotUnlocked() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.GetAchievementAndUnlockTime(AchievementName, out achieved, out unlockTime)).Returns(true); - service.Init(AppId); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); (bool Achieved, DateTime? UnlockTime) result = service.GetAchievement(AchievementName); @@ -53,9 +56,8 @@ public void GetAchievementShouldReturnValuesWhenSteamWorksProviderReturnedValues _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.GetAchievementAndUnlockTime(AchievementName, out achieved, out unlockTime)); - - service.Init(AppId); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); (bool Achieved, DateTime? UnlockTime) result = service.GetAchievement(AchievementName); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs index ce636a8..3517dd5 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs @@ -1,10 +1,13 @@ -using BB84.SAU.Domain.Exceptions; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Domain.Exceptions; using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -14,12 +17,11 @@ public void InitializeShouldReturnTrueWhenSteamWorksProviderReturnedTrue() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); - int appId = 123456; - bool result = service.Init(appId); + bool result = service.Initialize(AppId); Assert.IsTrue(result); Assert.IsTrue(service.Initialized); - Assert.AreEqual(appId, service.AppId); + Assert.AreEqual(AppId, service.AppId); Assert.AreEqual(1, _fileProviderMock.Invocations.Count); Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); @@ -32,8 +34,7 @@ public void InitializeShouldReturnFalseWhenSteamWorksProviderReturnedFalse() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(false); - int appId = 123456; - bool result = service.Init(appId); + bool result = service.Initialize(AppId); Assert.IsFalse(result); Assert.IsFalse(service.Initialized); @@ -50,8 +51,7 @@ public void InitializeShouldReturnFalseWhenExceptionGetsThrown() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Throws(new SteamSdkException("Failure")); - int appId = 123456; - bool result = service.Init(appId); + bool result = service.Initialize(AppId); Assert.IsFalse(result); Assert.IsFalse(service.Initialized); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs index cd9dfc6..48975fc 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs @@ -1,10 +1,13 @@ -using BB84.SAU.Domain.Exceptions; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Domain.Exceptions; using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -13,7 +16,7 @@ public void RequestStatsShouldReturnFalseWhenNotInitialized() { SteamApiService service = CreateMockedService(); - bool result = service.RequestCurrentStats(); + bool result = service.RequestStats(); Assert.IsFalse(result); Assert.IsFalse(service.StatsRequested); @@ -28,9 +31,9 @@ public void RequestStatsShouldReturnFalseWhenSteamWorksProviderReturnedFalse() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(false); - service.Init(AppId); + service.Initialize(AppId); - bool result = service.RequestCurrentStats(); + bool result = service.RequestStats(); Assert.IsFalse(result); Assert.IsFalse(service.StatsRequested); @@ -45,9 +48,9 @@ public void RequestStatsShouldReturnTrueWhenSteamWorksProviderReturnedTrue() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); - service.Init(AppId); + service.Initialize(AppId); - bool result = service.RequestCurrentStats(); + bool result = service.RequestStats(); Assert.IsTrue(result); Assert.IsTrue(service.StatsRequested); @@ -62,9 +65,9 @@ public void RequestStatsShouldReturnFalseWhenExceptionGetThrown() SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Throws(new SteamSdkException("Failure")); - service.Init(AppId); + service.Initialize(AppId); - bool result = service.RequestCurrentStats(); + bool result = service.RequestStats(); Assert.IsFalse(result); Assert.IsFalse(service.StatsRequested); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs index 201a0cb..4fd1f2a 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs @@ -1,9 +1,12 @@ -using BB84.SAU.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -12,7 +15,7 @@ public void ResetAchievementShouldReturnFalseWhenStatsNotRequested() { SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); - service.Init(1); + service.Initialize(AppId); bool result = service.ResetAchievement(AchievementName); @@ -29,9 +32,8 @@ public void ResetAchievementShouldReturnFalseWhenNotFound() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.ClearAchievement(AchievementName)).Returns(false); - - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.ResetAchievement(AchievementName); @@ -48,9 +50,8 @@ public void ResetAchievementShouldReturnTrueWhenFound() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.ClearAchievement(AchievementName)).Returns(true); - - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.ResetAchievement(AchievementName); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs index cf8cbf5..f4a6594 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs @@ -1,9 +1,12 @@ -using BB84.SAU.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -25,7 +28,7 @@ public void ShutdownShouldSuccedWhenInitialized() { SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); - service.Init(1); + service.Initialize(AppId); service.Shutdown(); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs index 615e3aa..1b952e5 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs @@ -1,9 +1,12 @@ -using BB84.SAU.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -12,7 +15,7 @@ public void StoreStatsShouldReturnFalseWhenStatsNotRequested() { SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); - service.Init(1); + service.Initialize(AppId); bool result = service.StoreStats(); @@ -29,8 +32,8 @@ public void StoreStatsShouldReturnFalseWhenStoreStatsReturnedFalse() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.StoreStats()).Returns(false); - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.StoreStats(); @@ -47,8 +50,8 @@ public void StoreStatsShouldReturnTrueWhenStoreStatsReturnedTrue() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.StoreStats()).Returns(true); - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.StoreStats(); diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs index 302e6bc..88c7f80 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs @@ -1,9 +1,12 @@ -using BB84.SAU.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; + +using BB84.SAU.Infrastructure.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BB84.SAU.Infrastructure.Tests.Services; +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] public sealed partial class SteamApiServiceTests { [TestMethod] @@ -12,7 +15,7 @@ public void UnlockAchievementShouldReturnFalseWhenStatsNotRequested() { SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); - service.Init(1); + service.Initialize(AppId); bool result = service.UnlockAchievement(AchievementName); @@ -29,8 +32,8 @@ public void UnlockAchievementShouldReturnFalseWhenAchievementNotUnlocked() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.SetAchievement(AchievementName)).Returns(false); - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.UnlockAchievement(AchievementName); @@ -47,8 +50,8 @@ public void UnlockAchievementShouldReturnTrueWhenAchievementUnlocked() _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); _steamWorksProviderMock.Setup(x => x.RequestCurrentStats()).Returns(true); _steamWorksProviderMock.Setup(x => x.SetAchievement(AchievementName)).Returns(true); - service.Init(1); - service.RequestCurrentStats(); + service.Initialize(AppId); + service.RequestStats(); bool result = service.UnlockAchievement(AchievementName); From 422839e4f5bd1de865fd4f60dc01c860bd7ab15b Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sat, 29 Jun 2024 09:29:54 +0200 Subject: [PATCH 10/14] feat: improve the overall unit test rate changes: - steam web service unit tests added --- .../Services/SteamWebServiceTests.GetGames.cs | 66 +++++++++++++++++++ .../Services/SteamWebServiceTests.cs | 4 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs new file mode 100644 index 0000000..0276038 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs @@ -0,0 +1,66 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; + +using BB84.SAU.Application.Services; +using BB84.SAU.Domain.Models; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.Services; + +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] +public sealed partial class SteamWebServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public async Task GetGamesShouldReturnEmptyResultWhenExceptionGetsThrown() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())).Throws(); + + IEnumerable result = await service.GetGames(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsFalse(result.Any()); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetGamesShouldReturnEmptyResultWhenNotSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.NotFound)); + + IEnumerable result = await service.GetGames(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsFalse(result.Any()); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetGamesShouldReturnOneResultWhenSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.OK, GamesResponse)); + + IEnumerable result = await service.GetGames(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsTrue(result.Any()); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs index aed6efe..11b1c4b 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs @@ -16,9 +16,11 @@ namespace BB84.SAU.Application.Tests.Services; [TestClass] public sealed partial class SteamWebServiceTests { - private const int APPID = 1; + private const int APPID = int.MaxValue; private const string APIKEY = "UnitTestKey"; + private const long STEAMID = long.MaxValue; private const string AchievmentsResponse = @"{""game"":{""gameName"":""MyTestGame"",""gameVersion"":""1"",""availableGameStats"":{""achievements"":[{""name"":""Achievement"",""defaultvalue"":0,""displayName"":""Achievement"",""hidden"":0,""description"":""Achievement"",""icon"":""https://www.unittest.dev/1.jpg"",""icongray"":""https://www.unittest.dev/2.jpg""}]}}}"; + private const string GamesResponse = @"{""response"":{""games"":[{""appid"": 1,""name"":""UnitTest""}]}}"; private Mock> _loggerServiceMock = new(); private Mock _notificationServiceMock = new(); From 387b1a0e6bec020db727b674d9764f838ab36903 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sat, 29 Jun 2024 09:57:47 +0200 Subject: [PATCH 11/14] feat: improve the overall unit test rate changes: - steam web service unit tests finished --- .../Services/SteamWebService.cs | 4 ++ .../SteamWebServiceTests.GetGameDetails.cs | 66 +++++++++++++++++++ .../SteamWebServiceTests.GetUserData.cs | 66 +++++++++++++++++++ .../Services/SteamWebServiceTests.cs | 10 +-- 4 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs create mode 100644 tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs diff --git a/src/BB84.SAU.Application/Services/SteamWebService.cs b/src/BB84.SAU.Application/Services/SteamWebService.cs index e8c4de3..4ded1cb 100644 --- a/src/BB84.SAU.Application/Services/SteamWebService.cs +++ b/src/BB84.SAU.Application/Services/SteamWebService.cs @@ -158,6 +158,8 @@ public async Task> GetGames(long steamId, string apiKey, if (jsonElement.TryGetProperty("header_image", out value)) imageUrl = value.GetString(); + notificationService.Send($"Details for {title} loaded."); + return new(id, title, description, imageUrl, dateTimeProvider.Now); } catch (Exception ex) @@ -200,6 +202,8 @@ public async Task> GetGames(long steamId, string apiKey, DateTime created = DateTimeOffset.FromUnixTimeMilliseconds(jsonElement.GetProperty("timecreated").GetInt64()).LocalDateTime; DateTime lastLogOff = DateTimeOffset.FromUnixTimeMilliseconds(jsonElement.GetProperty("lastlogoff").GetInt64()).LocalDateTime; + notificationService.Send($"User data for {name} loaded."); + return new(name, imageUrl, profileUrl, created, lastLogOff, dateTimeProvider.Now); } catch (Exception ex) diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs new file mode 100644 index 0000000..38b2d55 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs @@ -0,0 +1,66 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; + +using BB84.SAU.Application.Services; +using BB84.SAU.Domain.Models; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.Services; + +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] +public sealed partial class SteamWebServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public async Task GetGameDetailsShouldReturnNullWhenExceptionGetsThrown() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())).Throws(); + + GameDetailModel? result = await service.GetGameDetails(APPID) + .ConfigureAwait(false); + + Assert.IsNull(result); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetGameDetailsShouldReturnNullWhenNotSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.NotFound)); + + GameDetailModel? result = await service.GetGameDetails(APPID) + .ConfigureAwait(false); + + Assert.IsNull(result); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetGameDetailsShouldReturnResultWhenSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.OK, GameDetailsResponse)); + + GameDetailModel? result = await service.GetGameDetails(APPID) + .ConfigureAwait(false); + + Assert.IsNotNull(result); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs new file mode 100644 index 0000000..aa79c79 --- /dev/null +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs @@ -0,0 +1,66 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; + +using BB84.SAU.Application.Services; +using BB84.SAU.Domain.Models; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace BB84.SAU.Application.Tests.Services; + +[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] +public sealed partial class SteamWebServiceTests +{ + [TestMethod] + [TestCategory("Methods")] + public async Task GetUserDataShouldReturnNullWhenExceptionGetsThrown() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())).Throws(); + + UserDataModel? result = await service.GetUserProfile(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsNull(result); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetUserDataShouldReturnNullWhenNotSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.NotFound)); + + UserDataModel? result = await service.GetUserProfile(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsNull(result); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(1, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task GetUserDataShouldReturnResultWhenSuccessful() + { + SteamWebService service = CreateMockedInstance(); + _httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(CreateMockedClient(HttpStatusCode.OK, UserDataResponse)); + + UserDataModel? result = await service.GetUserProfile(STEAMID, APIKEY) + .ConfigureAwait(false); + + Assert.IsNotNull(result); + Assert.AreEqual(1, _httpClientFactoryMock.Invocations.Count); + Assert.AreEqual(1, _httpMessageHandler.Invocations.Count); + Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); + Assert.AreEqual(1, _notificationServiceMock.Invocations.Count); + } +} diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs index 11b1c4b..ebcdde1 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs @@ -15,13 +15,15 @@ namespace BB84.SAU.Application.Tests.Services; [TestClass] public sealed partial class SteamWebServiceTests -{ - private const int APPID = int.MaxValue; +{ + private const int APPID = 1; private const string APIKEY = "UnitTestKey"; - private const long STEAMID = long.MaxValue; + private const long STEAMID = 1; private const string AchievmentsResponse = @"{""game"":{""gameName"":""MyTestGame"",""gameVersion"":""1"",""availableGameStats"":{""achievements"":[{""name"":""Achievement"",""defaultvalue"":0,""displayName"":""Achievement"",""hidden"":0,""description"":""Achievement"",""icon"":""https://www.unittest.dev/1.jpg"",""icongray"":""https://www.unittest.dev/2.jpg""}]}}}"; private const string GamesResponse = @"{""response"":{""games"":[{""appid"": 1,""name"":""UnitTest""}]}}"; - + private const string GameDetailsResponse = @"{""1"":{""data"":{""name"":""UnitTest"",""steam_appid"":1,""short_description"":""UnitTest"",""header_image"":""https://www.unittest.dev/1.jpg""}}}"; + private const string UserDataResponse = @"{""response"":{""players"":[{""steamid"":""1"",""personaname"":""UnitTest"",""profileurl"":""http://unittest.dev/profiles/1/"",""avatarfull"":""http://unittest.dev/1.jpg"",""lastlogoff"":1,""timecreated"":1}]}}"; + private Mock> _loggerServiceMock = new(); private Mock _notificationServiceMock = new(); private Mock _dateTimeProviderMock = new(); From b87f6843b02d03de306c9256d1856d20c9b208ec Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sat, 29 Jun 2024 11:12:26 +0200 Subject: [PATCH 12/14] chore: clean up --- Directory.Build.props | 8 +++----- .../BB84.SAU.Application.Tests/ApplicationTestBase.cs | 4 +--- .../BB84.SAU.Application.Tests.csproj | 6 +++++- .../Installer/DependencyInjectionTests.cs | 5 +---- .../Services/NavigationServiceTests.cs | 4 +--- .../Services/NotificationServiceTests.cs | 2 -- .../Services/SteamWebServiceTests.GetAchievments.cs | 5 +---- .../Services/SteamWebServiceTests.GetGameDetails.cs | 5 +---- .../Services/SteamWebServiceTests.GetGames.cs | 5 +---- .../Services/SteamWebServiceTests.GetUserData.cs | 5 +---- .../Services/SteamWebServiceTests.cs | 2 -- .../ViewModels/AboutViewModelTests.cs | 4 +--- .../ViewModels/GamesViewModelTests.LoadGamesCommand.cs | 10 ++++------ .../GamesViewModelTests.SelectGameCommand.cs | 2 -- .../ViewModels/GamesViewModelTests.cs | 3 +-- .../ViewModels/LogbookViewModelTests.cs | 7 +------ .../ViewModels/MainViewModelTests.cs | 2 -- .../ViewModels/SettingsViewModelTests.cs | 1 - .../ViewModels/UserDataViewModelTests.cs | 1 - .../BB84.SAU.Domain.Tests/BB84.SAU.Domain.Tests.csproj | 6 +++++- .../Exceptions/SteamSdkExceptionTests.cs | 2 -- .../Installer/DependencyInjectionTests.cs | 5 +---- tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs | 2 -- .../Models/AchievementModelTests.cs | 2 -- .../Models/GameDetailModelTests.cs | 2 -- tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs | 2 -- .../BB84.SAU.Domain.Tests/Models/LogbookModelTests.cs | 2 -- .../BB84.SAU.Domain.Tests/Models/UserDataModelTests.cs | 2 -- .../Settings/SteamSettingsTests.cs | 3 --- .../BB84.SAU.Infrastructure.Tests.csproj | 6 +++++- .../Installer/DependencyInjectionTests.cs | 5 +---- .../Services/SteamApiServiceTests.GetAchievement.cs | 6 +----- .../Services/SteamApiServiceTests.Initialize.cs | 6 +----- .../Services/SteamApiServiceTests.RequestStats.cs | 6 +----- .../Services/SteamApiServiceTests.ResetAchievement.cs | 6 +----- .../Services/SteamApiServiceTests.Shutdown.cs | 6 +----- .../Services/SteamApiServiceTests.StoreStats.cs | 6 +----- .../Services/SteamApiServiceTests.UnlockAchievement.cs | 6 +----- .../Services/SteamApiServiceTests.cs | 2 -- 39 files changed, 41 insertions(+), 123 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 42df74d..3700650 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -61,11 +61,9 @@ - - - - - + + + diff --git a/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs b/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs index 6e2822c..f3e9259 100644 --- a/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs +++ b/tests/BB84.SAU.Application.Tests/ApplicationTestBase.cs @@ -1,6 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace BB84.SAU.Application.Tests; +namespace BB84.SAU.Application.Tests; [TestClass] public abstract class ApplicationTestBase diff --git a/tests/BB84.SAU.Application.Tests/BB84.SAU.Application.Tests.csproj b/tests/BB84.SAU.Application.Tests/BB84.SAU.Application.Tests.csproj index 424ce91..19ccc55 100644 --- a/tests/BB84.SAU.Application.Tests/BB84.SAU.Application.Tests.csproj +++ b/tests/BB84.SAU.Application.Tests/BB84.SAU.Application.Tests.csproj @@ -3,7 +3,11 @@ - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/BB84.SAU.Application.Tests/Installer/DependencyInjectionTests.cs b/tests/BB84.SAU.Application.Tests/Installer/DependencyInjectionTests.cs index a69e066..43b1975 100644 --- a/tests/BB84.SAU.Application.Tests/Installer/DependencyInjectionTests.cs +++ b/tests/BB84.SAU.Application.Tests/Installer/DependencyInjectionTests.cs @@ -1,9 +1,6 @@ -using System.Diagnostics.CodeAnalysis; +using BB84.SAU.Application.Installer; using Microsoft.Extensions.DependencyInjection; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using BB84.SAU.Application.Installer; namespace BB84.SAU.Application.Tests.Installer; diff --git a/tests/BB84.SAU.Application.Tests/Services/NavigationServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/NavigationServiceTests.cs index c2b8137..96460c3 100644 --- a/tests/BB84.SAU.Application.Tests/Services/NavigationServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/NavigationServiceTests.cs @@ -1,6 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using BB84.SAU.Application.Services; +using BB84.SAU.Application.Services; using BB84.SAU.Application.ViewModels; namespace BB84.SAU.Application.Tests.Services; diff --git a/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs index 9099a39..b064e3c 100644 --- a/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Application.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Application.Tests.Services; [TestClass] diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs index f850f8d..28a1e92 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetAchievments.cs @@ -1,11 +1,8 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net; +using System.Net; using BB84.SAU.Application.Services; using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.Services; diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs index 38b2d55..0aa4b47 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGameDetails.cs @@ -1,11 +1,8 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net; +using System.Net; using BB84.SAU.Application.Services; using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.Services; diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs index 0276038..ab59887 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetGames.cs @@ -1,11 +1,8 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net; +using System.Net; using BB84.SAU.Application.Services; using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.Services; diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs index aa79c79..2703fd2 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.GetUserData.cs @@ -1,11 +1,8 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net; +using System.Net; using BB84.SAU.Application.Services; using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.Services; diff --git a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs index ebcdde1..fd2de32 100644 --- a/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/SteamWebServiceTests.cs @@ -6,8 +6,6 @@ using BB84.SAU.Application.Interfaces.Infrastructure.Services; using BB84.SAU.Application.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; using Moq.Protected; diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/AboutViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/AboutViewModelTests.cs index a9d9aa8..116f6c6 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/AboutViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/AboutViewModelTests.cs @@ -1,6 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using BB84.SAU.Application.ViewModels; +using BB84.SAU.Application.ViewModels; using BB84.SAU.Domain.Models; namespace BB84.SAU.Application.Tests.ViewModels; diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs index e716d06..7e719f3 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.LoadGamesCommand.cs @@ -2,8 +2,6 @@ using BB84.SAU.Domain.Models; using BB84.SAU.Domain.Settings; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.ViewModels; @@ -19,9 +17,9 @@ public async Task LoadGamesCommandShouldLoadGamesWhenLastUpdateIsSet() GameDetailModel game = new(appId, appTitle); List games = [new(appId, appTitle)]; GamesViewModel viewModel = CreateViewModelMock(); - _optionsMock.Setup(x => x.Value).Returns(SteamSettings); - _steamWebServiceMock.Setup(x => x.GetGames(SteamSettings.Id, SteamSettings.ApiKey, default)).ReturnsAsync(games); - _steamWebServiceMock.Setup(x => x.GetGameDetails(appId, default)).ReturnsAsync(game); + _ = _optionsMock.Setup(x => x.Value).Returns(SteamSettings); + _ = _steamWebServiceMock.Setup(x => x.GetGames(SteamSettings.Id, SteamSettings.ApiKey, default)).ReturnsAsync(games); + _ = _steamWebServiceMock.Setup(x => x.GetGameDetails(appId, default)).ReturnsAsync(game); viewModel.Model.LastUpdate = DateTime.MinValue; await viewModel.LoadGamesCommand.ExecuteAsync() @@ -34,7 +32,7 @@ await viewModel.LoadGamesCommand.ExecuteAsync() [TestCategory("Commands")] public async Task LoadGamesCommandShouldNotLoadGamesWhenLastUpdateIsNull() { - GamesViewModel viewModel = CreateViewModelMock(); + GamesViewModel viewModel = CreateViewModelMock(); viewModel.Model.LastUpdate = null; await viewModel.LoadGamesCommand.ExecuteAsync() diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs index ff0f332..8751a5f 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.SelectGameCommand.cs @@ -1,8 +1,6 @@ using BB84.SAU.Application.ViewModels; using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Application.Tests.ViewModels; public sealed partial class GamesViewModelTests diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs index 536bb80..9172bfd 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/GamesViewModelTests.cs @@ -6,7 +6,6 @@ using BB84.SAU.Domain.Settings; using Microsoft.Extensions.Options; -using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -45,7 +44,7 @@ private GamesViewModel CreateViewModelMock() _steamApiServiceMock = new(); _steamWebServiceMock = new(); _optionsMock = new(); - _optionsMock.Setup(x => x.Value).Returns(SteamSettings); + _ = _optionsMock.Setup(x => x.Value).Returns(SteamSettings); AchievementsViewModel achievementsViewModel = new(_steamApiServiceMock.Object, _steamWebServiceMock.Object, _optionsMock.Object, _dateTimeProviderMock.Object); diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs index 4483fd4..1f4af96 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/LogbookViewModelTests.cs @@ -1,11 +1,6 @@ -using BB84.SAU.Application.Interfaces.Application.Services; -using BB84.SAU.Application.Services; +using BB84.SAU.Application.Services; using BB84.SAU.Application.ViewModels; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using Moq; - namespace BB84.SAU.Application.Tests.ViewModels; [TestClass] diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs index 3e0816e..9babca0 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/MainViewModelTests.cs @@ -1,8 +1,6 @@ using BB84.SAU.Application.Interfaces.Application.Services; using BB84.SAU.Application.ViewModels; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Application.Tests.ViewModels; diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs index 0f90c29..bcffddc 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/SettingsViewModelTests.cs @@ -3,7 +3,6 @@ using BB84.SAU.Domain.Settings; using Microsoft.Extensions.Options; -using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; diff --git a/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs b/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs index 236f028..4553c33 100644 --- a/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs +++ b/tests/BB84.SAU.Application.Tests/ViewModels/UserDataViewModelTests.cs @@ -4,7 +4,6 @@ using BB84.SAU.Domain.Settings; using Microsoft.Extensions.Options; -using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; diff --git a/tests/BB84.SAU.Domain.Tests/BB84.SAU.Domain.Tests.csproj b/tests/BB84.SAU.Domain.Tests/BB84.SAU.Domain.Tests.csproj index f103728..6865ce8 100644 --- a/tests/BB84.SAU.Domain.Tests/BB84.SAU.Domain.Tests.csproj +++ b/tests/BB84.SAU.Domain.Tests/BB84.SAU.Domain.Tests.csproj @@ -3,7 +3,11 @@ - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs b/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs index 106050c..7459175 100644 --- a/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Exceptions/SteamSdkExceptionTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Exceptions; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Exceptions; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Installer/DependencyInjectionTests.cs b/tests/BB84.SAU.Domain.Tests/Installer/DependencyInjectionTests.cs index cfe5a9e..a62b162 100644 --- a/tests/BB84.SAU.Domain.Tests/Installer/DependencyInjectionTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Installer/DependencyInjectionTests.cs @@ -1,9 +1,6 @@ -using System.Diagnostics.CodeAnalysis; +using BB84.SAU.Domain.Installer; using Microsoft.Extensions.DependencyInjection; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using BB84.SAU.Domain.Installer; namespace BB84.SAU.Domain.Tests.Installer; diff --git a/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs index f06b9e3..2f7d38d 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/AboutModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs index 4de5c64..6178b03 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/AchievementModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs index 817bec0..c72991b 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/GameDetailModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs index d7aa479..1ca5e6f 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/GameModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass()] diff --git a/tests/BB84.SAU.Domain.Tests/Models/LogbookModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/LogbookModelTests.cs index a56f801..ffabb56 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/LogbookModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/LogbookModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Models/UserDataModelTests.cs b/tests/BB84.SAU.Domain.Tests/Models/UserDataModelTests.cs index ca2d4ab..ab90a99 100644 --- a/tests/BB84.SAU.Domain.Tests/Models/UserDataModelTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Models/UserDataModelTests.cs @@ -1,7 +1,5 @@ using BB84.SAU.Domain.Models; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Domain.Tests.Models; [TestClass] diff --git a/tests/BB84.SAU.Domain.Tests/Settings/SteamSettingsTests.cs b/tests/BB84.SAU.Domain.Tests/Settings/SteamSettingsTests.cs index 47f49b0..dbf8ddc 100644 --- a/tests/BB84.SAU.Domain.Tests/Settings/SteamSettingsTests.cs +++ b/tests/BB84.SAU.Domain.Tests/Settings/SteamSettingsTests.cs @@ -1,7 +1,4 @@ using BB84.Extensions; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - using BB84.SAU.Domain.Settings; namespace BB84.SAU.Domain.Tests.Settings; diff --git a/tests/BB84.SAU.Infrastructure.Tests/BB84.SAU.Infrastructure.Tests.csproj b/tests/BB84.SAU.Infrastructure.Tests/BB84.SAU.Infrastructure.Tests.csproj index 1895c31..62d286e 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/BB84.SAU.Infrastructure.Tests.csproj +++ b/tests/BB84.SAU.Infrastructure.Tests/BB84.SAU.Infrastructure.Tests.csproj @@ -3,7 +3,11 @@ - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/BB84.SAU.Infrastructure.Tests/Installer/DependencyInjectionTests.cs b/tests/BB84.SAU.Infrastructure.Tests/Installer/DependencyInjectionTests.cs index ca7170c..8b89943 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Installer/DependencyInjectionTests.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Installer/DependencyInjectionTests.cs @@ -1,13 +1,10 @@ -using System.Diagnostics.CodeAnalysis; +using BB84.SAU.Infrastructure.Installer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using BB84.SAU.Infrastructure.Installer; - namespace BB84.SAU.Infrastructure.Tests.Installer; [TestClass] diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs index 1cf2269..fc5746d 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs @@ -1,8 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Infrastructure.Services; - -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Infrastructure.Services; namespace BB84.SAU.Infrastructure.Tests.Services; diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs index 3517dd5..db9ab77 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Initialize.cs @@ -1,10 +1,6 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Domain.Exceptions; +using BB84.SAU.Domain.Exceptions; using BB84.SAU.Infrastructure.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Infrastructure.Tests.Services; [SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs index 48975fc..ada3f37 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.RequestStats.cs @@ -1,10 +1,6 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Domain.Exceptions; +using BB84.SAU.Domain.Exceptions; using BB84.SAU.Infrastructure.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; - namespace BB84.SAU.Infrastructure.Tests.Services; [SuppressMessage("Style", "IDE0058", Justification = "Not relevant here, unit testing.")] diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs index 4fd1f2a..5b49001 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.ResetAchievement.cs @@ -1,8 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Infrastructure.Services; - -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Infrastructure.Services; namespace BB84.SAU.Infrastructure.Tests.Services; diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs index f4a6594..90e135a 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.Shutdown.cs @@ -1,8 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Infrastructure.Services; - -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Infrastructure.Services; namespace BB84.SAU.Infrastructure.Tests.Services; diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs index 1b952e5..c2feb0c 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.StoreStats.cs @@ -1,8 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Infrastructure.Services; - -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Infrastructure.Services; namespace BB84.SAU.Infrastructure.Tests.Services; diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs index 88c7f80..94019f9 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.UnlockAchievement.cs @@ -1,8 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -using BB84.SAU.Infrastructure.Services; - -using Microsoft.VisualStudio.TestTools.UnitTesting; +using BB84.SAU.Infrastructure.Services; namespace BB84.SAU.Infrastructure.Tests.Services; diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs index e170c4a..e025728 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.cs @@ -3,8 +3,6 @@ using BB84.SAU.Infrastructure.Interfaces.Provider; using BB84.SAU.Infrastructure.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; - using Moq; namespace BB84.SAU.Infrastructure.Tests.Services; From df0e3779449d608abe5b2a13a2dca6e340ef65c1 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sat, 29 Jun 2024 17:12:46 +0200 Subject: [PATCH 13/14] feat: improve the overall unit test rate changes: - notification service unit tests added --- .../Services/NotificationService.cs | 5 ++++- .../Services/NotificationServiceTests.cs | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/BB84.SAU.Application/Services/NotificationService.cs b/src/BB84.SAU.Application/Services/NotificationService.cs index 1bc5820..6cb50d1 100644 --- a/src/BB84.SAU.Application/Services/NotificationService.cs +++ b/src/BB84.SAU.Application/Services/NotificationService.cs @@ -12,7 +12,10 @@ internal sealed class NotificationService : INotificationService public event NotificationEventHandler? NotificationReceived; public void Send(string message) - => NotificationReceived?.Invoke(this, message); + { + if (NotificationReceived is not null) + NotificationReceived.Invoke(this, message); + } public async Task SendAsync(string message) { diff --git a/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs index b064e3c..77c4ce0 100644 --- a/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs +++ b/tests/BB84.SAU.Application.Tests/Services/NotificationServiceTests.cs @@ -6,13 +6,28 @@ namespace BB84.SAU.Application.Tests.Services; public sealed class NotificationServiceTests { [TestMethod] - public void SendTest() + [TestCategory("Methods")] + public void SendShouldRaiseNotificationReceivedEventHandler() { bool notificationReceived = false; NotificationService service = new(); service.NotificationReceived += (s, e) => notificationReceived = true; - service.Send("Unit test message."); + service.Send(string.Empty); + + Assert.IsTrue(notificationReceived); + } + + [TestMethod] + [TestCategory("Methods")] + public async Task SendAsyncShouldNotRaiseAsyncNotificationReceivedEventHandler() + { + bool notificationReceived = false; + NotificationService service = new(); + service.AsyncNotificationReceived += (s, e) => Task.Run(() => notificationReceived = true); + + await service.SendAsync(string.Empty) + .ConfigureAwait(true); Assert.IsTrue(notificationReceived); } From 88289c734ab64c4787739975b0791a4b283691cd Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sat, 29 Jun 2024 17:35:02 +0200 Subject: [PATCH 14/14] fix: unit test unix time --- .../Services/SteamApiServiceTests.GetAchievement.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs index fc5746d..887080e 100644 --- a/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs +++ b/tests/BB84.SAU.Infrastructure.Tests/Services/SteamApiServiceTests.GetAchievement.cs @@ -46,7 +46,7 @@ public void GetAchievementShouldReturnFalseWhenAchievementNotUnlocked() [TestCategory("Methods")] public void GetAchievementShouldReturnValuesWhenSteamWorksProviderReturnedValues() { - uint unlockTime = 946681200; + uint unlockTime = 0; bool achieved = true; SteamApiService service = CreateMockedService(); _steamWorksProviderMock.Setup(x => x.Init()).Returns(true); @@ -58,7 +58,7 @@ public void GetAchievementShouldReturnValuesWhenSteamWorksProviderReturnedValues (bool Achieved, DateTime? UnlockTime) result = service.GetAchievement(AchievementName); Assert.IsTrue(result.Achieved); - Assert.AreEqual(new DateTime(2000, 1, 1), result.UnlockTime); + Assert.AreEqual(DateTimeOffset.FromUnixTimeSeconds(unlockTime).LocalDateTime, result.UnlockTime); Assert.AreEqual(0, _loggerServiceMock.Invocations.Count); Assert.AreEqual(0, _notificationServiceMock.Invocations.Count); Assert.AreEqual(3, _steamWorksProviderMock.Invocations.Count);