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

531 lines
20 KiB
C#
Raw Normal View History

2023-02-22 14:03:35 +01:00
using System;
2022-08-30 15:42:25 +02:00
using System.Threading.Tasks;
using MonkeyCache.FileStore;
2021-06-26 20:57:55 +02:00
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
using TINK.Repository.Exception;
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-06-26 20:57:55 +02:00
namespace TestTINKLib.Fixtures.ObjectTests.Connector
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestCopriProviderHttps
{
private const string BIKESAVAILABLE = @"{
2021-06-26 20:57:55 +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""
}
}
}";
2022-09-06 16:08:19 +02:00
private const string BIKESOCCUPIED = @"{
2021-06-26 20:57:55 +02:00
""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""
}
}
}";
2022-09-06 16:08:19 +02:00
private const string STATIONSALL = @"{
2021-06-26 20:57:55 +02:00
""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""
}";
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestGetBikesAvailable_NotExpired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesAvailableExpired.Returns(false);
cache.GetBikesAvailableAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "456",
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesAvailable();
Assert.AreEqual(1, bikes.Response.bikes.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesAvailable_ExpiredForceCache()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesAvailableExpired.Returns(true);
cache.GetBikesAvailableAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(1, bikes.Response.bikes.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesAvailable_Expired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesAvailableExpired.Returns(true);
https.GetBikesAvailableAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesAvailable();
Assert.AreEqual(1, bikes.Response.bikes.Count);
Assert.AreEqual(typeof(CopriCallsHttps).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesAvailable_Exception()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesAvailableExpired.Returns(true);
cache.GetBikesAvailableAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE)));
2023-03-08 13:18:54 +01:00
https.GetBikesAvailableAsync().Returns<BikesAvailableResponse>(x => { throw new WebConnectFailureException("Bang...", new System.Exception()); });
2022-09-06 16:08:19 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesAvailable();
Assert.AreEqual(1, bikes.Response.bikes.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.AreEqual("Bang...", bikes.Exception.Message);
}
[Test]
public async Task TestGetBikesOccupied_NotExpired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesOccupiedExpired.Returns(false);
cache.GetBikesOccupiedAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"12345678", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "f782a208d9399291ba8d086b5dcc2509",
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesOccupied();
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesOccupied_ExpiredForceCache()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesOccupiedExpired.Returns(true);
cache.GetBikesOccupiedAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"12345678", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "f782a208d9399291ba8d086b5dcc2509",
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesOccupied_Expired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesOccupiedExpired.Returns(true);
https.GetBikesOccupiedAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"12345678", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "f782a208d9399291ba8d086b5dcc2509",
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesOccupied();
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
Assert.AreEqual(typeof(CopriCallsHttps).Name, bikes.Source.Name);
Assert.IsNull(bikes.Exception);
}
[Test]
public async Task TestGetBikesOccupied_Exception()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsBikesOccupiedExpired.Returns(true);
cache.GetBikesOccupiedAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED)));
2023-03-08 13:18:54 +01:00
https.GetBikesOccupiedAsync().Returns<BikesReservedOccupiedResponse>(x => { throw new WebConnectFailureException("Bang...", new System.Exception()); });
2022-09-06 16:08:19 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"12345678", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "f782a208d9399291ba8d086b5dcc2509",
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var bikes = await provider.GetBikesOccupied();
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, bikes.Source.Name);
Assert.AreEqual("Bang...", bikes.Exception.Message);
}
[Test]
public async Task TestGetStations_NotExpired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsStationsExpired.Returns(false);
cache.GetStationsAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123", // Merchant id
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /*UI language */,
2023-02-22 14:03:35 +01:00
sessionCookie: "456", // cookie
2022-09-06 16:08:19 +02:00
cacheServer: cache,
httpsServer: https);
var stations = await provider.GetStations();
Assert.AreEqual(3, stations.Response.stations.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, stations.Source.Name);
Assert.IsNull(stations.Exception);
}
[Test]
public async Task TestGetStations_ExpiredForceCache()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsStationsExpired.Returns(true);
cache.GetStationsAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var stations = await provider.GetStations(true);
Assert.AreEqual(3, stations.Response.stations.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, stations.Source.Name);
Assert.IsNull(stations.Exception);
}
[Test]
public async Task TestGetStations_Expired()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsStationsExpired.Returns(true);
https.GetStationsAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL)));
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var stations = await provider.GetStations();
Assert.AreEqual(3, stations.Response.stations.Count);
Assert.AreEqual(typeof(CopriCallsHttps).Name, stations.Source.Name);
Assert.IsNull(stations.Exception);
}
[Test]
public async Task TestGetStations_Exception()
{
var cache = Substitute.For<ICopriCache>();
var https = Substitute.For<ICopriServer>();
cache.IsStationsExpired.Returns(true);
cache.GetStationsAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL)));
2023-03-08 13:18:54 +01:00
https.GetStationsAsync().Returns<StationsAvailableResponse>(x => { throw new WebConnectFailureException("Bang...", new System.Exception()); });
2022-09-06 16:08:19 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
cacheServer: cache,
httpsServer: https);
var stations = await provider.GetStations();
Assert.AreEqual(3, stations.Response.stations.Count);
Assert.AreEqual(typeof(CopriCallsMonkeyStore).Name, stations.Source.Name);
Assert.AreEqual("Bang...", stations.Exception.Message);
}
[Test]
public async Task Test_AddToCache_Stations()
{
if (string.IsNullOrEmpty(Barrel.ApplicationId))
{
Barrel.ApplicationId = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
}
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123456789",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */);
var stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
try
{
// Do not add if an excption occurred
2023-03-08 13:18:54 +01:00
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData(), new System.Exception("Bang...")));
2022-09-06 16:08:19 +02:00
stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
// Do not add if results from cache
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData()));
stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
// Add result
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData()));
stations = await provider.GetStations(true);
Assert.AreEqual(3, stations.Response.stations.Count);
}
finally
{
Barrel.Current.EmptyAll();
}
}
[Test]
public async Task Test_AddToCache_BikesAvailable()
{
if (string.IsNullOrEmpty(Barrel.ApplicationId))
{
Barrel.ApplicationId = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
}
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123456789",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */);
var bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
try
{
// Do not add if an excption occurred
2023-03-08 13:18:54 +01:00
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData(), new System.Exception("Bang...")));
2022-09-06 16:08:19 +02:00
bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
// Do not add if results from cache
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData()));
bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
// Add result
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData()));
bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(1, bikes.Response.bikes.Count);
}
finally
{
Barrel.Current.EmptyAll();
}
}
[Test]
public async Task Test_AddToCache_BikesOccupied()
{
if (string.IsNullOrEmpty(Barrel.ApplicationId))
{
Barrel.ApplicationId = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
}
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123456789",
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
null /* langugage */,
2023-02-22 14:03:35 +01:00
sessionCookie: "876");
2022-09-06 16:08:19 +02:00
var bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
try
{
// Do not add if an excption occurred
2023-03-08 13:18:54 +01:00
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData(), new System.Exception("Bang...")));
2022-09-06 16:08:19 +02:00
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
// Do not add if results from cache
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData()));
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
// Add result
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData()));
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
}
finally
{
Barrel.Current.EmptyAll();
}
}
}
2021-06-26 20:57:55 +02:00
}