public void RunTest_FailedTest()
        {
            // Arrange
            var componentUnderTest = new HealthCheck();
            var loggedData         = new List <LoggedData>();
            var loggedExceptions   = new List <LoggedException>();
            var referenceException = new NullReferenceException();

            StubLogger.ShouldLogLoggingLevelString = (level, s) => true;

            StubLogger.LogLoggingLevelStringFuncOfStringObjectArray = (level, category, messageFormat, args) =>
            {
                loggedData.Add(new LoggedData
                {
                    LoggingLevel = level,
                    Category     = category,
                    Message      = messageFormat(),
                    Args         = args
                });
            };

            StubLogger.LogExceptionStringException = (category, exception) =>
            {
                loggedExceptions.Add(new LoggedException
                {
                    Exception = exception,
                    Category  = category
                });
            };

            // Act
            componentUnderTest.RunTest(() => { throw referenceException; }, "simple");

            // Assert
            loggedData.ShouldHaveSameValueAs(new List <LoggedData>
            {
                new LoggedData
                {
                    LoggingLevel = LoggingLevel.Info,
                    Category     = CoreLoggingCategory.HealthCheck,
                    Message      = "simple failed",
                    Args         = new object[0]
                }
            });
            loggedExceptions.ShouldHaveSameValueAs(new List <LoggedException>
            {
                new LoggedException
                {
                    Category  = CoreLoggingCategory.HealthCheck,
                    Exception = referenceException
                }
            });

            componentUnderTest.AllPassed.ShouldHaveSameValueAs(false);
        }
 public static void RunDbContextTest <T>(this HealthCheck healthCheck, string connectionStringOrName, string name)
     where T : DbContext
 {
     healthCheck.RunTest(() =>
     {
         using (var db = (T)Activator.CreateInstance(typeof(T), connectionStringOrName))
         {
             using (var conn = db.Database.Connection)
             {
                 conn.Open();
             }
         }
     }, name);
 }
 public static void RunDbContextTest <T>(this HealthCheck healthCheck, string name)
     where T : DbContext, new()
 {
     healthCheck.RunTest(() =>
     {
         using (var db = new T())
         {
             using (var conn = db.Database.Connection)
             {
                 conn.Open();
             }
         }
     }, name);
 }
        public void AllPassed_FailedTest()
        {
            // Arrange
            var componentUnderTest = new HealthCheck();

            componentUnderTest.RunTest(() => {  }, "test1");
            componentUnderTest.RunTest(() => { throw new Exception(); }, "test2");

            // Act
            var actual = componentUnderTest.AllPassed;

            // Assert
            actual.ShouldHaveSameValueAs(false);
        }
        public void AllPassed_PassingTest()
        {
            // Arrange
            var componentUnderTest = new HealthCheck();

            componentUnderTest.RunTest(() => { }, "test1");
            componentUnderTest.RunTest(() => { }, "test2");

            // Act
            var actual = componentUnderTest.AllPassed;

            // Assert
            actual.ShouldHaveSameValueAs(true);
        }
 /// <summary>
 /// Run a health check test against another health check service.
 /// </summary>
 /// <param name="healthCheck">The <see cref="HealthCheck"/> instance to use to run the actual test.</param>
 /// <param name="baseUri">The base uri of the health check service being tested.</param>
 /// <param name="serviceHealthCheckPath">The path appended to the <paramref name="baseUri"/> of the health check service api call.</param>
 /// <param name="name">The name of the health check test that gets written to the log.</param>
 public static void RunHealthCheckServiceTest(this HealthCheck healthCheck, string baseUri, string serviceHealthCheckPath, string name)
 {
     healthCheck.RunTest(() =>
     {
         using (var client = new HttpClient())
         {
             client.BaseAddress = new Uri(baseUri);
             client.DefaultRequestHeaders.Accept.Clear();
             var response = client.GetAsync(new Uri(baseUri + serviceHealthCheckPath)).Result;
             if (!response.IsSuccessStatusCode)
             {
                 throw new HttpRequestException($"Health check failed with status code {response.StatusCode}, {response.ReasonPhrase}.");
             }
         }
     }, name);
 }
        public void RunTest_PassedTest()
        {
            // Arrange
            var componentUnderTest = new HealthCheck();
            var loggedData         = new List <LoggedData>();

            StubLogger.ShouldLogLoggingLevelString = (level, s) => true;

            StubLogger.LogLoggingLevelStringFuncOfStringObjectArray = (level, category, messageFormat, args) =>
            {
                loggedData.Add(new LoggedData
                {
                    LoggingLevel = level,
                    Category     = category,
                    Message      = messageFormat(),
                    Args         = args
                });
            };

            // Act
            componentUnderTest.RunTest(() => { }, "simple");

            // Assert
            loggedData.ShouldHaveSameValueAs(new List <LoggedData>
            {
                new LoggedData
                {
                    LoggingLevel = LoggingLevel.Info,
                    Category     = CoreLoggingCategory.HealthCheck,
                    Message      = "simple passed",
                    Args         = new object[0]
                }
            });

            componentUnderTest.AllPassed.ShouldHaveSameValueAs(true);
        }