public void Load_StorageFilePathContainsEmptyArray_ResultIsEmpty()
        {
            // Arrange
            string configfurationFileName = "File-With-EmptyArray" + TestConfigFileExtension;

            File.WriteAllText(
                configfurationFileName,
                "[]",
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsEmpty(result);
        }
        public void Load_StorageFilePathContainsValidContent_ResultContainsAllItems()
        {
            // Arrange
            string configfurationFileName = "File-With-Valid-Content" + TestConfigFileExtension;
            var    itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            File.WriteAllText(
                configfurationFileName,
                JsonConvert.SerializeObject(itemArray),
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.AreEqual(itemArray, result);
        }
        public void Load_StorageFilePathContainsInvalidContent_ResultIsNull()
        {
            // Arrange
            string configfurationFileName = "File-With-Invalid-Content" + TestConfigFileExtension;

            File.WriteAllText(
                configfurationFileName,
                "Garble Garble. Yada Yada. I will cause an exception when you try to deserialize me.",
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsNull(result);
        }
        public void Constructotor_AllParametersAreSet_ObjectIsInstantiated()
        {
            // Arrange
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();
            var encodingProvider      = new Mock <IEncodingProvider>();

            // Act
            var messageQueuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, encodingProvider.Object);

            // Assert
            Assert.IsNotNull(messageQueuePersistence);
        }
        public void Constructotor_AllParametersAreSet_ObjectIsInstantiated()
        {
            // Arrange
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            var encodingProvider = new Mock<IEncodingProvider>();

            // Act
            var messageQueuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, encodingProvider.Object);

            // Assert
            Assert.IsNotNull(messageQueuePersistence);
        }
        public void Save_TargetFileNameIsInvalid_MessageQueuePersistenceExceptionIsThrown(string targetFileName)
        {
            // Arrange
            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = targetFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(itemArray);
        }
        public void Save_ParameterIsNull_ArgumentNullExceptionIsThrown()
        {
            // Arrange
            string configfurationFileName = "A-New-Config-File" + TestConfigFileExtension;

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(null);
        }
        public void Load_StorageFilePathDoesNotExist_ResultIsNull()
        {
            // Arrange
            string configfurationFileName = "Non-Existing-Config-File" + TestConfigFileExtension;

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsNull(result);
        }
        public void Save_TargetFileIsValid_DoesNotYetExist_TargetfileIsIsNotEmptyAfterSave()
        {
            // Arrange
            string configfurationFileName = "New-File+" + Guid.NewGuid() + TestConfigFileExtension;
            var    itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(itemArray);

            // Assert
            Assert.IsNotNullOrEmpty(File.ReadAllText(configfurationFileName));
        }
        public void Load_StorageFilePathContainsInvalidContent_ResultIsNull()
        {
            // Arrange
            string configfurationFileName = "File-With-Invalid-Content" + TestConfigFileExtension;
            File.WriteAllText(
                configfurationFileName,
                "Garble Garble. Yada Yada. I will cause an exception when you try to deserialize me.",
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsNull(result);
        }
        public void Load_StorageFilePathContainsEmptyArray_ResultIsEmpty()
        {
            // Arrange
            string configfurationFileName = "File-With-EmptyArray" + TestConfigFileExtension;
            File.WriteAllText(
                configfurationFileName,
                "[]",
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsEmpty(result);
        }
        public void Save_TargetFileIsLockedByAnotherProcess_MessageQueuePersistenceExceptionIsThrown()
        {
            // Arrange
            string configfurationFileName = "Locked-File" + TestConfigFileExtension;

            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration {
                FilePath = configfurationFileName
            };
            var configurationProvider = new Mock <IJSONMessageQueuePersistenceConfigurationProvider>();

            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            using (var fileStream = File.OpenWrite(configfurationFileName))
            {
                queuePersistence.Save(itemArray);
            }
        }
        public void RunFor10Seconds_SendFailsForAllItems_DispatcherStopsOnlyIfTheQueueIsEmptyAndAllRetryAttempsHaveFailed()
        {
            // Arrange
            int runtimeInMilliseconds = 10 * 1000;
            int itemsReturnedFromSystemInformationProvider = 0;
            int attemptsToSend = 0;

            // prepare system information provider
            var provider = new Mock <ISystemInformationProvider>();

            provider.Setup(p => p.GetSystemInfo()).Returns(() =>
            {
                itemsReturnedFromSystemInformationProvider++;
                return(new SystemInformation {
                    MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow
                });
            });

            // prepare sender
            var sender = new Mock <ISystemInformationSender>();

            sender.Setup(s => s.Send(It.IsAny <SystemInformation>())).Callback(() => { attemptsToSend++; }).Throws(new SendSystemInformationFailedException("Send failed.", null));

            IMessageQueue <SystemInformation>         workQueue            = new SystemInformationMessageQueue();
            IMessageQueue <SystemInformation>         errorQueue           = new SystemInformationMessageQueue();
            IMessageQueueProvider <SystemInformation> messageQueueProvider = new SystemInformationMessageQueueProvider(workQueue, errorQueue);

            IMessageQueueFeeder messageQueueFeeder = new SystemInformationMessageQueueFeeder(provider.Object, workQueue);
            IMessageQueueWorker messageQueueWorker = new SystemInformationMessageQueueWorker(sender.Object, workQueue, errorQueue);

            var agentCoordinationService        = new Mock <IAgentCoordinationService>();
            var agentCoordinationServiceFactory = new Mock <IAgentCoordinationServiceFactory>();

            agentCoordinationServiceFactory.Setup(f => f.GetAgentCoordinationService(It.IsAny <Action>(), It.IsAny <Action>())).Returns(
                agentCoordinationService.Object);

            var messageQueueFeederFactory = new Mock <IMessageQueueFeederFactory>();

            messageQueueFeederFactory.Setup(f => f.GetMessageQueueFeeder()).Returns(messageQueueFeeder);

            var messageQueueWorkerFactory = new Mock <IMessageQueueWorkerFactory>();

            messageQueueWorkerFactory.Setup(f => f.GetMessageQueueWorker()).Returns(messageQueueWorker);

            IMessageQueuePersistence <SystemInformation> messageQueuePersistence =
                new JSONSystemInformationMessageQueuePersistence(this.jsonMessageQueuePersistenceConfigurationProvider, this.encodingProvider);

            var systemInformationDispatchingService = new SystemInformationDispatchingService(
                agentCoordinationServiceFactory.Object,
                messageQueueFeederFactory.Object,
                messageQueueWorkerFactory.Object,
                messageQueueProvider,
                messageQueuePersistence);

            // Act
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var dispatcher = new Task(systemInformationDispatchingService.Start);

            dispatcher.Start();

            Thread.Sleep(runtimeInMilliseconds);
            systemInformationDispatchingService.Stop();

            Task.WaitAll(new[] { dispatcher });

            stopwatch.Stop();

            // Assert
            int queueSize = workQueue.GetSize();

            Console.WriteLine(
                "After a runtime of {0} milliseconds the dispatcher has been stopped with {1} items in queue. It took {2} milliseconds until the queue worker stopped sending out all queue items (Attempts To Send: {3}).",
                runtimeInMilliseconds,
                itemsReturnedFromSystemInformationProvider,
                stopwatch.ElapsedMilliseconds,
                attemptsToSend);

            Assert.AreEqual(0, queueSize);
        }
        public void Load_StorageFilePathContainsValidContent_ResultContainsAllItems()
        {
            // Arrange
            string configfurationFileName = "File-With-Valid-Content" + TestConfigFileExtension;
            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            File.WriteAllText(
                configfurationFileName,
                JsonConvert.SerializeObject(itemArray),
                this.encodingProvider.GetEncoding());

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.AreEqual(itemArray, result);
        }
        public void Load_StorageFilePathDoesNotExist_ResultIsNull()
        {
            // Arrange
            string configfurationFileName = "Non-Existing-Config-File" + TestConfigFileExtension;

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            var result = queuePersistence.Load();

            // Assert
            Assert.IsNull(result);
        }
        public void RunFor10Seconds_SendFailsForAllItems_DispatcherStopsOnlyIfTheQueueIsEmptyAndAllRetryAttempsHaveFailed()
        {
            // Arrange
            int runtimeInMilliseconds = 10 * 1000;
            int itemsReturnedFromSystemInformationProvider = 0;
            int attemptsToSend = 0;

            // prepare system information provider
            var provider = new Mock<ISystemInformationProvider>();
            provider.Setup(p => p.GetSystemInfo()).Returns(() =>
                {
                    itemsReturnedFromSystemInformationProvider++;
                    return new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
                });

            // prepare sender
            var sender = new Mock<ISystemInformationSender>();
            sender.Setup(s => s.Send(It.IsAny<SystemInformation>())).Callback(() => { attemptsToSend++; }).Throws(new SendSystemInformationFailedException("Send failed.", null));

            IMessageQueue<SystemInformation> workQueue = new SystemInformationMessageQueue();
            IMessageQueue<SystemInformation> errorQueue = new SystemInformationMessageQueue();
            IMessageQueueProvider<SystemInformation> messageQueueProvider = new SystemInformationMessageQueueProvider(workQueue, errorQueue);

            IMessageQueueFeeder messageQueueFeeder = new SystemInformationMessageQueueFeeder(provider.Object, workQueue);
            IMessageQueueWorker messageQueueWorker = new SystemInformationMessageQueueWorker(sender.Object, workQueue, errorQueue);

            var agentCoordinationService = new Mock<IAgentCoordinationService>();
            var agentCoordinationServiceFactory = new Mock<IAgentCoordinationServiceFactory>();
            agentCoordinationServiceFactory.Setup(f => f.GetAgentCoordinationService(It.IsAny<Action>(), It.IsAny<Action>())).Returns(
                agentCoordinationService.Object);

            var messageQueueFeederFactory = new Mock<IMessageQueueFeederFactory>();
            messageQueueFeederFactory.Setup(f => f.GetMessageQueueFeeder()).Returns(messageQueueFeeder);

            var messageQueueWorkerFactory = new Mock<IMessageQueueWorkerFactory>();
            messageQueueWorkerFactory.Setup(f => f.GetMessageQueueWorker()).Returns(messageQueueWorker);

            IMessageQueuePersistence<SystemInformation> messageQueuePersistence =
                new JSONSystemInformationMessageQueuePersistence(this.jsonMessageQueuePersistenceConfigurationProvider, this.encodingProvider);

            var systemInformationDispatchingService = new SystemInformationDispatchingService(
                agentCoordinationServiceFactory.Object,
                messageQueueFeederFactory.Object,
                messageQueueWorkerFactory.Object,
                messageQueueProvider,
                messageQueuePersistence);

            // Act
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            var dispatcher = new Task(systemInformationDispatchingService.Start);
            dispatcher.Start();

            Thread.Sleep(runtimeInMilliseconds);
            systemInformationDispatchingService.Stop();

            Task.WaitAll(new[] { dispatcher });

            stopwatch.Stop();

            // Assert
            int queueSize = workQueue.GetSize();
            Console.WriteLine(
                "After a runtime of {0} milliseconds the dispatcher has been stopped with {1} items in queue. It took {2} milliseconds until the queue worker stopped sending out all queue items (Attempts To Send: {3}).",
                runtimeInMilliseconds,
                itemsReturnedFromSystemInformationProvider,
                stopwatch.ElapsedMilliseconds,
                attemptsToSend);

            Assert.AreEqual(0, queueSize);
        }
        public void Save_TargetFileNameIsInvalid_MessageQueuePersistenceExceptionIsThrown(string targetFileName)
        {
            // Arrange
            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = targetFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(itemArray);
        }
        public void Save_TargetFileIsValid_DoesNotYetExist_TargetfileIsIsNotEmptyAfterSave()
        {
            // Arrange
            string configfurationFileName = "New-File+" + Guid.NewGuid() + TestConfigFileExtension;
            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(itemArray);

            // Assert
            Assert.IsNotNullOrEmpty(File.ReadAllText(configfurationFileName));
        }
        public void Save_TargetFileIsLockedByAnotherProcess_MessageQueuePersistenceExceptionIsThrown()
        {
            // Arrange
            string configfurationFileName = "Locked-File" + TestConfigFileExtension;

            var itemArray = TestUtilities.GetSystemInformationObjects(5).Select(systemInformation => new SystemInformationQueueItem(systemInformation)).ToArray();

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            using (var fileStream = File.OpenWrite(configfurationFileName))
            {
                queuePersistence.Save(itemArray);
            }
        }
        public void Save_ParameterIsNull_ArgumentNullExceptionIsThrown()
        {
            // Arrange
            string configfurationFileName = "A-New-Config-File" + TestConfigFileExtension;

            var configuration = new JSONMessageQueuePersistenceConfiguration { FilePath = configfurationFileName };
            var configurationProvider = new Mock<IJSONMessageQueuePersistenceConfigurationProvider>();
            configurationProvider.Setup(c => c.GetConfiguration()).Returns(configuration);

            var queuePersistence = new JSONSystemInformationMessageQueuePersistence(configurationProvider.Object, this.encodingProvider);

            // Act
            queuePersistence.Save(null);
        }