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
{
///
/// Use case: Cancel reservation.
/// Final state: Reservation canceled successfully
///
[Test]
public async Task TestCancelReservation()
{
var bike = Substitute.For();
var connector = Substitute.For();
var listener = Substitute.For();
await InvokeAsync(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.DoCancelReservation(Arg.Any());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.CancelReservation);
await connector.Command.DoCancelReservation(bike);
});
}
///
/// Use case: Cancel reservation.
/// Final state: Still reserved.
///
[Test]
public void TestCancelReservationNoWebException()
{
var bike = Substitute.For();
var connector = Substitute.For();
var listener = Substitute.For();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.DoCancelReservation(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.CancelReservation);
await connector.Command.DoCancelReservation(bike);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
///
/// Use case: Cancel reservation.
/// Final state: Still reserved.
///
[Test]
public void TestCancelReservationGeneralException()
{
var bike = Substitute.For();
var connector = Substitute.For();
var listener = Substitute.For();
connector.Command.DoCancelReservation(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.CancelReservation);
await connector.Command.DoCancelReservation(bike);
await listener.ReportStateAsync(State.GeneralCancelReservationError, "Context info.");
});
}
///
/// Use case: Cancel reservation.
/// Final state: Still reserved.
///
[Test]
public void TestCancelReservationTooManyBikesException()
{
var bike = Substitute.For();
var connector = Substitute.For();
var listener = Substitute.For();
bike.Id.Returns("1543");
connector.Command.DoCancelReservation(Arg.Any()).Returns(x
=> throw new InvalidAuthorizationResponseException("mail", new ShareeBike.Repository.Response.ResponseBase()));
Assert.That(
async () => await InvokeAsync(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf());
// 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.");
});
}
}
}