sharee.bike-App/TestShareeLib/Model/Bike/TestBikeCollectionMutable.cs

268 lines
11 KiB
C#
Raw Permalink Normal View History

2022-04-25 22:15:15 +02:00
using Newtonsoft.Json;
using NUnit.Framework;
2021-05-13 20:09:46 +02:00
using System;
using System.Collections.Generic;
using TINK.Model.Bike;
using TINK.Model.Bikes.Bike;
2022-04-25 22:15:15 +02:00
using TINK.Model.Connector;
2021-05-13 20:09:46 +02:00
using TINK.Model.State;
2022-01-04 18:59:16 +01:00
using TINK.Model.Station;
2021-05-13 20:09:46 +02:00
using BikeInfo = TINK.Model.Bike.BC.BikeInfo;
namespace TestTINKLib
{
[TestFixture]
public class TestBikeCollectionMutable
{
private class BikeInfoMutable : TINK.Model.Bike.BC.BikeInfoMutable
{
public BikeInfoMutable(
2021-06-26 20:57:55 +02:00
string id,
2022-04-25 22:15:15 +02:00
LockModel lockType,
2021-05-13 20:09:46 +02:00
bool isDemo = false,
IEnumerable<string> group = null,
WheelType? wheelType = null,
TypeOfBike? typeOfBike = null,
string description = null,
2022-01-04 18:59:16 +01:00
string stationId = null,
string stationName = null,
2021-05-13 20:09:46 +02:00
Uri operatorUri = null,
TariffDescription tariffDescription = null,
Func<DateTime> dateTimeProvider = null,
2022-04-25 22:15:15 +02:00
IStateInfo stateInfo = null) : base(id, lockType, isDemo, group, wheelType, typeOfBike, description, stationId, stationName, operatorUri, tariffDescription, dateTimeProvider, stateInfo)
2021-05-13 20:09:46 +02:00
{
}
}
/// <summary> Tests the member.</summary>
[Test]
public void TestAdd()
{
var l_oColl = new BikeCollectionMutable();
2022-04-25 22:15:15 +02:00
l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround));
2021-05-13 20:09:46 +02:00
2022-04-25 22:15:15 +02:00
Assert.Throws<Exception>(() => l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo)));
2021-05-13 20:09:46 +02:00
}
[Test]
public void TestUpdate_Null()
{
2022-04-25 22:15:15 +02:00
var l_oBikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
2021-05-13 20:09:46 +02:00
l_oBikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, "john@long", "1234"));
var l_oBikeColl = new BikeCollectionMutable
{
2022-04-25 22:15:15 +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),
2021-05-13 20:09:46 +02:00
l_oBikeRequested,
};
// Verify initial state
2021-06-26 20:57:55 +02:00
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"));
2021-05-13 20:09:46 +02:00
2022-01-04 18:59:16 +01:00
l_oBikeColl.Update(null, null );
2021-05-13 20:09:46 +02:00
// Verify modified state
2021-06-26 20:57:55 +02:00
Assert.Null(l_oBikeColl.GetById("63"));
Assert.Null(l_oBikeColl.GetById("57"));
Assert.Null(l_oBikeColl.GetById("20"));
Assert.Null(l_oBikeColl.GetById("33"));
2021-05-13 20:09:46 +02:00
}
[Test]
public void TestUpdate()
{
2022-04-25 22:15:15 +02:00
var bikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
2022-01-04 18:59:16 +01:00
bikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, "john@long", "1234"));
2021-05-13 20:09:46 +02:00
2022-01-04 18:59:16 +01:00
var bikeColl = new BikeCollectionMutable
2021-05-13 20:09:46 +02:00
{
2022-04-25 22:15:15 +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),
2022-01-04 18:59:16 +01:00
bikeRequested,
2021-05-13 20:09:46 +02:00
};
// Verify initial state
2022-01-04 18:59:16 +01:00
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")); //
2021-05-13 20:09:46 +02:00
2022-04-25 22:15:15 +02:00
var bikeResponse = new List<BikeInfo>
2021-05-13 20:09:46 +02:00
{
2022-04-25 22:15:15 +02:00
new TINK.Model.Bike.CopriLock.BikeInfo(
"57" /* bike id*/,
DateTime.Now,
"john@long",
"7" /*station id*/,
new TINK.Model.Bikes.Bike.CopriLock.LockInfo(),
null),
new TINK.Model.Bike.CopriLock.BikeInfo(
"20",
DateTime.Now,
"john@long",
"7",
new TINK.Model.Bikes.Bike.CopriLock.LockInfo(),
null),
new TINK.Model.Bike.CopriLock.BikeInfo(
"33",
"7",
new TINK.Model.Bikes.Bike.CopriLock.LockInfo()),
2021-05-13 20:09:46 +02:00
};
2022-01-04 18:59:16 +01:00
2022-04-25 22:15:15 +02:00
bikeColl.Update(bikeResponse, new List<IStation>());
2021-05-13 20:09:46 +02:00
// Verify modified state
2022-01-04 18:59:16 +01:00
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()
{
var bikeColl = new BikeCollectionMutable();
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Null);
var l_oBikeResponse = new List<BikeInfo>
{
2022-04-25 22:15:15 +02:00
new TINK.Model.Bike.CopriLock.BikeInfo(
"57" /* bike id*/,
"7" /*station id*/,
new TINK.Model.Bikes.Bike.CopriLock.LockInfo()),
2022-01-04 18:59:16 +01:00
};
var stations = new List<IStation>
{
new Station("7", new List<string>(), null, "My facy 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 facy station"));
}
[Test]
public void TestUpdate_StationName_UpdateExistingBike()
{
var bikeColl = new BikeCollectionMutable
{
2022-04-25 22:15:15 +02:00
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
2022-01-04 18:59:16 +01:00
};
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Not.Null);
var bikeResponse = new List<BikeInfo>
{
2022-04-25 22:15:15 +02:00
new BikeInfo("57" /* bike id*/, LockModel.ILockIt, false, new List<string> {"TINK" }, WheelType.Trike, TypeOfBike.Allround, "Test description", "7" /*station id*/, null /*operator uri*/, null, DateTime.Now, "john@long,", "1234"),
2022-01-04 18:59:16 +01:00
};
var stations = new List<IStation>
{
new Station("7", new List<string>(), null, "My facy 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();
2022-04-25 22:15:15 +02:00
var bikeResponse = new List<BikeInfo>
2022-01-04 18:59:16 +01:00
{
2022-04-25 22:15:15 +02:00
new TINK.Model.Bike.CopriLock.BikeInfo(
"57" /* bike id*/,
"7" /*station id*/,
new TINK.Model.Bikes.Bike.CopriLock.LockInfo(),
null /*operator uri*/),
2022-01-04 18:59:16 +01:00
};
2022-04-25 22:15:15 +02:00
bikeColl.Update(bikeResponse, null);
2022-01-04 18:59:16 +01:00
// Verify modified state
Assert.That(bikeColl.GetById("57").StationId, Is.EqualTo("7"));
Assert.That(bikeColl.GetById("57").StationName, Is.EqualTo(""));
2021-05-13 20:09:46 +02:00
}
2022-04-25 22:15:15 +02:00
[Test]
public void TestCreateBluetoothBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<TINK.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""Ilockit""
}"));
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(TINK.Model.Bike.BluetoothLock.BikeInfoMutable)));
}
[Test]
public void TestCreateCopriBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<TINK.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""SIGO""
}"));
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(TINK.Model.Bike.CopriLock.BikeInfoMutable)));
}
[Test]
public void TestCreateBCBike()
{
var bikeInfo = BikeInfoFactory.Create(JsonConvert.DeserializeObject<TINK.Repository.Response.BikeInfoAvailable>(
@"{
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9"",
""system"" : ""LOCK""
}"));
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1"),
Is.Null);
}
2021-05-13 20:09:46 +02:00
}
}