public async Task CourseDirectoryImportFunction_ManualImport_Returns_Expected_Result()
        {
            var service = Substitute.For <ICourseDirectoryDataService>();

            service
            .ImportProvidersFromCourseDirectoryApi()
            .Returns((10, 4));
            service
            .ImportQualificationsFromCourseDirectoryApi()
            .Returns((12, 2));

            var functionContext = FunctionObjectsBuilder.BuildFunctionContext();
            var request         = FunctionObjectsBuilder.BuildHttpRequestData(HttpMethod.Get);

            var functions = BuildCourseDirectoryImportFunctions(service);
            var result    = await functions.ManualImport(request, functionContext);

            result.StatusCode.Should().Be(HttpStatusCode.OK);

            var body = await result.Body.ReadAsString();

            body.Should().Be(
                "Inserted or updated 10 and deleted 4 providers.\r\n" +
                "Inserted or updated 12 and deleted 2 qualifications.");
        }
        public async Task CourseDirectoryImportFunction_ManualImport_Exception_Returns_Expected_Result()
        {
            var service = Substitute.For <ICourseDirectoryDataService>();

            service
            .ImportProvidersFromCourseDirectoryApi()
            .ThrowsForAnyArgs(new InvalidOperationException());

            var functionContext = FunctionObjectsBuilder.BuildFunctionContext();
            var request         = FunctionObjectsBuilder.BuildHttpRequestData(HttpMethod.Get);

            var functions = BuildCourseDirectoryImportFunctions(service);
            var result    = await functions.ManualImport(request, functionContext);

            result.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
        }
        public async Task CourseDirectoryImportFunction_GetQualifications_Exception_Returns_Expected_Result()
        {
            var tableStorageService = Substitute.For <ITableStorageService>();

            tableStorageService
            .GetAllQualifications()
            .ThrowsForAnyArgs(new InvalidOperationException());

            var functionContext = FunctionObjectsBuilder.BuildFunctionContext();
            var request         = FunctionObjectsBuilder.BuildHttpRequestData(HttpMethod.Get);

            var functions = BuildCourseDirectoryImportFunctions(tableStorageService: tableStorageService);
            var result    = await functions.GetQualifications(request, functionContext);

            result.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
        }
        public async Task CourseDirectoryImportFunction_GetCourseDirectoryDetailJson_Returns_Expected_Result()
        {
            const string expectedJson = "{ \"detail\": \"result\" } ";

            var service = Substitute.For <ICourseDirectoryDataService>();

            service
            .GetTLevelDetailJsonFromCourseDirectoryApi()
            .Returns(expectedJson);

            var functionContext = FunctionObjectsBuilder.BuildFunctionContext();
            var request         = FunctionObjectsBuilder.BuildHttpRequestData(HttpMethod.Get);

            var functions = BuildCourseDirectoryImportFunctions(service);
            var result    = await functions.GetCourseDirectoryDetailJson(request, functionContext);

            result.Headers.GetValues("Content-Type").Should().NotBeNull();
            result.Headers.GetValues("Content-Type").First().Should().Be("application/json");

            var body = await result.Body.ReadAsString();

            body.Should().Be(expectedJson);
        }
        public async Task CourseDirectoryImportFunction_GetQualifications_Returns_Expected_Result()
        {
            var builder        = new QualificationBuilder();
            var qualifications = builder.BuildList();
            var expectedResult = builder.BuildJson().PrettifyJsonString();

            var tableStorageService = Substitute.For <ITableStorageService>();

            tableStorageService.GetAllQualifications().Returns(qualifications);

            var functionContext = FunctionObjectsBuilder.BuildFunctionContext();
            var request         = FunctionObjectsBuilder.BuildHttpRequestData(HttpMethod.Get);

            var functions = BuildCourseDirectoryImportFunctions(tableStorageService: tableStorageService);
            var result    = await functions.GetQualifications(request, functionContext);

            result.Headers.GetValues("Content-Type").Should().NotBeNull();
            result.Headers.GetValues("Content-Type").First().Should().Be("application/json");

            var json = await result.Body.ReadAsString();

            json.PrettifyJsonString().Should().Be(expectedResult);
        }