private void VerifyTournamentGroups(IEnumerable <tournamentGroup> apiGroups, IEnumerable <GroupCI> ciTourGroups)
        {
            var sapiGroups = apiGroups?.ToList();
            var ciGroups   = ciTourGroups?.ToList();

            if (sapiGroups.IsNullOrEmpty())
            {
                Assert.IsTrue(ciGroups.IsNullOrEmpty());
                return;
            }

            Assert.AreEqual(sapiGroups.Count, ciGroups.Count);
            foreach (var sapiGroup in sapiGroups)
            {
                if (!string.IsNullOrEmpty(sapiGroup.id))
                {
                    Assert.IsTrue(ciGroups.Exists(a => a.Id.Equals(sapiGroup.id)));
                }

                if (!string.IsNullOrEmpty(sapiGroup.name))
                {
                    Assert.IsTrue(ciGroups.Exists(a => a.Name.Equals(sapiGroup.name)));
                }

                GroupCI matchingGroup = MergerHelper.FindExistingGroup(ciGroups, new GroupDTO(sapiGroup));

                Assert.AreEqual(sapiGroup.competitor.Length, matchingGroup.CompetitorsIds.Count());

                foreach (var sapiTeam in sapiGroup.competitor)
                {
                    Assert.IsTrue(matchingGroup.CompetitorsIds.Contains(URN.Parse(sapiTeam.id)));
                }
            }
        }
        private void VerifyTournamentGroups(IEnumerable <tournamentGroup> apiGroups, IEnumerable <GroupCI> ciTourGroups)
        {
            var sapiGroups = apiGroups?.ToList();
            var ciGroups   = ciTourGroups?.ToList();

            //if api groups are empty, so must be ci groups
            if (sapiGroups.IsNullOrEmpty())
            {
                Assert.IsTrue(ciGroups.IsNullOrEmpty());
                return;
            }

            Assert.IsNotNull(sapiGroups);
            Assert.IsNotNull(ciGroups);
            Assert.AreEqual(sapiGroups.Count, ciGroups.Count);
            foreach (var sapiGroup in sapiGroups)
            {
                // find by id
                if (!string.IsNullOrEmpty(sapiGroup.id))
                {
                    Assert.IsTrue(ciGroups.Exists(a => a.Id.Equals(sapiGroup.id)));
                }

                //find by name
                if (!string.IsNullOrEmpty(sapiGroup.name))
                {
                    Assert.IsTrue(ciGroups.Exists(a => a.Name.Equals(sapiGroup.name)));
                }

                var matchingGroup = MergerHelper.FindExistingGroup(ciGroups, new GroupDTO(sapiGroup));

                Assert.AreEqual(sapiGroup.competitor.Length, matchingGroup.CompetitorsIds.Count());

                // each competitor in api group must also be in matching group
                foreach (var sapiTeam in sapiGroup.competitor)
                {
                    Assert.IsTrue(matchingGroup.CompetitorsIds.Contains(URN.Parse(sapiTeam.id)));
                }
            }
        }
        /// <summary>
        /// Merges the groups
        /// </summary>
        /// <param name="groups">The groups</param>
        /// <param name="culture">The culture</param>
        private void MergeGroups(IEnumerable <GroupDTO> groups, CultureInfo culture)
        {
            Guard.Argument(culture, nameof(culture)).NotNull();

            if (groups == null)
            {
                return;
            }

            var tmpGroups = _groups == null
                ? new List <GroupCI>()
                : new List <GroupCI>(_groups);

            var groupDTOs = groups.ToList();

            // remove obsolete groups
            if (!_groups.IsNullOrEmpty())
            {
                try
                {
                    if (groupDTOs.Count > 0 && !groupDTOs.Count.Equals(tmpGroups.Count))
                    {
                        tmpGroups.Clear();
                    }
                    else if (tmpGroups.Any(c => string.IsNullOrEmpty(c.Id) && string.IsNullOrEmpty(c.Name)) && !groupDTOs.Any(c => string.IsNullOrEmpty(c.Id) && string.IsNullOrEmpty(c.Name)))
                    {
                        tmpGroups.Clear();
                    }
                    else
                    {
                        foreach (var tmpGroup in _groups)
                        {
                            if (!string.IsNullOrEmpty(tmpGroup.Id))
                            {
                                if (groupDTOs.FirstOrDefault(f => f.Id.Equals(tmpGroup.Id, StringComparison.InvariantCultureIgnoreCase)) == null)
                                {
                                    tmpGroups.Remove(tmpGroup);
                                    continue;
                                }
                            }

                            if (string.IsNullOrEmpty(tmpGroup.Id) && !string.IsNullOrEmpty(tmpGroup.Name))
                            {
                                if (groupDTOs.FirstOrDefault(f => !string.IsNullOrEmpty(f.Name) && f.Name.Equals(tmpGroup.Name, StringComparison.InvariantCultureIgnoreCase)) == null)
                                {
                                    tmpGroups.Remove(tmpGroup);
                                    continue;
                                }
                            }

                            if (string.IsNullOrEmpty(tmpGroup.Id) && string.IsNullOrEmpty(tmpGroup.Name))
                            {
                                if (groupDTOs.First(f => string.IsNullOrEmpty(f.Id) && string.IsNullOrEmpty(f.Name)) == null)
                                {
                                    tmpGroups.Remove(tmpGroup);
                                    continue;
                                }
                            }

                            if (tmpGroups.Count > 0 && tmpGroups.Any(tmpG => !string.IsNullOrEmpty(tmpG.Id) && !_groups.Any(s => s.Id != null && s.Id.Equals(tmpG.Id))))
                            {
                                tmpGroups.Clear();
                                break;
                            }

                            // if no group with matching competitors
                            var groupExists = MergerHelper.FindExistingGroup(groupDTOs, tmpGroup);
                            if (groupExists == null)
                            {
                                tmpGroups.Remove(tmpGroup);
                                continue;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            else
            {
                if (tmpGroups.Count > 0)
                {
                    tmpGroups.Clear();
                }
            }

            foreach (var group in groupDTOs)
            {
                var tempGroup = MergerHelper.FindExistingGroup(tmpGroups, group);
                if (tempGroup == null)
                {
                    tmpGroups.Add(new GroupCI(group, culture));
                }
                else
                {
                    tempGroup.Merge(group, culture);
                }
            }
            _groups = new ReadOnlyCollection <GroupCI>(tmpGroups);
        }