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

299 lines
9.4 KiB
C#
Raw Normal View History

2022-08-30 15:42:25 +02:00
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.Services.CopriApi;
namespace TestShareeLib.Model.Connector
{
2022-09-06 16:08:19 +02:00
[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>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
}
},
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
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>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
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>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes_occupied"": {
""FR1005"": {
""lock_state"": ""occupied"",
""system"": ""Sigo"",
""state"": ""occupied"",
""bike"": ""FR1005"",
""station"": ""REN0"",
}
},
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
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>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
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>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesAvailableCache = JsonConvert.DeserializeObject<BikesAvailableResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes"": {
""FR1005"": {
""system"": ""Sigo"",
""state"": ""available"",
""bike"": ""FR1005"",
""station"": ""REN0"",
""co2saving"" : """"
}
},
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
var bikesOccupied = JsonConvert.DeserializeObject<BikesReservedOccupiedResponse>(@"
2022-08-30 15:42:25 +02:00
{
""bikes_occupied"": { },
""copri_version"": ""4.1.12.5"",
}");
2022-09-06 16:08:19 +02:00
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));
}
}
2022-08-30 15:42:25 +02:00
}