public void TestCourseSearchResultWithValidModelStateAndInvalidSearchResult()
        {
            var fromQuery = new CourseSearchRequestModel()
            {
                SubjectKeyword = "TestSubjectKeyword", LocationRadius = 20
            };
            var criteria           = new CourseSearchCriteria("test");
            var courseSearchResult = Result.Ok(new CourseSearchResult(1, 1, 1, new CourseItem[] { }));
            var expected           = new CourseSearchResultViewModel(courseSearchResult)
            {
                SubjectKeyword = fromQuery.SubjectKeyword, Location = fromQuery.Location
            };

            MockTelemetryClient.Setup(x => x.TrackEvent(It.IsAny <string>(), null, null)).Verifiable();
            MockTelemetryClient.Setup(x => x.Flush()).Verifiable();
            MockCourseDirectory.Setup(x => x.CreateCourseSearchCriteria(fromQuery)).Returns(criteria);
            MockCourseDirectory.Setup(x => x.IsSuccessfulResult(
                                          It.IsAny <IResult <CourseSearchResult> >(), It.IsAny <ITelemetryClient>(), It.IsAny <string>(),
                                          It.IsAny <string>(), It.IsAny <DateTime>()
                                          )).Returns(false); // Mock that Is Invalid Search Result
            //MockCourseDirectoryService.Setup(x => x.CourseDirectorySearch(criteria, It.IsAny<PagingOptions>()))
            //    .Returns(courseSearchResult);

            var result = Controller.CourseSearchResult(fromQuery) as ViewResult;

            MockTelemetryClient.Verify(x => x.TrackEvent(It.IsAny <string>(), null, null), (Times.AtLeastOnce()));
            MockTelemetryClient.Verify(x => x.Flush(), (Times.Never()));
            AssertDefaultErrorView(result);
        }
        public void TestHasQualificationLevelsIsTrue()
        {
            const bool expected = true;
            var        request  = new CourseSearchRequestModel
            {
                QualificationLevels = new[] { 1 }
            };

            var actual = Helper.HasQualificationLevels(request);

            expected.IsSame(actual);
        }
        public void TestHasQualificationLevelsIsFalseGivenEmpty()
        {
            const bool expected = false;
            var        request  = new CourseSearchRequestModel
            {
                QualificationLevels = new int[] {}
            };

            var actual = Helper.HasQualificationLevels(request);

            expected.IsSame(actual);
        }
        // GET: CourseDirectory
        // ASB TODO - Should we not be returning OK objects? rather than empty Views if something goes wrong?
        public ActionResult CourseSearchResult([FromQuery]  CourseSearchRequestModel requestModel)
        {
            Telemetry.TrackEvent($"Logging: Started: Controller = {nameof(CourseDirectoryController)}: Action = {nameof(CourseSearchResult)}: {nameof(Environment.MachineName)} = {Environment.MachineName}: {nameof(CorrelationContextAccessor.CorrelationContext.CorrelationId)} = {CorrelationContextAccessor.CorrelationContext.CorrelationId}");

            var dtStart           = DateTime.Now;
            var isPostcodeInvalid = false;

            if (TempData != null)
            {
                isPostcodeInvalid = (TempData["Location_IsInvalid"] != null && (bool)TempData["Location_IsInvalid"] == true);

                if (!string.IsNullOrWhiteSpace(requestModel.Location))
                {
                    var postcodeResult = PostcodeService.IsValidAsync(requestModel.Location).Result;
                    if (postcodeResult.IsFailure)
                    {
                        isPostcodeInvalid = true;
                        TempData["Location_IsInvalid"] = isPostcodeInvalid;
                        TempData["Location_Postcode"]  = requestModel.Location;

                        if (new UriBuilder(Request.Headers["Referer"]).Path != Request.Path)
                        {
                            return(RedirectToAction(nameof(Index)));
                        }
                    }
                }
                else
                {
                    TempData.Remove("Location_IsInvalid");
                    TempData.Remove("Location_Postcode");
                }
            }

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Starting to create course search criteria.");
            var criteria = CourseDirectory.CreateCourseSearchCriteria(requestModel);

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Finished creating course search criteria.");

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Starting call to course directory search from the course directory service.");
            var result = Service.CourseDirectorySearch(criteria, new PagingOptions(CourseDirectoryHelper.GetSortBy(requestModel.SortBy), requestModel.PageNo));

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Finished calling course directory search from the course directory service.");

            if (!CourseDirectory.IsSuccessfulResult(result, Telemetry, "Course Search", requestModel.SubjectKeyword, dtStart))
            {
                return(View(nameof(Error), new Models.ErrorViewModel()
                {
                    RequestId = "Course Search: " + requestModel.SubjectKeyword.ToString() + ". " + (null != result ? result.Error : string.Empty)
                }));
            }

            //DEBUG_FIX - Add the flush to see if working straightaway
            //ASB TODO Why are we flushing here? We may not end up here due to higher up returns.
            //So that we could test the telemetry, a la the DEBUG_FIX
            Telemetry.Flush();

            int perPage = int.TryParse(Configuration["Tribal:PerPage"], out perPage) ? perPage : 0;

            Telemetry.TrackEvent($"Logging: Ended: Controller = {nameof(CourseDirectoryController)}: Action = {nameof(CourseSearchResult)}: {nameof(Environment.MachineName)} = {Environment.MachineName}: {nameof(CorrelationContextAccessor.CorrelationContext.CorrelationId)} = {CorrelationContextAccessor.CorrelationContext.CorrelationId}");

            return(View(new CourseSearchResultViewModel(result)
            {
                SubjectKeyword = requestModel.SubjectKeyword,
                Location = requestModel.Location,
                LocationHasError = isPostcodeInvalid,
                LocationRadius = (RadiusDistance)requestModel.LocationRadius,
                PerPage = perPage,
                StudyModes = requestModel.StudyModes,
                AttendanceModes = requestModel.AttendanceModes,
                AttendancePatterns = requestModel.AttendancePatterns,
                QualificationLevels = requestModel.QualificationLevels,
                IsDfe1619Funded = requestModel.IsDfe1619Funded,
                SortBy = CourseDirectoryHelper.GetSortBy(requestModel.SortBy),
            }));
        }