Esempio n. 1
0
        public async Task CourseSearchAsyncReturnsBlankResultsWhenApiReturnsBadRequest()
        {
            // Arrange
            var courseSearchRequest = new CourseSearchRequest {
                SubjectKeyword = "Somekeyword"
            };

            var httpResponse = new HttpResponseMessage {
                StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("{}")
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse);

            var auditService = A.Fake <IAuditService>();

            var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://SomeDummyUrl")
            };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            var result = await findACourseClient.CourseSearchAsync(courseSearchRequest).ConfigureAwait(false);

            // Assert
            Assert.NotNull(result);
            Assert.Empty(result.Results);
            httpResponse.Dispose();
            httpClient.Dispose();
            fakeHttpMessageHandler.Dispose();
        }
Esempio n. 2
0
        public async Task CourseSearchAsyncReturnsHttpRequestExceptionWhenApiReturnsNotSuccessful()
        {
            // Arrange
            var courseSearchRequest = new CourseSearchRequest {
                SubjectKeyword = "Somekeyword"
            };

            var httpResponse = new HttpResponseMessage {
                StatusCode = HttpStatusCode.ServiceUnavailable, Content = new StringContent("{}")
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse);

            var auditService = A.Fake <IAuditService>();

            var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://SomeDummyUrl")
            };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            await Assert.ThrowsAsync <HttpRequestException>(async() => await findACourseClient.CourseSearchAsync(courseSearchRequest).ConfigureAwait(false)).ConfigureAwait(false);

            // Assert
            httpResponse.Dispose();
            httpClient.Dispose();
            fakeHttpMessageHandler.Dispose();
        }
        public async Task CourseSearchAsyncCatchesExceptionWhenApiReturnsNotSuccessful()
        {
            // Arrange
            var courseSearchRequest = new CourseSearchRequest {
                SubjectKeyword = "Somekeyword"
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Throws <Exception>();

            var auditService = A.Fake <IAuditService>();

            using var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            using var httpClient             = new HttpClient(fakeHttpMessageHandler)
                  {
                      BaseAddress = new Uri("http://SomeDummyUrl")
                  };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            var result = await findACourseClient.CourseSearchAsync(courseSearchRequest).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
Esempio n. 4
0
        public async Task CourseSearchAsyncReturnsResponseObjectWhenApiCallIsSuccessful()
        {
            // Arrange
            var courseSearchRequest = new CourseSearchRequest {
                SubjectKeyword = "Somekeyword"
            };
            var expectedResponse = new CourseSearchResponse
            {
                Results = new List <Result>
                {
                    new Result
                    {
                        CourseId    = courseId,
                        CourseRunId = courseRunId,
                        CourseName  = "CourseName",
                    },
                },
            };

            var httpResponse = new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(expectedResponse))
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse);

            var auditService = A.Fake <IAuditService>();

            var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://SomeDummyUrl")
            };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            var result = await findACourseClient.CourseSearchAsync(courseSearchRequest).ConfigureAwait(false);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(expectedResponse.Results.FirstOrDefault()?.CourseName, result.Results?.FirstOrDefault()?.CourseName);
            Assert.Equal(expectedResponse.Results?.FirstOrDefault()?.CourseRunId, result.Results?.FirstOrDefault()?.CourseRunId);
            Assert.Equal(expectedResponse.Results?.FirstOrDefault()?.CourseId, result.Results?.FirstOrDefault()?.CourseId);

            httpResponse.Dispose();
            httpClient.Dispose();
            fakeHttpMessageHandler.Dispose();
        }