public void UploadFile_CsvFileAndProjectId_CallServiceToSaveFileCorrectly() { //Arrange var projectId = new Guid("4d75753e-618d-4067-9019-f91a64407bca"); var coupaHeaderDTO = new CoupaImporterJobDefinitionDTO { Id = new Guid("93281c6c-8e96-429c-bb8a-d838b6abe621"), FileName = "Test" }; _controller.ControllerContext = new ControllerContext(ContextWithValidRequest()); _service.CreateInvoiceImportJobDefinition(projectId, Arg.Any <MemoryStream>(), Arg.Any <string>()) .Returns(coupaHeaderDTO); _service.CreatePurchaseOrderImportJobDefinition(projectId, Arg.Any <MemoryStream>(), Arg.Any <string>()) .Returns(coupaHeaderDTO); _service.CreateRequisitionImportJobDefinition(projectId, Arg.Any <MemoryStream>(), Arg.Any <string>()) .Returns(coupaHeaderDTO); //Act var invoiceResult = _controller.UploadInvoice(projectId).Result as OkObjectResult; var purchaseOrderResult = _controller.UploadPurchaseOrder(projectId).Result as OkObjectResult; var requisitionResult = _controller.UploadRequisition(projectId).Result as OkObjectResult; //Assert Assert.IsNotNull(invoiceResult); Assert.AreEqual(200, invoiceResult.StatusCode); Assert.IsNotNull(purchaseOrderResult); Assert.AreEqual(200, purchaseOrderResult.StatusCode); Assert.IsNotNull(requisitionResult); Assert.AreEqual(200, requisitionResult.StatusCode); _unitOfWork.Received(3).SaveChangesAsync(); }
public void CreateInvoiceImportJobDefinition_StreamAndFileName_ReturnValidCoupaImporterJobDefinitionDTO() { //Arrange var projectId = new Guid("4d75753e-618d-4067-9019-f91a64407bca"); var jobDefinition = new CoupaImporterJobDefinition { Id = new Guid("45744a2e-3dc1-472f-9bb5-378a0a9eeda4"), FileName = "fileTest.csv", Status = (int)CoupaImporterStatus.Pending, TimeStamp = DateTime.UtcNow, ProjectId = projectId }; _coupaImporterRepository.Add(Arg.Is <CoupaImporterJobDefinition>(c => c.FileName == jobDefinition.FileName && c.ProjectId == projectId)) .Returns(jobDefinition); //Act var result = _service.CreateInvoiceImportJobDefinition(projectId, ValidCsv(), "fileTest.csv"); //Assert Assert.AreEqual(jobDefinition.Id, result.Id); }
public async Task <IActionResult> UploadInvoice(Guid projectId) { if (Request.Form.Files.Count == 0) { return(new UnsupportedMediaTypeResult()); } var file = Request.Form.Files[0]; if (!file.ContentType.Contains("text/csv") && !file.ContentType.Contains("ms-excel")) { return(new UnsupportedMediaTypeResult()); } if (file.Length == 0) { return(new UnsupportedMediaTypeResult()); } CoupaImporterJobDefinitionDTO jobDefinitionDTO; using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); jobDefinitionDTO = _service.CreateInvoiceImportJobDefinition(projectId, memoryStream, file.FileName); if (jobDefinitionDTO == null) { return(BadRequest( "An error occurred while uploading the file, please try again or contact the system administrator.")); } } await _unitOfWork.SaveChangesAsync(); // Enqueue background job to import all data in Invoice table _backgroundJobClient.Enqueue(() => _service.ProcessInvoiceImportJob(jobDefinitionDTO.Id)); return(Ok(jobDefinitionDTO)); }