2017-07-21 12:05:31 +02:00
|
|
|
|
/*
|
2022-01-21 16:33:52 +01:00
|
|
|
|
* Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-21 12:05:31 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Moq;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
|
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2018-08-31 10:06:27 +02:00
|
|
|
|
using SafeExamBrowser.Core.OperationModel;
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.UnitTests.OperationModel
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public class OperationSequenceTests
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
private Mock<ILogger> loggerMock;
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
loggerMock = new Mock<ILogger>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCreateCopyOfOperationQueue()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
operations.Clear();
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-03 15:25:09 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyPropagateEventSubscription()
|
|
|
|
|
{
|
|
|
|
|
var actionRequiredCalled = false;
|
|
|
|
|
var actionRequiredHandler = new ActionRequiredEventHandler(args => actionRequiredCalled = true);
|
|
|
|
|
var statusChangedCalled = false;
|
|
|
|
|
var statusChangedHandler = new StatusChangedEventHandler(t => statusChangedCalled = true);
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success).Raises(o => o.ActionRequired += null, new Mock<ActionRequiredEventArgs>().Object);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success).Raises(o => o.StatusChanged += null, default(TextKey));
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.ActionRequired += actionRequiredHandler;
|
|
|
|
|
sut.StatusChanged += statusChangedHandler;
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(actionRequiredCalled);
|
|
|
|
|
Assert.IsTrue(statusChangedCalled);
|
|
|
|
|
|
|
|
|
|
actionRequiredCalled = false;
|
|
|
|
|
statusChangedCalled = false;
|
|
|
|
|
sut.ActionRequired -= actionRequiredHandler;
|
|
|
|
|
sut.StatusChanged -= statusChangedHandler;
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(actionRequiredCalled);
|
|
|
|
|
Assert.IsFalse(statusChangedCalled);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
#region Perform Tests
|
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustCorrectlyAbortPerform()
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Aborted);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustPerformOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustPerformOperationsInSequence()
|
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => a = ++current);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => c = ++current);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
Assert.IsTrue(a == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(c == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperationsInCaseOfError()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operationD = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
operations.Enqueue(operationD.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationD.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationD.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public void MustRevertOperationsInSequenceAfterPerformError()
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0, d = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operationD = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationA.Setup(o => o.Revert()).Callback(() => a = ++current);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationB.Setup(o => o.Revert()).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationC.Setup(o => o.Revert()).Callback(() => c = ++current);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationD.Setup(o => o.Revert()).Callback(() => d = ++current);
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
operations.Enqueue(operationD.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
Assert.IsTrue(d == 0);
|
|
|
|
|
Assert.IsTrue(c == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(a == 3);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
[TestMethod]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public void MustContinueToRevertOperationsAfterPerformError()
|
2017-10-09 12:11:33 +02:00
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-10-09 12:11:33 +02:00
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
operationC.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationB.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationA.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
var success = sut.TryPerform();
|
2017-10-09 12:11:33 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedWithEmptyQueue()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailInCaseOfUnexpectedError()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
sut.ProgressChanged += (args) => throw new Exception();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Revert Tests
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
operationA.Setup(o => o.Revert()).Returns(OperationResult.Success);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
operationB.Setup(o => o.Revert()).Returns(OperationResult.Success);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
operationC.Setup(o => o.Revert()).Returns(OperationResult.Success);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperationsInSequence()
|
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-10-12 15:23:43 +02:00
|
|
|
|
operationA.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => a = ++current);
|
|
|
|
|
operationB.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => c = ++current);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
Assert.IsTrue(c == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(a == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustContinueToRevertOperationsInCaseOfError()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
operationA.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationB.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationC.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustOnlyRevertPerformedOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
operationA.Setup(o => o.Revert()).Returns(OperationResult.Success);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Aborted);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
operationB.Setup(o => o.Revert()).Returns(OperationResult.Success);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustSucceedWithEmptyQueueWhenReverting()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-10-10 09:19:03 +02:00
|
|
|
|
|
|
|
|
|
var result = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustSucceedRevertingWithoutCallingPerform()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailInCaseOfUnexpectedErrorWhenReverting()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
sut.ProgressChanged += (args) => throw new Exception();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = sut.TryRevert();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|