public async Task UpdateCertificate() { // arrange var certificateData = Builder <CertificateData> .CreateNew().With(cd => cd.CertificateReference = "DRAFT CERTIFICATE") .With(cd => cd.Standard = Builder <Standard> .CreateNew().Build()) .With(cd => cd.Learner = Builder <Learner> .CreateNew().Build()) .With(cd => cd.LearningDetails = Builder <LearningDetails> .CreateNew().Build()) .With(cd => cd.PostalContact = Builder <PostalContact> .CreateNew().Build()) .Build(); var status = new Status { CurrentStatus = "Draft" }; var created = new Created { CreatedAt = DateTime.UtcNow.AddHours(-1), CreatedBy = "Test" }; var certificate = new Certificate { CertificateData = certificateData, Status = status, Created = created }; var certificateRequest = new UpdateCertificateRequest { CertificateReference = certificate.CertificateData.CertificateReference, Learner = certificate.CertificateData.Learner, Standard = certificate.CertificateData.Standard, LearningDetails = certificate.CertificateData.LearningDetails, PostalContact = certificate.CertificateData.PostalContact }; var expectedResponse = new List <UpdateCertificateResponse> { new UpdateCertificateResponse { Certificate = certificate } }; _MockHttp.When(HttpMethod.Put, $"{apiBaseAddress}/api/v1/certificate") .Respond(HttpStatusCode.OK, "application/json", JsonConvert.SerializeObject(expectedResponse)); // act var actual = await _ApiClient.UpdateCertificates(new List <UpdateCertificateRequest> { certificateRequest }); // assert Assert.That(actual, Has.Count.EqualTo(1)); Assert.That(actual.First().ValidationErrors, Has.Count.EqualTo(0)); Assert.That(actual.First().Certificate, Is.EqualTo(expectedResponse.First().Certificate)); }
public async Task UpdateCertificatesExample() { // NOTE: You will need to know what the certificate currently looks like Certificate currentCertificate = new Certificate { Status = new Status { CurrentStatus = "Draft" }, Created = new Created { CreatedAt = DateTime.UtcNow, CreatedBy = "Example" }, CertificateData = new CertificateData { CertificateReference = "00012001", Learner = new Learner { Uln = 1234567890, GivenNames = "Fred", FamilyName = "Bloggs" }, Standard = new Standard { StandardCode = 1, StandardReference = "ST0127", Level = 1, StandardName = "Example Standard", }, LearningDetails = new LearningDetails { LearningStartDate = DateTime.UtcNow.AddYears(-1), ProviderName = "Test Provider", ProviderUkPrn = 123456, OverallGrade = "Pass", AchievementDate = DateTime.UtcNow }, PostalContact = new PostalContact { ContactName = "Shreya Smith", Organisation = "Contoso Ltd", AddressLine1 = "123 Test Road", City = "Townsville", PostCode = "ZY99ZZ" } } }; // Let's pretend the apprentice got a better grade UpdateCertificateRequest updatedCertificate = new UpdateCertificateRequest { CertificateReference = currentCertificate.CertificateData.CertificateReference, Learner = currentCertificate.CertificateData.Learner, Standard = currentCertificate.CertificateData.Standard, LearningDetails = currentCertificate.CertificateData.LearningDetails, PostalContact = currentCertificate.CertificateData.PostalContact, }; updatedCertificate.LearningDetails.OverallGrade = "Merit"; if (updatedCertificate.IsValid(out _)) { // NOTE: The External API performs validation, however it is a good idea to check beforehand await _CertificateApiClient.UpdateCertificates(new List <UpdateCertificateRequest> { updatedCertificate }); } }
public async Task UpdateCertificatesExample() { const string filePath = @"CsvFiles\updateCertificates.csv"; IEnumerable <UpdateCertificateRequest> certificates; using (TextReader textReader = File.OpenText(filePath)) { using (CsvReader csv = new CsvReader(textReader)) { csv.Configuration.HeaderValidated = null; csv.Configuration.MissingFieldFound = null; certificates = csv.GetRecords <UpdateCertificateRequest>().ToList(); } } // Let's pretend the first and last apprentices got better grades certificates.First().LearningDetails.AchievementOutcome = "MERIT"; certificates.Last().LearningDetails.AchievementOutcome = "PASS"; // NOTE: The External API performs validation, however it is a good idea to check beforehand. bool invalidDataSupplied = certificates.Any(c => !c.IsValid(out _)); if (invalidDataSupplied) { throw new InvalidOperationException("The supplied CSV file contains invalid data. Please correct and then try again."); } else { var response = (await _CertificateApiClient.UpdateCertificates(certificates)).ToList(); // NOTE: You may want to deal with good & bad records separately var goodCertificates = response.Where(c => c.Certificate != null && !c.ValidationErrors.Any()); var badCertificates = response.Except(goodCertificates); Console.WriteLine($"Good Certificates: {goodCertificates.Count()}, Bad Certificates: {badCertificates.Count()} "); } }