Version 3.0.370

This commit is contained in:
Anja 2023-08-31 12:20:06 +02:00
parent f5cf9bb22f
commit bdb2dec1c1
233 changed files with 10252 additions and 6779 deletions

View file

@ -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.");
});
}
}
}

View file

@ -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);
});
}
}
}

View file

@ -1,7 +1,12 @@
using System;
using NSubstitute;
using NUnit.Framework;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
using TINK.Model.Bikes.BikeInfoNS.DriveNS;
using TINK.Model.Connector;
using TINK.Services.BluetoothLock;
using TINK.Services.Geolocation;
using TINK.ViewModel;
namespace TestShareeLib.Model.Bike.BluetoothLock
{
@ -13,6 +18,11 @@ namespace TestShareeLib.Model.Bike.BluetoothLock
{
Assert.That(
new BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new TINK.Model.Bikes.BikeInfoNS.BikeNS.Bike(
"MyBikeId",
@ -31,6 +41,11 @@ namespace TestShareeLib.Model.Bike.BluetoothLock
{
Assert.That(
new BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new TINK.Model.Bikes.BikeInfoNS.BikeNS.Bike(
"MyBikeId",
@ -50,7 +65,14 @@ namespace TestShareeLib.Model.Bike.BluetoothLock
public void TestCtorBikeNull()
{
Assert.That(
() => new BikeInfoMutable(null, "My Station Name"),
() => new BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
null,
"My Station Name"),
Throws.ArgumentNullException);
}
}

View file

@ -95,7 +95,7 @@ namespace TestTINKLib.Fixtures.ObjectTests.Bike.BluetoothLock
lockInfo.Location = new Geolocation.Builder { Latitude = 47.99, Longitude = 7.78}.Build();
// Veryfy that location is kept because bike might be returned later.
// Verify that location is kept because bike might be returned later.
Assert.That(
lockInfo.Location.Latitude,
Is.EqualTo(47.99).Within(0.001));
@ -113,7 +113,7 @@ namespace TestTINKLib.Fixtures.ObjectTests.Bike.BluetoothLock
lockInfo.State = LockingState.Closed;
// Veryfy that location is kept because bike might be returned later.
// Verify that location is kept because bike might be returned later.
Assert.That(
lockInfo.Location,
Is.Null);

View file

@ -1,15 +1,20 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using TINK.Model.Bikes;
using TINK.Model.Bikes.BikeInfoNS;
using TINK.Model.Bikes.BikeInfoNS.BikeNS;
using TINK.Model.Bikes.BikeInfoNS.DriveNS;
using TINK.Model.Connector;
using TINK.Model.Connector.Updater;
using TINK.Model.State;
using TINK.Model.Stations;
using TINK.Model.Stations.StationNS;
using TINK.Services.BluetoothLock;
using TINK.Services.Geolocation;
using TINK.ViewModel;
using BikeInfo = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfo;
namespace TestTINKLib
@ -54,7 +59,12 @@ namespace TestTINKLib
[Test]
public void TestAdd()
{
var l_oColl = new BikeCollectionMutable();
var l_oColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
l_oColl.Add(new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround));
@ -67,7 +77,12 @@ namespace TestTINKLib
var l_oBikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
l_oBikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
var l_oBikeColl = new BikeCollectionMutable
var l_oBikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
@ -95,7 +110,12 @@ namespace TestTINKLib
var bikeRequested = new BikeInfoMutable("20", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Allround);
bikeRequested.State.Load(new StateInfo(() => DateTime.Now, DateTime.Now, TimeSpan.FromMinutes(15), "john@long", "1234"));
var bikeColl = new BikeCollectionMutable
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("63", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Two, TypeOfBike.Allround),
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
@ -152,7 +172,12 @@ namespace TestTINKLib
[Test]
public void TestUpdate_StationName_NewBike()
{
var bikeColl = new BikeCollectionMutable();
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
// Verify initial state
Assert.That(bikeColl.GetById("57"), Is.Null);
@ -184,7 +209,12 @@ namespace TestTINKLib
public void TestUpdate_StationName_UpdateExistingBike()
{
var bikeColl = new BikeCollectionMutable
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>())
{
new BikeInfoMutable("57", LockModel.ILockIt, false, new List<string> { "TINK" }, WheelType.Trike, TypeOfBike.Cargo),
};
@ -229,7 +259,12 @@ namespace TestTINKLib
public void TestUpdate_StationName_Nullstations()
{
var bikeColl = new BikeCollectionMutable();
var bikeColl = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
var bikeResponse = new List<BikeInfo>
@ -265,7 +300,13 @@ namespace TestTINKLib
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1").GetType(),
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable)));
}
@ -284,7 +325,13 @@ namespace TestTINKLib
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1").GetType(),
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1").GetType(),
Is.EqualTo(typeof(TINK.Model.Bikes.BikeInfoNS.CopriLock.BikeInfoMutable)));
}
@ -304,7 +351,13 @@ namespace TestTINKLib
TINK.Model.Bikes.BikeInfoNS.BC.DataSource.Copri);
Assert.That(
BikeCollectionMutable.BikeInfoMutableFactory.Create(bikeInfo, "Stat1"),
BikeCollectionMutable.BikeInfoMutableFactory.Create(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
bikeInfo, "Stat1"),
Is.Null);
}
}

