diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 5899292..09cb41a 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -42,7 +42,9 @@ private bool TrySendFile(File file, X509Certificate certificate) if (!CheckFormat(document) || !CheckActual(document)) return false; var signedContent = cryptographer.Sign(document.Content, certificate); - return sender.TrySend(signedContent); + var successSend = sender.TrySend(signedContent); + + return successSend; } private bool CheckFormat(Document document) @@ -86,6 +88,7 @@ public void SetUp() signedContent = new byte[] {1, 7}; cryptographer = A.Fake(); + A.CallTo(() => cryptographer.Sign(file.Content, certificate)).Returns(signedContent); sender = A.Fake(); recognizer = A.Fake(); fileSender = new FileSender(cryptographer, sender, recognizer); @@ -95,65 +98,190 @@ public void SetUp() [TestCase("3.1")] public void Send_WhenGoodFormat(string format) { + // Arrange var document = new Document(file.Name, file.Content, DateTime.Now, format); - A.CallTo(() => recognizer.TryRecognize(file, out document)) - .Returns(true); - A.CallTo(() => cryptographer.Sign(document.Content, certificate)) - .Returns(signedContent); - A.CallTo(() => sender.TrySend(signedContent)) - .Returns(true); - - fileSender.SendFiles(new[] {file}, certificate) - .SkippedFiles.Should().BeEmpty(); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true); + A.CallTo(() => sender.TrySend(signedContent)).Returns(true); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().BeEmpty(); + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } - [Test] - [Ignore("Not implemented")] - public void Skip_WhenBadFormat() + [TestCase("0.1")] + [TestCase("2.3")] + [TestCase("10.10.10")] + public void Skip_WhenBadFormat(string format) { - throw new NotImplementedException(); + // Arrange + var document = new Document(file.Name, file.Content, DateTime.Now, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true); + A.CallTo(() => sender.TrySend(signedContent)).Returns(true); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().HaveCount(1); + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } - [Test] - [Ignore("Not implemented")] - public void Skip_WhenOlderThanAMonth() + [TestCase("4.0")] + [TestCase("3.1")] + public void Skip_WhenOlderThanAMonth(string format) { - throw new NotImplementedException(); + var olderThanMonthDate = DateTime.Now.AddMonths(-1).AddSeconds(-30); + + var document = new Document(file.Name, file.Content, olderThanMonthDate, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true); + A.CallTo(() => sender.TrySend(signedContent)).Returns(true); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().HaveCount(1); + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } - [Test] - [Ignore("Not implemented")] - public void Send_WhenYoungerThanAMonth() + [TestCase("4.0")] + [TestCase("3.1")] + public void Send_WhenYoungerThanAMonth(string format) { - throw new NotImplementedException(); + // Arrange + var youngerThanAMonthDate = DateTime.Now.AddMonths(-1).AddSeconds(30); + + var document = new Document(file.Name, file.Content, youngerThanAMonthDate, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true); + A.CallTo(() => sender.TrySend(signedContent)).Returns(true); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().BeEmpty(); + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } - [Test] - [Ignore("Not implemented")] - public void Skip_WhenSendFails() + [TestCase("4.0")] + [TestCase("3.1")] + public void Skip_WhenSendFails(string format) { - throw new NotImplementedException(); + // Arrange + var document = new Document(file.Name, file.Content, DateTime.Now, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true); + A.CallTo(() => sender.TrySend(signedContent)).Returns(false); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().HaveCount(1); + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } - [Test] - [Ignore("Not implemented")] - public void Skip_WhenNotRecognized() + [TestCase("4.0")] + [TestCase("3.1")] + public void Skip_WhenNotRecognized(string format) { - throw new NotImplementedException(); + // Arrange + var document = new Document(file.Name, file.Content, DateTime.Now, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(false); + A.CallTo(() => sender.TrySend(signedContent)).Returns(true); + + // Act + var result = fileSender.SendFiles(new[] { file }, certificate); + + // Assert + result.SkippedFiles.Should().HaveCount(1); + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { - throw new NotImplementedException(); + // Arrange + File[] files = + { + new File("valid", new byte[] { 1 }), + new File("outdated", new byte[] { 2 }), + new File("invalidVersion", new byte[] { 3 }) + }; + + var validDoc = new Document(file.Name, file.Content, DateTime.Now, "4.0"); + var outDatedDoc = new Document(file.Name, file.Content, DateTime.Now.AddMonths(-2), "4.0"); + var invalidVersionDoc = new Document(file.Name, file.Content, DateTime.Now, "0.0.0.0"); + + Document _; + A.CallTo(() => recognizer.TryRecognize(files[0], out _)) + .Returns(true) + .AssignsOutAndRefParameters(validDoc); + + A.CallTo(() => recognizer.TryRecognize(files[1], out _)) + .Returns(true) + .AssignsOutAndRefParameters(outDatedDoc); + + A.CallTo(() => recognizer.TryRecognize(files[2], out _)) + .Returns(true) + .AssignsOutAndRefParameters(invalidVersionDoc); + + A.CallTo(() => cryptographer.Sign(A.Ignored, certificate)).Returns(signedContent); + A.CallTo(() => sender.TrySend(A.Ignored)).Returns(true); + + + // Act + var result = fileSender.SendFiles(files, certificate); + + // Assert + result.SkippedFiles.Should().BeEquivalentTo(files[1], files[2]); + A.CallTo(() => cryptographer.Sign(A.Ignored, certificate)).MustHaveHappenedOnceExactly(); + A.CallTo(() => sender.TrySend(A.Ignored)).MustHaveHappenedOnceExactly(); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { - throw new NotImplementedException(); + // Arrange + File[] files = + { + new File("file1", new byte[] { 1 }), + new File("file2", new byte[] { 2 }), + new File("file3", new byte[] { 3 }) + }; + + var validDoc1 = new Document(file.Name, files[0].Content, DateTime.Now, "4.0"); + var validDoc2 = new Document(file.Name, files[1].Content, DateTime.Now, "4.0"); + var validDoc3 = new Document(file.Name, files[2].Content, DateTime.Now, "4.0"); + + Document _; + A.CallTo(() => recognizer.TryRecognize(files[0], out _)) + .Returns(true) + .AssignsOutAndRefParameters(validDoc1); + + A.CallTo(() => recognizer.TryRecognize(files[1], out _)) + .Returns(true) + .AssignsOutAndRefParameters(validDoc2); + + A.CallTo(() => recognizer.TryRecognize(files[2], out _)) + .Returns(true) + .AssignsOutAndRefParameters(validDoc3); + + A.CallTo(() => cryptographer.Sign(A.Ignored, certificate)).ReturnsLazily(c => (byte[])c.Arguments[0]); + + A.CallTo(() => sender.TrySend(files[0].Content)).Returns(true); + A.CallTo(() => sender.TrySend(files[1].Content)).Returns(false); + A.CallTo(() => sender.TrySend(files[2].Content)).Returns(false); + + // Act + var result = fileSender.SendFiles(files, certificate); + + // Assert + result.SkippedFiles.Should().BeEquivalentTo(files[1], files[2]); + A.CallTo(() => cryptographer.Sign(A.Ignored, certificate)).MustHaveHappened(3, Times.Exactly); + A.CallTo(() => sender.TrySend(A.Ignored)).MustHaveHappened(3, Times.Exactly); } } } diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 2d3859d..f50e92d 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using FakeItEasy; +using FluentAssertions; using NUnit.Framework; namespace MockFramework @@ -44,18 +46,88 @@ public class ThingCache_Should [SetUp] public void SetUp() { - //thingService = A... + thingService = A.Fake(); + + Thing _; + A.CallTo(() => thingService.TryRead(thingId1, out _)) + .Returns(true) + .AssignsOutAndRefParameters(thing1); + + A.CallTo(() => thingService.TryRead(thingId2, out _)) + .Returns(true) + .AssignsOutAndRefParameters(thing2); + thingCache = new ThingCache(thingService); } - // TODO: Написать простейший тест, а затем все остальные - // Live Template tt работает! + [Test] + public void Get_NotExistingThing_ReturnsNull_CallServiceOnce() + { + // Act + var notExistingThingId = "NotExistingThing"; + var thing = thingCache.Get(notExistingThingId); + + // Assert + thing.Should().BeNull(); + Thing _; + A.CallTo(() => thingService.TryRead(notExistingThingId, out _)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void DoubleGet_NotExistingThing_ReturnsNull_CallServiceTwice() + { + // Act + var notExistingThingId = "NotExistingThing"; + var firstTry = thingCache.Get(notExistingThingId); + var secondTry = thingCache.Get(notExistingThingId); + + // Assert + firstTry.Should().BeNull(); + secondTry.Should().BeNull(); + + Thing _; + A.CallTo(() => thingService.TryRead(notExistingThingId, out _)).MustHaveHappenedTwiceExactly(); + } - // Пример теста [Test] - public void GiveMeAGoodNamePlease() + public void Get_ExistingThing1_ReturnsCorrectThing_CallServiceOnce() + { + // Act + var thing = thingCache.Get(thingId1); + + // Assert + thing.Should().Be(thing1); + Thing _; + A.CallTo(() => thingService.TryRead(thingId1, out _)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void DoubleGet_ExistingThing1_ReturnsCorrectThing_CallServiceOnce() + { + // Act + var firstTry = thingCache.Get(thingId1); + var secondTry = thingCache.Get(thingId1); + + // Assert + firstTry.Should().Be(thing1); + secondTry.Should().Be(thing1); + + Thing _; + A.CallTo(() => thingService.TryRead(thingId1, out _)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void Get_ExistingThing2_ReturnsCorrectThing_CallServiceOnce() { + // Act + var thing = thingCache.Get(thingId2); + + // Assert + thing.Should().Be(thing2); + Thing _; + A.CallTo(() => thingService.TryRead(thingId2, out _)).MustHaveHappenedOnceExactly(); } + /** Проверки в тестах * Assert.AreEqual(expectedValue, actualValue);