sharee.bike-App/SharedBusinessLogic.Tests/Model/Bikes/BikeInfoNS/BikeNS/Command/TestCancelReservationCommand.cs

140 lines
4.2 KiB
C#
Raw Normal View History

2024-04-09 12:53:23 +02:00
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.CancelReservationCommand;
using ShareeBike.Repository.Exception;
namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BikeNS.Command
{
[TestFixture]
public class TestCancelReservationCommand
{
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Reservation canceled successfully
/// </summary>
[Test]
public async Task TestCancelReservation()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<ICancelReservationCommandListener>();
await InvokeAsync<TestCancelReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.DoCancelReservation(Arg.Any<IBikeInfoMutable>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.CancelReservation);
await connector.Command.DoCancelReservation(bike);
});
}
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestCancelReservationNoWebException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<ICancelReservationCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.DoCancelReservation(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
Assert.That(
async () => await InvokeAsync<TestCancelReservationCommand>(
bike,
() => false, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<WebConnectFailureException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.CancelReservation);
await connector.Command.DoCancelReservation(bike);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestCancelReservationGeneralException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<ICancelReservationCommandListener>();
connector.Command.DoCancelReservation(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new Exception("Context info."));
Assert.That(
async () => await InvokeAsync<TestCancelReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<Exception>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.CancelReservation);
await connector.Command.DoCancelReservation(bike);
await listener.ReportStateAsync(State.GeneralCancelReservationError, "Context info.");
});
}
/// <summary>
/// Use case: Cancel reservation.
/// Final state: Still reserved.
/// </summary>
[Test]
public void TestCancelReservationTooManyBikesException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<ICancelReservationCommandListener>();
bike.Id.Returns("1543");
connector.Command.DoCancelReservation(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new InvalidAuthorizationResponseException("mail", new ShareeBike.Repository.Response.ResponseBase()));
Assert.That(
async () => await InvokeAsync<TestCancelReservationCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<InvalidAuthorizationResponseException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.CancelReservation);
await connector.Command.DoCancelReservation(bike);
await listener.ReportStateAsync(State.InvalidResponse, "Your session has expired. Please login new and try again.");
});
}
}
}