sharee.bike-App/TestShareeLib/Model/Connector/TestCachedQueryLoggedIn.cs
2023-05-09 08:47:52 +02:00

613 lines
22 KiB
C#

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using TINK.Model.Connector;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
using TINK.Repository.Response;
using TINK.Repository.Response.Stations;
using TINK.Services.CopriApi;
namespace TestShareeLib.Model.Connector
{
[TestFixture]
public class TestCachedQueryLoggedIn
{
/// <summary>
/// Verifies that available bikes are not considered to by kind of MyBikes.
/// </summary>
[Test]
public async Task TestGetBikesOccupiedAsyncMyBikesNone()
{
var server = Substitute.For<ICachedCopriServer>();
var bikesAvailable = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
}
},
""copri_version"": ""4.1.12.5"",
}");
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
var resultAvailable = new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps), // Marks that response was taken from cache
bikesAvailable,
new GeneralData());
var resultOccupied = new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
bikesOccupied,
new GeneralData());
server.GetBikesAvailable(false).Returns(Task.FromResult(resultAvailable));
server.GetBikesOccupied(false).Returns(Task.FromResult(resultOccupied));
var query = new CachedQueryLoggedIn(
server,
"SessionCookie",
"Mail",
() => new DateTime(2022, 7, 16));
var bikesResponse = await query.GetBikesOccupiedAsync();
Assert.That(
bikesResponse.Response.Count,
Is.EqualTo(0));
}
/// <summary>
/// Verifies that available bikes with Co2Saving entry set are considered to by kind of MyBikes.
/// </summary>
[Test]
public async Task TestGetBikesOccupiedAsyncMyBikesOneFeedbackPending()
{
var server = Substitute.For<ICachedCopriServer>();
var bikesAvailable = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
var resultAvailable = new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps),
bikesAvailable,
new GeneralData());
var resultOccupied = new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
bikesOccupied,
new GeneralData());
server.GetBikesAvailable(false).Returns(Task.FromResult(resultAvailable));
server.GetBikesOccupied(false).Returns(Task.FromResult(resultOccupied));
var query = new CachedQueryLoggedIn(
server,
"SessionCookie",
"Mail",
() => new DateTime(2022, 7, 16));
var bikesResponse = await query.GetBikesOccupiedAsync();
Assert.That(
bikesResponse.Response.Count,
Is.EqualTo(1));
}
/// <summary>
/// .
/// </summary>
[Test]
public async Task TestGetBikesOccupiedAsyncMyBikesOneOccupied()
{
var server = Substitute.For<ICachedCopriServer>();
var bikesAvailable = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": { },
""copri_version"": ""4.1.12.5"",
}");
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": {
""FR1005"": {
""lock_state"": ""occupied"",
""system"": ""Sigo"",
""state"": ""occupied"",
""bike"": ""FR1005"",
""station"": ""REN0"",
}
},
""copri_version"": ""4.1.12.5"",
}");
var resultAvailable = new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps),
bikesAvailable,
new GeneralData());
var resultOccupied = new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
bikesOccupied,
new GeneralData());
server.GetBikesAvailable(false).Returns(Task.FromResult(resultAvailable)); // Get bikes from copri
server.GetBikesOccupied(false).Returns(Task.FromResult(resultOccupied)); // Get bikes from cache
var query = new CachedQueryLoggedIn(
server,
"SessionCookie",
"Mail",
() => new DateTime(2022, 7, 16));
var bikesResponse = await query.GetBikesOccupiedAsync();
Assert.That(
bikesResponse.Response.Count,
Is.EqualTo(1));
}
/// <summary>
/// Scenario: Call of GetBikesAvailable fails data is taken from cache.
/// </summary>
[Test]
public async Task TestGetBikesOccupiedAsyncGetAvailableFailsMyBikeOneFeedbackPending()
{
var server = Substitute.For<ICachedCopriServer>();
var bikesAvailable = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
var resultAvailable = new Result<BikesAvailableResponse>(
typeof(CopriCallsMonkeyStore),
bikesAvailable,
new GeneralData());
var resultOccupied = new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsMonkeyStore),
bikesOccupied,
new GeneralData());
server.GetBikesAvailable(false).Returns(Task.FromResult(resultAvailable));
server.GetBikesOccupied(true).Returns(Task.FromResult(resultOccupied));
var query = new CachedQueryLoggedIn(
server,
"SessionCookie",
"Mail",
() => new DateTime(2022, 7, 16));
var bikesResponse = await query.GetBikesOccupiedAsync();
Assert.That(
bikesResponse.Response.Count,
Is.EqualTo(1));
}
/// <summary>
/// Scenario: Call of GetBikesAvailable succeeds but because call of GetBikesOccupied fails data is taken from cache.
/// </summary>
[Test]
public async Task TestGetBikesOccupiedAsyncGetGetBikesOccupiedFailsMyBikeFeedbackPending()
{
var server = Substitute.For<ICachedCopriServer>();
var bikesAvailableCopri = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": { },
""copri_version"": ""4.1.12.5"",
}");
var bikesAvailableCache = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
var resultAvailableCopri = new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps), // Marks that response was taken from copri
bikesAvailableCache,
new GeneralData());
var resultAvailableCache = new Result<BikesAvailableResponse>(
typeof(CopriCallsMonkeyStore), // Marks that response was taken from cache
bikesAvailableCache,
new GeneralData());
var resultOccupied = new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsMonkeyStore),
bikesOccupied,
new GeneralData());
server.GetBikesAvailable(false).Returns(Task.FromResult(resultAvailableCopri)); // Get bikes from copri
server.GetBikesOccupied(false).Returns(Task.FromResult(resultOccupied)); // Get bikes from copri fails
server.GetBikesAvailable(true).Returns(Task.FromResult(resultAvailableCache)); // Get bikes from cache
var query = new CachedQueryLoggedIn(
server,
"SessionCookie",
"Mail",
() => new DateTime(2022, 7, 16));
var bikesResponse = await query.GetBikesOccupiedAsync();
Assert.That(
bikesResponse.Response.Count,
Is.EqualTo(1));
}
private const string BIKESAVAILABLE = @"{
""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""
}
}
}";
private const string BIKESAVAILABLEEMPTY = @"{
""copri_version"" : ""4.1.0.0"",
""bikes"" : {},
""response_state"" : ""OK"",
""apiserver"" : ""https://app.tink-konstanz.de"",
""authcookie"" : """",
""response"" : ""bikes_available"",
""bikes"" : {
}
}";
private const string BIKESOCCUPIED = @"{
""authcookie"" : ""6103_f782a208d9399291ba8d086b5dcc2509_12345678"",
""debuglevel"" : ""2"",
""user_group"" : [ ""TINK"" ],
""user_id"" : ""javaminister@gmail.com"",
""response"" : ""user_bikes_occupied"",
""response_state"" : ""OK"",
""response_text"" : ""Die Liste der reservierten und gebuchten Fahrräder wurde erfolgreich geladen"",
""apiserver"" : ""https://tinkwwp.copri-bike.de"",
""bikes_occupied"" : {
""89004"" : {
""start_time"" : ""2018-01-27 17:33:00.989464+01"",
""station"" : ""9"",
""unit_price"" : ""2.00"",
""tariff_description"": {
""free_hours"" : ""0.5"",
""name"" : ""TINK Tarif"",
""max_eur_per_day"" : ""9.00""
},
""timeCode"" : ""2061"",
""description"" : ""Cargo Long"",
""bike"" : ""4"",
""total_price"" : ""20.00"",
""state"" : ""requested"",
""real_hours"" : ""66.05"",
""bike_group"" : [ ""TINK"" ],
""now_time"" : ""2018-01-30 11:36:45"",
""request_time"" : ""2018-01-27 17:33:00.989464+01"",
""computed_hours"" : ""10.0""
}
}
}";
private const string STATIONSALL = @"{
""copri_version"" : ""4.1.0.0"",
""stations"" : {
""5"" : {
""station"" : ""5"",
""bike_soll"" : ""0"",
""bike_ist"" : ""7"",
""station_group"" : [ ""TINK"" ],
""gps"" : { ""latitude"": ""47.66756"", ""longitude"": ""9.16477"" },
""state"" : ""available"",
""description"" : """"
},
""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""
}";
/// <summary>
/// Response updated (manually edited) combining JSON form responses <see cref="STATIONSALL"/> and <see cref="BIKESOCCUPIED"/>
/// </summary>
private const string STATIONSALLV2 = @"{
""copri_version"" : ""4.1.0.0"",
""stations"" : {
""5"" : {
""station"" : ""5"",
""bike_soll"" : ""0"",
""bike_ist"" : ""7"",
""station_group"" : [ ""TINK"" ],
""gps"" : { ""latitude"": ""47.66756"", ""longitude"": ""9.16477"" },
""state"" : ""available"",
""description"" : """"
},
""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""
}
},
""bikes_occupied"" : {
""89004"" : {
""start_time"" : ""2018-01-27 17:33:00.989464+01"",
""station"" : ""9"",
""unit_price"" : ""2.00"",
""tariff_description"": {
""free_hours"" : ""0.5"",
""name"" : ""TINK Tarif"",
""max_eur_per_day"" : ""9.00""
},
""timeCode"" : ""2061"",
""description"" : ""Cargo Long"",
""bike"" : ""4"",
""total_price"" : ""20.00"",
""state"" : ""requested"",
""real_hours"" : ""66.05"",
""bike_group"" : [ ""TINK"" ],
""now_time"" : ""2018-01-30 11:36:45"",
""request_time"" : ""2018-01-27 17:33:00.989464+01"",
""computed_hours"" : ""10.0""
}
},
""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""
}";
private const string STATIONSALLEMPTY = @"{
""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""
}";
[Test]
public async Task TestGetStations_StationsFromCache()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetStations(false).Returns(Task.Run(() => new Result<StationsAvailableResponse>(
typeof(CopriCallsMonkeyStore),
JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALLV2),
new GeneralData(),
new Exception("Bang when getting stations..."))));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesAndStationsAsync();
Assert.AreEqual(3, result.Response.StationsAll.Count);
Assert.AreEqual(1, result.Response.BikesOccupied.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore), result.Source);
Assert.AreEqual("Bang when getting stations...", result.Exception.Message);
}
[Test]
public async Task TestGetStations()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetStations(false).Returns(Task.Run(() => new Result<StationsAvailableResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALLV2),
new GeneralData())));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesAndStationsAsync();
Assert.AreEqual(3, result.Response.StationsAll.Count);
Assert.AreEqual(1, result.Response.BikesOccupied.Count);
Assert.AreEqual(typeof(CopriCallsHttps), result.Source);
Assert.IsNull(result.Exception);
}
[Test]
public async Task TestGetBikes_BikesAvailableFromCache()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetBikesAvailable().Returns(Task.Run(() => new Result<BikesAvailableResponse>(
typeof(CopriCallsMonkeyStore),
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
new GeneralData(),
new System.Exception("Bang, bikes avail..."))));
server.GetBikesOccupied(true).Returns(Task.Run(() => new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED),
new GeneralData())));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesAsync();
Assert.AreEqual(2, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore), result.Source);
Assert.AreEqual("Bang, bikes avail...", result.Exception.Message);
}
[Test]
public async Task TestGetBikes_BikesOccupiedFromCache()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetBikesAvailable(false).Returns(Task.Run(() => new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLEEMPTY),
new GeneralData())));
server.GetBikesOccupied(false).Returns(Task.Run(() => new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsMonkeyStore),
JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED),
new GeneralData(),
new System.Exception("Bang, error bikes occupied"))));
server.GetBikesAvailable(true).Returns(Task.Run(() => new Result<BikesAvailableResponse>(
typeof(CopriCallsMonkeyStore),
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
new GeneralData())));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesAsync();
Assert.AreEqual(2, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore), result.Source);
Assert.AreEqual("Bang, error bikes occupied", result.Exception.Message);
}
[Test]
public async Task TestGetBikes()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetBikesAvailable().Returns(Task.Run(() => new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
new GeneralData())));
server.GetBikesOccupied().Returns(Task.Run(() => new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED),
new GeneralData())));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesAsync();
Assert.AreEqual(2, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsHttps), result.Source);
Assert.IsNull(result.Exception);
}
[Test]
public async Task TestGetBikesOccupied()
{
var server = Substitute.For<ICachedCopriServer>();
server.GetBikesAvailable().Returns(Task.Run(() => new Result<BikesAvailableResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesAvailableResponse>("{}"),
new GeneralData())));
server.GetBikesOccupied().Returns(Task.Run(() => new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED),
new GeneralData())));
var result = await new CachedQueryLoggedIn(server, "123", "a@b", () => DateTime.Now).GetBikesOccupiedAsync();
Assert.AreEqual(1, result.Response.Count);
Assert.AreEqual(typeof(CopriCallsHttps), result.Source);
Assert.IsNull(result.Exception);
}
}
}