internal long GetLastUpdated(int testPlanId)
 {
     var allTestPoints = _tfsBase.GetAllTestPointsInPlan(testPlanId);
     return GetLastUpdatedAsTicks(allTestPoints);
 }
        internal int Save(TestRun testrun, int onlyForTestCaseWithId = -1)
        {
            var allPoints = _tfsBase.GetAllTestPointsInPlan(testrun.TestPlanId, onlyForTestCaseWithId != -1 ? new List <int> {
                onlyForTestCaseWithId
            } : testrun.Tests.Select(x => x.TestCaseId).Distinct());

            if (testrun.Tests.Count == 0)
            {
                return(-1);
            }

            int testRunId = -1;
            var testRun   = CreateTestRun(testrun.Started, testrun.Finished, allPoints, ref testRunId, testrun.ForDeployment);

            foreach (var test in testrun.Tests)
            {
                if (onlyForTestCaseWithId != -1 && test.TestCaseId != onlyForTestCaseWithId)
                {
                    continue;
                }

                var testCase = _tfsBase.GetTestCase(test.TestCaseId);
                var grouping = test.Points.GroupBy(t => t.Id);

                foreach (var item in grouping)
                {
                    var id        = item.Key;
                    var testPoint = allPoints.SingleOrDefault(p => p.Id == id);
                    if (testPoint == null)
                    {
                        continue;
                    }

                    var testCaseResult = GetTestCaseResult(testRun, testCase, testPoint);

                    var count       = 1;
                    var testOutcome = TestOutcome.Passed;
                    foreach (var rowOfData in item)
                    {
                        var iteration     = CreateIterationResult(testCaseResult, count);
                        var testConductor = new TestConductor(iteration, testCase, rowOfData.Data);
                        var result        = testConductor.SetStepResults(rowOfData.TestSteps);

                        result.Outcome  = testConductor.TestOutCome;
                        result.Duration = rowOfData.Duration;
                        testCaseResult.Iterations.Add(result);

                        if (result.Outcome != TestOutcome.Passed)
                        {
                            testOutcome = result.Outcome;
                        }

                        if (_testStepBlobStore != null && testConductor.ResultBlob != null && testRunId > 0)
                        {
                            _testStepBlobStore.SaveErrorInformationBlob(testRunId, test.TestCaseId, count, testConductor.ResultBlob);
                        }

                        SetNotes(testCaseResult, testConductor);
                        count++;
                    }

                    SaveTestCaseResult(testCaseResult, testOutcome);
                }
            }

            return(testRunId);
        }