sharee.bike-App/SharedBusinessLogic.Tests/Model/State/TestStateInfoSerializeJSON.cs
2024-04-09 12:53:23 +02:00

213 lines
8.5 KiB
C#

using System;
using Newtonsoft.Json;
using NUnit.Framework;
using SharedBusinessLogic.Tests.Framework;
using ShareeBike.Model.State;
namespace SharedBusinessLogic.Tests.Fixtures.State
{
[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 = @"
{
""StateInfoObject"":
{
""$type"":""ShareeBike.Model.State.StateAvailableInfo, SharedBusinessLogic""
}
}";
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))));
}
{
// Deserialize object
var l_oInUseStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
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);
}
}
[Test]
public void TestSerializeJSON_Booked()
{
// Create object to test.
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 11, 18, 23, 20, 0) // Mocked time stamp returned when StateInfo- object is crated
);
l_oInUseState.Load(
InUseStateEnum.Booked,
new DateTime(2017, 11, 18, 23, 19, 0), // Time booked at
TimeSpan.FromMinutes(15),
"heiz@mustermann",
"173"); // Code
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"));
// Verify json
const string EXPECTED = @"
{
""StateInfoObject"":
{
""$type"":""ShareeBike.Model.State.StateOccupiedInfo, SharedBusinessLogic"",
""From"":""2017 - 11 - 18T23: 19:00"",
""MailAddress"":""heiz @mustermann"",""Code"":""173""
}
}";
var l_oDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
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)));
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_oDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
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"));
}
[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),
TimeSpan.FromMinutes(15),
"heiz@mustermann");
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);
l_strJSONDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
// Verify json
const string EXPECTED = @"
{
""StateInfoObject"":
{
""$type"":""ShareeBike.Model.State.StateRequestedInfo, SharedBusinessLogic"",
""From"":""2017 - 09 - 21T23: 20:00"",
""MailAddress"":""heiz @mustermann""
}
}";
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))));
}
{
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
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);
}
}
[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,
TimeSpan.FromMinutes(15),
"z@C",
"01815A");
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"));
l_strJSONDetected = JsonConvert.SerializeObject(l_oInUseState, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
});
// Verify json
const string EXPECTED = @"
{
""StateInfoObject"":
{
""$type"":""ShareeBike.Model.State.StateRequestedInfo, SharedBusinessLogic"",
""From"":""2017 - 11 - 18T23: 18:00"",
""MailAddress"":""z @C"",
""Code"":""01815A""
}
}";
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))));
}
{
// Deserialize object an verify state.
var l_oStateTarget = JsonConvert.DeserializeObject<StateInfoMutable>(l_strJSONDetected, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
// Verify state.
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"));
}
}
}
}