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.AuthCommand; using ShareeBike.Repository.Exception; namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BikeNS.Command { [TestFixture] public class TestAuthCommand { /// /// Use case: Authenticate. /// Final state: Authentication successfully /// [Test] public async Task TestAuth() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); await InvokeAsync( bike, () => true, // isConnectedDelegate (isConnected) => connector, listener); await connector.Command.CalculateAuthKeys(Arg.Any()); // Verify behavior Received.InOrder(async () => { listener.ReportStep(Step.Authenticate); await connector.Command.CalculateAuthKeys(bike); }); } /// /// Use case: Authenticate. /// Final state: Authentication failed /// [Test] public void TestAuthNoWebException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Closed); connector.Command.CalculateAuthKeys(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.Authenticate); await connector.Command.CalculateAuthKeys(bike); await listener.ReportStateAsync(State.WebConnectFailed, "Context info."); }); } /// /// Use case: Authenticate. /// Final state: Authentication failed /// [Test] public void TestAuthGeneralException() { var bike = Substitute.For(); var connector = Substitute.For(); var listener = Substitute.For(); bike.LockInfo.State.Returns(LockingState.Closed); connector.Command.CalculateAuthKeys(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.Authenticate); await connector.Command.CalculateAuthKeys(bike); await listener.ReportStateAsync(State.GeneralAuthError, "Context info."); }); } } }