Ejemplo n.º 1
0
        /// <summary>
        /// It writes the failed workitems in error worksheet
        /// </summary>
        /// <param name="failedDSWorkItem"></param>
        private void ReportError(FailedSourceWorkItem failedDSWorkItem)
        {
            // gets the error sheet
            Worksheet errorSheet = m_reportBook.Worksheets[ErrorSheetName] as Worksheet;

            // Write error in error column
            WriteValueAt(errorSheet, m_currentErrorRow, m_errorHeaderToColumn[ErrorField], failedDSWorkItem.Error);

            // Write rest of workitem
            m_currentErrorRow = WriteXLWorkItem(errorSheet, m_currentErrorRow, m_errorHeaderToColumn, failedDSWorkItem);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses and returns the next workitem present in the Excel Source
        /// </summary>
        /// <returns></returns>
        public ISourceWorkItem GetNextWorkItem()
        {
            if (m_currentRow > m_lastRow)
            {
                return(null);
            }

            int workItemStartingRow = -1;

            string error = null;

            // It represents the state that it has started the parsing of the excel workitem or not
            bool isReadingNextWorkItemStarted = false;

            // The Internal Representation of TFS workItem.
            ISourceWorkItem xlWorkItem = new SourceWorkItem();

            // The List of Test Steps
            List <SourceTestStep> steps = new List <SourceTestStep>();

            // While The end of work item is not encountered parse the current row and update the Excel Work Item
            while (!IsWorkItemCompleted(isReadingNextWorkItemStarted))
            {
                if (workItemStartingRow == -1)
                {
                    workItemStartingRow = m_currentRow;
                }

                m_storageInfo.ProgressPercentage = (m_currentRow * 100) / m_lastRow;
                // Parsing of work item is now started. So switch on the bool variable
                isReadingNextWorkItemStarted = true;

                string testStepTitle          = string.Empty;
                string testStepExpectedResult = string.Empty;

                // Itrerating throgh each fields and reading value present in corresponding cells and then updating
                // m_dataStore.DataValuesByFieldName and Excel WorkItem
                foreach (KeyValuePair <string, int> kvp in m_selectedHeadersToColumn)
                {
                    // Get Value at(currentrow,columnForCurrentHeaderInProcess)
                    string value = GetValueAt(m_currentRow, kvp.Value);
                    if (!string.IsNullOrEmpty(value))
                    {
                        // If the Current field is mapped to TFS Test Step Title field then update the step's title
                        if (FieldNameToFields[kvp.Key] is TestStepTitleField)
                        {
                            testStepTitle = value;
                        }
                        // else if the Current field is mapped to TFS Test Step Expected result field then update the step's expected result
                        else if (FieldNameToFields[kvp.Key] is TestStepExpectedResultField)
                        {
                            testStepExpectedResult = value;
                        }
                        // else if it is mapped to a date time field then parse it
                        else if (FieldNameToFields[kvp.Key].Type == typeof(DateTime))
                        {
                            double excelDate;
                            if (double.TryParse(value, out excelDate))
                            {
                                DateTime dateOfReference = new DateTime(1900, 1, 1);
                                excelDate = excelDate - 2;
                                try
                                {
                                    xlWorkItem.FieldValuePairs[kvp.Key] = dateOfReference.AddDays(excelDate);
                                }
                                catch (ArgumentException)
                                {
                                    // If argument provided is wrong then don't set anyting
                                    xlWorkItem.FieldValuePairs[kvp.Key] = null;
                                }
                            }
                            else
                            {
                                xlWorkItem.FieldValuePairs[kvp.Key] = null;
                            }
                        }
                        // else just update the exel workitem
                        else
                        {
                            if (!xlWorkItem.FieldValuePairs.ContainsKey(kvp.Key))
                            {
                                xlWorkItem.FieldValuePairs.Add(kvp.Key, value);
                            }
                            else
                            {
                                xlWorkItem.FieldValuePairs[kvp.Key] += "\n" + value;
                            }
                        }
                    }
                    // else if this field is not already filled and it is not test step(title or expected result) field then set it to null.
                    else if (!xlWorkItem.FieldValuePairs.ContainsKey(kvp.Key) &&
                             !(FieldNameToFields[kvp.Key] is TestStepTitleField) &&
                             !(FieldNameToFields[kvp.Key] is TestStepExpectedResultField))
                    {
                        xlWorkItem.FieldValuePairs.Add(kvp.Key, null);
                    }
                }

                // If we found step in the current row then update the list of excel steps
                if (!string.IsNullOrEmpty(testStepTitle) || !string.IsNullOrEmpty(testStepExpectedResult))
                {
                    SourceTestStep step = new SourceTestStep();
                    step.title          = testStepTitle.Trim();
                    step.expectedResult = testStepExpectedResult.Trim();
                    steps.Add(step);
                }

                // Done with parsing of current row. Move to next row
                m_currentRow++;
            }

            if (!string.IsNullOrEmpty(StorageInfo.SourceIdFieldName))
            {
                string value = GetValueAt(workItemStartingRow, m_headersToColumn[StorageInfo.SourceIdFieldName]);
                if (string.IsNullOrEmpty(value))
                {
                    error += "Source Id Is Not found\n";
                }
                else
                {
                    xlWorkItem.SourceId = value;
                }
            }
            if (!string.IsNullOrEmpty(StorageInfo.TestSuiteFieldName))
            {
                string value = GetValueAt(workItemStartingRow, m_headersToColumn[StorageInfo.TestSuiteFieldName]);
                if (!string.IsNullOrEmpty(value))
                {
                    foreach (string testSuite in value.Split(';', '\n'))
                    {
                        string s = testSuite.Trim();
                        if (!string.IsNullOrEmpty(s))
                        {
                            xlWorkItem.TestSuites.Add(s);
                        }
                    }
                }
            }

            foreach (ILinkRule linkInfo in StorageInfo.LinkRules)
            {
                string value = GetValueAt(workItemStartingRow, m_headersToColumn[linkInfo.SourceFieldNameOfEndWorkItemCategory]);
                if (!string.IsNullOrEmpty(value))
                {
                    foreach (string linkedWorkItemId in value.Split(';'))
                    {
                        string trimmedId = linkedWorkItemId.Trim();
                        if (!string.IsNullOrEmpty(trimmedId))
                        {
                            Link link = new Link();
                            link.StartWorkItemCategory = linkInfo.StartWorkItemCategory;
                            link.StartWorkItemSourceId = xlWorkItem.SourceId;
                            link.LinkTypeName          = linkInfo.LinkTypeReferenceName;
                            link.EndWorkItemCategory   = linkInfo.EndWorkItemCategory;
                            link.EndWorkItemSourceId   = trimmedId;
                            xlWorkItem.Links.Add(link);
                        }
                    }
                }
            }

            if (steps != null && steps.Count > 0)
            {
                // Done with parsing of XL Work Item. Add the List of steps in the XL Work Item.
                xlWorkItem.FieldValuePairs.Add(TestStepsFieldKeyName, steps);
            }

            xlWorkItem.SourcePath = StorageInfo.Source;

            if (!string.IsNullOrEmpty(error))
            {
                xlWorkItem = new FailedSourceWorkItem(xlWorkItem, error);
            }

            RawSourceWorkItems.Add(xlWorkItem);

            xlWorkItem = ApplySettings(xlWorkItem);

            ParsedSourceWorkItems.Add(xlWorkItem);

            // return the Excel WorkItem
            return(xlWorkItem);
        }
        /// <summary>
        /// Migrates all the workitems with the help of WorkItemGenerator and returns the list of updated source workitems
        /// </summary>
        /// <param name="workItemGenerator"></param>
        /// <returns></returns>
        public IList <ISourceWorkItem> Migrate(IWorkItemGenerator workItemGenerator)
        {
            // The List of source workitems which has to be returned
            var resultSourceWorkItems = new List <ISourceWorkItem>();

            // Now process each source workitems which are to migrate
            foreach (ISourceWorkItem sourceWorkItem in SourceWorkItems)
            {
                if (sourceWorkItem is FailedSourceWorkItem)
                {
                    AddSourceWorkItemInResultSourceWorkItems(resultSourceWorkItems, sourceWorkItem);
                    continue;
                }

                // bool variable to track the return value of Premigration event
                bool preMigrationReturnValue = true;

                // bool variable to track the return value of PostMigration event
                bool postMigrationEventValue = true;

                IWorkItem workItem = null;
                try
                {
                    // Create IWorkitem with the help of WorkItemGenerator
                    workItem = workItemGenerator.CreateWorkItem(sourceWorkItem);
                }
                catch (WorkItemMigratorException ex)
                {
                    var failedWorkItem = new FailedSourceWorkItem(sourceWorkItem, ex.Args.Title);
                    AddSourceWorkItemInResultSourceWorkItems(resultSourceWorkItems, failedWorkItem);
                    continue;
                }

                if (PreMigration != null)
                {
                    preMigrationReturnValue = PreMigration(sourceWorkItem, workItem);
                }

                // If PreMigration allowed to process further then save the workitem at destination
                if (preMigrationReturnValue)
                {
                    // Saves the workitem at Destination and get the updated sourceworkitem
                    var migratedSourceWorkItem = workItemGenerator.SaveWorkItem(workItem, sourceWorkItem);

                    postMigrationEventValue = AddSourceWorkItemInResultSourceWorkItems(resultSourceWorkItems, migratedSourceWorkItem);

                    // If PostMigration event has return false to terminate the further migration
                    if (!postMigrationEventValue)
                    {
                        break;
                    }
                }
                // If preMigration event returned false the skip this sourceworkitem
                else
                {
                    AddSourceWorkItemInResultSourceWorkItems(resultSourceWorkItems, new SkippedSourceWorkItem(sourceWorkItem));
                }
            }

            // returning the list of updated sourceworkitem
            return(resultSourceWorkItems);
        }