public void TestHandleUploadResponseStream() { var transactionLog = new TransactionLogType(); var transactionLogXml = GetXml(transactionLog); var stream = new MemoryStream(); var writer = new StreamWriter(stream, Encoding.Unicode); writer.Write(transactionLogXml); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); var user = new User(1); var dto = new SevisBatchProcessingDTO(); dto.BatchId = "batchId"; Action <User, string, string, IDS2019FileProvider> callback = (u, batchId, xml, fileProvider) => { Assert.IsTrue(Object.ReferenceEquals(u, user)); Assert.IsNotNull(fileProvider); Assert.IsInstanceOfType(fileProvider, typeof(NullDS2019FileProvider)); Assert.AreEqual(transactionLogXml, xml); Assert.AreEqual(dto.BatchId, batchId); }; service.Setup(x => x.ProcessTransactionLog(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IDS2019FileProvider>())) .Callback(callback); var handler = new ZipArchiveSevisApiResponseHandler(service.Object); handler.HandleUploadResponseStream(user, dto, stream); }
/// <summary> /// /// </summary> /// <param name="user"></param> /// <param name="dto"></param> /// <param name="stream"></param> /// <returns></returns> public Task HandleDownloadResponseStreamAsync(User user, SevisBatchProcessingDTO dto, Stream stream) { Contract.Requires(user != null, "The user must not be null."); Contract.Requires(dto != null, "The dto must not be null."); Contract.Requires(stream != null, "The stream must not be null."); return(Task.FromResult <Object>(null)); }
/// <summary> /// Handles a sevis api response which provides a zip archive as the stream. /// </summary> /// <param name="user">The user performing the operations.</param> /// <param name="dto">The sevis batch processing instance representing the stream response.</param> /// <param name="stream">The stream representing the api response.</param> public void HandleDownloadResponseStream(User user, SevisBatchProcessingDTO dto, Stream stream) { using (var sevisBatchFileHandler = new SevisBatchZipArchiveHandler(this.zipArchiveDelegate(stream))) { var transactionLogXml = sevisBatchFileHandler.GetTransactionLogXml(); service.ProcessTransactionLog(user, dto.BatchId, transactionLogXml, sevisBatchFileHandler); } }
/// <summary> /// Handles a sevis api upload response. /// </summary> /// <param name="user">The user performing the processing.</param> /// <param name="dto">The dto representing the uploaded batch.</param> /// <param name="stream">The response stream.</param> public void HandleUploadResponseStream(User user, SevisBatchProcessingDTO dto, Stream stream) { using (var streamReader = new StreamReader(stream)) { var xml = streamReader.ReadToEnd(); this.service.ProcessTransactionLog(user, dto.BatchId, xml, new NullDS2019FileProvider()); } }
public async Task TestHandleDownloadResponseStreamAsync() { using (ShimsContext.Create()) { var transactionLog = new TransactionLogType(); var transactionLogXml = GetXml(transactionLog); var zipFileName = String.Format("{0}", SevisBatchZipArchiveHandler.TRANSACTION_LOG_FILE_NAME); var entry = new System.IO.Compression.Fakes.ShimZipArchiveEntry { NameGet = () => zipFileName, Open = () => { var stream = new MemoryStream(); var writer = new StreamWriter(stream, Encoding.Unicode); writer.Write(transactionLogXml); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); return(stream); } }; var entries = new List <ZipArchiveEntry>(); entries.Add(entry); var readonlyEntries = new ReadOnlyCollection <ZipArchiveEntry>(entries); var zipArchive = new System.IO.Compression.Fakes.ShimZipArchive { EntriesGet = () => readonlyEntries, }; var memoryStream = new MemoryStream(); Func <Stream, ZipArchive> zipArchiveDelegate = (s) => { Assert.IsTrue(Object.ReferenceEquals(memoryStream, s)); return(zipArchive); }; var user = new User(1); var dto = new SevisBatchProcessingDTO(); dto.BatchId = "batchId"; var handler = new ZipArchiveSevisApiResponseHandler(service.Object, zipArchiveDelegate); Action <User, string, string, IDS2019FileProvider> callback = (u, batchId, xml, fileProvider) => { Assert.IsTrue(Object.ReferenceEquals(u, user)); Assert.IsNotNull(fileProvider); Assert.IsInstanceOfType(fileProvider, typeof(SevisBatchZipArchiveHandler)); Assert.AreEqual(transactionLogXml, xml); Assert.AreEqual(dto.BatchId, batchId); }; service.Setup(x => x.ProcessTransactionLogAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IDS2019FileProvider>())) .Returns(Task.FromResult <object>(null)) .Callback(callback); await handler.HandleDownloadResponseStreamAsync(user, dto, memoryStream); } }
/// <summary> /// Handles a sevis api upload response. /// </summary> /// <param name="user">The user performing the processing.</param> /// <param name="dto">The dto representing the uploaded batch.</param> /// <param name="stream">The response stream.</param> public async Task HandleUploadResponseStreamAsync(User user, SevisBatchProcessingDTO dto, Stream stream) { using (var streamReader = new StreamReader(stream)) { var xml = await streamReader.ReadToEndAsync(); await this.service.ProcessTransactionLogAsync(user, dto.BatchId, xml, new NullDS2019FileProvider()); } }
/// <summary> /// Handles a sevis api response which provides a zip archive as the stream. /// </summary> /// <param name="user">The user performing the operations.</param> /// <param name="dto">The sevis batch processing instance representing the stream response.</param> /// <param name="stream">The stream representing the api response.</param> public async Task HandleDownloadResponseStreamAsync(User user, SevisBatchProcessingDTO dto, Stream stream) { using (var sevisBatchFileHandler = new SevisBatchZipArchiveHandler(this.zipArchiveDelegate(stream))) { var transactionLogXml = await sevisBatchFileHandler.GetTransactionLogXmlAsync(); await service.ProcessTransactionLogAsync(user, dto.BatchId, transactionLogXml, sevisBatchFileHandler); } }
public void TestGetProcessDispositionCodeAsCode_ProcessCodeIsNull() { var dto = new SevisBatchProcessingDTO { ProcessDispositionCode = null }; Assert.IsNull(dto.GetProcessDispositionCodeAsCode()); }
public void TestGetProcessDispositionCodeAsCode() { var code = DispositionCode.Success; var dto = new SevisBatchProcessingDTO { ProcessDispositionCode = code.Code }; Assert.AreEqual(code, dto.GetProcessDispositionCodeAsCode()); }
public async Task TestUploadBatchesAsync_HasBatchToUpload() { using (ShimsContext.Create()) { var userId = 1; appSettings.Add(AppSettings.SYSTEM_USER_ID_KEY, userId.ToString()); var dto = new SevisBatchProcessingDTO { BatchId = "batchId", SendString = "<root></root>", SevisOrgId = "sevis org Id", SevisUsername = "******" }; service.Setup(x => x.GetNextBatchToUploadAsync()).ReturnsAsync(dto); var responseContent = "hello world"; var message = new HttpResponseMessage { Content = new StringContent(responseContent), StatusCode = System.Net.HttpStatusCode.OK }; var shimComm = new ECA.Net.Fakes.ShimSevisComm { UploadAsyncXElementStringStringString = (xElement, batchId, sOrgId, sUsername) => { Assert.AreEqual(dto.BatchId, batchId); Assert.AreEqual(dto.SevisOrgId, sOrgId); Assert.AreEqual(dto.SevisUsername, sUsername); service.Setup(x => x.GetNextBatchToUploadAsync()).ReturnsAsync(null); return(Task.FromResult <HttpResponseMessage>(message)); } }; Action <User, SevisBatchProcessingDTO, Stream> callback = (u, d, s) => { Assert.AreEqual(userId, u.Id); Assert.IsTrue(Object.ReferenceEquals(dto, d)); Assert.IsNotNull(s); using (var streamReader = new StreamReader(s)) { var stringContent = streamReader.ReadToEnd(); Assert.AreEqual(responseContent, stringContent); } }; responseHandler.Setup(x => x.HandleUploadResponseStreamAsync(It.IsAny <User>(), It.IsAny <SevisBatchProcessingDTO>(), It.IsAny <Stream>())) .Returns(Task.FromResult <Object>(null)) .Callback(callback); await instance.UploadBatchesAsync(shimComm); responseHandler.Verify(x => x.HandleUploadResponseStreamAsync(It.IsAny <User>(), It.IsAny <SevisBatchProcessingDTO>(), It.IsAny <Stream>()), Times.Once()); } }
public async Task TestProcessAsync() { using (ShimsContext.Create()) { var userId = 1; appSettings.Add(AppSettings.SYSTEM_USER_ID_KEY, userId.ToString()); var uploadCalled = false; var downloadCalled = false; var dto = new SevisBatchProcessingDTO { BatchId = "batchId", SendString = "<root></root>", SevisOrgId = "sevis org Id", SevisUsername = "******" }; var responseContent = "hello world"; var message = new HttpResponseMessage { Content = new StringContent(responseContent), StatusCode = System.Net.HttpStatusCode.OK }; var shimComm = new ECA.Net.Fakes.ShimSevisComm { UploadAsyncXElementStringStringString = (xElement, batchId, sOrgId, sUsername) => { uploadCalled = true; service.Setup(x => x.GetNextBatchToUploadAsync()).ReturnsAsync(null); return(Task.FromResult <HttpResponseMessage>(message)); }, DownloadAsyncStringStringString = (batchId, sOrgId, sUsername) => { downloadCalled = true; service.Setup(x => x.GetNextBatchToDownloadAsync()).ReturnsAsync(null); return(Task.FromResult <HttpResponseMessage>(message)); } }; service.Setup(x => x.GetNextBatchToDownloadAsync()).ReturnsAsync(dto); service.Setup(x => x.GetNextBatchToUploadAsync()).ReturnsAsync(dto); await instance.ProcessAsync(service.Object, shimComm, settings); Assert.IsTrue(uploadCalled); Assert.IsTrue(downloadCalled); service.Verify(x => x.DeleteProcessedBatchesAsync(), Times.Once()); } }
public async Task TestUploadBatchAsync_IsFailure() { using (ShimsContext.Create()) { var userId = 1; appSettings.Add(AppSettings.SYSTEM_USER_ID_KEY, userId.ToString()); var dto = new SevisBatchProcessingDTO { Id = 1, BatchId = "batchId", SendString = "<root></root>", SevisOrgId = "sevis org Id", SevisUsername = "******" }; var responseContent = "hello world"; var message = new HttpResponseMessage { Content = new StringContent(responseContent), StatusCode = System.Net.HttpStatusCode.BadRequest }; var shimComm = new ECA.Net.Fakes.ShimSevisComm { UploadAsyncXElementStringStringString = (xElement, batchId, sOrgId, sUsername) => { Assert.AreEqual(dto.BatchId, batchId); Assert.AreEqual(dto.SevisOrgId, sOrgId); Assert.AreEqual(dto.SevisUsername, sUsername); return(Task.FromResult <HttpResponseMessage>(message)); } }; Action <int, Exception> callback = (bId, exc) => { Assert.AreEqual(dto.Id, bId); Assert.IsNull(exc); }; service.Setup(x => x.HandleFailedUploadBatchAsync(It.IsAny <int>(), It.IsAny <Exception>())) .Returns(Task.FromResult <object>(null)) .Callback(callback); await instance.UploadBatchAsync(shimComm, dto); service.Verify(x => x.HandleFailedUploadBatchAsync(It.IsAny <int>(), It.IsAny <Exception>()), Times.Once()); } }
/// <summary> /// Calls the sevis api for results to the sevis batch. /// </summary> /// <param name="batchComm">The sevis comm instance.</param> /// <param name="dtoToDownload">The batch to get download results for.</param> /// <returns>The task.</returns> public async Task DownloadBatchAsync(SevisComm batchComm, SevisBatchProcessingDTO dtoToDownload) { Contract.Requires(batchComm != null, "The batchComm must not be null."); Contract.Requires(dtoToDownload != null, "The dto to download must not be null."); // ask for download logger.Info("Getting Download, BatchId: {0}", dtoToDownload.BatchId); var response = await batchComm.DownloadAsync(dtoToDownload.BatchId, dtoToDownload.SevisOrgId, dtoToDownload.SevisUsername); //process response if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); await responseHandler.HandleDownloadResponseStreamAsync(GetSystemUser(), dtoToDownload, stream); logger.Info("Processed Download Response"); } else { logger.Error("Download encountered an error, status code: {0}, reason: {1}", response.StatusCode.ToString(), response.ReasonPhrase); await service.HandleFailedDownloadBatchAsync(dtoToDownload.Id, null); } }
/// <summary> /// Uploads a batch to the sevis api. /// </summary> /// <param name="batchComm">The sevis communication instance.</param> /// <param name="dtoToUpload">The batch to upload.</param> /// <returns></returns> public async Task UploadBatchAsync(SevisComm batchComm, SevisBatchProcessingDTO dtoToUpload) { Contract.Requires(batchComm != null, "The batchComm must not be null."); Contract.Requires(dtoToUpload != null, "The dto to upload must not be null."); //do the send here logger.Info("Sending Upload, BatchId: {0}", dtoToUpload.BatchId); var response = await batchComm.UploadAsync(XElement.Parse(dtoToUpload.SendString), dtoToUpload.BatchId, dtoToUpload.SevisOrgId, dtoToUpload.SevisUsername); //process response message if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); await responseHandler.HandleUploadResponseStreamAsync(GetSystemUser(), dtoToUpload, stream); logger.Info("Processed Upload Response"); } else { logger.Error("Upload encountered an error, status code: {0}, reason: {1}", response.StatusCode.ToString(), response.ReasonPhrase); await service.HandleFailedUploadBatchAsync(dtoToUpload.Id, null); } }
/// <summary> /// /// </summary> /// <param name="user"></param> /// <param name="dto"></param> /// <param name="stream"></param> public void HandleUploadResponseStream(User user, SevisBatchProcessingDTO dto, Stream stream) { Contract.Requires(user != null, "The user must not be null."); Contract.Requires(dto != null, "The dto must not be null."); Contract.Requires(stream != null, "The stream must not be null."); }