From 1db30e7d2cc5e9c3796b8d40cf62acb863e28d08 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 12 Feb 2019 14:30:12 +0100 Subject: [PATCH] SEBWIN-296: Implemented unit tests for runtime connection operation. --- .../RuntimeConnectionOperationTests.cs | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/SafeExamBrowser.Client.UnitTests/Operations/RuntimeConnectionOperationTests.cs b/SafeExamBrowser.Client.UnitTests/Operations/RuntimeConnectionOperationTests.cs index f3a90811..abaa62fb 100644 --- a/SafeExamBrowser.Client.UnitTests/Operations/RuntimeConnectionOperationTests.cs +++ b/SafeExamBrowser.Client.UnitTests/Operations/RuntimeConnectionOperationTests.cs @@ -6,9 +6,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using SafeExamBrowser.Client.Operations; using SafeExamBrowser.Contracts.Communication.Proxies; +using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Logging; namespace SafeExamBrowser.Client.UnitTests.Operations @@ -18,18 +21,85 @@ namespace SafeExamBrowser.Client.UnitTests.Operations { private Mock logger; private Mock runtime; + private Guid token; + private RuntimeConnectionOperation sut; [TestInitialize] public void Initialize() { logger = new Mock(); runtime = new Mock(); + token = Guid.NewGuid(); + + sut = new RuntimeConnectionOperation(logger.Object, runtime.Object, token); } [TestMethod] - public void TODO() + public void MustConnectOnPerform() { - Assert.Fail(); + runtime.Setup(r => r.Connect(It.Is(t => t == token), true)).Returns(true); + + var result = sut.Perform(); + + runtime.Verify(r => r.Connect(It.Is(t => t == token), true), Times.Once); + runtime.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustCorrectlyFailOnPerform() + { + runtime.Setup(r => r.Connect(It.Is(t => t == token), true)).Returns(false); + + var result = sut.Perform(); + + runtime.Verify(r => r.Connect(It.Is(t => t == token), true), Times.Once); + runtime.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Failed, result); + } + + [TestMethod] + public void MustDisconnectOnRevert() + { + runtime.Setup(r => r.Connect(It.IsAny(), It.IsAny())).Returns(true); + runtime.Setup(r => r.Disconnect()).Returns(true); + sut.Perform(); + + var result = sut.Revert(); + + runtime.Verify(r => r.Connect(It.IsAny(), It.IsAny()), Times.Once); + runtime.Verify(r => r.Disconnect(), Times.Once); + runtime.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustCorrectlyFailOnRevert() + { + runtime.Setup(r => r.Connect(It.IsAny(), It.IsAny())).Returns(true); + runtime.Setup(r => r.Disconnect()).Returns(false); + sut.Perform(); + + var result = sut.Revert(); + + runtime.Verify(r => r.Connect(It.IsAny(), It.IsAny()), Times.Once); + runtime.Verify(r => r.Disconnect(), Times.Once); + runtime.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Failed, result); + } + + [TestMethod] + public void MustDoNothingOnRevertIfNotConnected() + { + var result = sut.Revert(); + + runtime.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Success, result); } } }