Exemplo n.º 1
0
        public void Should_throw_if_there_are_several_database_configuration_information()
        {
            //Arrange
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.ReadAllText("File1.xml")).Returns(() => "<Database><Url>A</Url><Username>A</Username><Password>A</Password><Name>A</Name></Database>");
            fileLoaderMock.Setup(x => x.ReadAllText("File2.xml")).Returns(() => "<Database><Url>A</Url><Username>A</Username><Password>A</Password><Name>A</Name></Database>");
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);
            IConfig config = null;
            Exception exception = null;

            //Act
            try
            {
                config = configBusiness.LoadFiles(new[] { "File1.xml", "File2.xml" });
            }
            catch (Exception exp)
            {
                exception = exp;
            }

            //Assert            
            Assert.That(config, Is.Null);
            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.EqualTo("There are database configuration sections in more than one config file."));
        }
        public void Should_create_folder_when_it_does_not_exist()
        {
            //Arrange
            var directoryPath = "ABC";
            var doesFolderExist = false;
            var doesFileExist = true;
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.GetApplicationFolderPath()).Returns(directoryPath);
            fileLoaderMock.Setup(x => x.DoesDirectoryExist(directoryPath)).Returns(() => doesFolderExist).Callback(() => { doesFolderExist = true; });
            fileLoaderMock.Setup(x => x.DoesFileExist(It.IsAny<string>())).Returns(() => doesFileExist).Callback(() => { doesFileExist = false; }); ;
            fileLoaderMock.Setup(x => x.CreateDirectory(directoryPath));
            fileLoaderMock.Setup(x => x.DeleteFile(It.IsAny<string>()));
            fileLoaderMock.Setup(x => x.WriteAllText(It.IsAny<string>(), It.IsAny<string>()));
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns("<a/>");
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);

            //Act
            var config = configBusiness.OpenDatabaseConfig();

            //Assert
            Assert.That(config, Is.Not.Null);
            Assert.That(config.Url, Is.EqualTo(Constants.NoConfigUrl));
            Assert.That(config.Username, Is.EqualTo(null));
            Assert.That(config.Password, Is.EqualTo(null));
            Assert.That(config.Name, Is.EqualTo(null));
            fileLoaderMock.Verify(x => x.CreateDirectory(directoryPath), Times.Once);
        }
Exemplo n.º 3
0
        public WindowsService()
        {
            _console = new ServerConsole();
            ServiceName = Constants.ServiceName;

            //TODO: This can be removed when the new version of Tharga.Toolkit.Console is used. One version after 1.5.13.0 will do this for you.
            if (!EventLog.SourceExists(ServiceName))
            {
                EventLog.CreateEventSource(ServiceName, "Application");
            }

            //TODO: Inject before this point
            var configBusiness = new ConfigBusiness(new FileLoaderAgent());
            configBusiness.InvalidConfigEvent += InvalidConfigEvent;
            var influxDbAgentLoader = new InfluxDbAgentLoader();
            var counterBusiness = new CounterBusiness();
            counterBusiness.GetPerformanceCounterEvent += GetPerformanceCounterEvent;
            var sendBusiness = new SendBusiness(configBusiness, influxDbAgentLoader);
            sendBusiness.SendBusinessEvent += SendBusinessEvent;
            var tagLoader = new TagLoader(configBusiness);
            _processor = new Processor(configBusiness, counterBusiness, sendBusiness, tagLoader);
            _processor.EngineActionEvent += _processor_EngineActionEvent;

            // These Flags set whether or not to handle that specific
            //  type of event. Set to true if you need it, false otherwise.
            CanHandlePowerEvent = true;
            CanHandleSessionChangeEvent = true;
            CanPauseAndContinue = true;
            CanShutdown = true;
            CanStop = true;
        }
Exemplo n.º 4
0
        public WindowsService()
        {
            _console = new ServerConsole();
            ServiceName = Constants.ServiceName;

            //TODO: Inject before this point
            var configBusiness = new ConfigBusiness(new FileLoaderAgent());
            configBusiness.InvalidConfigEvent += InvalidConfigEvent;
            var influxDbAgentLoader = new InfluxDbAgentLoader();
            var counterBusiness = new CounterBusiness();
            var publisherBusiness = new PublisherBusiness();
            counterBusiness.GetPerformanceCounterEvent += GetPerformanceCounterEvent;
            CounterBusiness.ChangedCurrentCultureEvent += ChangedCurrentCultureEvent;
            var sendBusiness = new SendBusiness(configBusiness, influxDbAgentLoader);
            sendBusiness.SendBusinessEvent += SendBusinessEvent;
            var tagLoader = new TagLoader(configBusiness);
            _processor = new Processor(configBusiness, counterBusiness, publisherBusiness, sendBusiness, tagLoader);
            _processor.EngineActionEvent += _processor_EngineActionEvent;
            _logger = new MyLogger();

            // These Flags set whether or not to handle that specific
            //  type of event. Set to true if you need it, false otherwise.
            CanHandlePowerEvent = true;
            CanHandleSessionChangeEvent = true;
            CanPauseAndContinue = true;
            CanShutdown = true;
            CanStop = true;

            _console.LineWrittenEvent += _console_LineWrittenEvent;
        }
