Example #1
0
        public void Should_Return_Null_For_Invalid_Urls_Or_Without_Csv_Extension()
        {
            ICsvParserService        csvParserService = Substitute.For <ICsvParserService>();
            ILogger <CsvParserLogic> logger           = Substitute.For <ILogger <CsvParserLogic> >();

            ICsvParserLogic      csvParserLogic = new CsvParserLogic(csvParserService, logger);
            IEnumerable <string> invalidUrls;

            using (Stream stream = new MemoryStream())
            {
                csvParserService.ParseCsvAsync(Arg.Any <string>(), Arg.Any <OutputFormats>()).Returns(Task.FromResult(stream));

                invalidUrls = GetInvalidUrls();

                foreach (string url in invalidUrls)
                {
                    Stream result = csvParserLogic.ParseCsvAsync(url, OutputFormats.json).Result;
                    Assert.Null(result);
                }

                IEnumerable <string> validUrls = GetValidUrls();

                foreach (string url in validUrls)
                {
                    Stream result = csvParserLogic.ParseCsvAsync(url, OutputFormats.json).Result;
                    Assert.NotNull(result);
                }
            }
        }
Example #2
0
        public async Task <Stream> ParseCsvAsync(string csvUri, OutputFormats ouputFormat)
        {
            try
            {
                if (!Uri.IsWellFormedUriString(csvUri, UriKind.Absolute))
                {
                    Console.WriteLine("The Uri is not Well Formatted");

                    return(null);
                }

                bool isUriCreated = Uri.TryCreate(csvUri, UriKind.Absolute, out Uri uri);

                if (isUriCreated == false || uri == null)
                {
                    Console.WriteLine("Invalid Uri");

                    return(null);
                }


                if (Path.GetExtension(csvUri) != ".csv")
                {
                    Console.WriteLine("Invalid Csv file");

                    return(null);
                }

                return(await CsvParserService.ParseCsvAsync(csvUri, ouputFormat));
            }

            catch (Exception e)
            {
                Logger.LogError($"Error: {e.Message}");

                return(null);
            }
        }