public void SearchOrganizationInformationViewModelTest_EMailSearch_SearchResultIsUpdated()
        {
            // Arrange
            PubSub <ObservableCollection <RoleModel> > .RegisterEvent(EventNames.RoleSearchResultReceivedEvent, this.RoleSearchResultReceivedEventHandler);

            Mock <ILog> logger = new Mock <ILog>();

            IList <Role> roles = new List <Role>();

            roles.Add(new Role());

            Mock <IRestQuery> query = new Mock <IRestQuery>();
            var ting = new List <KeyValuePair <string, string> >()
            {
            };

            query.Setup(s => s.Get <Role>(It.Is <List <KeyValuePair <string, string> > >(l => l.Count == 2))).Returns(roles);


            SearchRolesAndRightsInformationModel search = new SearchRolesAndRightsInformationModel
            {
                SubjectSearchText  = "16024400143",
                ReporteeSearchText = "910028146"
            };

            SearchRolesAndRightsInformationViewModel target = new SearchRolesAndRightsInformationViewModel(logger.Object, mapper, query.Object);

            // Act
            target.SearchCommand.Execute(search);

            // Wait for tasks to complete.
            Thread.Sleep(1000);

            // Assert
            query.VerifyAll();

            Assert.IsNotNull(this.searchResult);
            Assert.IsNotNull(target.SearchCommand);
            Assert.IsNotNull(target.Model);
        }
        public void SearchRolesAndRightsInformationViewModelTest_SendsEventWhenSearchResultIsReceived()
        {
            // Arrange
            PubSub <ObservableCollection <RoleModel> > .RegisterEvent(EventNames.RoleSearchResultReceivedEvent, this.RoleSearchResultReceivedEventHandler);

            SearchRolesAndRightsInformationModel search = new SearchRolesAndRightsInformationModel
            {
                SubjectSearchText  = "16024400143",
                ReporteeSearchText = "910028146"
            };

            SearchRolesAndRightsInformationViewModel target = GetViewModel();

            // Act
            target.SearchCommand.Execute(search);

            // Wait for tasks to complete.
            Thread.Sleep(1000);

            // Asserts
            Assert.IsNotNull(this.searchResult);
        }
        private async void SearchCommandHandler(SearchRolesAndRightsInformationModel obj)
        {
            this.logger.Debug(this.GetType().FullName + " Searching for subject " + obj.SubjectSearchText + " og reportee " + obj.ReporteeSearchText);

            obj.LabelText  = string.Empty;
            obj.LabelBrush = Brushes.Green;

            // Removing all whitespaces from the search strings.
            string subjectSearchText  = new string(obj.SubjectSearchText.Where(c => !char.IsWhiteSpace(c)).ToArray());
            string reporteeSearchText = new string(obj.ReporteeSearchText.Where(c => !char.IsWhiteSpace(c)).ToArray());

            if (string.IsNullOrEmpty(subjectSearchText) || string.IsNullOrEmpty(reporteeSearchText))
            {
                obj.LabelText  = Resources.SearchLabelEmptySearch;
                obj.LabelBrush = Brushes.Red;

                // Preventing an empty search. It takes a lot of time and the result is useless.
                return;
            }

            PubSub <bool> .RaiseEvent(EventNames.RoleSearchStartedEvent, this, new PubSubEventArgs <bool>(true));

            SearchType subjectSearchType  = IdentifySearchType(subjectSearchText);
            SearchType reporteeSearchType = IdentifySearchType(reporteeSearchText);

            IList <Role> roles = new List <Role>();

            try
            {
                if (subjectSearchType == SearchType.Unknown || reporteeSearchType == SearchType.Unknown)
                {
                    obj.LabelBrush = Brushes.Red;
                    obj.LabelText  = Resources.SearchLabelErrorInvalidInput;
                }
                else
                {
                    roles = await this.GetRoles(subjectSearchText, reporteeSearchText);
                }
            }
            catch (RestClientException rex)
            {
                this.logger.Error("Exception from the RestClient", rex);

                obj.LabelBrush = Brushes.Red;

                switch (rex.ErrorCode)
                {
                case RestClientErrorCodes.RemoteApiReturnedStatusBadRequest:
                    obj.LabelText = Resources.SearchLabelErrorGeneralError;
                    break;

                case RestClientErrorCodes.RemoteApiReturnedStatusUnauthorized:
                    obj.LabelText = Resources.SearchLabelErrorUnauthorized;
                    break;

                case RestClientErrorCodes.RemoteApiReturnedStatusForbidden:
                    obj.LabelText = Resources.SearchLabelErrorForbidden;
                    break;

                case RestClientErrorCodes.RemoteApiReturnedStatusNotFound:
                    obj.LabelText = Resources.SearchLabelErrorOrganizationNotFound;
                    break;

                default:
                    obj.LabelText = Resources.SearchLabelErrorGeneralError;
                    break;
                }
            }

            ObservableCollection <RoleModel> rolesmodellist = roles != null
                         ? this.mapper.Map <ICollection <Role>, ObservableCollection <RoleModel> >(roles)
                         : new ObservableCollection <RoleModel>();

            PubSub <ObservableCollection <RoleModel> > .RaiseEvent(
                EventNames.RoleSearchResultReceivedEvent, this, new PubSubEventArgs <ObservableCollection <RoleModel> >(rolesmodellist));
        }