Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionAttachmentManager.AddAttachment
DataCollectionAttachmentManager.cs
Source:DataCollectionAttachmentManager.cs  
...121            }122            return attachments;123        }124        /// <inheritdoc/>125        public void AddAttachment(FileTransferInformation fileTransferInfo, AsyncCompletedEventHandler sendFileCompletedCallback, Uri uri, string friendlyName)126        {127            ValidateArg.NotNull(fileTransferInfo, nameof(fileTransferInfo));128            if (string.IsNullOrEmpty(this.SessionOutputDirectory))129            {130                if (EqtTrace.IsErrorEnabled)131                {132                    EqtTrace.Error(133                        "DataCollectionAttachmentManager.AddAttachment: Initialize not invoked.");134                }135                return;136            }137            if (!this.AttachmentSets.ContainsKey(fileTransferInfo.Context))138            {139                var uriAttachmentSetMap = new Dictionary<Uri, AttachmentSet>();140                this.AttachmentSets.Add(fileTransferInfo.Context, uriAttachmentSetMap);141                this.attachmentTasks.Add(fileTransferInfo.Context, new List<Task>());142            }143            if (!this.AttachmentSets[fileTransferInfo.Context].ContainsKey(uri))144            {145                this.AttachmentSets[fileTransferInfo.Context].Add(uri, new AttachmentSet(uri, friendlyName));146            }147            this.AddNewFileTransfer(fileTransferInfo, sendFileCompletedCallback, uri, friendlyName);...DataCollectionAttachmentManagerTests.cs
Source:DataCollectionAttachmentManagerTests.cs  
...61            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);62            Assert.AreEqual(Path.Combine(TempDirectoryPath, this.sessionId.Id.ToString()), this.attachmentManager.SessionOutputDirectory);63        }64        [TestMethod]65        public void AddAttachmentShouldNotAddNewFileTransferIfSessionIsNotConfigured()66        {67            var filename = "filename.txt";68            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);69            var datacollectioncontext = new DataCollectionContext(this.sessionId);70            var friendlyName = "TestDataCollector";71            var uri = new Uri("datacollector://Company/Product/Version");72            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), false);73            this.attachmentManager.AddAttachment(dataCollectorDataMessage, null, uri, friendlyName);74            Assert.AreEqual(this.attachmentManager.AttachmentSets.Count, 0);75        }76        [TestMethod]77        public void AddAttachmentShouldAddNewFileTransferAndCopyFileToOutputDirectoryIfDeleteFileIsFalse()78        {79            var filename = "filename.txt";80            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);81            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);82            var datacollectioncontext = new DataCollectionContext(this.sessionId);83            var friendlyName = "TestDataCollector";84            var uri = new Uri("datacollector://Company/Product/Version");85            EventWaitHandle waitHandle = new AutoResetEvent(false);86            var handler = new AsyncCompletedEventHandler((a, e) => { waitHandle.Set(); });87            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), false);88            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);89            // Wait for file operations to complete90            waitHandle.WaitOne(Timeout);91            Assert.IsTrue(File.Exists(Path.Combine(TempDirectoryPath, filename)));92            Assert.IsTrue(File.Exists(Path.Combine(TempDirectoryPath, this.sessionId.Id.ToString(), filename)));93            Assert.AreEqual(1, this.attachmentManager.AttachmentSets[datacollectioncontext][uri].Attachments.Count);94        }95        [TestMethod]96        public void AddAttachmentsShouldAddFilesCorrespondingToDifferentDataCollectors()97        {98            var filename = "filename.txt";99            var filename1 = "filename1.txt";100            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);101            File.WriteAllText(Path.Combine(TempDirectoryPath, filename1), string.Empty);102            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);103            var datacollectioncontext = new DataCollectionContext(this.sessionId);104            var friendlyName = "TestDataCollector";105            var uri = new Uri("datacollector://Company/Product/Version");106            var uri1 = new Uri("datacollector://Company/Product/Version1");107            EventWaitHandle waitHandle = new AutoResetEvent(false);108            var handler = new AsyncCompletedEventHandler((a, e) => { waitHandle.Set(); });109            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), false);110            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);111            // Wait for file operations to complete112            waitHandle.WaitOne(Timeout);113            waitHandle.Reset();114            dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename1), false);115            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri1, friendlyName);116            // Wait for file operations to complete117            waitHandle.WaitOne(Timeout);118            Assert.AreEqual(1, this.attachmentManager.AttachmentSets[datacollectioncontext][uri].Attachments.Count);119            Assert.AreEqual(1, this.attachmentManager.AttachmentSets[datacollectioncontext][uri1].Attachments.Count);120        }121        [TestMethod]122        public void AddAttachmentShouldAddNewFileTransferAndMoveFileToOutputDirectoryIfDeleteFileIsTrue()123        {124            var filename = "filename1.txt";125            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);126            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);127            var datacollectioncontext = new DataCollectionContext(this.sessionId);128            var friendlyName = "TestDataCollector";129            var uri = new Uri("datacollector://Company/Product/Version");130            var waitHandle = new AutoResetEvent(false);131            var handler = new AsyncCompletedEventHandler((a, e) => { waitHandle.Set(); });132            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), true);133            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);134            // Wait for file operations to complete135            waitHandle.WaitOne(Timeout);136            Assert.AreEqual(1, this.attachmentManager.AttachmentSets[datacollectioncontext][uri].Attachments.Count);137            Assert.IsTrue(File.Exists(Path.Combine(TempDirectoryPath, this.sessionId.Id.ToString(), filename)));138            Assert.IsFalse(File.Exists(Path.Combine(TempDirectoryPath, filename)));139        }140        [TestMethod]141        public void AddAttachmentShouldAddMultipleAttachmentsForSameDC()142        {143            var filename = "filename.txt";144            var filename1 = "filename1.txt";145            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);146            File.WriteAllText(Path.Combine(TempDirectoryPath, filename1), string.Empty);147            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);148            var datacollectioncontext = new DataCollectionContext(this.sessionId);149            var friendlyName = "TestDataCollector";150            var uri = new Uri("datacollector://Company/Product/Version");151            EventWaitHandle waitHandle = new AutoResetEvent(false);152            var handler = new AsyncCompletedEventHandler((a, e) => { waitHandle.Set(); });153            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), false);154            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);155            // Wait for file operations to complete156            waitHandle.WaitOne(Timeout);157            waitHandle.Reset();158            dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename1), false);159            this.attachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);160            // Wait for file operations to complete161            waitHandle.WaitOne(Timeout);162            Assert.AreEqual(2, this.attachmentManager.AttachmentSets[datacollectioncontext][uri].Attachments.Count);163        }164        [TestMethod]165        public void AddAttachmentShouldNotAddNewFileTransferIfNullIsPassed()166        {167            Assert.ThrowsException<ArgumentNullException>(() =>168            {169                this.attachmentManager.AddAttachment(null, null, null, null);170            });171        }172        [TestMethod]173        public void GetAttachmentsShouldReturnAllAttachmets()174        {175            var filename = "filename1.txt";176            File.WriteAllText(Path.Combine(TempDirectoryPath, filename), string.Empty);177            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);178            var datacollectioncontext = new DataCollectionContext(this.sessionId);179            var friendlyName = "TestDataCollector";180            var uri = new Uri("datacollector://Company/Product/Version");181            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, Path.Combine(TempDirectoryPath, filename), true);182            this.attachmentManager.AddAttachment(dataCollectorDataMessage, null, uri, friendlyName);183            Assert.AreEqual(1, this.attachmentManager.AttachmentSets.Count);184            var result = this.attachmentManager.GetAttachments(datacollectioncontext);185            Assert.AreEqual(0, this.attachmentManager.AttachmentSets.Count);186            Assert.AreEqual(1, result.Count);187            Assert.AreEqual(friendlyName, result[0].DisplayName);188            Assert.AreEqual(uri, result[0].Uri);189            Assert.AreEqual(1, result[0].Attachments.Count);190        }191        [TestMethod]192        public void GetAttachmentsShouldNotReutrnAnyDataWhenActiveFileTransferAreNotPresent()193        {194            this.attachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);195            var datacollectioncontext = new DataCollectionContext(this.sessionId);196            var result = this.attachmentManager.GetAttachments(datacollectioncontext);197            Assert.AreEqual(0, result.Count);198        }199        [TestMethod]200        public void GetAttachmentsShouldNotReturnAttachmentsAfterCancelled()201        {202            var fileHelper = new Mock<IFileHelper>();203            var testableAttachmentManager = new TestableDataCollectionAttachmentManager(fileHelper.Object);204            var attachmentPath = Path.Combine(TempDirectoryPath, "filename.txt");205            File.WriteAllText(attachmentPath, string.Empty);206            var datacollectioncontext = new DataCollectionContext(this.sessionId);207            var friendlyName = "TestDataCollector";208            var uri = new Uri("datacollector://Company/Product/Version");209            var dataCollectorDataMessage = new FileTransferInformation(datacollectioncontext, attachmentPath, true);210            var waitHandle = new AutoResetEvent(false);211            var handler = new AsyncCompletedEventHandler((a, e) => { Assert.Fail("Handler shouldn't be called since operation is canceled."); });212            // We cancel the operation in the actual operation. This ensures the follow up task to is never called, attachments213            // are not added.214            Action cancelAddAttachment = () => testableAttachmentManager.Cancel();215            fileHelper.Setup(fh => fh.MoveFile(It.IsAny<string>(), It.IsAny<string>())).Callback(cancelAddAttachment);216            testableAttachmentManager.Initialize(this.sessionId, TempDirectoryPath, this.messageSink.Object);217            testableAttachmentManager.AddAttachment(dataCollectorDataMessage, handler, uri, friendlyName);218            // Wait for the attachment transfer tasks to complete219            var result = testableAttachmentManager.GetAttachments(datacollectioncontext);220            Assert.AreEqual(0, result[0].Attachments.Count);221        }222        private class TestableDataCollectionAttachmentManager : DataCollectionAttachmentManager223        {224            public TestableDataCollectionAttachmentManager(IFileHelper fileHelper)225                : base(fileHelper)226            {227            }228        }229    }230}...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
