mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-06-22 13:57:28 +02:00
Version 3.0.370
This commit is contained in:
parent
f5cf9bb22f
commit
bdb2dec1c1
233 changed files with 10252 additions and 6779 deletions
|
@ -0,0 +1,491 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NSubstitute;
|
||||
using NUnit.Framework;
|
||||
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using TINK.Model.Connector;
|
||||
using TINK.Model.State;
|
||||
using TINK.Repository.Request;
|
||||
using TINK.Services.BluetoothLock;
|
||||
using TINK.Services.BluetoothLock.Tdo;
|
||||
using TINK.Services.Geolocation;
|
||||
using Geolocation = TINK.Services.Geolocation.Geolocation;
|
||||
using static TINK.Model.Bikes.BikeInfoNS.BluetoothLock.Command.CloseCommand;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using TINK.Services.BluetoothLock.Exception;
|
||||
using TINK.Repository.Exception;
|
||||
using Newtonsoft.Json;
|
||||
using TINK.Repository.Response;
|
||||
using TINK.ViewModel;
|
||||
|
||||
namespace TestShareeLib.Model.Bikes.BikeInfoNS.BluetoothLock.Command
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestCloseCommand
|
||||
{
|
||||
[Test]
|
||||
public async Task TestCloseGeolocationServiceGetAsyncFails()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
var startOfTest = DateTime.Now;
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Throws(new Exception("Ups no location..."));
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Closed)); // Return lock state indicating success
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
() => true, // isConnectedDelegate
|
||||
(isConnexted) => connector,
|
||||
listener, null);
|
||||
|
||||
// Verify that location is kept because bike might be returned later.
|
||||
Assert.That(
|
||||
bike.LockInfo.Location,
|
||||
Is.Null);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
await listener.Received().ReportStateAsync(State.StartGeolocationException, "Ups no location...");
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use case: Close lock
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task TestClose()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(Task.FromResult((LockitLockingState?)LockitLockingState.Closed)); // Return lock state indicating success
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
() => true, // isConnectedDelegate
|
||||
(isConnexted) => connector,
|
||||
listener, null);
|
||||
|
||||
// Verify that location is kept because bike might be returned later.
|
||||
Assert.That(
|
||||
bike.LockInfo.Location.Latitude,
|
||||
Is.EqualTo(47.99).Within(0.001));
|
||||
Assert.That(
|
||||
bike.LockInfo.Location.Longitude,
|
||||
Is.EqualTo(7.78).Within(0.001));
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.Closed));
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
listener.Received().ReportStep(Step.WaitStopPollingQueryLocation);
|
||||
listener.Received().ReportStep(Step.QueryLocationTerminated);
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsOutOfReachException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new OutOfReachException());
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
() => true, // isConnectedDelegate
|
||||
(isConnexted) => connector,
|
||||
listener, null),
|
||||
Throws.InstanceOf<OutOfReachException>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.Received().ReportStateAsync(State.OutOfReachError, "Exception of type 'TINK.Services.BluetoothLock.Exception.OutOfReachException' was thrown.");
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.UnknownDisconnected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsCouldntCloseMovingException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new CouldntCloseMovingException());
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(
|
||||
bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null),
|
||||
Throws.InstanceOf<CouldntCloseMovingException>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.Received().ReportStateAsync(State.CouldntCloseMovingError, "The process is motion sensitive. Step close to the lock, do not move, and try again.");
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.Open));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsCouldntCloseBoltBlockedException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new CouldntCloseBoltBlockedException());
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null),
|
||||
Throws.InstanceOf<CouldntCloseBoltBlockedException>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.Received().ReportStateAsync(State.CouldntCloseBoltBlockedError, "Lock bolt is blocked. Make sure that no spoke or any other obstacle prevents the lock from closing and try again.");
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.UnknownFromHardwareError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsCouldntCloseBoltBlockedExceptionOpen()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new CouldntCloseBoltBlockedException(LockingState.Open));
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null),
|
||||
Throws.InstanceOf<CouldntCloseBoltBlockedException>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.Received().ReportStateAsync(State.CouldntCloseBoltBlockedError, "Lock bolt is blocked. Make sure that no spoke or any other obstacle prevents the lock from closing and try again.");
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.Open));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsCouldntCloseInconsistentState()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new CouldntCloseInconsistentStateExecption(LockitLockingState.Unknown.GetLockingState()));
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
bike.LockInfo.State.Returns(LockingState.Open);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null),
|
||||
Throws.InstanceOf<CouldntCloseInconsistentStateExecption>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.Received().ReportStateAsync(State.GeneralCloseError, "Lock reports unknown bold position. Lock could be closed or open. Please try again or contact customer support.");
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.UnknownFromHardwareError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCloseCloseFailsException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var command = Substitute.For<ICommand>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78 }.Build());
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns<Task<LockitLockingState?>>(x => throw new Exception("Exception message."));
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null),
|
||||
Throws.InstanceOf<Exception>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await listener.ReportStateAsync(State.GeneralCloseError, "Exception message.");
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Is<LocationDto>(x => x.Latitude == 47.99 && x.Longitude == 7.78));
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.UnknownDisconnected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestCloseWaitGeolocationServiceGetAsyncFails()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var command = Substitute.For<ICommand>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(LockitLockingState.Closed); // Return lock state indicating success
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Returns(Task.FromException<IGeolocation>(new Exception("Ups, some error...")));
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
listener.Received().ReportStep(Step.WaitStopPollingQueryLocation);
|
||||
await listener.Received().ReportStateAsync(State.WaitGeolocationException, "Ups, some error...");
|
||||
listener.Received().ReportStep(Step.QueryLocationTerminated);
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task TestCloseUpdateLockingStateFailsWebConnectFailureException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var command = Substitute.For<ICommand>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(LockitLockingState.Closed); // Return lock state indicating success
|
||||
|
||||
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>()).Returns(x => throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
|
||||
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestCloseUpdateLockingStateFailsResponseException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var command = Substitute.For<ICommand>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(LockitLockingState.Closed); // Return lock state indicating success
|
||||
|
||||
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>()).Returns(x =>
|
||||
throw new ReturnBikeException(JsonConvert.DeserializeObject<DoReturnResponse>(@"{ ""response_text"" : ""Some invalid data received!""}"), "Outer message."));
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
|
||||
await listener.ReportStateAsync(State.ResponseIsInvalid, "Outer message.");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task TestCloseUpdateLockingStateFailsException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var connector = Substitute.For<IConnector>();
|
||||
var command = Substitute.For<ICommand>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<ICloseCommandListener>();
|
||||
var viewUpdateManager = Substitute.For<IPollingUpdateTaskManager>();
|
||||
|
||||
locks[0].CloseAsync()
|
||||
.Returns(LockitLockingState.Closed); // Return lock state indicating success
|
||||
|
||||
connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>()).Returns(x => throw new Exception("Exception message."));
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
await InvokeAsync<TestCloseCommand>(bike, geolocation, locks, () => true, (isConnexted) => connector, listener, null);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.Received().ReportStep(Step.StartingQueryingLocation);
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
listener.Received().ReportStep(Step.ClosingLock);
|
||||
await locks.Received()[0].CloseAsync(); // Lock must be closed
|
||||
await connector.Command.UpdateLockingStateAsync(bike, Arg.Any<LocationDto>());
|
||||
await listener.ReportStateAsync(State.BackendUpdateFailed, "Exception message.");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,203 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using NUnit.Framework;
|
||||
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock.Command;
|
||||
using TINK.Model.Connector;
|
||||
using TINK.Model.Device;
|
||||
using TINK.Model.State;
|
||||
using TINK.Model.User;
|
||||
using TINK.Services.BluetoothLock;
|
||||
using TINK.Services.Geolocation;
|
||||
using static TINK.Model.Bikes.BikeInfoNS.BluetoothLock.Command.GetLockedLocationCommand;
|
||||
|
||||
namespace TestShareeLib.Model.Bikes.BikeInfoNS.BluetoothLock.Command
|
||||
{
|
||||
public class TestGetBikeLocationCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Use case: End rental.
|
||||
/// Final state: Booked, lock state unknown.
|
||||
/// </summary>
|
||||
/// <remarks> Replaces test TestReturnOutOfReach which was removed for sharee.bike version ~3.0.362. </remarks>
|
||||
[Test]
|
||||
public void TestReturnLastGeolocatonNullLockOutOfReach()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<IGetLockedLocationCommandListener>();
|
||||
|
||||
bike.Id.Returns("0");
|
||||
|
||||
bike.LockInfo.Location.Returns((IGeolocation)null); // When locking bike geolocation was not available.
|
||||
|
||||
locks[0].GetDeviceState().Returns(DeviceState.Disconnected); // Simulate bike out of reach.
|
||||
|
||||
locks.DisconnectAsync(0, Arg.Any<Guid>()).Returns(LockingState.UnknownDisconnected); // Simulate disconnecting.
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked);
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestGetBikeLocationCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
listener: listener),
|
||||
Throws.TypeOf<Exception>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(() =>
|
||||
{
|
||||
listener.ReportStep(Step.StartingQueryLocation);
|
||||
var location = bike.LockInfo.Location;
|
||||
locks[0].GetDeviceState();
|
||||
listener.ReportStateAsync(State.DisconnetedNoLocationError, "");
|
||||
listener.ReportStep(Step.DisconnectingLockOnDisconnectedNoLocationError);
|
||||
locks.DisconnectAsync(0, Arg.Any<Guid>());
|
||||
listener.ReportStateAsync(State.QueryLocationSucceeded, "");
|
||||
});
|
||||
|
||||
Assert.That(
|
||||
bike.LockInfo.State,
|
||||
Is.EqualTo(LockingState.UnknownDisconnected));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use case: End rental.
|
||||
/// Final state: Booked, lock state unknown.
|
||||
/// </summary>
|
||||
/// <remarks>Replaces test TestReturnLockInReachNoGeolocation which was removed for sharee.bike older ~ 3.0.362.</remarks>
|
||||
[Test]
|
||||
public void TestReturnStartGetGeolocationException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<IGetLockedLocationCommandListener>();
|
||||
|
||||
bike.Id.Returns("0");
|
||||
|
||||
bike.LockInfo.Location.Returns((IGeolocation)null); // When locking bike geolocation was not available.
|
||||
|
||||
locks[0].GetDeviceState().Returns(DeviceState.Connected); // Simulate bike in reach.
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>()).Throws(new Exception("Ups...")); // Simulate error when starting query for geolocation.
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking state does not change.
|
||||
|
||||
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
|
||||
|
||||
var subsequent = InvokeAsync<TestGetBikeLocationCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
listener: listener);
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.ReportStep(Step.StartingQueryLocation);
|
||||
locks[0].GetDeviceState();
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
await listener.ReportStateAsync(State.QueryLocationFailed, "Ups...");
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use case: End rental.
|
||||
/// Final state: Booked, lock state unknown.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestReturnGetGeolocationException()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<IGetLockedLocationCommandListener>();
|
||||
|
||||
bike.Id.Returns("0");
|
||||
|
||||
bike.LockInfo.Location.Returns((IGeolocation)null); // When locking bike geolocation was not available.
|
||||
|
||||
locks[0].GetDeviceState().Returns(DeviceState.Connected); // Simulate bike in reach.
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>())
|
||||
.Returns(Task.FromException<IGeolocation>(new Exception("Ups..."))); // Simulate error starting query for geolocation.
|
||||
|
||||
bike.State.Value.Returns(InUseStateEnum.Booked); // Booking state does not change.
|
||||
|
||||
bike.LockInfo.State.Returns(LockingState.Closed); // Locking state does not change.
|
||||
|
||||
Assert.That(
|
||||
async () => await InvokeAsync<TestGetBikeLocationCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
listener: listener),
|
||||
Throws.TypeOf<Exception>());
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.ReportStep(Step.StartingQueryLocation);
|
||||
locks[0].GetDeviceState();
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime?>());
|
||||
await listener.ReportStateAsync(State.QueryLocationFailed, "Ups...");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Use case: End rental.
|
||||
/// Final state: Disposable closed
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task TestReturnLockInReach()
|
||||
{
|
||||
var bike = Substitute.For<IBikeInfoMutable>();
|
||||
var geolocation = Substitute.For<IGeolocationService>();
|
||||
var locks = Substitute.For<ILocksService>();
|
||||
var listener = Substitute.For<IGetLockedLocationCommandListener>();
|
||||
|
||||
bike.LockInfo.Location.Returns((IGeolocation)null);
|
||||
|
||||
bike.Id.Returns("0");
|
||||
|
||||
locks[0].GetDeviceState().Returns(DeviceState.Connected); // Simulate bike in reach. If bike is out of reach bluetooth state changes to unknown.
|
||||
|
||||
var location = Substitute.For<IGeolocation>();
|
||||
location.Latitude.Returns(7);
|
||||
location.Longitude.Returns(9);
|
||||
location.Timestamp.Returns(DateTimeOffset.MinValue);
|
||||
|
||||
geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime>()).Returns(Task.FromResult(
|
||||
location
|
||||
)); ;
|
||||
|
||||
var lockingLocation = await InvokeAsync<TestGetBikeLocationCommand>(
|
||||
bike,
|
||||
geolocation,
|
||||
locks,
|
||||
() => DateTime.MinValue,
|
||||
listener);
|
||||
|
||||
Assert.That(lockingLocation.Latitude, Is.EqualTo(7));
|
||||
Assert.That(lockingLocation.Longitude, Is.EqualTo(9));
|
||||
|
||||
// Verify behavior
|
||||
Received.InOrder(async () =>
|
||||
{
|
||||
listener.ReportStep(Step.StartingQueryLocation);
|
||||
var dummyLoc = bike.LockInfo.Location; // Check if location was available when closing lock.
|
||||
locks[0].GetDeviceState(); // Check if bike is connected.
|
||||
await geolocation.GetAsync(Arg.Any<CancellationToken?>(), Arg.Any<DateTime>()); // Query current location.
|
||||
await listener.ReportStateAsync(State.QueryLocationSucceeded, string.Empty);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue