示例#1
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();
        }
        public async Task <ActionResult> CourseSearch([FromBody] CourseSearchRequest request)
        {
            var criteria = new SearchCriteriaStructure()
            {
                AttendancePatterns = request.AttendancePatterns,
                DeliveryModes      = request.DeliveryModes,
                Distance           = request.Distance,
                Limit               = request.Limit,
                Postcode            = request.Postcode,
                ProviderName        = request.ProviderName,
                QualificationLevels = request.QualificationLevels,
                SortBy              = request.SortBy,
                Start               = request.Start,
                StartDateFrom       = request.StartDateFrom,
                StartDateTo         = request.StartDateTo,
                StudyModes          = request.StudyModes,
                SubjectKeyword      = request.SubjectKeyword,
                Town = request.Town
            };

            var result = await _service.CourseSearch(_log, criteria);

            var response = new CourseSearchResponse()
            {
                Limit  = result.Limit,
                Start  = result.Start,
                Total  = result.Total,
                Facets = result.Facets.ToDictionary(
                    f => _courseSearchFacetMapping.GetValueOrDefault(f.Key, f.Key),
                    f => f.Value.Select(v => new FacetCountResult()
                {
                    Value = v.Value,
                    Count = v.Count.Value
                })),
                Results = result.Items.Select(i => new CourseSearchResponseItem()
                {
                    Cost                              = i.Course.Cost,
                    CostDescription                   = i.Course.CostDescription,
                    CourseDescription                 = i.Course.CourseDescription,
                    CourseName                        = i.Course.CourseName,
                    CourseId                          = i.Course.CourseId,
                    CourseRunId                       = i.Course.CourseRunId,
                    CourseText                        = i.Course.CourseText,
                    DeliveryMode                      = i.Course.DeliveryMode,
                    DeliveryModeDescription           = i.Course.DeliveryModeDescription,
                    Distance                          = !i.Course.National.GetValueOrDefault() ? i.Distance : null,
                    DurationUnit                      = i.Course.DurationUnit ?? DurationUnit.Undefined,
                    DurationValue                     = i.Course.DurationValue,
                    FlexibleStartDate                 = i.Course.FlexibleStartDate,
                    LearnAimRef                       = i.Course.LearnAimRef,
                    National                          = i.Course.National,
                    QualificationLevel                = i.Course.NotionalNVQLevelv2,
                    ProviderName                      = i.Course.ProviderName,
                    QualificationCourseTitle          = i.Course.QualificationCourseTitle,
                    Region                            = i.Course.Region,
                    SearchScore                       = i.Score,
                    StartDate                         = !i.Course.FlexibleStartDate.GetValueOrDefault() ? i.Course.StartDate : null,
                    UKPRN                             = i.Course.UKPRN,
                    UpdatedOn                         = i.Course.UpdatedOn,
                    VenueAddress                      = i.Course.VenueAddress,
                    VenueAttendancePattern            = i.Course.VenueAttendancePattern,
                    VenueAttendancePatternDescription = i.Course.VenueAttendancePatternDescription,
                    VenueLocation                     = i.Course.VenueLocation != null ?
                                                        new Coordinates()
                    {
                        Latitude  = i.Course.VenueLocation.Latitude,
                        Longitude = i.Course.VenueLocation.Longitude
                    } :
                    null,
                    VenueName                 = i.Course.VenueName,
                    VenueStudyMode            = i.Course.VenueStudyMode,
                    VenueStudyModeDescription = i.Course.VenueStudyModeDescription,
                    VenueTown                 = i.Course.VenueTown
                }).ToList()
            };

            return(new OkObjectResult(response));
        }