private async void ShouldReturnServiceAsUnhealthyWhenServiceHasStatus500()
        {
            var handlerMock          = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var httpClient           = new HttpClient(handlerMock.Object);
            var openmrsConfiguration = new OpenMrsConfiguration
            {
                Url      = "https://someurl/openmrs/",
                Username = "******",
                Password = "******"
            };
            var openmrsClient = new OpenMrsClient(httpClient, openmrsConfiguration);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest
            })
            .Verifiable();
            OpenMrsHealthCheckClient openMrsHealthCheckClient = new OpenMrsHealthCheckClient(new Dictionary <string, string> {
                { "Service", "path/to/resource" }
            }, openmrsClient);

            var result = await openMrsHealthCheckClient.CheckHealth();

            result["Service"].Should().Be("Unhealthy");
        }
Example #2
0
        public async Task ShouldThrowErrorIfGetAsyncReturnsNonSuccessStatusCode()
        {
            //Given
            var handlerMock          = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var httpClient           = new HttpClient(handlerMock.Object);
            var openmrsConfiguration = new OpenMrsConfiguration
            {
                Url      = "https://someurl/openmrs/",
                Username = "******",
                Password = "******"
            };
            var openmrsClient = new OpenMrsClient(httpClient, openmrsConfiguration);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent("some error message")
            })
            .Verifiable();

            //When
            Func <Task> getAsyncMethod = async() => { await openmrsClient.GetAsync("path/to/resource"); };

            //Then
            getAsyncMethod.Should().Throw <OpenMrsConnectionException>();
        }
Example #3
0
        public async Task ShouldThrowExceptionAndLogIfAnyExceptionIsThrown()
        {
            //Given
            var handlerMock          = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var httpClient           = new HttpClient(handlerMock.Object);
            var openmrsConfiguration = new OpenMrsConfiguration
            {
                Url      = "https://someurl/openmrs/",
                Username = "******",
                Password = "******"
            };
            var openmrsClient = new OpenMrsClient(httpClient, openmrsConfiguration);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ThrowsAsync(new Exception("some message here"))
            .Verifiable();

            //When
            Func <Task> getAsyncMethod = async() => { await openmrsClient.GetAsync("path/to/resource"); };

            //Then
            getAsyncMethod.Should().Throw <Exception>();
        }
Example #4
0
        public async Task ShouldInterrogateTheRightDataSource(string patientDiscoveryPath)
        {
            //Given
            var handlerMock          = new Mock <HttpMessageHandler>(MockBehavior.Loose);
            var httpClient           = new HttpClient(handlerMock.Object);
            var openmrsConfiguration = new OpenMrsConfiguration
            {
                Url      = "https://someurl/openmrs/",
                Username = "******",
                Password = "******"
            };
            var openmrsClient            = new OpenMrsClient(httpClient, openmrsConfiguration);
            var wasCalledWithTheRightUri = false;

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .Callback <HttpRequestMessage, CancellationToken>((response, token) =>
            {
                if (response.RequestUri.AbsoluteUri == "https://someurl/openmrs/path/to/resource" ||
                    response.RequestUri.AbsoluteUri == "https://someurl/openmrs//path/to/resource")
                {
                    wasCalledWithTheRightUri = true;
                }
            })
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK
            })
            .Verifiable();

            //When
            await openmrsClient.GetAsync(patientDiscoveryPath);

            wasCalledWithTheRightUri.Should().BeTrue();
        }
Example #5
0
        public async Task ShouldGetPatientDataRealCallAsync()
        {
            //Given
            // Disable SSL verification in test only
            var handler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback =
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            };
            var httpClient           = new HttpClient(handler);
            var openmrsConfiguration = new OpenMrsConfiguration
            {
                Url      = "https://someurl/openmrs/",
                Username = "******",
                Password = "******"
            };
            var openmrsClient       = new OpenMrsClient(httpClient, openmrsConfiguration);
            var discoveryDataSource = new FhirDiscoveryDataSource(openmrsClient);
            //When
            var patients = await discoveryDataSource.LoadPatientsAsync(null, null, null);

            //Then
            patients.Should().NotBeEmpty();
        }