using System; using System.Threading.Tasks; using NSubstitute; using NUnit.Framework; using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock; using ShareeBike.Model.Connector; using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartRentalCommand; using ShareeBike.Repository.Exception; namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BikeNS.Command { [TestFixture] public class TestStartRentalCommand { /// /// Use case: Start rental. /// Final state: Rental started successfully /// [Test] public async Task TestLockClosedStartRental() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Closed); await InvokeAsync( bike, () => true, // isConnectedDelegate (isConnected) => connector, listener); await connector.Command.DoBookAsync(Arg.Any(), LockingAction.Open); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike, LockingAction.Open); }); } /// /// Use case: Start rental. /// Final state: Still reserved. /// [Test] public void TestLockClosedStartRentalNoWebException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Closed); connector.Command.DoBookAsync(Arg.Any(), LockingAction.Open).Returns(x => throw new WebConnectFailureException("Context info.", new System.Exception("hoppla"))); Assert.That( async () => await InvokeAsync( bike, () => false, // isConnectedDelegate (isConnected) => connector, listener), Throws.InstanceOf()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike, LockingAction.Open); await listener.ReportStateAsync(State.WebConnectFailed, "Context info."); }); } /// /// Use case: Start rental. /// Final state: Still reserved. /// [Test] public void TestLockClosedStartRentalGeneralException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Closed); connector.Command.DoBookAsync(Arg.Any(), LockingAction.Open).Returns(x => throw new Exception("Context info.")); Assert.That( async () => await InvokeAsync( bike, () => true, // isConnectedDelegate (isConnected) => connector, listener), Throws.InstanceOf()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike, LockingAction.Open); await listener.ReportStateAsync(State.GeneralStartRentalError, "Context info."); }); } /// /// Use case: Start rental. /// Final state: Rental started successfully /// [Test] public async Task TestLockOpenStartRental() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Open); await InvokeAsync( bike, () => true, // isConnectedDelegate (isConnected) => connector, listener); await connector.Command.DoBookAsync(Arg.Any()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike); }); } /// /// Use case: Start rental. /// Final state: Still reserved. /// [Test] public void TestLockOpenStartRentalNoWebException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Open); connector.Command.DoBookAsync(Arg.Any()).Returns(x => throw new WebConnectFailureException("Context info.", new System.Exception("hoppla"))); Assert.That( async () => await InvokeAsync( bike, () => false, // isConnectedDelegate (isConnected) => connector, listener), Throws.InstanceOf()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike); await listener.ReportStateAsync(State.WebConnectFailed, "Context info."); }); } /// /// Use case: Start rental. /// Final state: Still reserved. /// [Test] public void TestLockOpenStartRentalGeneralException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Open); connector.Command.DoBookAsync(Arg.Any()).Returns(x => throw new Exception("Context info.")); Assert.That( async () => await InvokeAsync( bike, () => true, // isConnectedDelegate (isConnected) => connector, listener), Throws.InstanceOf()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.RentBike); await connector.Command.DoBookAsync(bike); await listener.ReportStateAsync(State.GeneralStartRentalError, "Context info."); }); } } }