diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 5899292..850dcee 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -74,6 +74,15 @@ public class FileSender_Should private readonly X509Certificate certificate = new X509Certificate(); private File file; private byte[] signedContent; + private Document document; + + private File invalidFile; + private byte[] invalidSignedContent; + private Document invalidDocument; + + private File couldNotSendFile; + private byte[] couldNotSendSignedContent; + private Document couldNotSendDocument; [SetUp] public void SetUp() @@ -89,13 +98,8 @@ public void SetUp() sender = A.Fake(); recognizer = A.Fake(); fileSender = new FileSender(cryptographer, sender, recognizer); - } - [TestCase("4.0")] - [TestCase("3.1")] - public void Send_WhenGoodFormat(string format) - { - var document = new Document(file.Name, file.Content, DateTime.Now, format); + document = new Document(file.Name, file.Content, DateTime.Now, "4.0"); A.CallTo(() => recognizer.TryRecognize(file, out document)) .Returns(true); A.CallTo(() => cryptographer.Sign(document.Content, certificate)) @@ -103,57 +107,114 @@ public void Send_WhenGoodFormat(string format) A.CallTo(() => sender.TrySend(signedContent)) .Returns(true); + + invalidFile = new File("invalidFile", new byte[] { 1, 2, 3 }); + invalidSignedContent = new byte[] { 1, 7 }; + invalidDocument = new Document(invalidFile.Name, invalidFile.Content, DateTime.Now, "111.222"); + + A.CallTo(() => recognizer.TryRecognize(invalidFile, out invalidDocument)) + .Returns(true); + A.CallTo(() => cryptographer.Sign(invalidDocument.Content, certificate)) + .Returns(invalidSignedContent); + A.CallTo(() => sender.TrySend(invalidSignedContent)) + .Returns(true); + + + couldNotSendFile = new File("invalidFile", new byte[] { 1, 2, 3 }); + couldNotSendSignedContent = new byte[] { 1, 7 }; + couldNotSendDocument = new Document(couldNotSendFile.Name, couldNotSendFile.Content, DateTime.Now, "4.0"); + + A.CallTo(() => recognizer.TryRecognize(couldNotSendFile, out couldNotSendDocument)) + .Returns(true); + A.CallTo(() => cryptographer.Sign(couldNotSendDocument.Content, certificate)) + .Returns(couldNotSendSignedContent); + A.CallTo(() => sender.TrySend(couldNotSendSignedContent)) + .Returns(false); + } + + [TestCase("4.0")] + [TestCase("3.1")] + public void Send_WhenGoodFormat(string format) + { + document.Format = format; + fileSender.SendFiles(new[] {file}, certificate) .SkippedFiles.Should().BeEmpty(); } [Test] - [Ignore("Not implemented")] - public void Skip_WhenBadFormat() + [TestCase("111.222")] + public void Skip_WhenBadFormat(string format) { - throw new NotImplementedException(); + document.Format = format; + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenOlderThanAMonth() { - throw new NotImplementedException(); + document.Created = document.Created.AddMonths(-1).AddDays(-1); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] - [Ignore("Not implemented")] public void Send_WhenYoungerThanAMonth() { - throw new NotImplementedException(); + document.Created = document.Created.AddMonths(-1).AddDays(1); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().BeEmpty(); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenSendFails() { - throw new NotImplementedException(); + fileSender.SendFiles(new[] { couldNotSendFile }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void Skip_WhenNotRecognized() { - throw new NotImplementedException(); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(false); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { - throw new NotImplementedException(); + var result = fileSender.SendFiles(new[] { file, invalidFile }, certificate); + + result.SkippedFiles.Should().HaveCount(1); + result.SkippedFiles.Should().Contain(invalidFile); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { - throw new NotImplementedException(); + var result = fileSender.SendFiles(new[] { file, couldNotSendFile }, certificate); + + result.SkippedFiles.Should().HaveCount(1); + result.SkippedFiles.Should().Contain(couldNotSendFile); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } } } diff --git a/FileSender/FileSender.csproj b/FileSender/FileSender.csproj index b4c7dfe..b0c413e 100644 --- a/FileSender/FileSender.csproj +++ b/FileSender/FileSender.csproj @@ -11,6 +11,7 @@ + diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 2d3859d..1283888 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using FakeItEasy; using NUnit.Framework; namespace MockFramework @@ -40,11 +41,17 @@ public class ThingCache_Should private const string thingId2 = "CoolBoots"; private Thing thing2 = new Thing(thingId2); + private const string thingIdNotExisted = "NotExisted"; + private Thing thing3 = null; + // Метод, помеченный атрибутом SetUp, выполняется перед каждым тестов [SetUp] public void SetUp() { - //thingService = A... + thingService = A.Fake(); + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).Returns(true); + A.CallTo(() => thingService.TryRead(thingId2, out thing2)).Returns(true); + A.CallTo(() => thingService.TryRead(thingIdNotExisted, out thing3)).Returns(false); thingCache = new ThingCache(thingService); } @@ -53,8 +60,43 @@ public void SetUp() // Пример теста [Test] - public void GiveMeAGoodNamePlease() + public void ThingCache_Get_ReturnsCorrectValue() + { + var actualValue = thingCache.Get(thingId1); + Assert.That(actualValue, Is.EqualTo(thing1)); + } + + [Test] + public void ThingCache_Get_ReturnsNoValue() + { + var notExistedValue = thingCache.Get(thingIdNotExisted); + Assert.That(notExistedValue, Is.Null); + } + + [Test] + public void ThingCache_Get_MethodCallingHappened() + { + thingCache.Get(thingId1); + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void ThingCache_Get_ReturnsValueFromCache() + { + thingCache.Get(thingId1); // put value to cache + thingCache.Get(thingId1); // get value via cache + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void ThingCache_Get_ReturnsValueFromCacheMany() { + thingCache.Get(thingId1); // put value to cache + thingCache.Get(thingId2); // put value to cache + thingCache.Get(thingId1); // get value via cache + thingCache.Get(thingId2); // get value via cache + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); + A.CallTo(() => thingService.TryRead(thingId2, out thing2)).MustHaveHappenedOnceExactly(); } /** Проверки в тестах diff --git a/ThingCache/ThingCache.csproj b/ThingCache/ThingCache.csproj index 6d6bed7..c915cd3 100644 --- a/ThingCache/ThingCache.csproj +++ b/ThingCache/ThingCache.csproj @@ -12,6 +12,7 @@ +