View file

@ -1,18 +1,23 @@
using System;
using System.Linq;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using TINK.Model;
using TINK.Model.Bikes;
using TINK.Model.Bikes.BikeInfoNS.BC;
using TINK.Model.Bikes.BikeInfoNS.BikeNS;
using TINK.Model.Bikes.BikeInfoNS.DriveNS;
using TINK.Model.Connector;
using TINK.Model.Connector.Updater;
using TINK.Model.State;
using TINK.Model.User.Account;
using TINK.Repository;
using TINK.Repository.Response;
using TINK.Repository.Response.Stations;
using TINK.Services.BluetoothLock;
using TINK.Services.Geolocation;
using TINK.ViewModel;
using Xamarin.Forms;
using static TINK.Repository.CopriCallsMemory;
@ -640,7 +645,13 @@ namespace TestShareeLib.Model.Connector.Updater
public void TestLoad_Reserved_CalculateAuthKeys()
{
// Construct requested bike.
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new TINK.Model.Bikes.BikeInfoNS.BikeNS.Bike("17", LockModel.ILockIt),
new Drive(),
DataSource.Copri,
@ -680,7 +691,13 @@ namespace TestShareeLib.Model.Connector.Updater
public void TestLoad_Booked_CalculateAuthKeys()
{
// Construct occupied bike.
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new TINK.Model.Bikes.BikeInfoNS.BikeNS.Bike("17", LockModel.ILockIt),
new Drive(),
DataSource.Copri,
@ -716,7 +733,13 @@ namespace TestShareeLib.Model.Connector.Updater
public void TestLoad_Reserved_DoBook()
{
// Construct requested bike.
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
var bike = new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>(),
new TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(
new TINK.Model.Bikes.BikeInfoNS.BikeNS.Bike("17", LockModel.ILockIt),
new Drive(),
DataSource.Copri,

View file

@ -16,8 +16,10 @@ using TINK.Model.Stations.StationNS;
using TINK.Model.User;
using TINK.Repository;
using TINK.Services;
using TINK.Services.BluetoothLock;
using TINK.Services.Geolocation;
using TINK.Services.Permissions;
using TINK.ViewModel;
using TINK.ViewModel.Settings;
using static TINK.Repository.CopriCallsMemory;
@ -36,7 +38,12 @@ namespace TestTINKLib.Fixtures.UseCases.SelectStation
IEnumerable<IStation> stations,
string selectedStationId)
{
var l_oBikesAtStation = new BikeCollectionMutable();
var l_oBikesAtStation = new BikeCollectionMutable(
Substitute.For<IGeolocationService>(),
Substitute.For<ILocksService>(),
() => false /* not connected */,
(_) => Substitute.For<IConnector>(),
() => Substitute.For<IPollingUpdateTaskManager>());
var l_oBikesAvailable = (await connector.Query.GetBikesAsync()).Response;