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

148 lines
4.4 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.Geolocation;
using ShareeBike.Repository.Request;
using ShareeBike.Model.Connector;
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartReservationCommand;
using ShareeBike.Model;
using ShareeBike.Repository.Exception;
namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BikeNS.Command
{
[TestFixture]
public class TestStartReservationCommand
{
/// <summary>
/// Use case: Start reservation.
/// Final state: Reservation started successfully
/// </summary>
[Test]
public async Task TestStartReservation()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartReservationCommandListener>();
await InvokeAsync<TestStartReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.DoReserve(Arg.Any<IBikeInfoMutable>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.ReserveBike);
await connector.Command.DoReserve(bike);
});
}
/// <summary>
/// Use case: Start reservation.
/// Final state: Reservation failed.
/// </summary>
[Test]
public void TestStartReservationNoWebException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartReservationCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.DoReserve(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
Assert.That(
async () => await InvokeAsync<TestStartReservationCommand>(
bike,
() => false, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<WebConnectFailureException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.ReserveBike);
await connector.Command.DoReserve(bike);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
/// <summary>
/// Use case: Start reservation.
/// Final state: Reservation failed.
/// </summary>
[Test]
public void TestStartReservationGeneralException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartReservationCommandListener>();
connector.Command.DoReserve(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new Exception("Context info."));
Assert.That(
async () => await InvokeAsync<TestStartReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<Exception>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.ReserveBike);
await connector.Command.DoReserve(bike);
await listener.ReportStateAsync(State.GeneralStartReservationError, "Context info.");
});
}
/// <summary>
/// Use case: Start reservation.
/// Final state: Reservation failed.
/// </summary>
[Test]
[Ignore("Exception needs to be initialized correctly.")]
public void TestStartReservationTooManyBikesException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IStartReservationCommandListener>();
bike.Id.Returns("1543");
BookingDeclinedException.IsBookingDeclined("Too many bikes", out BookingDeclinedException bookingDeclinedException);
bookingDeclinedException.MaxBikesCount.Returns(3);
connector.Command.DoReserve(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw bookingDeclinedException);
Assert.That(
async () => await InvokeAsync<TestStartReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<BookingDeclinedException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.ReserveBike);
await connector.Command.DoReserve(bike);
await listener.ReportStateAsync(State.TooManyBikesError, "Too many bikes");
});
}
}
}