sharee.bike-App/SharedBusinessLogic.Tests/Model/Bikes/BikeInfoNS/BikeNS/Command/TestStartRentalCommand.cs
2024-04-09 12:53:23 +02:00

206 lines
5.9 KiB
C#

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
{
/// <summary>
/// Use case: Start rental.
/// Final state: Rental started successfully
/// </summary>
[Test]
public async Task TestLockClosedStartRental()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
await InvokeAsync<TestStartRentalCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>(), LockingAction.Open);
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike, LockingAction.Open);
});
}
/// <summary>
/// Use case: Start rental.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestLockClosedStartRentalNoWebException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>(), LockingAction.Open).Returns(x
=> throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
Assert.That(
async () => await InvokeAsync<TestStartRentalCommand>(
bike,
() => false, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<WebConnectFailureException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike, LockingAction.Open);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
/// <summary>
/// Use case: Start rental.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestLockClosedStartRentalGeneralException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>(), LockingAction.Open).Returns(x
=> throw new Exception("Context info."));
Assert.That(
async () => await InvokeAsync<TestStartRentalCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<Exception>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike, LockingAction.Open);
await listener.ReportStateAsync(State.GeneralStartRentalError, "Context info.");
});
}
/// <summary>
/// Use case: Start rental.
/// Final state: Rental started successfully
/// </summary>
[Test]
public async Task TestLockOpenStartRental()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Open);
await InvokeAsync<TestStartRentalCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike);
});
}
/// <summary>
/// Use case: Start rental.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestLockOpenStartRentalNoWebException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Open);
connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
Assert.That(
async () => await InvokeAsync<TestStartRentalCommand>(
bike,
() => false, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<WebConnectFailureException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
/// <summary>
/// Use case: Start rental.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestLockOpenStartRentalGeneralException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartRentalCommandListener>();
bike.LockInfo.State.Returns(LockingState.Open);
connector.Command.DoBookAsync(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new Exception("Context info."));
Assert.That(
async () => await InvokeAsync<TestStartRentalCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<Exception>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.RentBike);
await connector.Command.DoBookAsync(bike);
await listener.ReportStateAsync(State.GeneralStartRentalError, "Context info.");
});
}
}
}