sharee.bike-App/TestShareeLib/Model/Bikes/BikeInfoNS/TestBikeCollectionMutable.cs

365 lines
12 KiB
C#
Raw Normal View History

2023-01-18 14:22:51 +01:00
using System;
2021-05-13 20:09:46 +02:00
using System.Collections.Generic;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
2023-08-31 12:20:06 +02:00
using NSubstitute;
2022-08-30 15:42:25 +02:00
using NUnit.Framework;
using TINK.Model.Bikes;
using TINK.Model.Bikes.BikeInfoNS;
using TINK.Model.Bikes.BikeInfoNS.BikeNS;
using TINK.Model.Bikes.BikeInfoNS.DriveNS;
2023-08-31 12:20:06 +02:00
using TINK.Model.Connector;
2022-08-30 15:42:25 +02:00
using TINK.Model.Connector.Updater;
2021-05-13 20:09:46 +02:00
using TINK.Model.State;
2023-04-19 12:14:14 +02:00
using TINK.Model.Stations;
using TINK.Model.Stations.StationNS;
2023-08-31 12:20:06 +02:00
using TINK.Services.BluetoothLock;
using TINK.Services.Geolocation;
using TINK.ViewModel;
2022-08-30 15:42:25 +02:00
using BikeInfo = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfo;
2021-05-13 20:09:46 +02:00
namespace TestTINKLib
{
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestBikeCollectionMutable
{
private class BikeInfoMutable : TINK.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable
{
public BikeInfoMutable(
string id,
LockModel lockType,
bool isDemo = false,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
2023-04-19 12:14:14 +02:00
AaRideType? aaRideType = null,
2022-09-06 16:08:19 +02:00
string description = null,
string stationId = null,
string stationName = null,
Uri operatorUri = null,
RentalDescription tariffDescription = null,
Func<DateTime> dateTimeProvider = null,
IStateInfo stateInfo = null) : base(
2023-04-19 12:14:14 +02:00
new Bike(id, lockType, wheelType, typeOfBike, aaRideType, description),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
isDemo,
group,
stationId,
stationName,
operatorUri,
tariffDescription,
dateTimeProvider,
stateInfo)
{
}
}
/// <summary> Tests the member.</summary>
[Test]
public void TestAdd()
{
2023-08-31 12:20:06 +02:00
var l_oColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
2022-09-06 16:08:19 +02:00
l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround));
Assert.Throws<Exception>(() => l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo)));
}
[Test]
public void TestUpdate_Null()
{
var l_oBikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
2023-06-06 12:00:24 +02:00
l_oBikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
var l_oBikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
2022-09-06 16:08:19 +02:00
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
l_oBikeRequested,
};
// Verify initial state
Assert.NotNull(l_oBikeColl.GetById("63"));
Assert.AreEqual(InUseStateEnum.Disposable, l_oBikeColl.GetById("57").State.Value);
Assert.AreEqual(InUseStateEnum.Reserved, l_oBikeColl.GetById("20").State.Value);
Assert.Null(l_oBikeColl.GetById("33"));
l_oBikeColl.Update(null, null);
// Verify modified state
Assert.Null(l_oBikeColl.GetById("63"));
Assert.Null(l_oBikeColl.GetById("57"));
Assert.Null(l_oBikeColl.GetById("20"));
Assert.Null(l_oBikeColl.GetById("33"));
}
[Test]
public void TestUpdate()
{
var bikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
2023-06-06 12:00:24 +02:00
bikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
2022-09-06 16:08:19 +02:00
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
bikeRequested,
};
// Verify initial state
Assert.NotNull(bikeColl.GetById("63")); // Will be removed
Assert.AreEqual(InUseStateEnum.Disposable, bikeColl.GetById("57").State.Value); // Will be requested
Assert.AreEqual(InUseStateEnum.Reserved, bikeColl.GetById("20").State.Value); // Will be booked
Assert.Null(bikeColl.GetById("33")); //
var bikeResponse = new List<BikeInfo>
{
new TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("57", LockModel.ILockIt),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
DateTime.Now,
"john@long",
"7" /*station id*/,
new TINK.Model.Bikes.BikeInfoNS.CopriLock.LockInfo(),
null),
new TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
new Bike("20", LockModel.ILockIt),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
DateTime.Now,
"john@long",
"7",
new TINK.Model.Bikes.BikeInfoNS.CopriLock.LockInfo(),
null),
new TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
2023-01-18 14:22:51 +01:00
new Bike("33", LockModel.Sigo),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
"7",
new TINK.Model.Bikes.BikeInfoNS.CopriLock.LockInfo()),
};
bikeColl.Update(bikeResponse, new List<IStation>());
// Verify modified state
Assert.Null(bikeColl.GetById("63"), "Bike not contained in response must not exist.");
Assert.AreEqual(InUseStateEnum.Booked, bikeColl.GetById("57").State.Value);
Assert.AreEqual(InUseStateEnum.Booked, bikeColl.GetById("20").State.Value);
Assert.NotNull(bikeColl.GetById("33"));
}
/// <summary>
/// Bike for which station name is updated is not contained in bike collection when calling update.
/// </summary>
[Test]
public void TestUpdate_StationName_NewBike()
{
2023-08-31 12:20:06 +02:00
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
2022-09-06 16:08:19 +02:00
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Null);
var l_oBikeResponse = new List<BikeInfo>
{
new TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
2023-01-18 14:22:51 +01:00
new Bike("57", LockModel.Sigo),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
"7" /*station id*/,
new TINK.Model.Bikes.BikeInfoNS.CopriLock.LockInfo()),
};
var stations = new List<IStation>
{
2023-04-19 12:14:14 +02:00
new Station("7", new List<string>(), null, "My fancy station")
2022-09-06 16:08:19 +02:00
};
bikeColl.Update(l_oBikeResponse, stations);
// Verify modified state
Assert.That(bikeColl.GetById("57").StationId, Is.EqualTo("7"));
2023-04-19 12:14:14 +02:00
Assert.That(bikeColl.GetById("57").StationName, Is.EqualTo("My fancy station"));
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestUpdate_StationName_UpdateExistingBike()
{
2023-08-31 12:20:06 +02:00
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
2022-09-06 16:08:19 +02:00
{
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
};
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Not.Null);
var bikeResponse = new List<BikeInfo>
{
2023-04-19 12:14:14 +02:00
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new Bike("57" /* bike id*/, LockModel.ILockIt, WheelType.Trike, TypeOfBike.Allround, AaRideType.NoAaRide, "Test description"),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-04-19 12:14:14 +02:00
TINK.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> {"TINK" } /* group id */
),
2022-09-06 16:08:19 +02:00
};
var stations = new List<IStation>
{
2023-04-19 12:14:14 +02:00
new Station("7", new List<string>(), null, "My fancy station")
2022-09-06 16:08:19 +02:00
};
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()
{
2023-08-31 12:20:06 +02:00
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
2022-09-06 16:08:19 +02:00
var bikeResponse = new List<BikeInfo>
{
new TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfo(
2023-01-18 14:22:51 +01:00
new Bike("57", LockModel.Sigo),
2023-08-31 12:31:38 +02:00
new DriveMutable(),
2023-01-18 14:22:51 +01:00
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri,
2022-09-06 16:08:19 +02:00
"7" /*station id*/,
new TINK.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<TINK.Repository.Response.BikeInfoAvailable>(
@"{
2022-04-25 22:15:15 +02:00
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""Ilockit""
2023-01-18 14:22:51 +01:00
}"),
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
Assert.That(
2023-08-31 12:20:06 +02:00
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
2022-09-06 16:08:19 +02:00
Is.EqualTo(typeof(TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable)));
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public void TestCreateCopriBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<TINK.Repository.Response.BikeInfoAvailable>(
@"{
2022-04-25 22:15:15 +02:00
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""SIGO""
2023-01-18 14:22:51 +01:00
}"),
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
Assert.That(
2023-08-31 12:20:06 +02:00
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
2022-09-06 16:08:19 +02:00
Is.EqualTo(typeof(TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfoMutable)));
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public void TestCreateBCBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<TINK.Repository.Response.BikeInfoAvailable>(
@"{
2022-04-25 22:15:15 +02:00
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""LOCK""
2023-01-18 14:22:51 +01:00
}"),
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
Assert.That(
2023-08-31 12:20:06 +02:00
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1"),
2022-09-06 16:08:19 +02:00
Is.Null);
}
}
2021-05-13 20:09:46 +02:00
}