Exemplo n.º 5
0
 public CompositeRoot()
 {
     ClientConsole = new ClientConsole();
     InfluxDbAgentLoader = new InfluxDbAgentLoader();
     FileLoaderAgent = new FileLoaderAgent();
     ConfigBusiness = new ConfigBusiness(FileLoaderAgent);
     ConfigBusiness.InvalidConfigEvent += InvalidConfigEvent;
     CounterBusiness = new CounterBusiness();
     SendBusiness = new SendBusiness(ConfigBusiness, InfluxDbAgentLoader);
     SendBusiness.SendBusinessEvent += SendBusinessEvent;
     TagLoader = new TagLoader(ConfigBusiness);
 }
Exemplo n.º 6
0
        public void Should_not_throw_if_there_is_no_database_configuration_information()
        {
            //Arrange
            var folderPath = "ABC";
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.GetApplicationFolderPath()).Returns(folderPath);
            fileLoaderMock.Setup(x => x.DoesDirectoryExist(folderPath)).Returns(true);
            fileLoaderMock.Setup(x => x.DoesFileExist(It.IsAny<string>())).Returns(false);
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns(() => "<A></A>");
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);

            //Act
            var config = configBusiness.LoadFile("myFile.xml");

            //Assert
            Assert.That(config, Is.Not.Null);
        }
Exemplo n.º 7
0
        public void Should_fill_database_configuration()
        {
            //Arrange
            var url = "A";
            var username = "******";
            var password = "******";
            var databaseName = "D";
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns(() => string.Format("<Database><Url>{0}</Url><Username>{1}</Username><Password>{2}</Password><Name>{3}</Name></Database>", url, username, password, databaseName));
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);

            //Act
            var config = configBusiness.LoadFile("myFile.xml");            

            //Assert            
            Assert.That(config.Database, Is.Not.Null);
            Assert.That(config.Database.Url, Is.EqualTo(url));
            Assert.That(config.Database.Username, Is.EqualTo(username));
            Assert.That(config.Database.Password, Is.EqualTo(password));
            Assert.That(config.Database.Name, Is.EqualTo(databaseName));
        }
Exemplo n.º 8
0
        public void Should_throw_if_group_SecondsInterval_is_not_numeric()
        {
            //Arrange
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns(() => string.Format("<CounterGroup Name=\"A\" SecondsInterval=\"A\"></CounterGroup>"));
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);
            IConfig config = null;
            Exception exception = null;

            //Act
            try
            {
                config = configBusiness.LoadFile("myFile.xml");
            }
            catch (Exception exp)
            {
                exception = exp;
            }

            //Assert            
            Assert.That(config, Is.Null);
            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.EqualTo("Cannot parse attribute SecondsInterval value to integer."));
        }
Exemplo n.º 9
0
        public void Should_throw_if_group_has_no_Name()
        {
            //Arrange
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns(() => string.Format("<CounterGroup></CounterGroup>"));
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);
            IConfig config = null;
            Exception exception = null;

            //Act
            try
            {
                config = configBusiness.LoadFile("myFile.xml");
            }
            catch (Exception exp)
            {
                exception = exp;
            }

            //Assert            
            Assert.That(config, Is.Null);
            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.EqualTo("No Name attribute specified for the CounterGroup."));
        }
Exemplo n.º 10
0
        public void Should_set_all_provided_values_to_the_counter()
        {
            //Arrange
            var counterGroupName = "AA";
            var secondsInterval = 11;
            var counterName = "A";
            var categoryName = "B";
            var instanceName = "C";
            var fieldName = "D";
            var max = 25.5f;
            var fileLoaderMock = new Mock<IFileLoaderAgent>(MockBehavior.Strict);
            fileLoaderMock.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns(() => string.Format(CultureInfo.InvariantCulture, "<{0}><Database><Url>X</Url><Username>X</Username><Password>X</Password><Name>X</Name></Database><CounterGroup Name=\"{1}\" SecondsInterval=\"{2}\"><Counter><CounterName>{3}</CounterName><CategoryName>{4}</CategoryName><InstanceName>{5}</InstanceName><FieldName>{6}</FieldName><Limits Max=\"{7}\" /></Counter></CounterGroup></{0}>", Constants.ServiceName, counterGroupName, secondsInterval, counterName, categoryName, instanceName, fieldName, max));
            var configBusiness = new ConfigBusiness(fileLoaderMock.Object);
            Exception exception = null;

            //Act
            var config = configBusiness.LoadFile("myFile.xml");

            //Assert            
            Assert.That(config, Is.Not.Null);
            Assert.That(exception, Is.Null);
            Assert.That(config.Groups.Count, Is.EqualTo(1));
            Assert.That(config.Groups.Single().Name, Is.EqualTo(counterGroupName));
            Assert.That(config.Groups.Single().SecondsInterval, Is.EqualTo(secondsInterval));
            Assert.That(config.Groups.Single().Counters.Count(), Is.EqualTo(1));
            Assert.That(config.Groups.Single().Counters.Single().CategoryName, Is.EqualTo(categoryName));
            Assert.That(config.Groups.Single().Counters.Single().CounterName, Is.EqualTo(counterName));
            Assert.That(config.Groups.Single().Counters.Single().InstanceName, Is.EqualTo(instanceName));
            Assert.That(config.Groups.Single().Counters.Single().FieldName, Is.EqualTo(fieldName));
            Assert.That(config.Groups.Single().Counters.Single().Max, Is.EqualTo(max));
        }