public async Task CreateCertificatesExample() { const string filePath = @"CsvFiles\createCertificates.csv"; IEnumerable <CreateCertificateRequest> certificatesToCreate; using (TextReader textReader = File.OpenText(filePath)) { using (CsvReader csv = new CsvReader(textReader)) { csv.Configuration.HeaderValidated = null; csv.Configuration.MissingFieldFound = null; certificatesToCreate = csv.GetRecords <CreateCertificateRequest>().ToList(); } } // NOTE: The External API performs validation, however it is a good idea to check beforehand. bool invalidDataSupplied = certificatesToCreate.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.CreateCertificates(certificatesToCreate)).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()} "); } }
public async Task CreateCertificate() { // arrange var certificateData = Builder <CertificateData> .CreateNew().With(cd => cd.Learner = Builder <Learner> .CreateNew().Build()) .With(cd => cd.Standard = Builder <Standard> .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, CreatedBy = "Test" }; var certificate = new Certificate { CertificateData = certificateData, Status = status, Created = created }; var certificateRequest = new CreateCertificateRequest { Learner = certificate.CertificateData.Learner, Standard = certificate.CertificateData.Standard, LearningDetails = certificate.CertificateData.LearningDetails, PostalContact = certificate.CertificateData.PostalContact }; var expectedResponse = new List <CreateCertificateResponse> { new CreateCertificateResponse { Certificate = certificate } }; _MockHttp.When(HttpMethod.Post, $"{apiBaseAddress}/api/v1/certificate") .Respond(HttpStatusCode.OK, "application/json", JsonConvert.SerializeObject(expectedResponse)); // act var actual = await _ApiClient.CreateCertificates(new List <CreateCertificateRequest> { 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 CreateCertificatesExample() { long uln = 1234567890; string firstName = "Fred"; string lastName = "Blogs"; int standardCode = 1; string standardReference = "ST0127"; string overallGrade = "PASS"; string contactName = "Shreya Smith"; string organisation = "Contoso Ltd"; string address = "123 Test Road"; string city = "Townsville"; string postcode = "ZY9 9ZY"; CreateCertificateRequest newCertificate = new CreateCertificateRequest { Learner = new Learner { Uln = uln, GivenNames = firstName, FamilyName = lastName }, Standard = new Standard { StandardCode = standardCode, StandardReference = standardReference }, LearningDetails = new LearningDetails { OverallGrade = overallGrade, AchievementDate = DateTime.UtcNow }, PostalContact = new PostalContact { ContactName = contactName, Organisation = organisation, AddressLine1 = address, City = city, PostCode = postcode } }; if (newCertificate.IsValid(out _)) { // NOTE: The External API performs validation, however it is a good idea to check beforehand await _CertificateApiClient.CreateCertificates(new List <CreateCertificateRequest> { newCertificate }); } }