Exemplo n.º 1
0
        public async Task GetPendingJobs_ShallBeAbleToReturnDataCorrectly()
        {
            // ARRANGE
            var config = Options.Create(new DicomAdapterConfiguration());

            config.Value.Services.ResultsServiceEndpoint = "http://test.com/";
            config.Value.Dicom.Scu.AeTitle = "clarascu";

            var mockLogger             = new Mock <ILogger <ResultsApi> >();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mockHttpMessageHandler
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = ReadContentFrom("GetPendingTest.json"),
            })
            .Verifiable();

            // use real http client with mocked handler here
            var httpClient = new HttpClient(mockHttpMessageHandler.Object)
            {
                BaseAddress = new Uri("http://test.com/"),
            };

            _httpClientFactory.Setup(p => p.CreateClient(It.IsAny <string>())).Returns(httpClient);
            var subjectUnderTest = new ResultsApi(config, _httpClientFactory.Object, mockLogger.Object);

            // ACT
            var result = await subjectUnderTest.GetPendingJobs(config.Value.Dicom.Scu.AeTitle, CancellationToken.None, 10);

            // ASSERT
            Assert.NotNull(result);
            Assert.Equal(3, result.Count);
            // also check the 'http' call was like we expected it
            var expectedUri = new Uri($"{config.Value.Services.ResultsServiceEndpoint}api/tasks/{config.Value.Dicom.Scu.AeTitle}/pending?size=10");

            mockHttpMessageHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri == expectedUri // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>());
        }
        public async Task GetPendingJobs_ShallReturnNullOnCallFailures()
        {
            // ARRANGE
            var config = Options.Create(new DicomAdapterConfiguration());

            config.Value.Services.ResultsServiceEndpoint = "http://test.com/";
            config.Value.Dicom.Scu.AeTitle = "clarascu";

            var mockLogger             = new Mock <ILogger <ResultsApi> >();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mockHttpMessageHandler
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ThrowsAsync(new HttpRequestException())
            .Verifiable();

            // use real http client with mocked handler here
            var httpClient = new HttpClient(mockHttpMessageHandler.Object)
            {
                BaseAddress = new Uri("http://test.com/"),
            };
            var subjectUnderTest = new ResultsApi(config, httpClient, mockLogger.Object);

            // ACT
            var result = await subjectUnderTest.GetPendingJobs(CancellationToken.None, 10);

            // ASSERT
            Assert.Empty(result);
            // also check the 'http' call was like we expected it
            var expectedUri = new Uri($"{config.Value.Services.ResultsServiceEndpoint}api/tasks/{config.Value.Dicom.Scu.AeTitle}/pending?size=10");

            mockHttpMessageHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(4), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri == expectedUri // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>());
        }