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;
|
2024-04-09 12:53:23 +02:00
|
|
|
using NSubstitute.ExceptionExtensions;
|
2021-06-26 20:57:55 +02:00
|
|
|
using NUnit.Framework;
|
2024-04-09 12:53:23 +02:00
|
|
|
using ShareeBike.Model.Services.CopriApi;
|
|
|
|
using ShareeBike.Repository;
|
|
|
|
using ShareeBike.Repository.Exception;
|
|
|
|
using ShareeBike.Repository.Response;
|
|
|
|
using ShareeBike.Repository.Response.Stations;
|
|
|
|
using ShareeBike.Services.CopriApi;
|
|
|
|
|
|
|
|
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Connector
|
2021-06-26 20:57:55 +02:00
|
|
|
{
|
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"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""user_group"" : [ ""ShareeBike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""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"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""name"" : ""ShareeBike Tarif"",
|
2021-06-26 20:57:55 +02:00
|
|
|
""max_eur_per_day"" : ""9.00""
|
|
|
|
},
|
|
|
|
""timeCode"" : ""2061"",
|
|
|
|
""description"" : ""Cargo Long"",
|
|
|
|
""bike"" : ""4"",
|
|
|
|
""total_price"" : ""20.00"",
|
|
|
|
""state"" : ""requested"",
|
|
|
|
""real_hours"" : ""66.05"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""bike_group"" : [ ""ShareeBike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""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"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""station_group"" : [ ""ShareeBike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""gps"" : { ""latitude"": ""47.66756"", ""longitude"": ""9.16477"" },
|
|
|
|
""state"" : ""available"",
|
|
|
|
""description"" : """"
|
|
|
|
},
|
|
|
|
""13"" : {
|
|
|
|
""station"" : ""13"",
|
|
|
|
""bike_soll"" : ""4"",
|
|
|
|
""bike_ist"" : ""1"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""station_group"" : [ ""ShareeBike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""gps"" : { ""latitude"": ""47.657756"", ""longitude"": ""9.176084"" },
|
|
|
|
""state"" : ""available"",
|
|
|
|
""description"" : """"
|
|
|
|
},
|
|
|
|
""30"" : {
|
|
|
|
""station"" : ""30"",
|
|
|
|
""bike_soll"" : ""5"",
|
|
|
|
""bike_ist"" : ""0"",
|
2024-04-09 12:53:23 +02:00
|
|
|
""station_group"" : [ ""ShareeBike"", ""Citybike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""gps"" : { ""latitude"": ""47.657766"", ""longitude"": ""9.176094"" },
|
|
|
|
""state"" : ""available"",
|
|
|
|
""description"" : ""Test für Stadtradstation""
|
|
|
|
}
|
|
|
|
},
|
2024-04-09 12:53:23 +02:00
|
|
|
""bikes_occupied"" : {},
|
|
|
|
""user_group"" : [ ""Citybike"", ""ShareeBike"" ],
|
2021-06-26 20:57:55 +02:00
|
|
|
""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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsHttps).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception.Message, Is.EqualTo("Bang..."));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsHttps).Name));
|
|
|
|
Assert.That(bikes.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(1));
|
|
|
|
Assert.That(bikes.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(bikes.Exception.Message, Is.EqualTo("Bang..."));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(3));
|
|
|
|
Assert.That(stations.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(stations.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(3));
|
|
|
|
Assert.That(stations.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(stations.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(3));
|
|
|
|
Assert.That(stations.Source.Name, Is.EqualTo(typeof(CopriCallsHttps).Name));
|
|
|
|
Assert.That(stations.Exception, Is.Null);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(3));
|
|
|
|
Assert.That(stations.Source.Name, Is.EqualTo(typeof(CopriCallsMonkeyStore).Name));
|
|
|
|
Assert.That(stations.Exception.Message, Is.EqualTo("Bang..."));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task Test_AddToCache_Stations()
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(Barrel.ApplicationId))
|
|
|
|
{
|
|
|
|
Barrel.ApplicationId = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
|
|
|
}
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
Barrel.Current.EmptyAll();
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-11-21 15:26:57 +01:00
|
|
|
// Do not add if an exception 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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// 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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// Add result
|
|
|
|
provider.AddToCache(new Result<StationsAvailableResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new GeneralData()));
|
|
|
|
stations = await provider.GetStations(true);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(stations.Response.stations.Count, Is.EqualTo(3));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Barrel.Current.EmptyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:26:57 +01:00
|
|
|
[Test]
|
2024-04-09 12:53:23 +02:00
|
|
|
public void Test_AddToCache()
|
2023-11-21 15:26:57 +01:00
|
|
|
{
|
|
|
|
var cache = Substitute.For<ICopriCache>();
|
|
|
|
|
|
|
|
var provider = new CopriProviderHttps(
|
|
|
|
new Uri("https://copri.net"),
|
|
|
|
"MerchId",
|
|
|
|
new AppContextInfo("MerchId", "Name", new Version()),
|
|
|
|
"de-de",
|
|
|
|
cacheServer: cache);
|
|
|
|
|
|
|
|
var uri = new Uri("https://Myserver.com");
|
|
|
|
var station = "MyStation";
|
|
|
|
var bike = "MyBike";
|
|
|
|
|
|
|
|
var result = new Result<BikesAvailableResponse>(typeof(CopriCallsHttps),
|
|
|
|
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
|
|
|
|
new GeneralData());
|
|
|
|
|
|
|
|
provider.AddToCache(result, uri, station, bike);
|
|
|
|
|
|
|
|
cache.Received().AddToCache(result.Response, uri, station, bike);
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
[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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-11-21 15:26:57 +01:00
|
|
|
// Do not add if an exception occurred
|
|
|
|
provider.AddToCache(
|
|
|
|
new Result<BikesAvailableResponse>(typeof(CopriCallsHttps),
|
|
|
|
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
|
|
|
|
new GeneralData(),
|
2024-04-09 12:53:23 +02:00
|
|
|
new System.Exception("Bang...")),
|
|
|
|
stationId: "9");
|
|
|
|
bikes = await provider.GetBikesAvailable(true, stationId: "9");
|
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// Do not add if results from cache
|
2023-11-21 15:26:57 +01:00
|
|
|
provider.AddToCache(new Result<BikesAvailableResponse>(
|
|
|
|
typeof(CopriCallsMonkeyStore),
|
|
|
|
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
|
2024-04-09 12:53:23 +02:00
|
|
|
new GeneralData()),
|
|
|
|
stationId: "9");
|
|
|
|
bikes = await provider.GetBikesAvailable(true, stationId: "9");
|
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// Add result
|
2023-11-21 15:26:57 +01:00
|
|
|
provider.AddToCache(
|
|
|
|
new Result<BikesAvailableResponse>(typeof(CopriCallsHttps),
|
|
|
|
JsonConvert.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE),
|
2024-04-09 12:53:23 +02:00
|
|
|
new GeneralData()),
|
|
|
|
stationId: "9");
|
2023-11-21 15:26:57 +01:00
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
bikes = await provider.GetBikesAvailable(true, stationId: "9");
|
|
|
|
Assert.That(bikes.Response.bikes.Count, Is.EqualTo(1));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
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
|
2024-04-09 12:53:23 +02:00
|
|
|
null /* language */,
|
2023-02-22 14:03:35 +01:00
|
|
|
sessionCookie: "876");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
var bikes = await provider.GetBikesOccupied(true);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
// Do not add if an exception 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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// 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);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
// Add result
|
|
|
|
provider.AddToCache(new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsHttps), JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new GeneralData()));
|
|
|
|
bikes = await provider.GetBikesOccupied(true);
|
2024-04-09 12:53:23 +02:00
|
|
|
Assert.That(bikes.Response.bikes_occupied.Count, Is.EqualTo(1));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Barrel.Current.EmptyAll();
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 12:53:23 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verify that cache updating functionality is called correctly.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public async Task TestDoReserveAsync()
|
|
|
|
{
|
|
|
|
var cache = Substitute.For<ICopriCache>();
|
|
|
|
var https = Substitute.For<ICopriServer>();
|
|
|
|
|
|
|
|
var provider = new CopriProviderHttps(
|
|
|
|
new Uri("https://copri.net"),
|
|
|
|
"MerchId",
|
|
|
|
new AppContextInfo("MerchId", "Name", new Version()),
|
|
|
|
"de-de",
|
|
|
|
cacheServer: cache,
|
|
|
|
httpsServer: https);
|
|
|
|
|
|
|
|
var uri = new Uri("https://Myserver.com");
|
|
|
|
const string bikeId = "BikeNr12";
|
|
|
|
|
|
|
|
https.DoReserveAsync(bikeId, uri).Returns(Task.FromResult(JsonConvert.DeserializeObject<ReservationBookingResponse>(@"
|
|
|
|
{
|
|
|
|
bike : ""BikeNr12"",
|
|
|
|
""bikes_occupied"" : {
|
|
|
|
""BikeNr3"" : {
|
|
|
|
""bike"" : ""BikeNr3"",
|
|
|
|
""station"" : ""Freiburg City""
|
|
|
|
},
|
|
|
|
""BikeNr12"" : {
|
|
|
|
""bike"" : ""BikeNr12"",
|
|
|
|
""station"" : ""HomeSweetHome""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
""response_state"" : ""OK""
|
|
|
|
}")));
|
|
|
|
|
|
|
|
await provider.DoReserveAsync(bikeId, uri);
|
|
|
|
|
|
|
|
cache.Received().Update(Arg.Is<BikeInfoReservedOrBooked>(x => x.station == "HomeSweetHome"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verify that cache updating functionality is not called in case of an exception being thrown.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestDoReserveAsyncException()
|
|
|
|
{
|
|
|
|
var cache = Substitute.For<ICopriCache>();
|
|
|
|
var https = Substitute.For<ICopriServer>();
|
|
|
|
|
|
|
|
var provider = new CopriProviderHttps(
|
|
|
|
new Uri("https://copri.net"),
|
|
|
|
"MerchId",
|
|
|
|
new AppContextInfo("MerchId", "Name", new Version()),
|
|
|
|
"de-de",
|
|
|
|
cacheServer: cache,
|
|
|
|
httpsServer: https);
|
|
|
|
|
|
|
|
var uri = new Uri("https://Myserver.com");
|
|
|
|
const string bikeId = "BikeNr12";
|
|
|
|
|
|
|
|
https.DoReserveAsync(bikeId, uri).Throws(new System.Exception("Something went wrong..."));
|
|
|
|
|
|
|
|
Assert.That(async() => await provider.DoReserveAsync(bikeId, uri), Throws.Exception);
|
|
|
|
|
|
|
|
cache.DidNotReceive().Update(Arg.Any<BikeInfoReservedOrBooked>());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verify that cache updating functionality is called correctly.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public async Task TestDoBookAsyncAsync()
|
|
|
|
{
|
|
|
|
var cache = Substitute.For<ICopriCache>();
|
|
|
|
var https = Substitute.For<ICopriServer>();
|
|
|
|
|
|
|
|
var provider = new CopriProviderHttps(
|
|
|
|
new Uri("https://copri.net"),
|
|
|
|
"MerchId",
|
|
|
|
new AppContextInfo("MerchId", "Name", new Version()),
|
|
|
|
"de-de",
|
|
|
|
cacheServer: cache,
|
|
|
|
httpsServer: https);
|
|
|
|
|
|
|
|
var uri = new Uri("https://Myserver.com");
|
|
|
|
const string bikeId = "BikeNr12";
|
|
|
|
|
|
|
|
https.DoBookAsync(uri, bikeId, new Guid(), 0).Returns(Task.FromResult(JsonConvert.DeserializeObject<ReservationBookingResponse>(@"
|
|
|
|
{
|
|
|
|
bike : ""BikeNr12"",
|
|
|
|
""bikes_occupied"" : {
|
|
|
|
""BikeNr3"" : {
|
|
|
|
""bike"" : ""BikeNr3"",
|
|
|
|
""station"" : ""Freiburg City""
|
|
|
|
},
|
|
|
|
""BikeNr12"" : {
|
|
|
|
""bike"" : ""BikeNr12"",
|
|
|
|
""station"" : ""HomeSweetHome""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
""response_state"" : ""OK""
|
|
|
|
}")));
|
|
|
|
|
|
|
|
await provider.DoBookAsync(uri, bikeId, new Guid(), 0);
|
|
|
|
|
|
|
|
cache.Received().Update(Arg.Is<BikeInfoReservedOrBooked>(x => x.station == "HomeSweetHome"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verify that cache updating functionality is not called in case of an exception being thrown.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestDoBookAsyncAsyncException()
|
|
|
|
{
|
|
|
|
var cache = Substitute.For<ICopriCache>();
|
|
|
|
var https = Substitute.For<ICopriServer>();
|
|
|
|
|
|
|
|
var provider = new CopriProviderHttps(
|
|
|
|
new Uri("https://copri.net"),
|
|
|
|
"MerchId",
|
|
|
|
new AppContextInfo("MerchId", "Name", new Version()),
|
|
|
|
"de-de",
|
|
|
|
cacheServer: cache,
|
|
|
|
httpsServer: https);
|
|
|
|
|
|
|
|
var uri = new Uri("https://Myserver.com");
|
|
|
|
const string bikeId = "BikeNr12";
|
|
|
|
|
|
|
|
https.DoBookAsync(uri, bikeId, new Guid(), 0).Throws(new System.Exception("Something went wrong..."));
|
|
|
|
|
|
|
|
Assert.That(async () => await provider.DoBookAsync(uri, bikeId, new Guid(), 0), Throws.Exception);
|
|
|
|
|
|
|
|
cache.DidNotReceive().Update(Arg.Any<BikeInfoReservedOrBooked>());
|
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2021-06-26 20:57:55 +02:00
|
|
|
}
|