示例#1
0
        public void can_import_data()
        {
            FileStream fileStream = new FileStream(Path.GetFullPath(@"Service\FileTest\Test.xml"), FileMode.Open);
            FileStream fileStream_AmountFailed     = new FileStream(Path.GetFullPath(@"Service\FileTest\TestAmountFailed.xml"), FileMode.Open);
            FileStream fileStream_CurrencyFailed   = new FileStream(Path.GetFullPath(@"Service\FileTest\TestCurrencyFailed.xml"), FileMode.Open);
            FileStream fileStream_DateFailed       = new FileStream(Path.GetFullPath(@"Service\FileTest\TestDateFailed.xml"), FileMode.Open);
            FileStream fileStream_StatusFailed     = new FileStream(Path.GetFullPath(@"Service\FileTest\TestStatusFailed.xml"), FileMode.Open);
            Mock <HttpPostedFileBase> uploadedFile = new Mock <HttpPostedFileBase>();
            XMLController             csvTest      = new XMLController();

            // Test Pass
            uploadedFile.Setup(f => f.InputStream).Returns(fileStream);
            NUnit.Framework.Assert.That(csvTest.TransactionExtract(uploadedFile.Object), Is.TypeOf <List <Transaction> >());
            fileStream.Close();

            // Test Amount failed
            uploadedFile.Setup(f => f.InputStream).Returns(fileStream_AmountFailed);
            NUnit.Framework.Assert.That(() => csvTest.TransactionExtract(uploadedFile.Object), Throws.Exception);
            fileStream_AmountFailed.Close();

            // Test Currency failed
            uploadedFile.Setup(f => f.InputStream).Returns(fileStream_CurrencyFailed);
            NUnit.Framework.Assert.That(() => csvTest.TransactionExtract(uploadedFile.Object), Throws.Exception);
            fileStream_CurrencyFailed.Close();

            // Test Date failed
            uploadedFile.Setup(f => f.InputStream).Returns(fileStream_DateFailed);
            NUnit.Framework.Assert.That(() => csvTest.TransactionExtract(uploadedFile.Object), Throws.Exception);
            fileStream_DateFailed.Close();

            // Test Status failed
            uploadedFile.Setup(f => f.InputStream).Returns(fileStream_StatusFailed);
            NUnit.Framework.Assert.That(() => csvTest.TransactionExtract(uploadedFile.Object), Throws.Exception);
            fileStream_StatusFailed.Close();
        }
        public ActionResult Index(HttpPostedFileBase postedFile)
        {
            CSVController CSV            = new CSVController();
            XMLController XML            = new XMLController();
            int           expectFilesize = 1 * 1024 * 1024;

            if (postedFile != null && postedFile.ContentLength <= expectFilesize)
            {
                try
                {
                    string fileExtension = Path.GetExtension(postedFile.FileName);

                    if (fileExtension == ".csv")
                    {
                        var Transactions = CSV.TransactionExtract(postedFile);
                        InsertTransaction(Transactions);
                    }
                    else if (fileExtension == ".xml")
                    {
                        var Transactions = XML.TransactionExtract(postedFile);
                        InsertTransaction(Transactions);
                    }

                    //Validate uploaded file and return error.
                    else
                    {
                        ViewBag.ErrorMessage = "Please select the csv or xml file";
                        Helper.WriteWarning(null, ViewBag.ErrorMessage);
                        return(View());
                    }
                    ViewBag.OkMessage = "Import File Complete!";
                }
                catch (Exception ex)
                {
                    Helper.WriteError(ex, "Error");
                    ViewBag.ErrorMessage = ex.Message;
                    return(View());
                }
            }
            else
            {
                ViewBag.ErrorMessage = "Please select the file first to upload.";
                Helper.WriteWarning(null, ViewBag.ErrorMessage);
            }

            return(View());
        }