public void PostEndpointsHealth_should_update_endpoints_health_and_adjust_check_time_with_clientServer_time_difference()
        {
            var update1 = new EndpointHealthUpdate {
                EndpointId = Guid.NewGuid(), CheckTimeUtc = _utcNow, Status = EndpointStatus.Offline, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary <string, string> {
                    { "a", "b" }
                }
            };
            var update2 = new EndpointHealthUpdate {
                EndpointId = Guid.NewGuid(), CheckTimeUtc = _utcNow, Status = EndpointStatus.Healthy, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary <string, string> {
                    { "a", "b" }
                }
            };
            var timeDifference = TimeSpan.FromMinutes(5);

            var updates  = new[] { update1, update2 };
            var expected = updates
                           .Select(u => new EndpointHealthUpdate
            {
                CheckTimeUtc = u.CheckTimeUtc - timeDifference,
                Details      = u.Details,
                EndpointId   = u.EndpointId,
                ResponseTime = u.ResponseTime,
                Status       = u.Status
            })
                           .ToArray();

            AuthorizeRequest(SecurityRole.Monitor);
            Assert.IsType <OkResult>(_controller.PostEndpointsHealth(_utcNow + timeDifference, update1, update2));


            foreach (var expectedEndpoint in expected)
            {
                _endpointRegistry.Verify(r => r.UpdateHealth(expectedEndpoint.EndpointId, It.Is <EndpointHealth>(h => AssertHealth(h, expectedEndpoint))));
            }
        }
Exemple #2
0
 private static bool AssertHealth(EndpointHealth actual, EndpointHealthUpdate 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 PostEndpointsHealth_should_update_endpoints_health()
        {
            var update1 = new EndpointHealthUpdate {
                EndpointId = Guid.NewGuid(), CheckTimeUtc = DateTime.UtcNow, Status = EndpointStatus.Offline, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary <string, string> {
                    { "a", "b" }
                }
            };
            var update2 = new EndpointHealthUpdate {
                EndpointId = Guid.NewGuid(), CheckTimeUtc = DateTime.UtcNow, Status = EndpointStatus.Healthy, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary <string, string> {
                    { "a", "b" }
                }
            };

            Assert.IsType <OkResult>(_controller.PostEndpointHealth(update1, update2));

            _endpointRegistry.Verify(r => r.UpdateHealth(update1.EndpointId, It.Is <EndpointHealth>(h => AssertHealth(h, update1))));
            _endpointRegistry.Verify(r => r.UpdateHealth(update2.EndpointId, It.Is <EndpointHealth>(h => AssertHealth(h, update2))));
        }
        public void PostEndpointsHealth_should_not_update_health_if_admin_credentials_provided()
        {
            var endpointId    = Guid.NewGuid();
            var hasBeenCalled = false;

            AuthorizeRequest(SecurityRole.Admin);
            _endpointRegistry.Setup(m => m.UpdateHealth(It.IsAny <Guid>(), It.IsAny <EndpointHealth>())).Callback(() =>
            {
                hasBeenCalled = true;
            });
            var update = new EndpointHealthUpdate {
                EndpointId = endpointId, CheckTimeUtc = DateTime.UtcNow, Status = EndpointStatus.Healthy, ResponseTime = TimeSpan.FromSeconds(5), Details = new Dictionary <string, string> {
                    { "a", "b" }
                }
            };

            Assert.Throws <UnauthorizedAccessException>(() => _controller.PostEndpointsHealth(null, update));
            Assert.False(hasBeenCalled);
        }
        public async Task UploadHealthAsync_should_pass_clientCurrentTime_with_request()
        {
            var mockHttp                = new MockHttpMessageHandler();
            var mockTimeCoordinator     = new Mock <ITimeCoordinator>();
            var mockCredentialsProvider = new Mock <ICredentialsProvider>();
            var currentTimeUtc          = DateTime.UtcNow;

            mockTimeCoordinator.Setup(c => c.UtcNow).Returns(currentTimeUtc);
            var client = new TestableHealthMonitorExchangeClient(mockHttp, mockTimeCoordinator.Object, mockCredentialsProvider.Object);

            mockHttp.Setup(
                $"http://mock/api/endpoints/health?clientCurrentTime={currentTimeUtc.ToString("u", CultureInfo.InvariantCulture)}",
                req => new HttpResponseMessage(HttpStatusCode.OK));

            try
            {
                var update = new EndpointHealthUpdate(Guid.NewGuid(), new EndpointHealth(DateTime.UtcNow, TimeSpan.Zero, EndpointStatus.Faulty));
                await client.UploadHealthAsync(new[] { update }, CancellationToken.None);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Client should not throw, but it did: {e.Message}", e);
            }
        }