sharee.bike-App/TestShareeLib/Model/Connector/TestCachedQuery.cs

170 lines
6.3 KiB
C#
Raw Normal View History

2023-04-05 15:02:10 +02:00
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
2023-04-05 15:02:10 +02:00
using NSubstitute;
2021-07-12 21:31:46 +02:00
using NUnit.Framework;
using TINK.Model.Connector;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
using TINK.Repository.Response;
2023-04-19 12:14:14 +02:00
using TINK.Repository.Response.Stations;
2022-01-04 18:54:03 +01:00
using TINK.Services.CopriApi;
2021-07-12 21:31:46 +02:00
namespace TestTINKLib.Fixtures.ObjectTests.Connector.Query
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestCachedQuery
{
private const string BIKESAVAILABLE = @"{
2021-07-12 21:31:46 +02:00
""copri_version"" : ""4.1.0.0"",
""bikes"" : {},
""response_state"" : ""OK"",
""apiserver"" : ""https://app.tink-konstanz.de"",
""authcookie"" : """",
""response"" : ""bikes_available"",
""bikes"" : {
""2352"" : {
""description"" : ""Cargo Long"",
""state"" : ""available"",
""bike"" : ""1"",
""gps"" : { ""latitude"": ""47.669888"", ""longitude"": ""9.167749"" },
""station"" : ""9""
}
}
}";
2023-05-09 08:47:52 +02:00
/// <summary>
/// Holds the response on stations_available request.
/// </summary>
/// <remarks> V1: Did not hold station_type entry.</remarks>
private const string STATIONSALLV2 = @"{
2021-07-12 21:31:46 +02:00
""copri_version"" : ""4.1.0.0"",
""stations"" : {
2023-05-09 08:47:52 +02:00
""9"" : {
""station"" : ""9"",
2021-07-12 21:31:46 +02:00
""bike_soll"" : ""0"",
""bike_ist"" : ""7"",
""station_group"" : [ ""TINK"" ],
""gps"" : { ""latitude"": ""47.66756"", ""longitude"": ""9.16477"" },
""state"" : ""available"",
2023-05-09 08:47:52 +02:00
""description"" : """",
""station_type"": {
""Cargobike"": {
""bike_count"": ""1"",
""bike_group"": ""TINK""
},
""Citybike"": {
""bike_count"": ""0"",
""bike_group"": ""FR300103""
}
}
2021-07-12 21:31:46 +02:00
},
""13"" : {
""station"" : ""13"",
""bike_soll"" : ""4"",
""bike_ist"" : ""1"",
""station_group"" : [ ""TINK"" ],
""gps"" : { ""latitude"": ""47.657756"", ""longitude"": ""9.176084"" },
""state"" : ""available"",
""description"" : """"
},
""30"" : {
""station"" : ""30"",
""bike_soll"" : ""5"",
""bike_ist"" : ""0"",
""station_group"" : [ ""TINK"", ""Konrad"" ],
""gps"" : { ""latitude"": ""47.657766"", ""longitude"": ""9.176094"" },
""state"" : ""available"",
""description"" : ""Test für Stadtradstation""
}
},
""user_group"" : [ ""Konrad"", ""TINK"" ],
""response_state"" : ""OK"",
""authcookie"" : ""6103_f782a208d9399291ba8d086b5dcc2509_12345678"",
""debuglevel"" : ""2"",
""response"" : ""stations_all"",
""user_id"" : ""javaminister@gmail.com"",
""apiserver"" : ""https://tinkwwp.copri-bike.de""
}";
2022-09-06 16:08:19 +02:00
private const string STATIONSALLEMPTY = @"{
2021-07-12 21:31:46 +02:00
""copri_version"" : ""4.1.0.0"",
""stations"" : {
},
""user_group"" : [ ""Konrad"", ""TINK"" ],
""response_state"" : ""OK"",
""authcookie"" : ""6103_f782a208d9399291ba8d086b5dcc2509_12345678"",
""debuglevel"" : ""2"",
""response"" : ""stations_all"",
""user_id"" : ""javaminister@gmail.com"",
""apiserver"" : ""https://tinkwwp.copri-bike.de""
}";
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestGetStations_StationsFromCache()
{
2023-04-05 15:02:10 +02:00
var server = Substitute.For<ICachedCopriServer>();
server.GetStations(false).Returns(Task.Run(() => new Result<StationsAvailableResponse>(
2022-09-06 16:08:19 +02:00
typeof(CopriCallsMonkeyStore),
2023-05-09 08:47:52 +02:00
JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALLV2),
2022-09-06 16:08:19 +02:00
new GeneralData(),
new System.Exception("Bang when getting stations..."))));
var result = await new CachedQuery(server).GetBikesAndStationsAsync();
Assert.AreEqual(3, result.Response.StationsAll.Count);
2023-05-09 08:47:52 +02:00
Assert.AreEqual(1, result.Response.StationsAll["9"].BikeGroups.AvailableCount);
Assert.AreEqual(0, result.Response.BikesOccupied.Count);
2022-09-06 16:08:19 +02:00
Assert.AreEqual(typeof(CopriCallsMonkeyStore), result.Source);
Assert.AreEqual("Bang when getting stations...", result.Exception.Message);
}
[Test]
public async Task TestGetStations()
{
2023-04-05 15:02:10 +02:00
var server = Substitute.For<ICachedCopriServer>();
2022-09-06 16:08:19 +02:00
2023-04-05 15:02:10 +02:00
server.GetStations(false).Returns(Task.Run(() => new Result<StationsAvailableResponse>(
2022-09-06 16:08:19 +02:00
typeof(CopriCallsHttps),
2023-05-09 08:47:52 +02:00
JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALLV2),
2022-09-06 16:08:19 +02:00
new GeneralData())));
var result = await new CachedQuery(server).GetBikesAndStationsAsync();
Assert.AreEqual(3, result.Response.StationsAll.Count);
2023-05-09 08:47:52 +02:00
Assert.AreEqual(1, result.Response.StationsAll["9"].BikeGroups.AvailableCount);
Assert.AreEqual(0, result.Response.BikesOccupied.Count, "There are no reserved or occupied bikes.");
2022-09-06 16:08:19 +02:00
Assert.AreEqual(typeof(CopriCallsHttps), result.Source);
Assert.IsNull(result.Exception);
}
[Test]
public async Task TestGetBikes()
{
2023-04-05 15:02:10 +02:00
var server = Substitute.For<ICachedCopriServer>();
2022-09-06 16:08:19 +02:00
2023-04-05 15:02:10 +02:00
server.GetBikesAvailable().Returns(Task.Run(() => new Result<BikesAvailableResponse>(
2022-09-06 16:08:19 +02:00
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
new GeneralData())));
var result = await new CachedQuery(server).GetBikesAsync();
Assert.AreEqual(1, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsHttps), result.Source);
Assert.IsNull(result.Exception);
}
[Test]
public async Task TestGetBikesOccupied()
{
2023-04-05 15:02:10 +02:00
var server = Substitute.For<ICachedCopriServer>();
2022-09-06 16:08:19 +02:00
var result = await new CachedQuery(server).GetBikesOccupiedAsync();
Assert.AreEqual(0, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore), result.Source);
Assert.AreEqual(result.Exception.Message, "Abfrage der reservierten/ gebuchten Räder nicht möglich. Kein Benutzer angemeldet.");
}
}
2021-07-12 21:31:46 +02:00
}