private void UserGeneralInfo_Organization_CheckTreeDepthCoherence_Test(OrganizationTreeDescriptor oldTreeRoot, OrganizationTreeDescriptor newTreeRoot) { bool keepGoing = true; int index = 0; IEnumerable <OrganizationTreeDescriptor> oldEntriesSameDepth; IEnumerable <OrganizationTreeDescriptor> newEntriesSameDepth; int oldCount; int newCount; var watch = new Stopwatch(); watch.Start(); var resultReport = new ResultReport(this.UserId, this.OldId, EnumTestUnitNames.UserGeneralInfo_Organizations_TreeDepthCoherence, "Comparing Organization Tree Depth Coherence"); while (keepGoing) { oldEntriesSameDepth = this.oldServiceOrganizationDescriptors.Where(x => x.Depth == index); newEntriesSameDepth = this.newServiceOrganizationDescriptors.Where(s => s.Depth == index); oldCount = oldEntriesSameDepth.Count(); newCount = newEntriesSameDepth.Count(); if (oldCount == 0 && newCount == 0) { keepGoing = false; } try { // the only we can compare is that the old tree does not return more entries than the new one at a given depth of depth // the new service may return more because it has enriched the old tree where some of the values may have been manually - ? - excluded Assert.IsFalse(oldCount > newCount, "Comparing at depth index " + index); resultReport.UpdateSeverity(EnumResultSeverityType.SUCCESS); } catch (AssertFailedException e) { resultReport.UpdateSeverity(EnumResultSeverityType.ERROR); resultReport.ErrorMessage = e.Message; resultReport.IdentifedDataBehaviors.Add(EnumIdentifiedDataBehavior.OLD_TREE_HAS_MORE_CHILDREN_GIVEN_DEPTH); resultReport.AddDetailedValues(this.oldServiceOrganizationDescriptors, oldTreeRoot, this.newServiceOrganizationDescriptors, newTreeRoot); resultReport.TreeComparisonIndexError = index; if (resultReport.Severity == EnumResultSeverityType.ERROR) { keepGoing = false; } } index++; } if (resultReport.Severity == EnumResultSeverityType.SUCCESS) { resultReport.IdentifedDataBehaviors.Add(EnumIdentifiedDataBehavior.NEW_TREE_COUNT_CONSISTENT); resultReport.AddDetailedValues(this.oldServiceOrganizationDescriptors, oldTreeRoot, this.newServiceOrganizationDescriptors, newTreeRoot); } watch.Stop(); resultReport.Duration = watch.Elapsed; this.DetailedResults.Add(resultReport.TestName, resultReport); LogManager.Instance.LogTestResult(this.Container.BuildOldServiceFullURL(this.OldId), this.BuildNewServiceURL(this.PageName), resultReport); }
private void UserGeneralInfo_Organization_MergingNewTreeToOldOne_Test(OrganizationTreeDescriptor oldTreeRoot, OrganizationTreeDescriptor newTreeRoot) { var watch = new Stopwatch(); watch.Start(); var resultReport = new ResultReport(this.UserId, this.OldId, EnumTestUnitNames.UserGeneralInfo_Organizations_TreeMerged, "Trying to merge Organization Trees together"); if (newTreeRoot != null && oldTreeRoot != null) { var newTreeRootDeepClone = newTreeRoot.DeepClone(); PopulateGapsOfOldTree(this.oldServiceOrganizationDescriptors, this.newServiceOrganizationDescriptors, resultReport, true); var missingElements = this.oldServiceOrganizationDescriptors.Where(x => x.HasBeenMatched == false && string.IsNullOrEmpty(x.ID) && string.IsNullOrEmpty(x.Name) && x.Depth >= 0) .GroupBy(x => x.Depth) .ToDictionary(t => t.Key, t => t.ToList()); int countMissingElements = missingElements.Count(); if (countMissingElements == 0) { resultReport.UpdateSeverity(EnumResultSeverityType.SUCCESS); } else { //if (countMissingElements > 0) //{ // PopulateGapsOfOldTree(this.oldServiceOrganizationDescriptors, this.newServiceOrganizationDescriptors, resultReport, false); // missingElements = this.oldServiceOrganizationDescriptors.Where(x => x.HasBeenMatched == false // && string.IsNullOrEmpty(x.ID) // && string.IsNullOrEmpty(x.Name) // && x.Depth >= 0) // .GroupBy(x => x.Depth) // .ToDictionary(t => t.Key, t => t.ToList()); // countMissingElements = missingElements.Count(); //} if (countMissingElements == 0) { resultReport.UpdateSeverity(EnumResultSeverityType.FALSE_POSITIVE); } else { resultReport.UpdateSeverity(EnumResultSeverityType.ERROR); } } OrganizationTreeDescriptor root = null; try { root = this.oldServiceOrganizationDescriptors.Where(x => x.Depth == 0).First(); } catch (Exception) { } resultReport.AddDetailedValues(null, root, null, newTreeRootDeepClone); } else { resultReport.UpdateSeverity(EnumResultSeverityType.WARNING_NO_DATA); } watch.Stop(); resultReport.Duration = watch.Elapsed; this.DetailedResults.Add(resultReport.TestName, resultReport); LogManager.Instance.LogTestResult(this.Container.BuildOldServiceFullURL(this.OldId), this.BuildNewServiceURL(this.PageName), resultReport); }
private static void PopulateGapsOfOldTree(HashSet <OrganizationTreeDescriptor> oldTree, HashSet <OrganizationTreeDescriptor> newTree, ResultReport resultReport, bool excludeMatchedElements, int previousCountMissingElements = -1) { int countUnmatchedChildrenOfMissingElement = 42; bool onlyOneOptionAvailable = false; bool matchingElementFound = false; OrganizationTreeDescriptor matchingElement; var missingElementsByDepth = oldTree.Where(x => x.HasBeenMatched == false && string.IsNullOrEmpty(x.ID) && string.IsNullOrEmpty(x.Name) && x.Depth >= 0) .GroupBy(x => x.Depth) .ToDictionary(t => t.Key, t => t.ToList()); int countMissingElements = missingElementsByDepth.Count(); Dictionary <int, List <OrganizationTreeDescriptor> > potentialMatchingElementsByDepth; if (excludeMatchedElements) { potentialMatchingElementsByDepth = newTree.Where(x => x.HasBeenMatched == false).GroupBy(g => g.Depth).ToDictionary(t => t.Key, t => t.ToList()); } else { potentialMatchingElementsByDepth = newTree.GroupBy(g => g.Depth).ToDictionary(t => t.Key, t => t.ToList()); } if (countMissingElements != previousCountMissingElements && countMissingElements > 0 && potentialMatchingElementsByDepth.Count() > 0) { foreach (var missingElementsPair in missingElementsByDepth) { foreach (var missingElement in missingElementsPair.Value) { if (potentialMatchingElementsByDepth.ContainsKey(missingElementsPair.Key)) { matchingElementFound = false; matchingElement = null; foreach (var potentialElement in potentialMatchingElementsByDepth[missingElementsPair.Key]) { onlyOneOptionAvailable = potentialMatchingElementsByDepth[missingElementsPair.Key].Count() == 1; if ((potentialElement.ParentId == missingElement.ParentId || string.IsNullOrEmpty(missingElement.ParentId)) && potentialElement.Children.Count() >= missingElement.Children.Count()) { if (potentialElement.Children.Select(x => x.MatchedPartner).Count() > 0) { countUnmatchedChildrenOfMissingElement = missingElement.Children.Except(potentialElement.Children.Select(x => x.MatchedPartner).Union(potentialElement.Children)).Count(); } else { countUnmatchedChildrenOfMissingElement = missingElement.Children.Select(y => y.ID).Except(potentialElement.Children.Select(x => x.ID)).Count(); } if (countUnmatchedChildrenOfMissingElement == 0) { matchingElementFound = true; matchingElement = potentialElement; } } // if there is only one potential match, don't be picky if (onlyOneOptionAvailable && !matchingElementFound) { matchingElementFound = true; matchingElement = potentialElement; matchingElement.WasOnlyOption = true; resultReport.IdentifedDataBehaviors.Add(EnumIdentifiedDataBehavior.MATCHING_SINGLE_ELEMENT_GIVEN_DEPTH_MISMATCH); resultReport.UpdateSeverity(EnumResultSeverityType.WARNING); } } if (matchingElementFound) { if (!excludeMatchedElements) { matchingElement.UsedMoreThanOnce = true; resultReport.IdentifedDataBehaviors.Add(EnumIdentifiedDataBehavior.REUSED_ELEMENT_TO_FILL_GAP); } matchingElement.HasBeenMatched = true; matchingElement.MatchedPartner = missingElement; missingElement.HasBeenMatched = true; missingElement.MatchedPartner = matchingElement; matchingElement.IsImportedFromNewService = true; missingElement.IsMissing = false; matchingElement.IsMissing = false; matchingElement.Children.Clear(); matchingElement.Parent = missingElement.Parent; // replace in old tree foreach (var childFromMissing in missingElement.Children) { childFromMissing.Parent = matchingElement; matchingElement.Children.Add(childFromMissing); } if (missingElement.Parent != null && missingElement.Parent.Children != null) { missingElement.Parent.Children.Add(matchingElement); missingElement.Parent.Children.Remove(missingElement); } oldTree.Add(matchingElement); oldTree.Remove(missingElement); } } } } PopulateGapsOfOldTree(oldTree, newTree, resultReport, excludeMatchedElements, countMissingElements); } }