Version 3.0.337

This commit is contained in:
Anja Müller-Meißner 2022-08-30 15:42:25 +02:00
parent fd0e63cf10
commit 573fe77e12
2336 changed files with 33688 additions and 86082 deletions

View file

@ -0,0 +1,220 @@
using System;
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
{
[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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when opening the lock!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error while opening lock!",
"Some error",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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);
}
}
}

View file

@ -0,0 +1,221 @@
using System;
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
{
[TestFixture]
public class TestBookedOpen
{
/// <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 BookedOpen(
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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when opening the lock!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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 BookedOpen(
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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error while opening lock!",
"Some error",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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 BookedOpen(
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
bikesViewModel.ActionText = "Opening lock...";
connector.Command.OpenLockAsync(bike); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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);
}
}
}

View file

@ -0,0 +1,656 @@
using System;
using System.Threading.Tasks;
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
{
public class TestDisposableClosed
{
/// <summary>
/// Use case: Start booking and cancel.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestBookAndReleaseCancel()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Rent bike Nr. 0 and open lock?",
"Yes",
"No").Returns(Task.FromResult(false)); // User chooes "No"
var subsequent = handler.HandleRequestOption1().Result;
// Verify behaviour
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
viewService.DisplayAlert(
string.Empty,
"Rent bike Nr. 0 and open lock?",
"Yes",
"No");
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book bike and release from station.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestBookAndReleaseBookAndOpenAyncFailsWebConnectFailureException()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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.
connector.Command.BookAndOpenAync(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // State remains available because booking request failed.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when renting the bike!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book bike and release from station.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestBookAndReleaseBookAndOpenAyncFailsException()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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.
connector.Command.BookAndOpenAync(bike).Returns(x => throw new Exception("Some error", new Exception("Inner exception")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // State remains available because booking request failed.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error when renting the bike!",
"Some error",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Book bike and release from station.
/// Final state: Bike is booked and released from station.
/// </summary>
[Test]
public void TestBookAndRelease()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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.Open); // After Booking and opening, lock state is open.
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked); // After Booking and opening, state is booked.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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);
}
/// <summary>
/// Use case: Cancel reserving.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestReserveCancel()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No").Returns(Task.FromResult(false)); // User chooes "No"
var subsequent = handler.HandleRequestOption2().Result;
// Verify behaviour
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No");
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book reserve bike.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestReserveDoReserveFailsBookingDeclinedException()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
connector.Command.DoReserve(bike).Returns(x => throw new BookingDeclinedException(4));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // State remains available because booking request 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
bikesViewModel.ActionText = "Reserving bike...";
connector.Command.DoReserve(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Hint",
"A reservation of bike Nr. 0 was rejected because the maximum allowed number of 4 reservations/ rentals had already been made.",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book reserve bike.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestReserveDoReserveFailsWebConnectFailureException()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
connector.Command.DoReserve(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // State remains available because booking request 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
bikesViewModel.ActionText = "Reserving bike...";
connector.Command.DoReserve(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when reserving the bike!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book reserve bike.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestReserveDoReserveFailsException()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
connector.Command.DoReserve(bike).Returns(x => throw new Exception("Some error", new Exception("Inner exception")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // State remains available because booking request 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
bikesViewModel.ActionText = "Reserving bike...";
connector.Command.DoReserve(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error when reserving the bike!",
"Some error",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Reserve bike.
/// Final state: Bike is booked and released from station.
/// </summary>
[Test]
public void TestReserve()
{
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 DisposableClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Reserve bike Nr. 0 free of charge for 15 min?",
"Yes",
"No").Returns(Task.FromResult(true));
bike.LockInfo.State.Returns(LockingState.Open); // After Booking and opening, lock state is open.
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked); // After Booking and opening, lock state is open.
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
bikesViewModel.ActionText = "Reserving bike...";
connector.Command.DoReserve(bike); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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);
}
}
}

View file

@ -0,0 +1,204 @@
using System;
using NSubstitute;
using NUnit.Framework;
using TINK;
using TINK.Model.Bikes.BikeInfoNS.CopriLock;
using TINK.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS;
using TINK.Model.Connector;
using TINK.Model.Device;
using TINK.Model.User;
using TINK.View;
using TINK.ViewModel;
using TINK.ViewModel.Bikes;
using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
namespace TestShareeLib.ViewModel.Bikes.Bike.CopriLock.RequestHandler
{
[TestFixture]
public class TestFeedbackPending
{
/// <summary>
/// Use case: Give feedback but submission fails.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedbackDoSubmitFeedbackFailsException()
{
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 FeedbackPending(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
connector.Command.DoSubmitFeedback(
Arg.Any<TINK.Model.Connector.IUserFeedback>(),
Arg.Any<Uri>()).Returns(x => throw new Exception("Context info", new Exception("Tst")));
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
viewService.DisplayUserFeedbackPopup(Arg.Any<IBattery>(), Arg.Any<string>());
bikesViewModel.ActionText = "Submitting feedback...";
connector.Command.DoSubmitFeedback(Arg.Any<TINK.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Submitting feedback failed!",
"Your feedback could not be send to server successfully.",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Give Feedback"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Give feedback.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedback()
{
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 FeedbackPending(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
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
viewService.DisplayUserFeedbackPopup(Arg.Any<IBattery>(), Arg.Any<string>());
bikesViewModel.ActionText = "Submitting feedback...";
connector.Command.DoSubmitFeedback(Arg.Any<TINK.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Give Feedback"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Give feedback.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedbackAndQuerry()
{
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 FeedbackPending(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.BookingFinishedModel.MiniSurvey.Questions.Count.Returns(1);
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
viewService.DisplayUserFeedbackPopup(Arg.Any<IBattery>(), Arg.Any<string>());
bikesViewModel.ActionText = "Submitting feedback...";
connector.Command.DoSubmitFeedback(Arg.Any<TINK.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
viewService.PushModalAsync(ViewTypes.MiniSurvey);
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Give Feedback"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
}
}

View file

@ -0,0 +1,641 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
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.Repository.Response;
using TINK.View;
using TINK.ViewModel;
using TINK.ViewModel.Bikes;
using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
namespace TestShareeLib.ViewModel.Bikes.Bike.CopriLock.RequestHandler
{
[TestFixture]
public class TestReservedClosed
{
/// <summary>
/// Use case: Cancel reservation.
/// 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>();
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,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No").Returns(Task.FromResult(false)); // User chooes "No"
var subsequent = handler.HandleRequestOption1().Result;
// Verify behaviour
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No");
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
[Test]
public void TestCancelReservationDoCancelReservationInvalidAuthorizationResponseException()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No").Returns(Task.FromResult(true));
connector.Command.DoCancelReservation(bike).Returns(x => throw new InvalidAuthorizationResponseException(
"otto@mustermann.de", JsonConvert.DeserializeObject<ResponseBase>(@"{}")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved); // Locking state does not change.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error when canceling the reservation!",
"Can not login user otto@mustermann.de. Mail address unknown or password invalid.",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
[Test]
public void TestCancelReservationDoCancelReservationFailsWebConnectFailureException()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No").Returns(Task.FromResult(true));
connector.Command.DoCancelReservation(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved); // Locking state does not change.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when canceling the reservation!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try cancel reservation.
/// Final state: Bike remains reserved.
/// </summary>
[Test]
public void TestCancelReservationDoCancelReservationFailsException()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No").Returns(Task.FromResult(true));
connector.Command.DoCancelReservation(bike).Returns(x => throw new Exception("Some error", new Exception("Inner exception")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved); // Locking state does not change.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Cancel reservation must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAlert(
"Error when canceling the reservation!",
"Some error",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Bike is available again.
/// </summary>
[Test]
public void TestCancelReservation()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Cancel reservation for bike Nr. 0?",
"Yes",
"No").Returns(Task.FromResult(true));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable); // Reservation gets canceled.
var subsequent = handler.HandleRequestOption1().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
bikesViewModel.ActionText = "Canceling reservation...";
connector.Command.DoCancelReservation(bike); // Cancel reservation must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Reserve bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Cancel booking.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestBookAndReleaseCancel()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.Id.Returns("0");
viewService.DisplayAlert(
string.Empty,
"Rent bike Nr. 0 and open lock?",
"Yes",
"No").Returns(Task.FromResult(false)); // User chooes "No"
var subsequent = handler.HandleRequestOption2().Result;
// Verify behaviour
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
viewService.DisplayAlert(
string.Empty,
"Rent bike Nr. 0 and open lock?",
"Yes",
"No");
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book bike and release from station.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestBookAndReleaseBookAndOpenAyncFailsWebConnectFailureException()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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));
connector.Command.BookAndOpenAync(bike).Returns(x => throw new WebConnectFailureException("Context info", new Exception("Tst")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved); // State remains reserved because booking request 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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Connection error when renting the bike!",
"Is WIFI available/ mobile networt available and mobile data activated / ... ?",
"Context info",
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Try book bike and release from station.
/// Final state: Bike is disposable.
/// </summary>
[Test]
public void TestBookAndReleaseBookAndOpenAyncFailsException()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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));
connector.Command.BookAndOpenAync(bike).Returns(x => throw new Exception("Some error", new Exception("Inner exception")));
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved); // State remains reserved because booking request 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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "";
viewService.DisplayAdvancedAlert(
"Error when renting the bike!",
"Some error",
String.Empty,
"OK");
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("Cancel bike reservation"));
Assert.That(
subsequent.IsButtonVisible,
Is.True);
Assert.That(
subsequent.LockitButtonText,
Is.EqualTo("Open lock & rent bike"));
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Book bike and release from station.
/// Final state: Bike is booked and released from station.
/// </summary>
[Test]
public void TestBookAndRelease()
{
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 ReservedClosed(
bike,
() => true, // isConnectedDelegate
(isConnexted) => connector,
() => 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.Open); // After Booking and 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
bikesViewModel.ActionText = "Renting bike...";
connector.Command.BookAndOpenAync(bike); // Booking must be performed
bikesViewModel.ActionText = "Updating...";
pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again
bikesViewModel.ActionText = "";
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);
}
}
}

View file

@ -0,0 +1,147 @@
using System;
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.View;
using TINK.ViewModel;
using TINK.ViewModel.Bikes;
using TINK.ViewModel.Bikes.Bike.CopriLock;
using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
namespace TestShareeLib.ViewModel.Bikes.Bike.CopriLock
{
[TestFixture]
public class TestReqeustHandlerFactory
{
[Test]
public void TestCreateDisposable()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var smartDevice = Substitute.For<ISmartDevice>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Disposable);
var requestHandler = RequestHandlerFactory.Create(
bike,
() => true /*isConnectedDelegate*/,
(isconnected) => connector,
() => pollingManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
Assert.That(
requestHandler.GetType(),
Is.EqualTo(typeof(DisposableClosed)));
}
[Test]
public void TestCreateReserved()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var smartDevice = Substitute.For<ISmartDevice>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Reserved);
var requestHandler = RequestHandlerFactory.Create(
bike,
() => true /*isConnectedDelegate*/,
(isconnected) => connector,
() => pollingManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
Assert.That(
requestHandler.GetType(),
Is.EqualTo(typeof(ReservedClosed)));
}
[Test]
public void TestCreateBooked()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var smartDevice = Substitute.For<ISmartDevice>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.Booked);
foreach (LockingState state in Enum.GetValues(typeof(LockingState)))
{
bike.LockInfo.State.Returns(state);
var requestHandler = RequestHandlerFactory.Create(
bike,
() => true /*isConnectedDelegate*/,
(isconnected) => connector,
() => pollingManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
Assert.That(
requestHandler.GetType(),
Is.EqualTo(state == LockingState.Closed
? typeof(BookedClosed)
: typeof(BookedOpen)));
}
}
[Test]
public void TestCreateFeedbackPending()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var smartDevice = Substitute.For<ISmartDevice>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
bike.State.Value.Returns(TINK.Model.State.InUseStateEnum.FeedbackPending);
foreach (LockingState state in Enum.GetValues(typeof(LockingState)))
{
bike.LockInfo.State.Returns(state);
var requestHandler = RequestHandlerFactory.Create(
bike,
() => true /*isConnectedDelegate*/,
(isconnected) => connector,
() => pollingManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
Assert.That(
requestHandler.GetType(),
Is.EqualTo(typeof(FeedbackPending)));
}
}
}
}