Exemplo n.º 1
0
        public async Task ReportFailure_ShallBeAbleToReturnDataCorrectly(bool retry)
        {
            // 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,
            })
            .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);
            var taskId           = Guid.NewGuid();

            // ACT
            var result = await subjectUnderTest.ReportFailure(taskId, retry, CancellationToken.None);

            // ASSERT
            Assert.True(result);
            // also check the 'http' call was like we expected it
            var expectedUri = new Uri($"{config.Value.Services.ResultsServiceEndpoint}api/tasks/failure/{taskId}");

            mockHttpMessageHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Put && // we expected a GET request
                                               req.RequestUri == expectedUri && // to this uri
                                               req.Content.ReadAsStringAsync().Result == JsonConvert.SerializeObject(new { RetryLater = retry })
                                               ),
                ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 2
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 ReportSuccess_ShallReturnFalseOnCallFailures()
        {
            // 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);
            var taskId           = Guid.NewGuid();

            // ACT
            var result = await subjectUnderTest.ReportSuccess(taskId, CancellationToken.None);

            // ASSERT
            Assert.False(result);
            // also check the 'http' call was like we expected it
            var expectedUri = new Uri($"{config.Value.Services.ResultsServiceEndpoint}api/tasks/success/{taskId}");

            mockHttpMessageHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(4), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Put && // we expected a GET request
                                               req.RequestUri == expectedUri // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 4
0
 public ObjectDetectionApi(IBitmovinApiClientFactory apiClientFactory)
 {
     _apiClient = apiClientFactory.CreateClient <IObjectDetectionApiClient>();
     Customdata = new CustomdataApi(apiClientFactory);
     Results    = new ResultsApi(apiClientFactory);
 }