2022-10-26 20:53:18 +02:00
|
|
|
using System;
|
2022-08-30 15:42:25 +02:00
|
|
|
using NSubstitute;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using TINK.Model.Bikes.BikeInfoNS.CopriLock;
|
|
|
|
using TINK.Model.Connector;
|
|
|
|
using TINK.Model.Device;
|
|
|
|
using TINK.Model.User;
|
|
|
|
using TINK.Repository.Exception;
|
|
|
|
using TINK.View;
|
|
|
|
using TINK.ViewModel;
|
|
|
|
using TINK.ViewModel.Bikes;
|
|
|
|
using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
|
|
|
|
|
|
|
|
namespace TestShareeLib.ViewModel.Bikes.Bike.CopriLock.RequestHandler
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
[TestFixture]
|
|
|
|
public class TestBookedClosed
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Use case: Try to open lock.
|
|
|
|
/// Final state: Bike remains locked and stays booked.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestOpenLockOpenLockAsyncFailsWebConnectFailureException()
|
|
|
|
{
|
|
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
|
|
var connector = Substitute.For<IConnector>();
|
|
|
|
var command = Substitute.For<ICommand>();
|
|
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
|
|
var viewService = Substitute.For<IViewService>();
|
|
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
|
|
|
|
var handler = new BookedClosed(
|
|
|
|
bike,
|
|
|
|
() => true, // isConnectedDelegate
|
|
|
|
(isConnexted) => connector,
|
|
|
|
() => pollingManager,
|
|
|
|
Substitute.For<ISmartDevice>(),
|
|
|
|
viewService,
|
|
|
|
bikesViewModel,
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
|
|
|
|
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
|
|
|
|
|
|
|
|
connector.Command.OpenLockAsync(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
|
|
|
|
|
|
|
|
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked); // State remains booked because opening failed.
|
|
|
|
|
|
|
|
var subsequent = handler.HandleRequestOption2().Result;
|
|
|
|
|
|
|
|
// Verify behaviour
|
|
|
|
Received.InOrder(() =>
|
|
|
|
{
|
|
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
|
|
pollingManager.StopUpdatePeridically(); // Polling must be stopped before any COPR and lock service action
|
2022-11-25 09:55:23 +01:00
|
|
|
bikesViewModel.ActionText = "<h4><b>Lock is opening.<br/>Please wait until it is completely open.</b></h4>";
|
2022-09-06 16:08:19 +02:00
|
|
|
connector.Command.OpenLockAsync(bike); // Booking must be performed
|
2023-02-22 14:03:35 +01:00
|
|
|
bikesViewModel.ActionText = string.Empty;
|
2022-09-06 16:08:19 +02:00
|
|
|
viewService.DisplayAdvancedAlert(
|
|
|
|
"Connection error when opening the lock!",
|
2022-10-26 20:53:18 +02:00
|
|
|
"Is WIFI available/ mobile network available and mobile data activated / ... ?",
|
2022-09-06 16:08:19 +02:00
|
|
|
"Context info",
|
|
|
|
"OK");
|
|
|
|
bikesViewModel.ActionText = "Updating...";
|
|
|
|
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
|
2023-02-22 14:03:35 +01:00
|
|
|
bikesViewModel.ActionText = string.Empty;
|
2022-09-06 16:08:19 +02:00
|
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify state after action
|
|
|
|
Assert.That(
|
|
|
|
subsequent.ButtonText,
|
|
|
|
Is.EqualTo("BookedClosed"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsButtonVisible,
|
|
|
|
Is.False);
|
|
|
|
Assert.That(
|
|
|
|
subsequent.LockitButtonText,
|
|
|
|
Is.EqualTo("Open lock"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsLockitButtonVisible,
|
|
|
|
Is.True);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Use case: Try to open lock.
|
|
|
|
/// Final state: Bike remains locked and stays booked.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestOpenLockOpenLockAsyncFailsException()
|
|
|
|
{
|
|
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
|
|
var connector = Substitute.For<IConnector>();
|
|
|
|
var command = Substitute.For<ICommand>();
|
|
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
|
|
var viewService = Substitute.For<IViewService>();
|
|
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
|
|
|
|
var handler = new BookedClosed(
|
|
|
|
bike,
|
|
|
|
() => true, // isConnectedDelegate
|
|
|
|
(isConnexted) => connector,
|
|
|
|
() => pollingManager,
|
|
|
|
Substitute.For<ISmartDevice>(),
|
|
|
|
viewService,
|
|
|
|
bikesViewModel,
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
|
|
|
|
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
|
|
|
|
|
|
|
|
connector.Command.OpenLockAsync(bike).Returns(x => throw new Exception("Some error", new Exception("Inner exception")));
|
|
|
|
|
|
|
|
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked); // State remains booked because opening failed.
|
|
|
|
|
|
|
|
var subsequent = handler.HandleRequestOption2().Result;
|
|
|
|
|
|
|
|
// Verify behaviour
|
|
|
|
Received.InOrder(() =>
|
|
|
|
{
|
|
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
|
|
pollingManager.StopUpdatePeridically(); // Polling must be stopped before any COPR and lock service action
|
2022-11-25 09:55:23 +01:00
|
|
|
bikesViewModel.ActionText = "<h4><b>Lock is opening.<br/>Please wait until it is completely open.</b></h4>";
|
2022-09-06 16:08:19 +02:00
|
|
|
connector.Command.OpenLockAsync(bike); // Booking must be performed
|
2023-02-22 14:03:35 +01:00
|
|
|
bikesViewModel.ActionText = string.Empty;
|
2022-09-06 16:08:19 +02:00
|
|
|
viewService.DisplayAlert(
|
|
|
|
"Error while opening lock!",
|
|
|
|
"Some error",
|
|
|
|
"OK");
|
|
|
|
bikesViewModel.ActionText = "Updating...";
|
|
|
|
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
|
2023-02-22 14:03:35 +01:00
|
|
|
bikesViewModel.ActionText = string.Empty;
|
2022-09-06 16:08:19 +02:00
|
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify state after action
|
|
|
|
Assert.That(
|
|
|
|
subsequent.ButtonText,
|
|
|
|
Is.EqualTo("BookedClosed"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsButtonVisible,
|
|
|
|
Is.False);
|
|
|
|
Assert.That(
|
|
|
|
subsequent.LockitButtonText,
|
|
|
|
Is.EqualTo("Open lock"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsLockitButtonVisible,
|
|
|
|
Is.True);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Use case: Open lock.
|
|
|
|
/// Final state: Lock is opened and bike remains booked.
|
|
|
|
/// </summary>
|
|
|
|
[Test]
|
|
|
|
public void TestOpenLock()
|
|
|
|
{
|
|
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
|
|
var connector = Substitute.For<IConnector>();
|
|
|
|
var command = Substitute.For<ICommand>();
|
|
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
|
|
var viewService = Substitute.For<IViewService>();
|
|
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
|
|
|
|
var handler = new BookedClosed(
|
|
|
|
bike,
|
|
|
|
() => true, // isConnectedDelegate
|
|
|
|
(isConnexted) => connector,
|
|
|
|
() => pollingManager,
|
|
|
|
Substitute.For<ISmartDevice>(),
|
|
|
|
viewService,
|
|
|
|
bikesViewModel,
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
|
|
|
|
bike.LockInfo.State.Returns(LockingState.Open); // After opening, lock state is open.
|
|
|
|
|
|
|
|
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked); // After Booking and opening, state is booked.
|
|
|
|
|
|
|
|
var subsequent = handler.HandleRequestOption2().Result;
|
|
|
|
|
|
|
|
// Verify behaviour
|
|
|
|
Received.InOrder(() =>
|
|
|
|
{
|
|
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
|
|
pollingManager.StopUpdatePeridically(); // Polling must be stopped before any COPR and lock service action
|
2022-11-25 09:55:23 +01:00
|
|
|
bikesViewModel.ActionText = "<h4><b>Lock is opening.<br/>Please wait until it is completely open.</b></h4>";
|
2022-09-06 16:08:19 +02:00
|
|
|
connector.Command.OpenLockAsync(bike); // Booking must be performed
|
|
|
|
bikesViewModel.ActionText = "Updating...";
|
|
|
|
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
|
2023-02-22 14:03:35 +01:00
|
|
|
bikesViewModel.ActionText = string.Empty;
|
2022-09-06 16:08:19 +02:00
|
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify state
|
|
|
|
Assert.That(
|
|
|
|
subsequent.ButtonText,
|
|
|
|
Is.EqualTo("BookedOpen"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsButtonVisible,
|
|
|
|
Is.False);
|
|
|
|
Assert.That(
|
|
|
|
subsequent.LockitButtonText,
|
|
|
|
Is.EqualTo("Open lock"));
|
|
|
|
Assert.That(
|
|
|
|
subsequent.IsLockitButtonVisible,
|
|
|
|
Is.True);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
}
|