using System; using System.Threading.Tasks; using Newtonsoft.Json; using NSubstitute; using NUnit.Framework; using ShareeBike.Model.Connector; using ShareeBike.Model.Services.CopriApi; using ShareeBike.Repository; using ShareeBike.Repository.Response; using ShareeBike.Repository.Response.Stations; using ShareeBike.Services.CopriApi; namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Connector.Query { [TestFixture] public class TestCachedQuery { 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"" } } }"; /// /// Holds the response on stations_available request. /// /// V1: Did not hold station_type entry. private const string STATIONSALLV2 = @"{ ""copri_version"" : ""4.1.0.0"", ""stations"" : { ""9"" : { ""station"" : ""9"", ""bike_soll"" : ""0"", ""bike_ist"" : ""7"", ""station_group"" : [ ""ShareeBike"" ], ""gps"" : { ""latitude"": ""47.66756"", ""longitude"": ""9.16477"" }, ""state"" : ""available"", ""description"" : """", ""station_type"": { ""Cargobike"": { ""bike_count"": ""1"", ""bike_group"": ""ShareeBike"" }, ""Citybike"": { ""bike_count"": ""0"", ""bike_group"": ""FR300103"" } } }, ""13"" : { ""station"" : ""13"", ""bike_soll"" : ""4"", ""bike_ist"" : ""1"", ""station_group"" : [ ""ShareeBike"" ], ""gps"" : { ""latitude"": ""47.657756"", ""longitude"": ""9.176084"" }, ""state"" : ""available"", ""description"" : """" }, ""30"" : { ""station"" : ""30"", ""bike_soll"" : ""5"", ""bike_ist"" : ""0"", ""station_group"" : [ ""ShareeBike"", ""Citybike"" ], ""gps"" : { ""latitude"": ""47.657766"", ""longitude"": ""9.176094"" }, ""state"" : ""available"", ""description"" : ""Test für Stadtradstation"" } }, ""user_group"" : [ ""Citybike"", ""ShareeBike"" ], ""response_state"" : ""OK"", ""authcookie"" : ""6103_f782a208d9399291ba8d086b5dcc2509_12345678"", ""debuglevel"" : ""2"", ""response"" : ""stations_all"", ""user_id"" : ""javaminister@gmail.com"", ""apiserver"" : ""https://tinkwwp.copri-bike.de"" }"; private const string STATIONSALLEMPTY = @"{ ""copri_version"" : ""4.1.0.0"", ""stations"" : { }, ""user_group"" : [ ""Citybike"", ""ShareeBike"" ], ""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 TestGetStations_StationsFromCache() { var server = Substitute.For(); server.GetStations(false).Returns(Task.Run(() => new Result( typeof(CopriCallsMonkeyStore), JsonConvert.DeserializeObject(STATIONSALLV2), new GeneralData(), new System.Exception("Bang when getting stations...")))); var result = await new CachedQuery(server).GetBikesAndStationsAsync(); Assert.That(result.Response.StationsAll.Count, Is.EqualTo(3)); Assert.That(result.Response.StationsAll["9"].BikeGroups.AvailableCount, Is.EqualTo(1)); Assert.That(result.Response.BikesOccupied.Count, Is.EqualTo(0)); Assert.That(result.Source, Is.EqualTo(typeof(CopriCallsMonkeyStore))); Assert.That(result.Exception.Message, Is.EqualTo("Bang when getting stations...")); } [Test] public async Task TestGetStations() { var server = Substitute.For(); server.GetStations(false).Returns(Task.Run(() => new Result( typeof(CopriCallsHttps), JsonConvert.DeserializeObject(STATIONSALLV2), new GeneralData()))); var result = await new CachedQuery(server).GetBikesAndStationsAsync(); Assert.That(result.Response.StationsAll.Count, Is.EqualTo(3)); Assert.That(result.Response.StationsAll["9"].BikeGroups.AvailableCount, Is.EqualTo(1)); Assert.That(result.Response.BikesOccupied.Count, Is.EqualTo(0), "There are no reserved or occupied bikes."); Assert.That(result.Source, Is.EqualTo(typeof(CopriCallsHttps))); Assert.That(result.Exception, Is.Null); } [Test] public async Task TestGetBikesAsync() { var server = Substitute.For(); var uri = new Uri("https://MyServer.com"); var station = "MyStation"; var bike = "MyBike"; var resultBikesAv = new Result( typeof(CopriCallsHttps), JsonConvert.DeserializeObject(BIKESAVAILABLE), new GeneralData()); server.GetBikesAvailable(operatorUri: uri, stationId: station, bikeId: bike).Returns(Task.Run(() => resultBikesAv)); var result = await new CachedQuery(server).GetBikesAsync(uri, station, bike); Assert.That(result.Response.Count, Is.EqualTo(1)); Assert.That(result.Source, Is.EqualTo(typeof(CopriCallsHttps))); Assert.That(result.Exception, Is.Null); await server.Received().GetBikesAvailable(operatorUri: uri, stationId: station, bikeId: bike); server.Received().AddToCache(resultBikesAv, operatorUri: uri, stationId: station, bikeId: bike); } [Test] public async Task TestGetBikesOccupied() { var server = Substitute.For(); var result = await new CachedQuery(server).GetBikesOccupiedAsync(); Assert.That(result.Response.Count, Is.EqualTo(0)); Assert.That(result.Source, Is.EqualTo(typeof(CopriCallsMonkeyStore))); Assert.That("Abfrage der reservierten/ gebuchten Räder nicht möglich. Kein Benutzer angemeldet.", Is.EqualTo(result.Exception.Message)); } } }