public void TestGetStream_ArchiveHasFile()
        {
            using (ShimsContext.Create())
            {
                var memoryStream = new MemoryStream();
                var sevisId      = "N000012345";
                var now          = DateTime.UtcNow;
                var zipFileName  = String.Format("_{0}_{1}.pdf", sevisId, now);
                var entry        = new System.IO.Compression.Fakes.ShimZipArchiveEntry
                {
                    NameGet = () => zipFileName,
                    Open    = () => memoryStream
                };

                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 archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);
                Assert.IsTrue(Object.ReferenceEquals(memoryStream, archiveHandler.GetStream(sevisId)));
                memoryStream.Dispose();
            }
        }
        public async Task TestGetTransactionLogXmlAsync_ArchiveDoesNotHaveFile()
        {
            using (ShimsContext.Create())
            {
                var transactionLog = new TransactionLogType();
                var xml            = GetXml(transactionLog);

                var xmlTransactionLogFileName = "transaction_log.xml";
                var zipFileName = String.Format("{0}", xmlTransactionLogFileName);


                var entries         = new List <ZipArchiveEntry>();
                var readonlyEntries = new ReadOnlyCollection <ZipArchiveEntry>(entries);

                var zipArchive = new System.IO.Compression.Fakes.ShimZipArchive
                {
                    EntriesGet = () => readonlyEntries
                };

                var archiveHandler = new SevisBatchZipArchiveHandler(zipArchive, xmlTransactionLogFileName);
                var testXml        = await archiveHandler.GetTransactionLogXmlAsync();

                Assert.IsNull(testXml);
            }
        }
        public async Task TestGetDS2019FileStream_ArchiveDoesNotHaveFile()
        {
            using (ShimsContext.Create())
            {
                var sevisId     = "N000012345";
                var now         = DateTime.UtcNow;
                var zipFileName = String.Format("_{0}_{1}.pdf", sevisId, now);

                var entries         = new List <ZipArchiveEntry>();
                var readonlyEntries = new ReadOnlyCollection <ZipArchiveEntry>(entries);

                var zipArchive = new System.IO.Compression.Fakes.ShimZipArchive
                {
                    EntriesGet = () => readonlyEntries
                };

                var archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);

                Action <Stream> tester = (testStream) =>
                {
                    Assert.IsNull(testStream);
                };
                var requestId   = new RequestId(1, RequestIdType.Participant, RequestActionType.Create);
                var stream      = archiveHandler.GetDS2019FileStream(requestId, sevisId);
                var streamAsync = await archiveHandler.GetDS2019FileStreamAsync(requestId, sevisId);

                tester(stream);
                tester(streamAsync);
            }
        }
        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);
            }
        }
 public void TestZipArchiveConstructor_UseDefaultTransactionLogFileName()
 {
     using (ShimsContext.Create())
     {
         var zipArchive     = new System.IO.Compression.Fakes.ShimZipArchive();
         var archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);
         Assert.IsNotNull(archiveHandler.ZipArchive);
         Assert.AreEqual(SevisBatchZipArchiveHandler.TRANSACTION_LOG_FILE_NAME, archiveHandler.TransactionLogFileName);
     }
 }
 public void TestZipArchiveConstructor_UseGivenTransactionLogFileName()
 {
     using (ShimsContext.Create())
     {
         var zipArchive             = new System.IO.Compression.Fakes.ShimZipArchive();
         var transactionLogFileName = "hello world";
         var archiveHandler         = new SevisBatchZipArchiveHandler(zipArchive, transactionLogFileName);
         Assert.IsNotNull(archiveHandler.ZipArchive);
         Assert.AreEqual(transactionLogFileName, archiveHandler.TransactionLogFileName);
     }
 }
        public void TestDispose()
        {
            using (ShimsContext.Create())
            {
                var sevisId     = "N000012345";
                var now         = DateTime.UtcNow;
                var zipFileName = String.Format("_{0}_{1}.pdf", sevisId, now);

                var zipArchive = new System.IO.Compression.Fakes.ShimZipArchive();

                var archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);
                Assert.IsNotNull(archiveHandler.ZipArchive);
                archiveHandler.Dispose();
                Assert.IsNull(archiveHandler.ZipArchive);
            }
        }
        public void TestGetStream_ArchiveDoesNotHaveFile()
        {
            using (ShimsContext.Create())
            {
                var sevisId = "N000012345";

                var entries         = new List <ZipArchiveEntry>();
                var readonlyEntries = new ReadOnlyCollection <ZipArchiveEntry>(entries);

                var zipArchive = new System.IO.Compression.Fakes.ShimZipArchive
                {
                    EntriesGet = () => readonlyEntries
                };

                var archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);
                Assert.IsNull(archiveHandler.GetStream(sevisId));
            }
        }
        public async Task TestGetDS2019FileStream_ArchiveHasFile()
        {
            using (ShimsContext.Create())
            {
                var memoryStream = new MemoryStream();
                var sevisId      = "N000012345";
                var now          = DateTime.UtcNow;
                var zipFileName  = String.Format("_{0}_{1}.pdf", sevisId, now);
                var entry        = new System.IO.Compression.Fakes.ShimZipArchiveEntry
                {
                    NameGet = () => zipFileName,
                    Open    = () => memoryStream
                };

                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 archiveHandler = new SevisBatchZipArchiveHandler(zipArchive);

                Action <Stream> tester = (testStream) =>
                {
                    Assert.IsTrue(Object.ReferenceEquals(memoryStream, testStream));
                };
                var requestId   = new RequestId(1, RequestIdType.Participant, RequestActionType.Create);
                var stream      = archiveHandler.GetDS2019FileStream(requestId, sevisId);
                var streamAsync = await archiveHandler.GetDS2019FileStreamAsync(requestId, sevisId);

                tester(stream);
                tester(streamAsync);

                memoryStream.Dispose();
            }
        }
        public async Task TestGetTransactionLogXmlAsync_ArchiveHasFile()
        {
            using (ShimsContext.Create())
            {
                var transactionLog = new TransactionLogType();
                var xml            = GetXml(transactionLog);

                var xmlTransactionLogFileName = "transaction_log.xml";
                var zipFileName = String.Format("{0}", xmlTransactionLogFileName);
                var entry       = new System.IO.Compression.Fakes.ShimZipArchiveEntry
                {
                    NameGet = () => zipFileName,
                    Open    = () =>
                    {
                        var stream = new MemoryStream();
                        var writer = new StreamWriter(stream, Encoding.Unicode);
                        writer.Write(xml);
                        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 archiveHandler = new SevisBatchZipArchiveHandler(zipArchive, xmlTransactionLogFileName);
                var testXml        = await archiveHandler.GetTransactionLogXmlAsync();

                Assert.AreEqual(xml, testXml);
            }
        }