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

365 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Bikes.BikeInfoNS;
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.State;
using ShareeBike.Model.Stations;
using ShareeBike.Model.Stations.StationNS;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.Geolocation;
using ShareeBike.ViewModel;
using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace SharedBusinessLogic.Tests
{
[TestFixture]
public class TestBikeCollectionMutable
{
private class BikeInfoMutable : ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable
{
public BikeInfoMutable(
string id,
LockModel lockType,
bool isDemo = false,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
AaRideType? aaRideType = null,
string description = null,
string stationId = null,
string stationName = null,
Uri operatorUri = null,
RentalDescription tariffDescription = null,
Func<DateTime> dateTimeProvider = null,
IStateInfo stateInfo = null) : base(
new Bike(id, lockType, wheelType, typeOfBike, aaRideType, description),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
isDemo,
group,
stationId,
stationName,
operatorUri,
tariffDescription,
dateTimeProvider,
stateInfo)
{
}
}
/// <summary> Tests the member.</summary>
[Test]
public void TestAdd()
{
var l_oColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Two, TypeOfBike.Allround));
Assert.Throws<Exception>(() => l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Cargo)));
}
[Test]
public void TestUpdate_Null()
{
var l_oBikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Allround);
l_oBikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
var l_oBikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Cargo),
l_oBikeRequested,
};
// Verify initial state
Assert.That(l_oBikeColl.GetById("63"), Is.Not.Null);
Assert.That(l_oBikeColl.GetById("57").State.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oBikeColl.GetById("20").State.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oBikeColl.GetById("33"), Is.Null);
l_oBikeColl.Update(null, null);
// Verify modified state
Assert.That(l_oBikeColl.GetById("63"), Is.Null);
Assert.That(l_oBikeColl.GetById("57"), Is.Null);
Assert.That(l_oBikeColl.GetById("20"), Is.Null);
Assert.That(l_oBikeColl.GetById("33"), Is.Null);
}
[Test]
public void TestUpdate()
{
var bikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Allround);
bikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Cargo),
bikeRequested,
};
// Verify initial state
Assert.That(bikeColl.GetById("63"), Is.Not.Null); // Will be removed
Assert.That(bikeColl.GetById("57").State.Value, Is.EqualTo(InUseStateEnum.Disposable)); // Will be requested
Assert.That(bikeColl.GetById("20").State.Value, Is.EqualTo(InUseStateEnum.Reserved)); // Will be booked
Assert.That(bikeColl.GetById("33"), Is.Null); //
var bikeResponse = new List<BikeInfo>
{
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("57", LockModel.ILockIt),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
DateTime.Now,
"john@long",
"7" /*station id*/,
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.LockInfo(),
null),
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("20", LockModel.ILockIt),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
DateTime.Now,
"john@long",
"7",
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.LockInfo(),
null),
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("33", LockModel.Sigo),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
"7",
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.LockInfo()),
};
bikeColl.Update(bikeResponse, new List<IStation>());
// Verify modified state
Assert.That(bikeColl.GetById("63"), Is.Null, "Bike not contained in response must not exist.");
Assert.That(bikeColl.GetById("57").State.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(bikeColl.GetById("20").State.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(bikeColl.GetById("33"), Is.Not.Null);
}
/// <summary>
/// Bike for which station name is updated is not contained in bike collection when calling update.
/// </summary>
[Test]
public void TestUpdate_StationName_NewBike()
{
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Null);
var l_oBikeResponse = new List<BikeInfo>
{
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("57", LockModel.Sigo),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
"7" /*station id*/,
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.LockInfo()),
};
var stations = new List<IStation>
{
new Station("7", new List<string>(), null, "My fancy station")
};
bikeColl.Update(l_oBikeResponse, stations);
// Verify modified state
Assert.That(bikeColl.GetById("57").StationId, Is.EqualTo("7"));
Assert.That(bikeColl.GetById("57").StationName, Is.EqualTo("My fancy station"));
}
[Test]
public void TestUpdate_StationName_UpdateExistingBike()
{
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "ShareeBike" }, WheelType.Trike, TypeOfBike.Cargo),
};
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Not.Null);
var bikeResponse = new List<BikeInfo>
{
new ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new Bike("57" /* bike id*/, LockModel.ILockIt, WheelType.Trike, TypeOfBike.Allround, AaRideType.NoAaRide, "Test description"),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
17, /* Lock id */
new Guid(),
new byte[0],
new byte[0],
new byte[0],
DateTime.Now,
"john@long,",
"7", /*station id*/
null, /*operator uri*/
null, /* tariff description */
false, /* is demo */
new List<string> {"ShareeBike" } /* group id */
),
};
var stations = new List<IStation>
{
new Station("7", new List<string>(), null, "My fancy station")
};
bikeColl.Update(bikeResponse, stations);
// Verify modified state
Assert.That(bikeColl.GetById("57").StationId, Is.Null, "Station id is only set when update creates BikeInfo object, not when updating object BikeInfo because station name does not change.");
Assert.That(bikeColl.GetById("57").StationName, Is.Null, "Station name is only set when update creates BikeInfo object, not when updating object BikeInfo because station name does not change.");
}
[Test]
public void TestUpdate_StationName_Nullstations()
{
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
var bikeResponse = new List<BikeInfo>
{
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("57", LockModel.Sigo),
new DriveMutable(),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
"7" /*station id*/,
new ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.LockInfo(),
operatorUri: null /*operator uri*/),
};
bikeColl.Update(bikeResponse, null);
// Verify modified state
Assert.That(bikeColl.GetById("57").StationId, Is.EqualTo("7"));
Assert.That(bikeColl.GetById("57").StationName, Is.EqualTo(""));
}
[Test]
public void TestCreateBluetoothBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<ShareeBike.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""Ilockit""
}"),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable)));
}
[Test]
public void TestCreateCopriBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<ShareeBike.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""SIGO""
}"),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(ShareeBike.Model.Bikes.BikeInfoNS.CopriLock.BikeInfoMutable)));
}
[Test]
public void TestCreateBCBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<ShareeBike.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""LOCK""
}"),
ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1"),
Is.Null);
}
}
}