sharee.bike-App/SharedBusinessLogic.Tests/Model/TestShareeBikeAppSelectStation.cs
2024-04-09 12:53:23 +02:00

112 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using Plugin.BLE.Abstractions.Contracts;
using SharedBusinessLogic.Tests.Framework.Model.Device;
using SharedBusinessLogic.Tests.Framework.Model.Services.Geolocation;
using SharedBusinessLogic.Tests.Framework.Model.User.Account;
using SharedBusinessLogic.Tests.Framework.Services.BluetoothLock;
using ShareeBike.Model;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Settings;
using ShareeBike.Model.Stations.StationNS;
using ShareeBike.Model.User;
using ShareeBike.Repository;
using ShareeBike.Services;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.Geolocation;
using ShareeBike.Services.Permissions;
using ShareeBike.ViewModel;
using ShareeBike.ViewModel.Settings;
using static ShareeBike.Repository.CopriCallsMemory;
namespace SharedBusinessLogic.Tests.Fixtures.UseCases.SelectStation
{
[TestFixture]
public class TestShareeBikeApp
{
/// <summary>
/// Get all bikes at a given station from copri.
/// </summary>
private static async Task<BikeCollectionMutable> GetBikesAtStation(
User user,
ShareeBike.Model.Connector.IConnector connector,
IEnumerable<IStation> stations,
string selectedStationId)
{
var l_oBikesAtStation = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
var l_oBikesAvailable = (await connector.Query.GetBikesAsync()).Response;
l_oBikesAtStation.Update(l_oBikesAvailable.GetAtStation(selectedStationId), stations);
return l_oBikesAtStation;
}
[Test]
public void TestBikesAtStation_AccountStoreMock_NoUser_CopriMock_Set2()
{
const string MERCH_ID = "MyMerchId";
var l_oConnector = new ConnectorCache(
new AppContextInfo(MERCH_ID, "MyApp", new Version(1, 2)),
null /*UI language */,
string.Empty,
string.Empty,
server: new CopriCallsMemory(MERCH_ID, SampleSets.Set2, 1));
var l_oShareeBikeApp = new ShareeBikeApp(
new Settings(
new ShareeBike.ViewModel.Map.GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
new StartupSettings(),
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
new StoreMock(),
isConnectedFunc: () => true,
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => new ConnectorCache(new AppContextInfo(MERCH_ID, "MyApp", new Version(1, 2)), null /*UI language */, sessionCookie, mail, server: new CopriCallsMemory(MERCH_ID, SampleSets.Set2, 1)),
merchantId: MERCH_ID,
bluetoothService: Substitute.For<IBluetoothLE>(),
locationPermissionsService: Substitute.For<ILocationPermission>(),
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null,
theme: null,
currentVersion: new Version(3, 2, 0, 115),
lastVersion: new Version(3, 0, 173)); // Current app version. Must be larger or equal 3.0.173 to
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.Count, Is.EqualTo(0));
l_oShareeBikeApp.SelectedStation = new ShareeBike.Model.Stations.StationNS.Station("5", new List<string>(), null);
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.Count, Is.EqualTo(3));
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.GetById("25").Id, Is.EqualTo("25"));
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.GetById("11").Id, Is.EqualTo("11"));
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.GetById("2").Id, Is.EqualTo("2"));
l_oShareeBikeApp.SelectedStation = new ShareeBike.Model.Stations.StationNS.Station("10", new List<string>(), null);
Assert.That(
GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.Count, Is.EqualTo(1));
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.GetById("18").Id, Is.EqualTo("18"));
l_oShareeBikeApp.SelectedStation = new ShareeBike.Model.Stations.StationNS.Station("91345", new List<string>(), null);
Assert.That(GetBikesAtStation(l_oShareeBikeApp.ActiveUser, l_oConnector, l_oShareeBikeApp.Stations, l_oShareeBikeApp.SelectedStation.Id).Result.Count, Is.EqualTo(0));
}
}
}