mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
298 lines
9.4 KiB
C#
298 lines
9.4 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.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));
|
|
}
|
|
}
|
|
}
|