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 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_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 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 Setup()
 {
     this.jsonMessageQueuePersistenceConfigurationProvider = new AppConfigJSONMessageQueuePersistenceConfigurationProvider();
     this.persistenceConfiguration = this.jsonMessageQueuePersistenceConfigurationProvider.GetConfiguration();
     this.encodingProvider         = new DefaultEncodingProvider();
 }