mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-17 07:36:26 +01:00
678 lines
25 KiB
C#
678 lines
25 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.State;
|
|
using TINK.Model.User;
|
|
using TINK.Repository.Exception;
|
|
using TINK.Repository.Response;
|
|
using TINK.Services.BluetoothLock;
|
|
using TINK.Services.BluetoothLock.Exception;
|
|
using TINK.Services.BluetoothLock.Tdo;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.View;
|
|
using TINK.ViewModel;
|
|
using TINK.ViewModel.Bikes;
|
|
using TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler;
|
|
|
|
namespace TestTINKLib.Fixtures.ObjectTests.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
|
|
{
|
|
[TestFixture]
|
|
public class TestReservedOpen
|
|
{
|
|
/// <summary>
|
|
/// Test construction of object.
|
|
/// </summary>
|
|
[Test]
|
|
public void Testctor()
|
|
{
|
|
var handler = new ReservedOpen(
|
|
Substitute.For<IBikeInfoMutable>(),
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => Substitute.For<IConnector>(),
|
|
Substitute.For<IGeolocationService>(),
|
|
Substitute.For<ILocksService>(),
|
|
() => Substitute.For<IPollingUpdateTaskManager>(),
|
|
Substitute.For<ISmartDevice>(),
|
|
Substitute.For<IViewService>(),
|
|
Substitute.For<IBikesViewModel>(),
|
|
Substitute.For<IUser>());
|
|
|
|
// Verify prerequisites.
|
|
Assert.AreEqual("Close lock or rent bike", handler.ButtonText);
|
|
Assert.IsTrue(handler.IsButtonVisible);
|
|
Assert.AreEqual("Alarm/ Sounds verwalten", handler.LockitButtonText);
|
|
Assert.IsFalse(handler.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: User books bike.
|
|
/// Final state: Booked.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestBook()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(false));
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Booked); // Requesthandler factory queries state to create appropriate request handler object.
|
|
bike.LockInfo.State.Returns(LockingState.Open); // Requesthandler factory queries lock state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Reading charging level...";
|
|
locks[0].GetBatteryPercentageAsync();
|
|
bikesViewModel.ActionText = "Renting bike...";
|
|
connector.Command.DoBookAsync(bike);
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Close lock", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
|
|
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: User books bike.
|
|
/// Final state: Reserved closed.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestBookBookFailsWebConnectFailureException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(false));
|
|
|
|
connector.Command.DoBookAsync(bike).Returns(x => throw new WebConnectFailureException("Context info.", new Exception("Tst")));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Reading charging level...";
|
|
locks[0].GetBatteryPercentageAsync();
|
|
bikesViewModel.ActionText = "Renting bike...";
|
|
connector.Command.DoBookAsync(bike);
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAlert(
|
|
"Bike could not be rented!",
|
|
"A stable Internet connection is required. Connect to WIFI or to mobile network and activate mobile data. Try again.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
|
|
Assert.IsTrue(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: User books bike.
|
|
/// Final state: Reserved closed.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestBookBookFailsException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(false));
|
|
|
|
connector.Command.DoBookAsync(bike).Returns(x => throw new Exception("Exception message."));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Reading charging level...";
|
|
locks[0].GetBatteryPercentageAsync();
|
|
bikesViewModel.ActionText = "Renting bike...";
|
|
connector.Command.DoBookAsync(bike);
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAdvancedAlert(
|
|
"Bike could not be rented!",
|
|
"Exception message.",
|
|
"Please try again.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
|
|
Assert.IsTrue(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Booked.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservation()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Disposable); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Canceling reservation...";
|
|
connector.Command.DoCancelReservation(bike);
|
|
bikesViewModel.ActionText = "Disconnecting lock...";
|
|
locks.DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Reserve bike", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("DisposableDisconnected", subsequent.LockitButtonText);
|
|
Assert.IsFalse(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Reserved open.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservationCloseFailsOutOfReachException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(
|
|
string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync()
|
|
.Returns<Task<LockitLockingState?>>(x => { throw new OutOfReachException(); });
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
|
|
bike.LockInfo.State.Returns(LockingState.Open);
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAlert(
|
|
"Lock could not be closed!",
|
|
"Make sure you have granted Bluetooth permission to the app. Step as close as possible to the bike lock and try again.",
|
|
"OK");
|
|
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("Search lock", subsequent.LockitButtonText);
|
|
Assert.IsTrue(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Same as initial state.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservationCloseFailsException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(
|
|
string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync()
|
|
.Returns<Task<LockitLockingState?>>(x => { throw new Exception("Exception message."); });
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
|
|
bike.LockInfo.State.Returns(LockingState.Open);
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAlert(
|
|
"Lock could not be closed!",
|
|
"Step close to the lock and try again or report bike to operator!\r\nException message.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
|
|
Assert.IsTrue(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("Search lock", subsequent.LockitButtonText);
|
|
Assert.IsTrue(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Reserved closed.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservationCancelReservationInvalidAuthorizationResponseException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
var response = JsonConvert.DeserializeObject<AuthorizationResponse>(@"
|
|
{
|
|
""response"" : ""authorization"",
|
|
""authcookie"" : ""4da3044c8657a04ba60e2eaa753bc51a"",
|
|
""user_group"" : [ ""TINK"", ""Konrad"" ],
|
|
""response_state"" : ""OK"",
|
|
""apiserver"" : ""https://tinkwwp.copri-bike.de""
|
|
}");
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
connector.Command.DoCancelReservation(bike).Returns(x => throw new InvalidAuthorizationResponseException("mustermann@server.de", response));
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Disposable); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Canceling reservation...";
|
|
connector.Command.DoCancelReservation(bike);
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAlert(
|
|
"Reservation could not be canceled!",
|
|
"Your session has expired. Please login new and try again.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("InvalidState", subsequent.ButtonText);
|
|
Assert.IsFalse(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("InvalidState", subsequent.LockitButtonText);
|
|
Assert.IsFalse(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Reserved closed.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservationCancelReservationWebConnectFailureException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
connector.Command.DoCancelReservation(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("chub")));
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Disposable); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Canceling reservation...";
|
|
connector.Command.DoCancelReservation(bike);
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAlert(
|
|
"Reservation could not be canceled!",
|
|
"A stable Internet connection is required. Connect to WIFI or to mobile network and activate mobile data. Try again.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("InvalidState", subsequent.ButtonText);
|
|
Assert.IsFalse(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("InvalidState", subsequent.LockitButtonText);
|
|
Assert.IsFalse(subsequent.IsLockitButtonVisible);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use case: Close lock and cancel reservation.
|
|
/// Final state: Reserved closed.
|
|
/// </summary>
|
|
[Test]
|
|
public void TestCloseLockAndCancelReservationCancelReservationException()
|
|
{
|
|
var bike = Substitute.For<IBikeInfoMutable>();
|
|
var connector = Substitute.For<IConnector>();
|
|
var command = Substitute.For<ICommand>();
|
|
var geolocation = Substitute.For<IGeolocationService>();
|
|
var locks = Substitute.For<ILocksService>();
|
|
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
|
|
var viewService = Substitute.For<IViewService>();
|
|
var bikesViewModel = Substitute.For<IBikesViewModel>();
|
|
var activeUser = Substitute.For<IUser>();
|
|
|
|
var handler = new ReservedOpen(
|
|
bike,
|
|
() => true, // isConnectedDelegate
|
|
(isConnexted) => connector,
|
|
geolocation,
|
|
locks,
|
|
() => pollingManager,
|
|
Substitute.For<ISmartDevice>(),
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser);
|
|
|
|
bike.Id.Returns("0");
|
|
|
|
viewService.DisplayAlert(string.Empty, "Close lock or rent bike Nr. 0?", "Close lock", "Rent bike").Returns(Task.FromResult(true));
|
|
|
|
locks[0].CloseAsync().Returns(LockitLockingState.Closed);
|
|
|
|
connector.Command.DoCancelReservation(bike).Returns(x => throw new Exception("Exception message.", new Exception("chub")));
|
|
|
|
bike.State.Value.Returns(InUseStateEnum.Disposable); // Requesthandler factory queries state to create appropriate request handler object.
|
|
|
|
var subsequent = handler.HandleRequestOption1().Result;
|
|
|
|
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
|
|
|
|
// Verify behavior
|
|
Received.InOrder(() =>
|
|
{
|
|
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
|
|
bikesViewModel.ActionText = "One moment please...";
|
|
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
|
|
bikesViewModel.ActionText = "Stay with the bike until the lock is closed.";
|
|
locks[0].CloseAsync();
|
|
bikesViewModel.ActionText = "Canceling reservation...";
|
|
connector.Command.DoCancelReservation(bike);
|
|
bikesViewModel.ActionText = string.Empty;
|
|
viewService.DisplayAdvancedAlert(
|
|
"Reservation could not be canceled!",
|
|
"Exception message.",
|
|
"Please try again.",
|
|
"OK");
|
|
bikesViewModel.ActionText = "Updating...";
|
|
pollingManager.StartAsync(); // polling must be restarted again
|
|
bikesViewModel.ActionText = string.Empty;
|
|
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
|
|
});
|
|
|
|
// Verify state after action
|
|
Assert.AreEqual("InvalidState", subsequent.ButtonText);
|
|
Assert.IsFalse(subsequent.IsButtonVisible);
|
|
Assert.AreEqual("InvalidState", subsequent.LockitButtonText);
|
|
Assert.IsFalse(subsequent.IsLockitButtonVisible);
|
|
}
|
|
}
|
|
}
|