private static bool AssertHealth(EndpointHealth actual, HealthUpdate expected)
 {
     Assert.Equal(expected.CheckTimeUtc, actual.CheckTimeUtc);
     Assert.Equal(expected.ResponseTime, actual.ResponseTime);
     Assert.Equal(expected.Status, actual.Status);
     Assert.Equal(expected.Details, actual.Details);
     return true;
 }
        public void PostEndpointHealth_should_update_health()
        {
            var endpointId = Guid.NewGuid();
            var update = new HealthUpdate { CheckTimeUtc = _utcNow, Status = EndpointStatus.Offline, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary<string, string> { { "a", "b" } } };

            _endpointRegistry.Setup(r => r.UpdateHealth(endpointId, It.IsAny<EndpointHealth>())).Returns(true);
            AuthorizeWithId(endpointId);
            Assert.IsType<OkResult>(_controller.PostEndpointHealth(endpointId, update));

            _endpointRegistry.Verify(r => r.UpdateHealth(endpointId, It.Is<EndpointHealth>(h => AssertHealth(h, update))), Times.Once);
        }
        public void PostEndpointHealth_should_return_NotFound_status_for_unknown_endpoint()
        {
            var endpointId = Guid.NewGuid();
            var update = new HealthUpdate { CheckTimeUtc = _utcNow, Status = EndpointStatus.Offline, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary<string, string> { { "a", "b" } } };

            _endpointRegistry.Setup(r => r.UpdateHealth(endpointId, It.IsAny<EndpointHealth>())).Returns(false);
            AuthorizeWithId(endpointId);
            Assert.IsType<NotFoundResult>(_controller.PostEndpointHealth(endpointId, update));
        }
        public void PostEndpointHealth_should_update_health_and_adjust_check_time_with_clientServer_time_difference()
        {
            var endpointId = Guid.NewGuid();
            var update = new HealthUpdate { CheckTimeUtc = _utcNow, Status = EndpointStatus.Offline, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary<string, string> { { "a", "b" } } };
            var timeDifference = TimeSpan.FromMinutes(5);

            var expected = new HealthUpdate
            {
                CheckTimeUtc = update.CheckTimeUtc - timeDifference,
                Details = update.Details,
                ResponseTime = update.ResponseTime,
                Status = update.Status
            };

            _endpointRegistry.Setup(r => r.UpdateHealth(endpointId, It.IsAny<EndpointHealth>())).Returns(true);
            AuthorizeWithId(endpointId);
            Assert.IsType<OkResult>(_controller.PostEndpointHealth(endpointId, update, _utcNow + timeDifference));
            _endpointRegistry.Verify(r => r.UpdateHealth(endpointId, It.Is<EndpointHealth>(h => AssertHealth(h, expected))), Times.Once);
        }