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

515 lines
22 KiB
C#
Raw Normal View History

2021-06-26 20:57:55 +02:00
using MonkeyCache.FileStore;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
using TINK.Repository.Exception;
using TINK.Repository.Response;
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
{
[TestFixture]
public class TestCopriProviderHttps
{
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 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""
}";
[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
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"456", // cookie
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",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +02:00
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",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +02:00
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)));
https.GetBikesAvailableAsync().Returns<BikesAvailableResponse>(x => { throw new WebConnectFailureException("Bang...", new Exception()); });
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +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.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
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"f782a208d9399291ba8d086b5dcc2509", // Auth cookie
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
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"f782a208d9399291ba8d086b5dcc2509", // Auth cookie
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
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"f782a208d9399291ba8d086b5dcc2509", // Auth cookie
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)));
https.GetBikesOccupiedAsync().Returns<BikesReservedOccupiedResponse>(x => { throw new WebConnectFailureException("Bang...", new Exception()); });
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"12345678", // Merchant id
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"f782a208d9399291ba8d086b5dcc2509", // Auth cookie
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)));
2021-06-26 20:57:55 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123", // Merchant id
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"456", // cookie
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)));
2021-06-26 20:57:55 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +02:00
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)));
2021-06-26 20:57:55 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +02:00
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)));
https.GetStationsAsync().Returns<StationsAvailableResponse>(x => { throw new WebConnectFailureException("Bang...", new Exception()); });
2021-06-26 20:57:55 +02:00
var provider = new CopriProviderHttps(
new Uri("http://1.2.3.4"),
"123",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
2021-06-26 20:57:55 +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.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",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)));
2021-06-26 20:57:55 +02:00
var stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
try
{
// Do not add if an excption occurred
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData(), new Exception("Bang...")));
2021-06-26 20:57:55 +02:00
stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
// Do not add if results from cache
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData()));
2021-06-26 20:57:55 +02:00
stations = await provider.GetStations(true);
Assert.AreEqual(0, stations.Response.stations.Count);
// Add result
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData()));
2021-06-26 20:57:55 +02:00
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",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)));
2021-06-26 20:57:55 +02:00
var bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
try
{
// Do not add if an excption occurred
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData(), new Exception("Bang...")));
2021-06-26 20:57:55 +02:00
bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
// Do not add if results from cache
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData()));
2021-06-26 20:57:55 +02:00
bikes = await provider.GetBikesAvailable(true);
Assert.AreEqual(0, bikes.Response.bikes.Count);
// Add result
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new GeneralData()));
2021-06-26 20:57:55 +02:00
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",
2022-01-04 18:54:03 +01:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)), // User agent
2021-06-26 20:57:55 +02:00
"876");
var bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
try
{
// Do not add if an excption occurred
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData(), new Exception("Bang...")));
2021-06-26 20:57:55 +02:00
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
// Do not add if results from cache
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData()));
2021-06-26 20:57:55 +02:00
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(0, bikes.Response.bikes_occupied.Count);
// Add result
2022-01-04 18:54:03 +01:00
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData()));
2021-06-26 20:57:55 +02:00
bikes = await provider.GetBikesOccupied(true);
Assert.AreEqual(1, bikes.Response.bikes_occupied.Count);
}
finally
{
Barrel.Current.EmptyAll();
}
}
}
}