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 { /// /// Use case: Cancel reservation. /// Final state: Same as initial state. /// [Test] public void TestCancelReservationCancel() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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(@"{}"))); 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 = string.Empty; 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 = string.Empty; 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(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; viewService.DisplayAdvancedAlert( "Connection error when canceling the reservation!", "Is WIFI available/ mobile network available and mobile data activated / ... ?", "Context info", "OK"); bikesViewModel.ActionText = "Updating..."; pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again bikesViewModel.ActionText = string.Empty; 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); } /// /// Use case: Try cancel reservation. /// Final state: Bike remains reserved. /// [Test] public void TestCancelReservationDoCancelReservationFailsException() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; viewService.DisplayAlert( "Error when canceling the reservation!", "Some error", "OK"); bikesViewModel.ActionText = "Updating..."; pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again bikesViewModel.ActionText = string.Empty; 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); } /// /// Use case: Cancel reservation. /// Final state: Bike is available again. /// [Test] public void TestCancelReservation() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; 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); } /// /// Use case: Cancel booking. /// Final state: Same as initial state. /// [Test] public void TestBookAndReleaseCancel() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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); } /// /// Use case: Try book bike and release from station. /// Final state: Bike is disposable. /// [Test] public void TestBookAndReleaseBookAndOpenAyncFailsWebConnectFailureException() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; viewService.DisplayAdvancedAlert( "Connection error when renting the bike!", "Is WIFI available/ mobile network available and mobile data activated / ... ?", "Context info", "OK"); bikesViewModel.ActionText = "Updating..."; pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again bikesViewModel.ActionText = string.Empty; 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); } /// /// Use case: Try book bike and release from station. /// Final state: Bike is disposable. /// [Test] public void TestBookAndReleaseBookAndOpenAyncFailsException() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; viewService.DisplayAdvancedAlert( "Error when renting the bike!", "Some error", String.Empty, "OK"); bikesViewModel.ActionText = "Updating..."; pollingManager.StartUpdateAyncPeridically(); // polling must be restarted again bikesViewModel.ActionText = string.Empty; 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); } /// /// Use case: Book bike and release from station. /// Final state: Bike is booked and released from station. /// [Test] public void TestBookAndRelease() { var bike = Substitute.For(); var connector = Substitute.For(); var command = Substitute.For(); var pollingManager = Substitute.For(); var viewService = Substitute.For(); var bikesViewModel = Substitute.For(); var activeUser = Substitute.For(); var handler = new ReservedClosed( bike, () => true, // isConnectedDelegate (isConnexted) => connector, () => pollingManager, Substitute.For(), 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 = string.Empty; 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); } } }