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

238 lines
8.9 KiB
C#
Raw Normal View History

2023-04-05 15:02:10 +02:00
using System;
2021-07-12 21:31:46 +02:00
using System.Collections.Generic;
2023-04-05 15:02:10 +02:00
using NSubstitute;
2022-08-30 15:42:25 +02:00
using NUnit.Framework;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.State;
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests
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 TestStateInfo
{
[Test]
public void TestConstruct()
{
var l_oInUseState = new StateInfoMutable(() => new DateTime(2017, 09, 20, 23, 20, 0));
// Verify initial values of properties.
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestConstructCopy()
{
// State is available.
2023-04-05 15:02:10 +02:00
var l_oSource = Substitute.For<IStateInfo>();
l_oSource.Value.Returns(InUseStateEnum.Disposable);
2022-09-06 16:08:19 +02:00
var l_oState = new StateInfoMutable(state: l_oSource);
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
// State is requested.
2023-04-05 15:02:10 +02:00
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");
2023-06-06 12:00:24 +02:00
l_oSource.MaxReservationTimeSpan.Returns(TimeSpan.FromMinutes(15));
2022-09-06 16:08:19 +02:00
l_oState = new StateInfoMutable(() => new DateTime(2018, 1, 4, 17, 30, 1), l_oSource);
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
// State is booked.
2023-04-05 15:02:10 +02:00
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");
2022-09-06 16:08:19 +02:00
l_oState = new StateInfoMutable(() => new DateTime(2018, 1, 4, 17, 30, 1), l_oSource);
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
}
[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
2021-07-12 21:31:46 +02:00
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
});
2022-09-06 16:08:19 +02:00
var l_oStateInfo = new StateInfoMutable(l_oDateTimeMock.GetDateTime);
// Update state from Copri.
l_oStateInfo.Load(
InUseStateEnum.Reserved, // Copri acknowledges state reserved.
l_oDateTimeMock.GetDateTime(),
2023-06-06 12:00:24 +02:00
TimeSpan.FromMinutes(15),
2022-09-06 16:08:19 +02:00
"heiz@mustermann"); // Owner from Copri.
// Invoke first update (after simulated 4mns)
l_oStateInfo.UpdateOnTimeElapsed();
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
// Invoke second update (after simulated 16 mns)
l_oStateInfo.UpdateOnTimeElapsed();
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestUpdateFromWebserver()
{
Func<DateTime> l_oNowMock = () => new DateTime(2017, 09, 21, 23, 40, 0);
var l_oStateInfo = new StateInfoMutable(l_oNowMock);
2023-06-06 12:00:24 +02:00
l_oStateInfo.Load(InUseStateEnum.Booked, new DateTime(2017, 09, 21, 23, 30, 0), TimeSpan.FromMinutes(15), "heiz@mustermann", "21");
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
DateTime FROM = new DateTime(2017, 09, 21, 23, 35, 0);
2023-06-06 12:00:24 +02:00
l_oStateInfo.Load(InUseStateEnum.Reserved, FROM, TimeSpan.FromMinutes(15), "heiz@mustermann", "22");
2022-09-06 16:08:19 +02:00
// Verify initial values of properties.
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
2023-06-06 12:00:24 +02:00
l_oStateInfo.Load(InUseStateEnum.Disposable, new DateTime(1970, 1, 1, 0, 0, 0), TimeSpan.FromMinutes(15), "heiz@mustermann", "unused");
2022-09-06 16:08:19 +02:00
// Verify initial values of properties.
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestLoad()
{
var l_oState = new StateInfoMutable(
() => new DateTime(2018, 01, 03, 21, 14, 0)); // Now
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
// 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
2023-06-06 12:00:24 +02:00
TimeSpan.FromMinutes(15),
2022-09-06 16:08:19 +02:00
"a@b",
"123");
l_oState.Load(l_oSource);
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
// Construct booked state object.
l_oSource = new StateInfo(new DateTime(2018, 01, 03, 21, 37, 0), "a@b", "123");
l_oState.Load(l_oSource);
2024-04-09 12:53:23 +02:00
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"));
2022-09-06 16:08:19 +02:00
// Construct disposable object
l_oSource = new StateInfo();
l_oState.Load(l_oSource);
2024-04-09 12:53:23 +02:00
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);
2022-09-06 16:08:19 +02:00
}
2023-06-06 12:00:24 +02:00
[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)));
2022-09-06 16:08:19 +02:00
}
2023-04-05 15:02:10 +02:00
}