Exemplo n.º 1
0
        // This test method is to validate the engine to fix wrong shorthands based on dates.
        public void GetGasQuoteFromString_WithWrongShorthand()
        {
            // Arange
            string wrongShorthandLine1       = "02/01/2009,,01/07/2010,30/09/2010,0.511";      // empty shorthand
            string wrongShorthandLine2       = "02/01/2009,Q,01/07/2010,30/09/2010,0.511";     // wrong shorthand 'Q'
            string wrongShorthandLine3       = "02/01/2009,Q5_11,01/07/2010,30/09/2010,0.511"; // wrong shorthand 'Q5_11'. Q5 is invalid.
            string expected                  = "Q3_10";
            GasQuoteConvertService qcService = new GasQuoteConvertService();

            // Act
            GasQuote gasQuote1 = qcService.GetGasQuoteFromString(wrongShorthandLine1);
            GasQuote gasQuote2 = qcService.GetGasQuoteFromString(wrongShorthandLine2);
            GasQuote gasQuote3 = qcService.GetGasQuoteFromString(wrongShorthandLine3);

            // Assert

            Assert.AreEqual(expected, gasQuote1.Shorthand);
            Assert.AreEqual(expected, gasQuote2.Shorthand);
            Assert.AreEqual(expected, gasQuote3.Shorthand);
        }
        // This function is getting GasQuote type data from CSV line.
        public GasQuote GetGasQuoteFromString(string csvLine)
        {
            string[] values = csvLine.Split(',');
            if (values.Length < 5)
            {
                Console.WriteLine("Invalid Line : {0}", csvLine);
                Console.WriteLine();
                return(null);
            }
            GasQuote item = new GasQuote();

            if (!DateTime.TryParseExact(values[0], dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out item.ObservationDate))
            {
                item.ObservationDate = dtLastProcessed;    // When observationdata is invalid, we will set it as last processed observation date
                Console.WriteLine("Found invalid observation date. So set the observationd date as previous line's observation date");
                Console.WriteLine("Line Content : {0}", csvLine);
                Console.WriteLine("Original Wrong Observation Date : {0}", values[0]);
                Console.WriteLine("New Modified Observation Date : {0}", dtLastProcessed.ToString("dd/MM/yyyy"));
                Console.WriteLine();
            }
            if (!CheckObservationDateValid(item.ObservationDate))
            {
                item.ObservationDate = dtLastProcessed;
                Console.WriteLine("Found invalid observation date. So set the observationd date as previous line's observation date");
                Console.WriteLine("Line Content : {0}", csvLine);
                Console.WriteLine("Original Wrong Observation Date : {0}", values[0]);
                Console.WriteLine("New Modified Observation Date : {0}", dtLastProcessed.ToString("dd/MM/yyyy"));
                Console.WriteLine();
            }
            item.sObservationDate = item.ObservationDate.ToString("dd/MM/yyyy");

            dtLastProcessed = item.ObservationDate;

            item.Shorthand = values[1];
            string[] codes = values[1].Split('_');      // it is needed for getting Year and Index.
            try
            {
                item.Year  = Convert.ToInt32(codes[1]);
                item.Index = Convert.ToInt32(codes[0].Substring(1));
                if (!ValidateShortHandWithFromDate(values[2], item.Year, item.Index))
                {
                    throw new Exception();
                }
            }
            catch
            {
                bool bValid = GetShortHandsFromDate(values[2], out item.Year, out item.Index);
                if (bValid)
                {
                    item.Shorthand = $"Q{item.Index}_{item.Year.ToString("D2")}";
                    Console.WriteLine("Found invalid shorthand. So get the shorthand from dates.");
                    Console.WriteLine("Line Content : {0}", csvLine);
                    Console.WriteLine("Original Wrong Shorthand : {0}", values[1]);
                    Console.WriteLine("New Modified Shorthand : {0}", item.Shorthand);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Found invalid shorthand. Could not fix this wrong shorthand.");
                    Console.WriteLine("Line Content : {0}", csvLine);
                    Console.WriteLine();
                    return(null);
                }
            }

            item.Price = values[4];

            return(item);
        }