2022-10-26 20:53:18 +02:00
using System ;
2021-07-12 21:31:46 +02:00
using System.Threading.Tasks ;
2022-08-30 15:42:25 +02:00
using NSubstitute ;
using NUnit.Framework ;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock ;
2021-07-12 21:31: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-07-12 21:31:46 +02:00
using TINK.Repository.Exception ;
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-07-12 21:31:46 +02:00
using TINK.View ;
using TINK.ViewModel ;
using TINK.ViewModel.Bikes ;
using TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler ;
namespace TestTINKLib.Fixtures.ObjectTests.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestDisposableOpen
{
/// <summary>
/// Test construction of object.
/// </summary>
[Test]
public void Testctor ( )
{
var handler = new DisposableOpen (
Substitute . For < IBikeInfoMutable > ( ) ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > Substitute . For < IConnector > ( ) ,
Substitute . For < IGeolocation > ( ) ,
Substitute . For < ILocksService > ( ) ,
( ) = > Substitute . For < IPollingUpdateTaskManager > ( ) ,
Substitute . For < ISmartDevice > ( ) ,
Substitute . For < IViewService > ( ) ,
Substitute . For < IBikesViewModel > ( ) ,
Substitute . For < IUser > ( ) ) ;
// Verify prerequisites.
Assert . AreEqual ( "Rent bike or close lock" , handler . ButtonText ) ;
Assert . IsTrue ( handler . IsButtonVisible ) ;
Assert . AreEqual ( nameof ( DisposableOpen ) , handler . LockitButtonText ) ;
Assert . IsFalse ( handler . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Disposable Closed.
/// </summary>
[Test]
public void TestCloseLock ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( false ) ) ;
locks [ 0 ] . CloseAsync ( ) . Returns ( Task . FromResult ( ( LockitLockingState ? ) LockitLockingState . Closed ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Disposable ) ; // Reqesthandler factory queries state to create appropriate request handler object.
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
2022-12-07 16:54:52 +01:00
bikesViewModel . ActionText = "<h4><b>Lock is closing.<br/>Please wait until it is completely closed.</b></h4>" ;
2022-09-06 16:08:19 +02:00
locks [ 0 ] . CloseAsync ( ) ;
bikesViewModel . ActionText = "Disconnecting lock..." ;
locks . DisconnectAsync ( Arg . Any < int > ( ) , Arg . Any < Guid > ( ) ) ;
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 . AreEqual ( "Reserve bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( "DisposableDisconnected" , subsequent . LockitButtonText ) ;
Assert . IsFalse ( subsequent . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCloseLockCloseFailsOutOfReachExcption ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( false ) ) ;
locks [ 0 ] . CloseAsync ( ) . Returns < Task < LockitLockingState ? > > ( x = > throw new OutOfReachException ( ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Disposable ) ; // Reqesthandler factory queries state to create appropriate request handler object.
bike . LockInfo . State . Returns ( LockingState . Open ) ;
var subsequent = handler . HandleRequestOption1 ( ) . Result ;
locks . DidNotReceive ( ) . DisconnectAsync ( Arg . Any < int > ( ) , Arg . Any < Guid > ( ) ) ;
// Verify behaviour
Received . InOrder ( ( ) = >
{
bikesViewModel . Received ( 1 ) . IsIdle = false ; // GUI must be locked
bikesViewModel . ActionText = "One moment please..." ;
pollingManager . StopUpdatePeridically ( ) ; // Polling must be stopped before any COPR and lock service action
2022-12-07 16:54:52 +01:00
bikesViewModel . ActionText = "<h4><b>Lock is closing.<br/>Please wait until it is completely closed.</b></h4>" ;
2022-09-06 16:08:19 +02:00
locks [ 0 ] . CloseAsync ( ) ;
bikesViewModel . ActionText = "" ;
viewService . DisplayAlert ( "Lock can not be closed!" , "Lock cannot be closed until bike is near." , "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 . AreEqual ( "Reserve bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( nameof ( DisposableDisconnected ) , subsequent . LockitButtonText ) ;
Assert . IsFalse ( subsequent . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Same as initial state.
/// </summary>
[Test]
public void TestCloseLockCloseFailsExcption ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( false ) ) ;
locks [ 0 ] . CloseAsync ( ) . Returns < Task < LockitLockingState ? > > ( x = > throw new Exception ( "Exception message." ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Disposable ) ; // Reqesthandler factory queries state to create appropriate request handler object.
bike . LockInfo . State . Returns ( LockingState . Open ) ;
var subsequent = handler . HandleRequestOption1 ( ) . Result ;
locks . DidNotReceive ( ) . DisconnectAsync ( Arg . Any < int > ( ) , Arg . Any < Guid > ( ) ) ;
// Verify behaviour
Received . InOrder ( ( ) = >
{
bikesViewModel . Received ( 1 ) . IsIdle = false ; // GUI must be locked
bikesViewModel . ActionText = "One moment please..." ;
pollingManager . StopUpdatePeridically ( ) ; // Polling must be stopped before any COPR and lock service action
2022-12-07 16:54:52 +01:00
bikesViewModel . ActionText = "<h4><b>Lock is closing.<br/>Please wait until it is completely closed.</b></h4>" ;
2022-09-06 16:08:19 +02:00
locks [ 0 ] . CloseAsync ( ) ;
bikesViewModel . ActionText = "" ;
viewService . DisplayAlert ( "Lock can not be closed!" , "Exception message." , "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 . AreEqual ( "Reserve bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( nameof ( DisposableDisconnected ) , subsequent . LockitButtonText ) ;
Assert . IsFalse ( subsequent . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Booked Open.
/// </summary>
[Test]
public void TestDoBook ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
bike . Id . Returns ( "0" ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( true ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Booked ) ;
bike . LockInfo . State . Returns ( LockingState . Open ) ;
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 = "Reading charging level..." ;
locks [ 0 ] . GetBatteryPercentageAsync ( ) ;
bikesViewModel . ActionText = "Renting bike..." ;
2022-12-27 21:08:09 +01:00
connector . Command . DoBookAsync ( bike ) ;
2022-09-06 16:08:19 +02:00
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 . AreEqual ( "Close lock & return bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( "Close lock" , subsequent . LockitButtonText ) ;
Assert . IsTrue ( subsequent . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Disposabled Closed.
/// </summary>
[Test]
public void TestDoBookDoBookFailsWebConnectFailureException ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
bike . Id . Returns ( "0" ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( true ) ) ;
2022-12-27 21:08:09 +01:00
connector . Command . DoBookAsync ( bike ) . Returns ( x = > throw new WebConnectFailureException ( "Context info." , new Exception ( "chub" ) ) ) ;
2022-09-06 16:08:19 +02:00
locks [ 0 ] . CloseAsync ( ) . Returns ( Task . FromResult ( ( LockitLockingState ? ) LockitLockingState . Closed ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Disposable ) ;
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 = "Reading charging level..." ;
locks [ 0 ] . GetBatteryPercentageAsync ( ) ;
bikesViewModel . ActionText = "Renting bike..." ;
2022-12-27 21:08:09 +01:00
connector . Command . DoBookAsync ( bike ) ;
2022-09-06 16:08:19 +02:00
bikesViewModel . ActionText = "" ;
viewService . DisplayAlert (
"Connection error when renting the bike!" ,
2022-10-26 20:53:18 +02:00
string . Format ( "Attention: Lock is closed!\r\n{0}\r\n{1}" , "Context info." , "Is WIFI available/ mobile network available and mobile data activated / ... ?" ) ,
2022-09-06 16:08:19 +02:00
"OK" ) ;
bikesViewModel . ActionText = "Verschließe Schloss..." ;
locks [ 0 ] . CloseAsync ( ) ;
bikesViewModel . ActionText = "Disconnecting lock..." ;
locks . DisconnectAsync ( Arg . Any < int > ( ) , Arg . Any < Guid > ( ) ) ;
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 . AreEqual ( "Reserve bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( "DisposableDisconnected" , subsequent . LockitButtonText ) ;
Assert . IsFalse ( subsequent . IsLockitButtonVisible ) ;
}
/// <summary>
/// Use case: Lock bike.
/// Final state: Disposabled Closed.
/// </summary>
[Test]
public void TestDoBookDoBookFailsException ( )
{
var bike = Substitute . For < IBikeInfoMutable > ( ) ;
var connector = Substitute . For < IConnector > ( ) ;
var command = Substitute . For < ICommand > ( ) ;
var geolocation = Substitute . For < IGeolocation > ( ) ;
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 DisposableOpen (
bike ,
( ) = > true , // isConnectedDelegate
( isConnexted ) = > connector ,
geolocation ,
locks ,
( ) = > pollingManager ,
Substitute . For < ISmartDevice > ( ) ,
viewService ,
bikesViewModel ,
activeUser ) ;
bike . Id . Returns ( "0" ) ;
viewService . DisplayAlert ( string . Empty , "Fahrrad Nr. 0 mieten oder Schloss schließen?" , "Mieten" , "Schloss schließen" ) . Returns ( Task . FromResult ( true ) ) ;
2022-12-27 21:08:09 +01:00
connector . Command . DoBookAsync ( bike ) . Returns ( x = > throw new Exception ( "Exception message." ) ) ;
2022-09-06 16:08:19 +02:00
locks [ 0 ] . CloseAsync ( ) . Returns ( Task . FromResult ( ( LockitLockingState ? ) LockitLockingState . Closed ) ) ;
bike . State . Value . Returns ( InUseStateEnum . Disposable ) ;
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 = "Reading charging level..." ;
locks [ 0 ] . GetBatteryPercentageAsync ( ) ;
bikesViewModel . ActionText = "Renting bike..." ;
2022-12-27 21:08:09 +01:00
connector . Command . DoBookAsync ( bike ) ;
2022-09-06 16:08:19 +02:00
bikesViewModel . ActionText = "" ;
viewService . DisplayAlert ( "Error when renting the bike!" , "Attention: Lock is closed!\r\nException message." , "OK" ) ;
bikesViewModel . ActionText = "Verschließe Schloss..." ;
locks [ 0 ] . CloseAsync ( ) ;
bikesViewModel . ActionText = "Disconnecting lock..." ;
locks . DisconnectAsync ( Arg . Any < int > ( ) , Arg . Any < Guid > ( ) ) ;
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 . AreEqual ( "Reserve bike" , subsequent . ButtonText ) ;
Assert . IsTrue ( subsequent . IsButtonVisible ) ;
Assert . AreEqual ( "DisposableDisconnected" , subsequent . LockitButtonText ) ;
Assert . IsFalse ( subsequent . IsLockitButtonVisible ) ;
}
}
2021-07-12 21:31:46 +02:00
}