public async Task SaveTestProviderResults_WhenItemsPasssedAndRepositoryFailed_ThenItemsNotSaved() { // Arrange ITestResultsRepository testResultsRepository = CreateTestResultsRepository(); testResultsRepository .SaveTestProviderResults(Arg.Any <IEnumerable <TestScenarioResult> >()) .Returns(HttpStatusCode.BadRequest); TestResultsService service = CreateTestResultsService(testResultsRepository); IEnumerable <ProviderResult> providerResults = Enumerable.Empty <ProviderResult>(); List <TestScenarioResult> itemsToUpdate = new List <TestScenarioResult> { CreateTestScenarioResult() }; // Act HttpStatusCode result = await service.SaveTestProviderResults(itemsToUpdate, providerResults); // Assert result.Should().Be(HttpStatusCode.InternalServerError); await testResultsRepository .Received(1) .SaveTestProviderResults(itemsToUpdate); }
public async Task SaveTestProviderResults_WhenItemsPasssed_ThenItemsSaved() { // Arrange ITestResultsRepository testResultsRepository = CreateTestResultsRepository(); ISearchRepository <TestScenarioResultIndex> searchRepository = CreateSearchRespository(); TestResultsService service = CreateTestResultsService(testResultsRepository, searchRepository); List <TestScenarioResult> itemsToUpdate = new List <TestScenarioResult> { CreateTestScenarioResult() }; testResultsRepository .SaveTestProviderResults(Arg.Any <IEnumerable <TestScenarioResult> >()) .Returns(HttpStatusCode.Created); IEnumerable <ProviderResult> providerResults = Enumerable.Empty <ProviderResult>(); // Act HttpStatusCode result = await service.SaveTestProviderResults(itemsToUpdate, providerResults); // Assert result.Should().Be(HttpStatusCode.Created); await testResultsRepository .Received(1) .SaveTestProviderResults(itemsToUpdate); await searchRepository .Received(1) .Index(Arg.Any <IEnumerable <TestScenarioResultIndex> >()); }
public async Task SaveTestProviderResults_WhenItemsPasssed_ThenTelemetryLogged() { // Arrange ITestResultsRepository testResultsRepository = CreateTestResultsRepository(); ISearchRepository <TestScenarioResultIndex> searchRepository = CreateSearchRespository(); ITelemetry telemetry = CreateTelemetry(); ITestResultsService service = CreateTestResultsService(testResultsRepository, searchRepository, telemetry: telemetry); IEnumerable <ProviderResult> providerResults = Enumerable.Empty <ProviderResult>(); List <TestScenarioResult> itemsToUpdate = new List <TestScenarioResult>(); TestScenarioResult testScenarioResult = CreateTestScenarioResult(); itemsToUpdate.Add(testScenarioResult); testResultsRepository .SaveTestProviderResults(Arg.Any <IEnumerable <TestScenarioResult> >()) .Returns(HttpStatusCode.Created); // Act HttpStatusCode result = await service.SaveTestProviderResults(itemsToUpdate, providerResults); // Assert result.Should().Be(HttpStatusCode.Created); telemetry .Received(1) .TrackEvent( Arg.Is("UpdateTestScenario"), Arg.Is <IDictionary <string, string> >(p => p.ContainsKey("SpecificationId") && p["SpecificationId"] == testScenarioResult.Specification.Id ), Arg.Is <IDictionary <string, double> >( m => m.ContainsKey("update-testscenario-elapsedMilliseconds") && m.ContainsKey("update-testscenario-recordsUpdated") && m["update-testscenario-elapsedMilliseconds"] > 0 && m["update-testscenario-recordsUpdated"] == 1 ) ); }
public async Task <HttpStatusCode> SaveTestProviderResults(IEnumerable <TestScenarioResult> testResults, IEnumerable <ProviderResult> providerResults) { Guard.ArgumentNotNull(testResults, nameof(testResults)); if (!testResults.Any()) { return(HttpStatusCode.NotModified); } Stopwatch stopwatch = Stopwatch.StartNew(); Task <HttpStatusCode> repoUpdateTask = _testResultsPolicy.ExecuteAsync(() => _testResultsRepository.SaveTestProviderResults(testResults)); IEnumerable <TestScenarioResultIndex> searchIndexItems = _mapper.Map <IEnumerable <TestScenarioResultIndex> >(testResults); foreach (TestScenarioResultIndex testScenarioResult in searchIndexItems) { ProviderResult providerResult = providerResults.FirstOrDefault(m => m.Provider.Id == testScenarioResult.ProviderId); if (providerResult != null) { testScenarioResult.EstablishmentNumber = providerResult.Provider.EstablishmentNumber; testScenarioResult.UKPRN = providerResult.Provider.UKPRN; testScenarioResult.UPIN = providerResult.Provider.UPIN; testScenarioResult.URN = providerResult.Provider.URN; testScenarioResult.LocalAuthority = providerResult.Provider.Authority; testScenarioResult.ProviderType = providerResult.Provider.ProviderType; testScenarioResult.ProviderSubType = providerResult.Provider.ProviderSubType; testScenarioResult.OpenDate = providerResult.Provider.DateOpened; } } Task <IEnumerable <IndexError> > searchUpdateTask = _testResultsSearchPolicy.ExecuteAsync(() => _searchRepository.Index(searchIndexItems)); await TaskHelper.WhenAllAndThrow(searchUpdateTask, repoUpdateTask); IEnumerable <IndexError> indexErrors = searchUpdateTask.Result; HttpStatusCode repositoryUpdateStatusCode = repoUpdateTask.Result; stopwatch.Stop(); if (!indexErrors.Any() && (repoUpdateTask.Result == HttpStatusCode.Created || repoUpdateTask.Result == HttpStatusCode.NotModified)) { _telemetry.TrackEvent("UpdateTestScenario", new Dictionary <string, string>() { { "SpecificationId", testResults.First().Specification.Id } }, new Dictionary <string, double>() { { "update-testscenario-elapsedMilliseconds", stopwatch.ElapsedMilliseconds }, { "update-testscenario-recordsUpdated", testResults.Count() }, } ); return(HttpStatusCode.Created); } foreach (IndexError indexError in indexErrors) { _logger.Error($"SaveTestProviderResults index error {{key}}: {indexError.ErrorMessage}", indexError.Key); } if (repositoryUpdateStatusCode == default(HttpStatusCode)) { _logger.Error("SaveTestProviderResults repository failed with no response code"); } else { _logger.Error("SaveTestProviderResults repository failed with response code: {repositoryUpdateStatusCode}", repositoryUpdateStatusCode); } return(HttpStatusCode.InternalServerError); }
public async Task SaveTestProviderResults_WhenItemsPasssed_ThenSearchResultsMapped() { // Arrange ITestResultsRepository testResultsRepository = CreateTestResultsRepository(); ISearchRepository <TestScenarioResultIndex> searchRepository = CreateSearchRespository(); TestResultsService service = CreateTestResultsService(testResultsRepository, searchRepository); IEnumerable <ProviderResult> providerResults = new[] { new ProviderResult { Provider = new ProviderSummary { UKPRN = "111", UPIN = "222", URN = "333", EstablishmentNumber = "123", DateOpened = DateTimeOffset.Now, Authority = "authority", ProviderSubType = "provider sub type", ProviderType = "provider type", Id = "ProviderId" } } }; List <TestScenarioResult> itemsToUpdate = new List <TestScenarioResult>(); TestScenarioResult testScenarioResult = CreateTestScenarioResult(); itemsToUpdate.Add(testScenarioResult); testResultsRepository .SaveTestProviderResults(Arg.Any <IEnumerable <TestScenarioResult> >()) .Returns(HttpStatusCode.Created); // Act HttpStatusCode result = await service.SaveTestProviderResults(itemsToUpdate, providerResults); // Assert result.Should().Be(HttpStatusCode.Created); await testResultsRepository .Received(1) .SaveTestProviderResults(itemsToUpdate); await searchRepository .Received(1) .Index(Arg.Is <IEnumerable <TestScenarioResultIndex> >(c => c.First().Id == testScenarioResult.Id && c.First().ProviderId == testScenarioResult.Provider.Id && c.First().ProviderName == testScenarioResult.Provider.Name && c.First().SpecificationId == testScenarioResult.Specification.Id && c.First().SpecificationName == testScenarioResult.Specification.Name && c.First().TestResult == Enum.GetName(typeof(Models.Results.TestResult), testScenarioResult.TestResult) && c.First().TestScenarioId == testScenarioResult.TestScenario.Id && c.First().TestScenarioName == testScenarioResult.TestScenario.Name && c.First().LastUpdatedDate > DateTimeOffset.Now.AddDays(-1) && c.First().EstablishmentNumber == "123" && c.First().UKPRN == "111" && c.First().UPIN == "222" && c.First().URN == "333" && c.First().LocalAuthority == "authority" && c.First().ProviderType == "provider type" && c.First().ProviderSubType == "provider sub type" && c.First().OpenDate.HasValue )); }