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 { /// /// Verifies that available bikes are not considered to by kind of MyBikes. /// [Test] public async Task TestGetBikesOccupiedAsyncMyBikesNone() { var server = Substitute.For(); var bikesAvailable = JsonConvert.DeserializeObject(@" { ""bikes"": { ""FR1005"": { ""system"": ""Sigo"", ""state"": ""available"", ""bike"": ""FR1005"", ""station"": ""REN0"", } }, ""copri_version"": ""4.1.12.5"", }"); var bikesOccupied = JsonConvert.DeserializeObject(@" { ""bikes_occupied"": { }, ""copri_version"": ""4.1.12.5"", }"); var resultAvailable = new Result( typeof(CopriCallsHttps), // Marks that response was taken from cache bikesAvailable, new GeneralData()); var resultOccupied = new Result( 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)); } /// /// Verifies that available bikes with Co2Saving entry set are considered to by kind of MyBikes. /// [Test] public async Task TestGetBikesOccupiedAsyncMyBikesOneFeedbackPending() { var server = Substitute.For(); var bikesAvailable = JsonConvert.DeserializeObject(@" { ""bikes"": { ""FR1005"": { ""system"": ""Sigo"", ""state"": ""available"", ""bike"": ""FR1005"", ""station"": ""REN0"", ""co2saving"" : """" } }, ""copri_version"": ""4.1.12.5"", }"); var bikesOccupied = JsonConvert.DeserializeObject(@" { ""bikes_occupied"": { }, ""copri_version"": ""4.1.12.5"", }"); var resultAvailable = new Result( typeof(CopriCallsHttps), bikesAvailable, new GeneralData()); var resultOccupied = new Result( 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)); } /// /// . /// [Test] public async Task TestGetBikesOccupiedAsyncMyBikesOneOccupied() { var server = Substitute.For(); var bikesAvailable = JsonConvert.DeserializeObject(@" { ""bikes"": { }, ""copri_version"": ""4.1.12.5"", }"); var bikesOccupied = JsonConvert.DeserializeObject(@" { ""bikes_occupied"": { ""FR1005"": { ""lock_state"": ""occupied"", ""system"": ""Sigo"", ""state"": ""occupied"", ""bike"": ""FR1005"", ""station"": ""REN0"", } }, ""copri_version"": ""4.1.12.5"", }"); var resultAvailable = new Result( typeof(CopriCallsHttps), bikesAvailable, new GeneralData()); var resultOccupied = new Result( 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)); } /// /// Scenario: Call of GetBikesAvailable fails data is taken from cache. /// [Test] public async Task TestGetBikesOccupiedAsyncGetAvailableFailsMyBikeOneFeedbackPending() { var server = Substitute.For(); var bikesAvailable = JsonConvert.DeserializeObject(@" { ""bikes"": { ""FR1005"": { ""system"": ""Sigo"", ""state"": ""available"", ""bike"": ""FR1005"", ""station"": ""REN0"", ""co2saving"" : """" } }, ""copri_version"": ""4.1.12.5"", }"); var bikesOccupied = JsonConvert.DeserializeObject(@" { ""bikes_occupied"": { }, ""copri_version"": ""4.1.12.5"", }"); var resultAvailable = new Result( typeof(CopriCallsMonkeyStore), bikesAvailable, new GeneralData()); var resultOccupied = new Result( 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)); } /// /// Scenario: Call of GetBikesAvailable succeeds but because call of GetBikesOccupied fails data is taken from cache. /// [Test] public async Task TestGetBikesOccupiedAsyncGetGetBikesOccupiedFailsMyBikeFeedbackPending() { var server = Substitute.For(); var bikesAvailableCopri = JsonConvert.DeserializeObject(@" { ""bikes"": { }, ""copri_version"": ""4.1.12.5"", }"); var bikesAvailableCache = JsonConvert.DeserializeObject(@" { ""bikes"": { ""FR1005"": { ""system"": ""Sigo"", ""state"": ""available"", ""bike"": ""FR1005"", ""station"": ""REN0"", ""co2saving"" : """" } }, ""copri_version"": ""4.1.12.5"", }"); var bikesOccupied = JsonConvert.DeserializeObject(@" { ""bikes_occupied"": { }, ""copri_version"": ""4.1.12.5"", }"); var resultAvailableCopri = new Result( typeof(CopriCallsHttps), // Marks that response was taken from copri bikesAvailableCache, new GeneralData()); var resultAvailableCache = new Result( typeof(CopriCallsMonkeyStore), // Marks that response was taken from cache bikesAvailableCache, new GeneralData()); var resultOccupied = new Result( 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)); } } }