public void GetServices_Database_Unknown()
        {
            CreateNeccessaryDir();

            var component = new Component()
            {
                Name = "site-status.Test",
                Version = "0.1.123"
            };

            List<Service> services = new List<Service>()
            {
                new Service
                {
                    Name = "NoDbConnectionMonitor",
                    Type = ServiceType.Database
                }
            };

            WriteFiles(component, services);

            var target = new SiteStatusRepository("");

            var actualServices = target.GetServices();
            Assert.IsNotNull(actualServices);
            Assert.AreEqual(1, actualServices.Count);
            Assert.AreEqual(actualServices[0].Status, ServiceStatus.Unknown);
        }
        public void GetConfiguration_NullArrayOutputs_All()
        {
            CreateNeccessaryDir();

            var component = new Component()
                    {
                        Name = "site-status.Test",
                        Version = "0.1.123"
                    };

            WriteFiles(component);

            var target = new SiteStatusRepository("");

            var report = target.GetConfiguration();
            var reportProperties = report.GetType();
            var reportArrayPropertyGets = (
                from prop in report.GetType().GetProperties()
                where
                    prop.PropertyType.IsArray
                    && prop.GetGetMethod() != null
                select prop.GetGetMethod()
            ).ToList();

            foreach(var get in reportArrayPropertyGets)
            {
                Assert.IsNull(get.Invoke(report, null));
            }
        }
        /// <summary>
        /// Writes the .ver file, and STatusConfiguration.xml for testing.
        /// </summary>
        /// <param name="component">The contents of the .ver file.</param>
        /// <param name="services">The contents of the StatusConfiguration.xml 
        /// file.</param>
        private void WriteFiles(Component component, List<Service> services = null)
        {
            #region PREPARES FOR THE TEST
            var statusConfig = new Report();
            if (services == null)
                services = new List<Service>();

            statusConfig.Service = services.ToArray();

            using (var writer = new StreamWriter(Path.Combine(SiteStatusDirectory, "Site.ver")))
            {
                component.SerializeTo(writer);
            }

            using (var writer = new StreamWriter(ConfigurationPath))
            {
                statusConfig.SerializeTo(writer);
            }
            #endregion
        }
        public void WriteErrors_StatusConfigurationFormatProblem()
        {
            CreateNeccessaryDir();

            var statusConfigContents = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
                + "<Report xmlns=\"com.iinteractive.reporting\">"
                // The quotations after Type="Service is missing.
                + "<Service Name=\"SomeServiceName\" Type=\"Service />"
                + "</Report>";

            var component = new Component()
            {
                Name = "site-status.Test",
                Version = "0.1.123"
            };

            #region PREPARES FOR THE TEST
            var statusConfig = new Report();

            using (var writer = new StreamWriter(Path.Combine(SiteStatusDirectory, "Site.ver")))
            {
                component.SerializeTo(writer);
            }

            using (var writer = new StreamWriter(ConfigurationPath))
            {
                writer.Write(statusConfigContents);
            }
            #endregion

            var target = new SiteStatusRepository("");
            var result = "";
            using(var writer = new StringWriter())
            {
                target.WriteErrors(writer);
                result = writer.ToString();
            }

            var serializer = new XmlSerializer(typeof(List<SiteStatusIssue>), new Type[] { typeof(SiteStatusIssue) });
            using(var reader = new StringReader(result))
            {
                var actualErrors = (List<SiteStatusIssue>)serializer.Deserialize(reader);
                Assert.IsNotNull(actualErrors);
                Assert.AreNotEqual(0, actualErrors.Count);
            }
        }
        public void WriteErrors_StatusConfigurationDoesNotExistError()
        {
            DeleteDir();

            CreateNeccessaryDir();

            var component = new Component()
            {
                Name = "site-status.Test",
                Version = "0.1.123"
            };

            List<Service> services = new List<Service>()
            {
                new Service
                {
                    Name = "NoDbConnectionMonitor",
                    Type = ServiceType.Database
                }
            };

            #region PREPARES FOR THE TEST
            var statusConfig = new Report();
            if (services == null)
                services = new List<Service>();

            statusConfig.Service = services.ToArray();

            using (var writer = new StreamWriter(Path.Combine(SiteStatusDirectory, "Site.ver")))
            {
                component.SerializeTo(writer);
            }
            #endregion

            var target = new SiteStatusRepository("");
            var result = "";
            using (var writer = new StringWriter())
            {
                target.WriteErrors(writer);
                result = writer.ToString();
            }

            var serializer = new XmlSerializer(typeof(List<SiteStatusIssue>), new Type[] { typeof(SiteStatusIssue) });
            using (var reader = new StringReader(result))
            {
                var actualErrors = (List<SiteStatusIssue>)serializer.Deserialize(reader);
                Assert.IsNotNull(actualErrors);
                Assert.AreNotEqual(0, actualErrors.Count);
            }
        }
        public void WriteErrors_NoErrors_All()
        {
            DeleteDir();

            CreateNeccessaryDir();

            var component = new Component()
            {
                Name = "site-status.Test",
                Version = "0.1.123"
            };

            List<Service> services = new List<Service>()
            {
                // We give it a bad connection, because it shouldn't be
                // reported as an error.
                new Service
                {
                    Name = "OracleDatabase",
                    Type = ServiceType.Database
                }
            };

            WriteFiles(component, services);

            var target = new SiteStatusRepository("");

            var result = "";
            using (var writer = new StringWriter())
            {
                target.WriteErrors(writer);
                result = writer.ToString();
            }

            var serializer = new XmlSerializer(typeof(List<SiteStatusIssue>), new Type[] { typeof(SiteStatusIssue) });
            using (var reader = new StringReader(result))
            {
                var actualErrors = (List<SiteStatusIssue>)serializer.Deserialize(reader);
                Assert.IsNotNull(actualErrors);
                Assert.AreEqual(0, actualErrors.Count);
            }
        }
        public void WriteErrors_ComponentFormatProblem()
        {
            CreateNeccessaryDir();

            var component = new Component()
            {
                Name = "site-status.Test",
                Version = "0.1.123"
            };

            List<Service> services = new List<Service>()
            {
                new Service
                {
                    Name = "OracleDatabase",
                    Type = ServiceType.Database
                }
            };

            WriteFiles(component, services);

            var directoryInfo = new DirectoryInfo(Path.Combine("", SiteStatusDirectory));
            Assert.IsTrue(directoryInfo.Exists);
            var componentFile = new FileInfo(Path.Combine(directoryInfo.FullName, "Site.ver"));

            var componentContents = "";
            using(var reader = new StreamReader(componentFile.FullName))
            {
                componentContents = reader.ReadToEnd();
                Assert.IsFalse(string.IsNullOrEmpty(componentContents));
            }

            componentContents = componentContents.Replace("Component", "Component12sad");
            using(var writer = new StreamWriter(componentFile.FullName))
            {
                writer.Write(componentContents);
            }

            var target = new SiteStatusRepository("");
            var result = "";
            using (var writer = new StringWriter())
            {
                target.WriteErrors(writer);
                result = writer.ToString();
            }

            var serializer = new XmlSerializer(typeof(List<SiteStatusIssue>), new Type[] { typeof(SiteStatusIssue) });
            using (var reader = new StringReader(result))
            {
                var actualErrors = (List<SiteStatusIssue>)serializer.Deserialize(reader);
                Assert.IsNotNull(actualErrors);
                Assert.AreNotEqual(0, actualErrors.Count);
            }
        }
        public void NoService_Config()
        {
            CreateNeccessaryDir();

            var component = new Component()
                    {
                        Name = "site-status.Test",
                        Version = "0.1.123"
                    };

            List<Service> services = new List<Service>();

            WriteFiles(component);

            #region PERFORMS THE TEST
            string result = null;
            var target = new SiteStatusRepository("");
            using (var writer = new StringWriter())
            {
                target.Write(writer);
                result = writer.ToString();
            }

            Assert.IsNotNull(result);

            using (var reader = new StringReader(result))
            {
                var actualReport = Report.DeserializeFrom(reader);
                Assert.IsNotNull(actualReport.Component);
                Assert.AreEqual(1, actualReport.Component.Length);
                if (services.Count == 0)
                    Assert.IsNull(actualReport.Service);
                else
                    Assert.AreEqual(services.Count, actualReport.Service.Length);
            }
            #endregion
        }