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

237 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.State;
namespace SharedBusinessLogic.Tests
{
[TestFixture]
public class TestStateInfo
{
[Test]
public void TestConstruct()
{
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 09, 20, 23, 20, 0));
// Verify initial values of properties.
Assert.That(l_oInUseState.Value, Is.EqualTo(InUseStateEnum.Disposable), "Default state is available");
Assert.That(l_oInUseState.ToString(), Is.EqualTo("Disposable"));
Assert.That(l_oInUseState.RemainingTime, Is.Null);
Assert.That(l_oInUseState.From, Is.Null);
Assert.That(l_oInUseState.Code, Is.Null);
}
[Test]
public void TestConstructCopy()
{
// State is available.
var l_oSource = Substitute.For<IStateInfo>();
l_oSource.Value.Returns(InUseStateEnum.Disposable);
var l_oState = new StateInfoMutable(state: l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oState.RemainingTime, Is.Null);
Assert.That(l_oState.From, Is.Null);
Assert.That(l_oState.Code, Is.Null);
// State is requested.
l_oSource = Substitute.For<IStateInfo>();
l_oSource.Value.Returns(InUseStateEnum.Reserved);
l_oSource.From.Returns(new DateTime(2018, 1, 4, 17, 26, 0));
l_oSource.MailAddress.Returns("who@the");
l_oSource.Code.Returns("323");
l_oSource.MaxReservationTimeSpan.Returns(TimeSpan.FromMinutes(15));
l_oState = new StateInfoMutable(() => new DateTime(2018, 1, 4, 17, 30, 1), l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oState.RemainingTime, Is.EqualTo(new TimeSpan(0, 10, 59)));
Assert.That(l_oState.From, Is.EqualTo(new DateTime(2018, 1, 4, 17, 26, 0)));
Assert.That(l_oState.MailAddress, Is.EqualTo("who@the"));
Assert.That(l_oState.Code, Is.EqualTo("323"));
// State is booked.
l_oSource = Substitute.For<IStateInfo>();
l_oSource.Value.Returns(InUseStateEnum.Booked);
l_oSource.From.Returns(new DateTime(2018, 1, 4, 17, 00, 0));
l_oSource.MailAddress.Returns("who@the");
l_oSource.Code.Returns("323");
l_oState = new StateInfoMutable(() => new DateTime(2018, 1, 4, 17, 30, 1), l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(l_oState.RemainingTime, Is.Null);
Assert.That(l_oState.From, Is.EqualTo(new DateTime(2018, 1, 4, 17, 00, 0)));
Assert.That(l_oState.MailAddress, Is.EqualTo("who@the"));
Assert.That(l_oState.Code, Is.EqualTo("323"));
}
[Test]
public void TestDoUpdate()
{
var l_oReservedAt = new DateTime(2017, 09, 20, 22, 01, 00);
var l_oDateTimeMock = new DateTimeMocker(new List<DateTime> {
l_oReservedAt, // Booking time
l_oReservedAt.Add(new TimeSpan(0, 4, 0)), // Reserved for 4 mns
l_oReservedAt.Add(new TimeSpan(0, 16, 0)) // Time elapsed since booking 16 mns
});
var l_oStateInfo = new StateInfoMutable(l_oDateTimeMock.GetDateTime);
// Update state from Copri.
l_oStateInfo.Load(
InUseStateEnum.Reserved, // Copri acknowledges state reserved.
l_oDateTimeMock.GetDateTime(),
TimeSpan.FromMinutes(15),
"heiz@mustermann"); // Owner from Copri.
// Invoke first update (after simulated 4mns)
l_oStateInfo.UpdateOnTimeElapsed();
Assert.That(l_oStateInfo.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oStateInfo.RemainingTime.Value.Minutes, Is.EqualTo(11));
Assert.That(l_oStateInfo.From.Value, Is.EqualTo(new DateTime(2017, 09, 20, 22, 01, 00)));
Assert.That(l_oStateInfo.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oStateInfo.Code, Is.Null);
// Invoke second update (after simulated 16 mns)
l_oStateInfo.UpdateOnTimeElapsed();
Assert.That(l_oStateInfo.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oStateInfo.RemainingTime, Is.Null);
Assert.That(l_oStateInfo.From, Is.Null);
Assert.That(l_oStateInfo.MailAddress, Is.Null);
Assert.That(l_oStateInfo.Code, Is.Null);
}
[Test]
public void TestUpdateFromWebserver()
{
Func<DateTime> l_oNowMock = () => new DateTime(2017, 09, 21, 23, 40, 0);
var l_oStateInfo = new StateInfoMutable(l_oNowMock);
l_oStateInfo.Load(InUseStateEnum.Booked, new DateTime(2017, 09, 21, 23, 30, 0), TimeSpan.FromMinutes(15), "heiz@mustermann", "21");
Assert.That(l_oStateInfo.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(l_oStateInfo.ToString(), Is.EqualTo("Booked"));
Assert.That(l_oStateInfo.RemainingTime, Is.Null);
Assert.That(l_oStateInfo.From.Value, Is.EqualTo(new DateTime(2017, 09, 21, 23, 30, 0)));
Assert.That(l_oStateInfo.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oStateInfo.Code, Is.EqualTo("21"));
DateTime FROM = new DateTime(2017, 09, 21, 23, 35, 0);
l_oStateInfo.Load(InUseStateEnum.Reserved, FROM, TimeSpan.FromMinutes(15), "heiz@mustermann", "22");
// Verify initial values of properties.
Assert.That(l_oStateInfo.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oStateInfo.ToString(), Is.EqualTo("Reserved"));
Assert.That(l_oStateInfo.RemainingTime.Value.Minutes, Is.EqualTo(new TimeSpan(0, 15, 0).Subtract(l_oNowMock().Subtract(FROM)).Minutes));
Assert.That(l_oStateInfo.From, Is.EqualTo(FROM));
Assert.That(l_oStateInfo.MailAddress, Is.EqualTo("heiz@mustermann"));
Assert.That(l_oStateInfo.Code, Is.EqualTo("22"));
l_oStateInfo.Load(InUseStateEnum.Disposable, new DateTime(1970, 1, 1, 0, 0, 0), TimeSpan.FromMinutes(15), "heiz@mustermann", "unused");
// Verify initial values of properties.
Assert.That(l_oStateInfo.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oStateInfo.ToString(), Is.EqualTo("Disposable"));
Assert.That(l_oStateInfo.RemainingTime, Is.Null);
Assert.That(l_oStateInfo.From, Is.Null);
Assert.That(l_oStateInfo.MailAddress, Is.Null);
Assert.That(l_oStateInfo.Code, Is.Null);
}
[Test]
public void TestLoad()
{
var l_oState = new StateInfoMutable(
() => new DateTime(2018, 01, 03, 21, 14, 0)); // Now
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oState.From, Is.Null);
Assert.That(l_oState.RemainingTime, Is.Null);
Assert.That(l_oState.MailAddress, Is.Null);
Assert.That(l_oState.Code, Is.Null);
// Construct requested state object.
var l_oSource = new StateInfo(
() => new DateTime(2018, 01, 03, 21, 53, 0),
new DateTime(2018, 01, 03, 21, 13, 0), // Requested from
TimeSpan.FromMinutes(15),
"a@b",
"123");
l_oState.Load(l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Reserved));
Assert.That(l_oState.From, Is.EqualTo(new DateTime(2018, 01, 03, 21, 13, 0)));
Assert.That(l_oState.RemainingTime.Value.Minutes, Is.EqualTo(14));
Assert.That(l_oState.MailAddress, Is.EqualTo("a@b"));
Assert.That(l_oState.Code, Is.EqualTo("123"));
// Construct booked state object.
l_oSource = new StateInfo(new DateTime(2018, 01, 03, 21, 37, 0), "a@b", "123");
l_oState.Load(l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Booked));
Assert.That(l_oState.From, Is.EqualTo(new DateTime(2018, 01, 03, 21, 37, 0)));
Assert.That(l_oState.RemainingTime, Is.Null);
Assert.That(l_oState.MailAddress, Is.EqualTo("a@b"));
Assert.That(l_oState.Code, Is.EqualTo("123"));
// Construct disposable object
l_oSource = new StateInfo();
l_oState.Load(l_oSource);
Assert.That(l_oState.Value, Is.EqualTo(InUseStateEnum.Disposable));
Assert.That(l_oState.From, Is.Null);
Assert.That(l_oState.RemainingTime, Is.Null);
Assert.That(l_oState.MailAddress, Is.Null);
Assert.That(l_oState.Code, Is.Null);
}
[Test]
public void TestLoadFromNull()
{
var state = new StateInfoMutable(
() => new DateTime(2018, 01, 03, 21, 14, 0));
state.Load(InUseStateEnum.Disposable,
null,
TimeSpan.FromMinutes(15));
Assert.That(
state.RemainingTime,
Is.Null);
}
[Test]
public void TestLoadReservationTimeSpanNull()
{
var state = new StateInfoMutable(
() => new DateTime(2018, 01, 03, 21, 14, 0));
state.Load(InUseStateEnum.Disposable,
DateTime.Now,
null);
Assert.That(
state.RemainingTime,
Is.Null);
}
[Test]
public void TestMaxReservationTimeSpanNullStateAvaialble()
=> Assert.That(new StateInfo().MaxReservationTimeSpan, Is.Null);
[Test]
public void TestMaxReservationTimeSpanNullStateFeedbackPending()
=> Assert.That(new StateInfo(true).MaxReservationTimeSpan, Is.Null);
[Test]
public void TestMaxReservationTimeSpanNullStateOccupied()
=> Assert.That(new StateInfo(new DateTime(2023, 5, 11), "a@b", "").MaxReservationTimeSpan, Is.Null);
[Test]
public void TestMaxReservationTimeSpan()
=> Assert.That(new StateInfo(() => DateTime.Now, new DateTime(2023, 5, 11), TimeSpan.FromMinutes(12), "a@b", "").MaxReservationTimeSpan, Is.EqualTo(TimeSpan.FromMinutes(12)));
}
}