public void TestTryBulkLoadPeopleWithKerb1()
        {
            #region Arrange
            var failCount = 0;
            var dupCount  = 0;
            var notAdded  = new List <KeyValuePair <string, string> >();
            var role      = CreateValidEntities.Role(1);
            var workgroup = CreateValidEntities.Workgroup(1);
            #endregion Arrange

            #region Act
            var result = WorkgroupService.TryBulkLoadPeople("kerb1,kerb2 kerb3", false, 1, role,
                                                            workgroup, 0, ref failCount, ref dupCount,
                                                            notAdded);
            #endregion Act

            #region Assert
            Assert.AreEqual(0, result);
            UserRepository.AssertWasCalled(a => a.GetNullableById("kerb1"));
            UserRepository.AssertWasCalled(a => a.GetNullableById("kerb2"));
            UserRepository.AssertWasCalled(a => a.GetNullableById("kerb3"));
            UserRepository.AssertWasCalled(a => a.GetNullableById(Arg <string> .Is.Anything), x => x.Repeat.Times(3));
            #endregion Assert
        }
        public ActionResult AddPeople(int id, WizardWorkgroupPeoplePostModel workgroupPeoplePostModel, string roleFilter, string bulkEmail, string bulkKerb)
        {
            var notAddedKvp = new List <KeyValuePair <string, string> >();

            var workgroup = _workgroupRepository.GetNullableById(id);

            if (workgroup == null)
            {
                Message = "Workgroup not found.";
                this.RedirectToAction <WorkgroupController>(a => a.Index(false));
            }
            if (workgroupPeoplePostModel.Users == null)
            {
                workgroupPeoplePostModel.Users = new List <string>();
            }

            ModelState.Clear();

            workgroupPeoplePostModel.Role = _roleRepository.Queryable.FirstOrDefault(a => a.Id == roleFilter);
            //Ensure role picked is valid.
            if (workgroupPeoplePostModel.Role != null)
            {
                if (!_roleRepository.Queryable.Any(a => !a.IsAdmin && a.Id == workgroupPeoplePostModel.Role.Id))
                {
                    ModelState.AddModelError("Role", "Invalid Role Selected - don't mess with the query string!");
                }
            }

            if (!ModelState.IsValid)
            {
                var viewModel = WorgroupPeopleCreateModel.Create(_roleRepository, workgroup);

                if (workgroupPeoplePostModel.Role != null)
                {
                    viewModel.Role = workgroupPeoplePostModel.Role;
                }
                if (workgroupPeoplePostModel.Users != null && workgroupPeoplePostModel.Users.Count > 0)
                {
                    var users = new List <IdAndName>();
                    foreach (var user in workgroupPeoplePostModel.Users)
                    {
                        var temp = _userRepository.GetNullableById(user);
                        if (temp != null)
                        {
                            users.Add(new IdAndName(temp.Id, temp.FullName));
                        }
                        else
                        {
                            var ldapuser = _searchService.FindUser(user);
                            if (ldapuser != null)
                            {
                                users.Add(new IdAndName(ldapuser.LoginId, string.Format("{0} {1}", ldapuser.FirstName, ldapuser.LastName)));
                            }
                        }
                    }
                    viewModel.Users = users;
                }
                ViewBag.rolefilter = roleFilter;
                if (!string.IsNullOrWhiteSpace(roleFilter))
                {
                    viewModel.Role = _roleRepository.Queryable.SingleOrDefault(a => !a.IsAdmin && a.Id == roleFilter);
                }
                if (viewModel.Role.Level == 0)
                {
                    ViewBag.StepNumber = 3 + (5 - 1); // Ugliness because a reviewer is level zero
                }
                else
                {
                    ViewBag.StepNumber = 3 + (viewModel.Role.Level - 1);
                }
                return(View(viewModel));
            }

            int successCount   = 0;
            int failCount      = 0;
            int duplicateCount = 0;

            foreach (var u in workgroupPeoplePostModel.Users)
            {
                successCount = _workgroupService.TryToAddPeople(id, workgroupPeoplePostModel.Role, workgroup, successCount, u, ref failCount, ref duplicateCount, notAddedKvp);
            }



            #region Bulk Load Email
            successCount = _workgroupService.TryBulkLoadPeople(bulkEmail, true, id, workgroupPeoplePostModel.Role, workgroup, successCount, ref failCount, ref duplicateCount, notAddedKvp);

            //const string regexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b";

            //// Find matches
            //System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(bulkEmail, regexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //foreach(System.Text.RegularExpressions.Match match in matches)
            //{
            //    var temp = match.ToString().ToLower();
            //    successCount = _workgroupService.TryToAddPeople(id, workgroupPeoplePostModel.Role, workgroup, successCount, temp,  ref failCount, notAddedKvp);
            //}
            #endregion

            #region Bulk Load Kerb

            successCount = _workgroupService.TryBulkLoadPeople(bulkKerb, false, id, workgroupPeoplePostModel.Role, workgroup, successCount, ref failCount, ref duplicateCount, notAddedKvp);

            //const string regexPattern4Kerb = @"\b[A-Z0-9]{2,10}\b";

            //// Find matches
            //System.Text.RegularExpressions.MatchCollection matchesKerb = System.Text.RegularExpressions.Regex.Matches(bulkKerb, regexPattern4Kerb, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //foreach(System.Text.RegularExpressions.Match match in matchesKerb)
            //{
            //    var temp = match.ToString().ToLower();
            //    successCount = _workgroupService.TryToAddPeople(id, workgroupPeoplePostModel.Role, workgroup, successCount, temp, ref failCount, notAddedKvp);
            //}
            #endregion

            Message = string.Format("Successfully added {0} people to workgroup as {1}. {2} not added because of duplicated role or not found.", successCount,
                                    workgroupPeoplePostModel.Role.Name, failCount);

            if (failCount > 0 || successCount == 0)
            {
                if (failCount == 0)
                {
                    ModelState.AddModelError("Users", "Must add at least 1 user or Skip");
                }
                else if (failCount == duplicateCount)
                {
                    Message = string.Format("The {0} users you added already have the role {1}", duplicateCount,
                                            workgroupPeoplePostModel.Role.Name);
                    return(this.RedirectToAction(a => a.People(id, workgroupPeoplePostModel.Role.Id)));
                }


                var viewModel = WorgroupPeopleCreateModel.Create(_roleRepository, workgroup);
                viewModel.ErrorDetails = notAddedKvp;
                // ViewBag.DetailedMessage = notAddedSb.ToString();

                ViewBag.rolefilter = roleFilter;
                if (!string.IsNullOrWhiteSpace(roleFilter))
                {
                    viewModel.Role = _roleRepository.Queryable.SingleOrDefault(a => !a.IsAdmin && a.Id == roleFilter);
                }
                if (viewModel.Role.Level == 0)
                {
                    ViewBag.StepNumber = 3 + (5 - 1); // Ugliness because a reviewer is level zero
                }
                else
                {
                    ViewBag.StepNumber = 3 + (viewModel.Role.Level - 1);
                }
                return(View(viewModel));
            }

            Message = string.Format("Successfully added {0} people to workgroup as {1}.", successCount,
                                    workgroupPeoplePostModel.Role.Name);
            return(this.RedirectToAction(a => a.People(id, workgroupPeoplePostModel.Role.Id)));
        }