sharee.bike-App/TestShareeLib/Repository/TestCopriCallsStatic.cs

241 lines
8.4 KiB
C#
Raw Normal View History

2023-04-05 15:02:10 +02:00
using System.Linq;
using Newtonsoft.Json;
2021-06-26 20:57:55 +02:00
using NUnit.Framework;
using TINK.Repository;
2022-08-30 15:42:25 +02:00
using TINK.Repository.Response;
2021-06-26 20:57:55 +02:00
namespace TestShareeLib.Repository
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestCopriCallsStatic
{
[Test]
public void TestDeserializeResponse_Factory_SupportedVersion()
{
var response = @"
2021-06-26 20:57:55 +02:00
{
""shareejson"" :
{
""copri_version"" : ""4.1.5.7""
}
}";
2022-09-06 16:08:19 +02:00
Assert.That(
response.DeserializeResponse(version => new ResponseBase()).copri_version,
Is.EqualTo("4.1.5.7"));
}
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public void TestDeserializeResponse_Factory_UnspportedVersion()
{
// Future version
var response = @"
2021-06-26 20:57:55 +02:00
{
""shareejson"" :
{
""copri_version"" : ""4.2.5.7""
}
}";
2022-09-06 16:08:19 +02:00
var factoryObject = JsonConvert.DeserializeObject<ResponseContainer<ResponseBase>>(
@"{
2021-06-26 20:57:55 +02:00
""shareejson"" :
{
""copri_version"" : ""9.9.9.9""
}
}");
2022-09-06 16:08:19 +02:00
Assert.That(
response.DeserializeResponse(version => factoryObject).shareejson.copri_version,
Is.EqualTo("9.9.9.9"));
}
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public void TestDeserializeResponse_Exception_SupportedVersion()
{
// Future version
var response = @"
2021-06-26 20:57:55 +02:00
{
""shareejson"" :
{
""copri_version"" : ""4.1.5.7""
}
}";
2022-09-06 16:08:19 +02:00
Assert.That(
response.DeserializeResponse<ResponseBase>(version => new System.Exception("hello")).copri_version,
Is.EqualTo("4.1.5.7"),
"No exception must be thrown because version 4.1.x is supported.");
}
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public void TestDeserializeResponse_Exception_UnspportedVersion()
{
var response = @"
2021-06-26 20:57:55 +02:00
{
""shareejson"" :
{
""copri_version"" : ""4.2.5.7""
}
}";
2022-09-06 16:08:19 +02:00
Assert.That(
() => response.DeserializeResponse<ResponseBase>(version => new System.Exception("Ho")).copri_version,
Throws.InstanceOf<System.Exception>());
}
2023-04-05 15:02:10 +02:00
[Test]
public void TestDeserializeObjectBikesAvailableValidResponse()
{
const string VALID_RESPONSE = @"
{
""shareejson"": {
""authcookie"": 0,
""apiserver"": ""https://tinkwwp.copri-bike.de"",
""response"": ""bikes_available"",
""bikes"": {
""3399"": {
""description"": ""Cargo Trike"",
""bike"": ""26"",
""state"": ""available"",
""gps"" : { ""latitude"": ""47.6586936667"", ""longitude"": ""9.16863116667"" },
""station"" : ""4""
},
},
""response_state"": ""OK"",
""copri_version"" : ""4.1.0.0""
}
}
";
// Ensure that answer holds a valid bike.
var l_oBike = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(VALID_RESPONSE).bikes.FirstOrDefault().Value;
Assert.NotNull(l_oBike, "Response must contain at leas one bike.");
Assert.Greater(l_oBike.description.Length, 0, "Bike despcription must never be empty.");
Assert.AreEqual(l_oBike.bike, "26");
Assert.That(
l_oBike.station,
Is.EqualTo("4"),
"Station index must never be negative");
Assert.AreEqual("available", l_oBike.state);
Assert.That(l_oBike.gps, Is.Not.Null, "Gps position must never be empty.");
}
[Test]
public void TestDeserializeObjectBikesAvailableValidResponse_NoDescription()
{
const string INVALID_RESPONSE = @"
{
""shareejson"": {
""authcookie"": 0,
""apiserver"": ""https://tinkwwp.copri-bike.de"",
""response"": ""bikes_available"",
""bikes"": {
""3399"": {
""bike"": 26,
""state"": ""available"",
""gps"" : { ""latitude"": ""47.6586936667"", ""longitude"": ""9.16863116667"" },
""station"" : 4
},
},
""response_state"": ""OK"",
""copri_version"" : ""4.1.0.0"",
}
}
";
// Ensure that answer holds a valid bike.
var l_oBike = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(INVALID_RESPONSE).bikes.FirstOrDefault().Value;
Assert.NotNull(l_oBike, "Response must contain at leas one bike.");
Assert.IsNull(l_oBike.description);
Assert.That(l_oBike.bike, Is.Not.Null);
Assert.That(l_oBike.station, Is.Not.Null);
Assert.AreEqual("available", l_oBike.state);
Assert.That(l_oBike.gps, Is.Not.Null, "Gps position must never be empty.");
}
[Test]
public void TestDeserializeObjectBikesAvailableValidResponse_NoBikeId()
{
const string VALID_RESPONSE = @"
{
""shareejson"": {
""authcookie"": 0,
""apiserver"": ""https://tinkwwp.copri-bike.de"",
""response"": ""bikes_available"",
""bikes"": {
""3399"": {
""description"": ""Cargo Trike"",
""state"": ""available"",
""gps"" : { ""latitude"": ""47.6586936667"", ""longitude"": ""9.16863116667"" },
""station"" : ""4""
},
},
""response_state"": ""OK"",
""copri_version"" : ""4.1.0.0"",
}
}";
// Ensure that answer holds a valid bike.
var l_oBike = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(VALID_RESPONSE).bikes.FirstOrDefault().Value;
Assert.NotNull(l_oBike, "Response must contain at leas one bike.");
Assert.Greater(l_oBike.description.Length, 0, "Bike despcription must never be empty.");
Assert.That(l_oBike.bike, Is.Null);
Assert.That(l_oBike.station, Is.Not.Null);
Assert.AreEqual("available", l_oBike.state);
Assert.That(l_oBike.gps, Is.Not.Null, "Gps position must never be empty.");
}
[Test]
public void TestDeserializeObjectBikesOccupiedValidResponse()
{
const string VALID_RESPONSE = @"
{
""shareejson"": {
""response_state"": ""OK"",
""bikes_occupied"": {
""87781"": {
""timeCode"": ""3630"",
""state"": ""occupied"",
""station"" : ""5"",
""description"": ""Cargo Long"",
""start_time"": ""2017-11-28 11:01:51.637747+01"",
""bike"": ""8""
},
""87782"": {
""timeCode"": ""2931"",
""state"": ""occupied"",
""station"" : ""4"",
""description"": ""Cargo Long"",
""start_time"": ""2017-11-28 13:06:55.147368+01"",
""bike"": ""7""
}
},
""authcookie"": ""b76b97e43a2d76b8499f32e6dd597af8"",
""response"": ""user_bikes_occupied"",
""apiserver"": ""https://tinkwwp.copri-bike.de"",
""copri_version"" : ""4.1.0.0"",
}
}";
// Ensure that answer holds a valid bike.
var l_oBike = CopriCallsStatic.DeserializeResponse<BikesReservedOccupiedResponse>(VALID_RESPONSE).bikes_occupied.FirstOrDefault().Value;
Assert.NotNull(l_oBike, "Response must contain at leas one bike.");
Assert.Greater(l_oBike.description.Length, 0, "Bike despcription must never be empty.");
Assert.That(l_oBike.bike, Is.Not.Null);
Assert.That(l_oBike.station, Is.Not.Null);
Assert.Greater(l_oBike.state.Length, 0, "State info must never be null or empty.");
// Todo: Requested bikes do not have a gps position. What is about booked bikes?
// Assert.Greater(l_oBike.gps.Length, 0, "Gps position must never be empty.");
Assert.Greater(l_oBike.start_time.Length, 0, "Time when request/ booking was performed must never be null or empty.");
Assert.Greater(l_oBike.timeCode.Length, 0, "Booking code must never be null or empty.");
}
2022-09-06 16:08:19 +02:00
}
2021-06-26 20:57:55 +02:00
}