sharee.bike-App/TestShareeLib/ViewModel/Bikes/Bike/BluetoothLock/RequestHandler/TestReservedClosed.cs

1198 lines
47 KiB
C#
Raw Normal View History

2022-10-26 20:53:18 +02:00
using System;
2022-08-30 15:42:25 +02:00
using System.Threading.Tasks;
using Newtonsoft.Json;
2021-05-13 20:09:46 +02:00
using NSubstitute;
using NUnit.Framework;
2022-08-30 15:42:25 +02:00
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
2021-05-13 20:09:46 +02:00
using TINK.Model.Connector;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device;
using TINK.Model.State;
using TINK.Model.User;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception;
2022-08-30 15:42:25 +02:00
using TINK.Repository.Request;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Response;
2021-05-13 20:09:46 +02:00
using TINK.Services.BluetoothLock;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
2022-04-10 17:38:34 +02:00
using TINK.Services.Geolocation;
2021-05-13 20:09:46 +02:00
using TINK.View;
using TINK.ViewModel;
using TINK.ViewModel.Bikes;
using TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler;
namespace TestShareeLib.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestReservedClosed
{
/// <summary>
/// Test construction of object.
/// </summary>
[Test]
public void Testctor()
{
var handler = new ReservedClosed(
Substitute.For<IBikeInfoMutable>(),
() => true, // isConnectedDelegate
(isConnexted) => Substitute.For<IConnector>(),
2023-04-05 15:02:10 +02:00
Substitute.For<IGeolocationService>(),
2022-09-06 16:08:19 +02:00
Substitute.For<ILocksService>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
Substitute.For<ISmartDevice>(),
Substitute.For<IViewService>(),
Substitute.For<IBikesViewModel>(),
Substitute.For<IUser>());
// Verify prerequisites.
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", handler.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(handler.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", handler.LockitButtonText);
Assert.IsTrue(handler.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Cancel reservation.
2023-06-06 12:00:24 +02:00
/// Comment: User decide to abort canceling (user is ased whether to really cancel)
2022-09-06 16:08:19 +02:00
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCancelReservationCancel()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(false));
var subsequent = handler.HandleRequestOption1().Result;
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No");
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Disposable.
/// </summary>
[Test]
public void TestCancelReservation()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
2023-06-06 12:00:24 +02:00
bike.State.Value.Returns(InUseStateEnum.Disposable); // Requesthandler factory queries state to create appropriate request handler object.
2023-05-09 08:47:52 +02:00
bike.LockInfo.State.Returns(LockingState.UnknownDisconnected); // Requesthandler factory queries lock state to create appropriate request handler object.
2022-09-06 16:08:19 +02:00
var subsequent = handler.HandleRequestOption1().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Booking must be performed
bikesViewModel.ActionText = "Disconnecting lock...";
locks.DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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.AreEqual("Reserve bike", subsequent.ButtonText);
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("DisposableDisconnected", subsequent.LockitButtonText);
Assert.IsFalse(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Cancel reservation.
/// Comment: Exception is thrown.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCancelReservationDoCancelReservationInvalidAuthorizationResponseException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
var response = JsonConvert.DeserializeObject<AuthorizationResponse>(@"
2021-05-13 20:09:46 +02:00
{
""response"" : ""authorization"",
""authcookie"" : ""4da3044c8657a04ba60e2eaa753bc51a"",
2021-06-26 20:57:55 +02:00
""user_group"" : [ ""TINK"", ""Konrad"" ],
2021-05-13 20:09:46 +02:00
""response_state"" : ""OK"",
""apiserver"" : ""https://tinkwwp.copri-bike.de""
}");
2022-09-06 16:08:19 +02:00
connector.Command.DoCancelReservation(bike).Returns(x => throw new InvalidAuthorizationResponseException("mustermann@server.de", response));
2023-06-06 12:00:24 +02:00
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
2023-05-09 08:47:52 +02:00
bike.LockInfo.State.Returns(LockingState.Closed); // Requesthandler factory queries lock state to create appropriate request handler object.
2022-09-06 16:08:19 +02:00
var subsequent = handler.HandleRequestOption1().Result;
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(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(
2023-08-31 12:20:06 +02:00
"Reservation could not be canceled!",
"Your session has expired. Please login new and try again.",
2022-09-06 16:08:19 +02:00
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Cancel reservation.
/// Comment: Canceling reservation fails.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCancelReservationDoCancelReservationWebConnectFailureException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
connector.Command.DoCancelReservation(bike).Returns(x => throw new WebConnectFailureException("Context info.", new Exception("chub")));
2023-06-06 12:00:24 +02:00
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
2023-05-09 08:47:52 +02:00
bike.LockInfo.State.Returns(LockingState.Closed); // Requesthandler factory queries lock state to create appropriate request handler object.
2022-09-06 16:08:19 +02:00
var subsequent = handler.HandleRequestOption1().Result;
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(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(
2023-08-31 12:20:06 +02:00
"Reservation could not be canceled!",
"A stable Internet connection is required. Connect to WIFI or to mobile network and activate mobile data. Try again.",
2022-09-06 16:08:19 +02:00
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Cancel reservation.
/// Comment: Canceling reservation fails.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCancelReservationDoCancelReservationException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Cancel reservation for bike {0}?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
connector.Command.DoCancelReservation(bike).Returns(x => throw new Exception("Exception message."));
2023-06-06 12:00:24 +02:00
bike.State.Value.Returns(InUseStateEnum.Reserved); // Requesthandler factory queries state to create appropriate request handler object.
2023-05-09 08:47:52 +02:00
bike.LockInfo.State.Returns(LockingState.Closed); // Requesthandler factory queries lock state to create appropriate request handler object.
2022-09-06 16:08:19 +02:00
var subsequent = handler.HandleRequestOption1().Result;
locks.DidNotReceive().DisconnectAsync(Arg.Any<int>(), Arg.Any<Guid>());
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Booking must be performed
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2023-08-31 12:20:06 +02:00
viewService.DisplayAdvancedAlert(
"Reservation could not be canceled!",
2022-09-06 16:08:19 +02:00
"Exception message.",
2023-08-31 12:20:06 +02:00
"Please try again.",
2022-09-06 16:08:19 +02:00
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: user cancels operation.
/// Final state: Reserved and closed.
/// </summary>
[Test]
public void TestBookAndOpenCancelBook()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(false));
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpen()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, null);
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: COPRI request to book bike fails (WebConnectFailureException). Lock is opened and closed again.
/// Final state: Reserved closed.
/// </summary>
[Test]
public void TestBookAndOpenBookingFailsWebConnectFailureException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
2022-09-06 16:08:19 +02:00
bike.State.Value.Returns(InUseStateEnum.Reserved); // State remains reserved because booking request failed.
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2023-08-31 12:20:06 +02:00
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.",
2022-09-06 16:08:19 +02:00
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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 "reserved closed" after action
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: COPRI request to book bike fails (Exception). Lock is opened and closed again.
/// Final state: Reserved closed.
/// </summary>
[Test]
public void TestBookAndOpenBookingFailsException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open).Returns(x => throw new Exception("Exception message."));
2022-09-06 16:08:19 +02:00
bike.State.Value.Returns(InUseStateEnum.Reserved); // When booking fails state remains reserved.
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // 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(
2023-08-31 12:20:06 +02:00
"Bike could not be rented!",
"Please try again.",
2022-09-06 16:08:19 +02:00
"Exception message.",
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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 "reserved closed" after action
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Open lock & rent bike", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike,
/// Comment: Unlocking bike throws an OutOfReach-Exception.
/// Final state: Reserved unknown.
/// </summary>
[Test]
public void TestBookAndOpenOpenFailsExceptionOutOfReachException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns<Task<LockitLockingState?>>(x => { throw new OutOfReachException(); });
bike.State.Value.Returns(InUseStateEnum.Reserved); // Booking call leads to setting of state to booked.
bike.LockInfo.State.Returns(LockingState.UnknownFromHardwareError);
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2023-08-31 12:20:06 +02:00
viewService.DisplayAdvancedAlert(
"Lock could not be opened!",
"Make sure you have granted Bluetooth permission to the app. Step as close as possible to the bike lock and try again.",
"Attention! Your rental has already started.\r\nIf the lock still won't open, make sure the lock is closed and end rental.\r\nImportant: Send an email to customer support (otherwise your paid rental will continue!) with: Problem description, bike number, drop-off station.",
"OK"
);
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Search lock", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Unlocking bike throws an exception.
/// Final state: Reserved unknown.
/// </summary>
[Test]
public void TestBookAndOpenOpenFailsException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns<Task<LockitLockingState?>>(x => throw new Exception("Exception message.")); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Reserved); // Booking call leads to setting of state to booked.
bike.LockInfo.State.Returns(LockingState.UnknownFromHardwareError);
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2023-08-31 12:20:06 +02:00
viewService.DisplayAdvancedAlert(
"Lock could not be opened!",
"Please try again.",
"Exception message.",
"OK"
);
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Search lock", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Unlocking bike does not return state bike opened.
/// Final state: Reserved unknown.
/// </summary>
[Test]
public void TestBookAndOpenOpenFailsNotOpen()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)null)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Reserved); // Booking state does not change.
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Cancel reservation", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
Assert.AreEqual("Search lock", subsequent.LockitButtonText);
Assert.IsTrue(subsequent.IsLockitButtonVisible);
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpenBatteryPercentageOutOfReachException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
locks[0].GetBatteryPercentageAsync().Returns<Task<double>>(x => throw new OutOfReachException());
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Battery status can only be read when bike is nearby.";
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, null);
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpenBatteryPercentageException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
locks[0].GetBatteryPercentageAsync().Returns<Task<double>>(x => throw new Exception());
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Battery status cannot be read.";
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, null);
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpenUpdateLockingStateFailsWebConnectFailureException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
connector.Command.UpdateLockingStateAsync(bike, null).Returns(x => throw new WebConnectFailureException("Context info", new System.Exception("hoppla")));
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, null);
2023-04-05 15:02:10 +02:00
bikesViewModel.ActionText = "Internet must be available for updating lock status. Please establish an Internet connection!";
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpenUpdateLockingStateFailsException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>()).Returns(x => throw new Exception("Exception message."));
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
bikesViewModel.ActionText = "Connection error on updating locking status.";
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Use case: Open lock and book bike.
/// Comment: Bike gets opened.
/// Final state: Booked open.
/// </summary>
[Test]
public void TestBookAndOpenUpdateLockingStateFailsResponseException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
2023-04-05 15:02:10 +02:00
var geolocation = Substitute.For<IGeolocationService>();
2022-09-06 16:08:19 +02:00
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
geolocation,
locks,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(string.Empty, string.Format("Rent bike {0} and open lock?", "Nr. 0"), "Yes", "No").Returns(Task.FromResult(true));
locks[0].OpenAsync()
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Open)); // Return lock state indicating success
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking call leads to setting of state to booked.
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>()).Returns(x =>
throw new ReturnBikeException(JsonConvert.DeserializeObject<ReservationCancelReturnResponse>(@"{ ""response_text"" : ""Some invalid data received!""}"), "Outer message."));
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Renting bike...";
2022-12-27 21:08:09 +01:00
connector.Command.DoBookAsync(bike, LockingAction.Open); // Booking must be performed
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
locks.Received()[0].OpenAsync(); // Lock must be opened
bikesViewModel.ActionText = "Reading charging level...";
locks[0].GetBatteryPercentageAsync();
bikesViewModel.ActionText = "Updating lock state...";
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
bikesViewModel.ActionText = "Status error on updating lock state.";
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // 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
2023-08-31 12:20:06 +02:00
Assert.AreEqual("Close lock", subsequent.ButtonText);
2022-09-06 16:08:19 +02:00
Assert.IsTrue(subsequent.IsButtonVisible);
2023-08-31 12:20:06 +02:00
Assert.AreEqual(string.Empty, subsequent.LockitButtonText);
Assert.That(subsequent.IsLockitButtonVisible, Is.False);
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:09:46 +02:00
}