public void SetUp() { this.configService = new Mock <IConfigurationService>(); this.configService.Setup(x => x.Get(ConfigurationKey.CSVIncidentDateFormatRegex, @"[0-9]{4}-[0-9]{2}")).Returns(@"[0-9]{4}-[0-9]{2}"); this.configService.Setup(x => x.Get(ConfigurationKey.CultureInfo, "en-GB")).Returns("en-GB"); this.configService.Setup(x => x.Get(ConfigurationKey.CSVIncidentColumnNumber, "12")).Returns("12"); this.fileIOService = new Mock <IFileIOService>(); this.loggerService = new Mock <ILogger>(); this.strategy = new IncidentCSVParseStrategy(this.configService.Object, this.loggerService.Object); }
// <inheritdoc> public ICollection <T> parseCSV <T>( String fileLocation, CSVParseType parseType, ICSVParseStrategy parseStrategy, bool hasHeader = false) where T : EntityBase { if (String.IsNullOrEmpty(fileLocation)) { this.logger.warn("File Location was null or empty for parsing CSV"); return(null); } //var parseStrategy = getStrategy(parseType); var returnSet = new HashSet <T>(); int numberOfHeadersExpected = Int32.Parse(this.configService.Get(ConfigurationKey.CSVIncidentColumnNumber, "12")); using (Stream underlyingStream = this.fileIOService.openStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.Read)) using (TextReader reader = new StreamReader(underlyingStream)) using (this.reader = new CsvReader(reader, hasHeader)) { var headers = this.reader.GetFieldHeaders(); if (numberOfHeadersExpected != headers.Length) { this.logger.warn(String.Format("Expected {0} headers but was: {1}", numberOfHeadersExpected, headers.Length)); return(new HashSet <T>()); } int errors = 0; while (this.reader.ReadNextRecord()) { string[] row = new string[headers.Length]; for (int i = 0; i < headers.Length; i++) { row[i] = this.reader[i]; } var result = (T)parseStrategy.parse(row); if (result != null) { returnSet.Add(result); } else { errors++; } } this.logger.warn(String.Format("{0} invalid rows", errors)); } return(returnSet); }
/// <summary> /// Sets the default grading. /// </summary> /// <param name="incidentGradingService">Incident grading service.</param> /// <param name="csvParseStrategy">Csv parse strategy.</param> private static void setDefaultGrading(IIncidentGradingService incidentGradingService, ICSVParseStrategy csvParseStrategy) { var defaultGrading = incidentGradingService.GetImportIncidentGrading(); if (defaultGrading == null) { defaultGrading = new IncidentGrading() { GradeValue = null, Description = "Imported" }; } csvParseStrategy.setDefaultValue(defaultGrading); }