sharee.bike-App/SharedBusinessLogic.Tests/Model/State/TestStateInfoSerializeJSON.cs

213 lines
8.5 KiB
C#
Raw Normal View History

2023-03-08 13:18:54 +01:00
using System;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
2021-07-12 21:31:46 +02:00
using NUnit.Framework;
2024-04-09 12:53:23 +02:00
using SharedBusinessLogic.Tests.Framework;
using ShareeBike.Model.State;
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests.Fixtures.State
2021-07-12 21:31:46 +02:00
{
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestStateInfoSerializeJSON
{
[Test]
public void TestSerializeJSON_Disposable()
{
string l_strJSONDetected;
{
// Create object to test.
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 09, 20, 23, 20, 0));
// Serialize object and verify json
l_strJSONDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
const string EXPECTED = @"
2021-07-12 21:31:46 +02:00
{
""StateInfoObject"":
{
2024-04-09 12:53:23 +02:00
""$type"":""ShareeBike.Model.State.StateAvailableInfo, SharedBusinessLogic""
2021-07-12 21:31:46 +02:00
}
}";
2024-04-09 12:53:23 +02:00
Assert.That(
TestHelper.PrepareXmlForStringCompare(l_strJSONDetected.Replace("\n", string.Empty).Replace("\r", string.Empty)), Is.EqualTo(TestHelper.PrepareXmlForStringCompare(EXPECTED.Replace("\n", string.Empty).Replace("\r", string.Empty))));
2022-09-06 16:08:19 +02:00
}
{
// Deserialize object
var l_oInUseStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
2024-04-09 12:53:23 +02:00
Assert.That(l_oInUseStateTarget.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oInUseStateTarget.ToString(), Is.EqualTo("Disposable"));
Assert.That(l_oInUseStateTarget.RemainingTime, Is.Null);
Assert.That(l_oInUseStateTarget.From, Is.Null);
Assert.That(l_oInUseStateTarget.MailAddress, Is.Null);
Assert.That(l_oInUseStateTarget.Code, Is.Null);
2022-09-06 16:08:19 +02:00
}
}
[Test]
public void TestSerializeJSON_Booked()
{
// Create object to test.
2023-06-06 12:00:24 +02:00
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 11, 18, 23, 20, 0) // Mocked time stamp returned when StateInfo- object is crated
2022-09-06 16:08:19 +02:00
);
l_oInUseState.Load(
InUseStateEnum.Booked,
new DateTime(2017, 11, 18, 23, 19, 0), // Time booked at
2023-06-06 12:00:24 +02:00
TimeSpan.FromMinutes(15),
2022-09-06 16:08:19 +02:00
"heiz@mustermann",
"173"); // Code
2024-04-09 12:53:23 +02:00
Assert.That(l_oInUseState.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(l_oInUseState.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oInUseState.From, Is.EqualTo(new DateTime(2017, 11, 18, 23, 19, 0)));
Assert.That(l_oInUseState.Code, Is.EqualTo("173"));
2022-09-06 16:08:19 +02:00
// Verify json
const string EXPECTED = @"
2021-07-12 21:31:46 +02:00
{
""StateInfoObject"":
{
2024-04-09 12:53:23 +02:00
""$type"":""ShareeBike.Model.State.StateOccupiedInfo, SharedBusinessLogic"",
2021-07-12 21:31:46 +02:00
""From"":""2017 - 11 - 18T23: 19:00"",
""MailAddress"":""heiz @mustermann"",""Code"":""173""
}
}";
2022-09-06 16:08:19 +02:00
var l_oDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
2024-04-09 12:53:23 +02:00
Assert.That(
TestHelper.PrepareXmlForStringCompare(l_oDetected).Replace("\n", string.Empty).Replace("\r", string.Empty), Is.EqualTo(TestHelper.PrepareXmlForStringCompare(EXPECTED).Replace("\n", string.Empty).Replace("\r", string.Empty)));
2022-09-06 16:08:19 +02:00
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_oDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
2024-04-09 12:53:23 +02:00
Assert.That(l_oStateTarget.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(l_oInUseState.From, Is.EqualTo(new DateTime(2017, 11, 18, 23, 19, 0)));
Assert.That(l_oStateTarget.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oInUseState.Code, Is.EqualTo("173"));
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestSerializeJSON_Reserved_NoCode()
{
string l_strJSONDetected;
{
// Create object to test.
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 09, 21, 23, 20, 0));
l_oInUseState.Load(
InUseStateEnum.Reserved,
new DateTime(2017, 09, 21, 23, 20, 0),
2023-06-06 12:00:24 +02:00
TimeSpan.FromMinutes(15),
2022-09-06 16:08:19 +02:00
"heiz@mustermann");
2024-04-09 12:53:23 +02:00
Assert.That(l_oInUseState.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oInUseState.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oInUseState.From, Is.EqualTo(new DateTime(2017, 09, 21, 23, 20, 0)));
Assert.That(l_oInUseState.RemainingTime, Is.EqualTo(new TimeSpan(0, 15, 0)));
Assert.That(l_oInUseState.Code, Is.Null);
2022-09-06 16:08:19 +02:00
l_strJSONDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
// Verify json
const string EXPECTED = @"
2021-07-12 21:31:46 +02:00
{
""StateInfoObject"":
{
2024-04-09 12:53:23 +02:00
""$type"":""ShareeBike.Model.State.StateRequestedInfo, SharedBusinessLogic"",
2021-07-12 21:31:46 +02:00
""From"":""2017 - 09 - 21T23: 20:00"",
""MailAddress"":""heiz @mustermann""
}
}";
2024-04-09 12:53:23 +02:00
Assert.That(
TestHelper.PrepareXmlForStringCompare(l_strJSONDetected.Replace("\n", string.Empty).Replace("\r", string.Empty)), Is.EqualTo(TestHelper.PrepareXmlForStringCompare(EXPECTED.Replace("\n", string.Empty).Replace("\r", string.Empty))));
2022-09-06 16:08:19 +02:00
}
{
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
2024-04-09 12:53:23 +02:00
Assert.That(l_oStateTarget.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oStateTarget.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oStateTarget.From, Is.EqualTo(new DateTime(2017, 09, 21, 23, 20, 0)));
Assert.That(l_oStateTarget.RemainingTime, Is.Null, "If deserialized date time provider is DateTime.Now. With a from- value of 2017-11- ... there is no more time remaining.");
Assert.That(l_oStateTarget.Code, Is.Null);
2022-09-06 16:08:19 +02:00
}
}
[Test]
public void TestSerializeJSON_Reserved()
{
string l_strJSONDetected;
{
Func<DateTime> l_oNow = () => new DateTime(2017, 11, 18, 23, 18, 0);
// Create object to test.
var l_oInUseState = new StateInfoMutable(l_oNow);
DateTime l_oFrom = new DateTime(2017, 11, 18, 23, 18, 0);
l_oInUseState.Load(
InUseStateEnum.Reserved,
l_oFrom,
2023-06-06 12:00:24 +02:00
TimeSpan.FromMinutes(15),
2022-09-06 16:08:19 +02:00
"z@C",
"01815A");
2024-04-09 12:53:23 +02:00
Assert.That(l_oInUseState.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oInUseState.MailAddress, Is.EqualTo("z@C"));
Assert.That(l_oInUseState.From, Is.EqualTo(new DateTime(2017, 11, 18, 23, 18, 0)));
Assert.That(l_oInUseState.RemainingTime, Is.EqualTo((new TimeSpan(0, 15, 0)).Subtract(l_oNow().Subtract(l_oFrom))));
Assert.That(l_oInUseState.Code, Is.EqualTo("01815A"));
2022-09-06 16:08:19 +02:00
l_strJSONDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
// Verify json
const string EXPECTED = @"
2021-07-12 21:31:46 +02:00
{
""StateInfoObject"":
{
2024-04-09 12:53:23 +02:00
""$type"":""ShareeBike.Model.State.StateRequestedInfo, SharedBusinessLogic"",
2021-07-12 21:31:46 +02:00
""From"":""2017 - 11 - 18T23: 18:00"",
""MailAddress"":""z @C"",
""Code"":""01815A""
}
}";
2024-04-09 12:53:23 +02:00
Assert.That(
TestHelper.PrepareXmlForStringCompare(l_strJSONDetected.Replace("\n", string.Empty).Replace("\r", string.Empty)), Is.EqualTo(TestHelper.PrepareXmlForStringCompare(EXPECTED.Replace("\n", string.Empty).Replace("\r", string.Empty))));
2022-09-06 16:08:19 +02:00
}
{
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
2024-04-09 12:53:23 +02:00
Assert.That(l_oStateTarget.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oStateTarget.MailAddress, Is.EqualTo("z@C"));
Assert.That(l_oStateTarget.From, Is.EqualTo(new DateTime(2017, 11, 18, 23, 18, 0)));
Assert.That(l_oStateTarget.RemainingTime, Is.Null, "If deserialized date time provider is DateTime.Now. With a from- value of 2017-11- ... there is no more time remaining.");
Assert.That(l_oStateTarget.Code, Is.EqualTo("01815A"));
2022-09-06 16:08:19 +02:00
}
}
}
2021-07-12 21:31:46 +02:00
}