public async Task DiscardChanges()
        {
            await FileAuditWorker.DiscardEvents(ExpectedData);

            MockFileStreamer.Verify(x => x.DisposeOfStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit"));
            MockFileStreamer.Verify(x => x.UnlockFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"));
        }
        public async Task ReadAllEventsSuccessful()
        {
            var result = await FileAuditWorker.ReadAllEvents <AuditableData>(ExpectedData.Id);

            MockFileStreamer.Verify(x => x.GetLockForFile(It.IsAny <string>()), Times.Once);
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), Times.Once);
            MockFileStreamer.Verify(x => x.ReadDataFromStream <AuditLog>(It.IsAny <string>()), Times.Once);
            Assert.AreSame(CreateExpectedAuditLog, result);
        }
        public async Task <bool> CommitEventsSuccess()
        {
            var result = await FileAuditWorker.CommitEvents(ExpectedData);

            MockFileStreamer.Verify(x => x.DisposeOfStream(It.IsAny <string>()), Times.Never);
            MockFileStreamer.Verify(x => x.UnlockFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    Times.Once);
            return(result);
        }
        public async Task <AuditLog> ReadAllEventsReadDataFromStreamFailsDueToException()
        {
            MockFileStreamer
            .Setup(x => x.ReadDataFromStream <AuditLog>(It.IsAny <string>()))
            .Throws <Exception>();
            var result = await FileAuditWorker.ReadAllEvents <AuditableData>(ExpectedData.Id);

            MockFileStreamer.Verify(x => x.GetLockForFile(It.IsAny <string>()), Times.Exactly(3));
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), Times.Exactly(3));
            MockFileStreamer.Verify(x => x.ReadDataFromStream <AuditLog>(It.IsAny <string>()), Times.Exactly(3));
            return(result);
        }
        public async Task <AuditLog> ReadAllEventsGetLockForFileFails()
        {
            MockFileStreamer
            .Setup(x => x.GetLockForFile(It.IsAny <string>()))
            .ReturnsAsync(false);
            var result = await FileAuditWorker.ReadAllEvents <AuditableData>(ExpectedData.Id);

            MockFileStreamer.Verify(x => x.GetLockForFile(It.IsAny <string>()), Times.Exactly(3));
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), Times.Never);
            MockFileStreamer.Verify(x => x.ReadDataFromStream <AuditLog>(It.IsAny <string>()), Times.Never);
            return(result);
        }
        public async Task <bool> CommitEventsCloseStreamFailsDueToException()
        {
            MockFileStreamer
            .Setup(x => x.CloseStream(It.IsAny <string>()))
            .Throws <Exception>();
            var result = await FileAuditWorker.CommitEvents(ExpectedData);

            MockFileStreamer.Verify(x => x.DisposeOfStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    Times.Once);
            MockFileStreamer.Verify(x => x.UnlockFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    Times.Once);
            return(result);
        }
        private async Task <bool> GetLockForFileFails(Func <Task <bool> > methodUnderTest,
                                                      Expression <Func <IAuditGenerator, AuditLog> > auditGenerator, Times retryAttempts)
        {
            MockFileStreamer
            .Setup(x => x.GetLockForFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"))
            .ReturnsAsync(false);
            var result = await methodUnderTest.Invoke();

            MockFileStreamer.Verify(x => x.GetLockForFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    retryAttempts);
            MockFileStreamer.Verify(x => x.WriteDataToStream(It.IsAny <string>(), It.IsAny <AuditableData>()),
                                    Times.Never);
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), Times.Never);
            MockAuditGenerator.Verify(auditGenerator, Times.Never);
            return(result);
        }
        private async Task <bool> SuccessEvent(Func <Task <bool> > methodUnderTest,
                                               Expression <Func <IAuditGenerator, AuditLog> > auditGenerator, AuditLog expectedAuditLog,
                                               Times getFileLockTimes, Times unlockFile)
        {
            MockFileStreamer
            .Setup(x => x.WriteDataToStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit", expectedAuditLog))
            .ReturnsAsync(true);
            var result = await methodUnderTest.Invoke();

            MockFileStreamer.Verify(x => x.GetLockForFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    getFileLockTimes);
            MockFileStreamer.Verify(
                x => x.WriteDataToStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit", expectedAuditLog),
                Times.Once);
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), unlockFile);
            MockAuditGenerator.Verify(auditGenerator, Times.Once);
            return(result);
        }
        private async Task <bool> GenerateAuditFailsDueToException(Func <Task <bool> > methodUnderTest,
                                                                   Expression <Func <IAuditGenerator, AuditLog> > auditGenerator, Times retryAttempts)
        {
            MockAuditGenerator
            .Setup(auditGenerator)
            .Throws <Exception>();
            var result = await methodUnderTest.Invoke();

            MockFileStreamer.Verify(x => x.GetLockForFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    retryAttempts);
            MockFileStreamer.Verify(
                x => x.WriteDataToStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit", It.IsAny <AuditLog>()),
                Times.Never);
            MockFileStreamer.Verify(x => x.UnlockFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    retryAttempts);
            MockAuditGenerator.Verify(auditGenerator, Times.Exactly(3));
            return(result);
        }
Ejemplo n.º 10
0
        private async Task <bool> WriteDataToStreamFailsDueToException(Func <Task <bool> > methodUnderTest,
                                                                       Expression <Func <IAuditGenerator, AuditLog> > auditGenerator, AuditLog expectedAuditLog, Times retryAttempts,
                                                                       Times lockFile)
        {
            MockFileStreamer
            .Setup(x => x.WriteDataToStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit", expectedAuditLog))
            .Throws <Exception>();
            var result = await methodUnderTest.Invoke();

            MockFileStreamer.Verify(x => x.GetLockForFile("Data\\AuditableData\\" + ExpectedData.Id + ".audit"),
                                    lockFile);
            MockFileStreamer.Verify(
                x => x.WriteDataToStream("Data\\AuditableData\\" + ExpectedData.Id + ".audit", expectedAuditLog),
                retryAttempts);
            MockFileStreamer.Verify(x => x.UnlockFile(It.IsAny <string>()), lockFile);
            MockAuditGenerator.Verify(auditGenerator, retryAttempts);
            return(result);
        }