Esempio n. 1
0
        public void SearchOrganizationInformationViewModelTest_SendsEventWhenSearchResultIsReceived()
        {
            // Arrange
            PubSub <ObservableCollection <OrganizationModel> > .RegisterEvent(EventNames.SearchResultReceivedEvent, this.SearchResultReceivedEventHandler);

            SearchOrganizationInformationModel search = new SearchOrganizationInformationModel
            {
                SearchType = SearchType.OrganizationNumber,
                SearchText = "910021451"
            };

            SearchOrganizationInformationViewModel target = GetViewModel();

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

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

            // Asserts
            Assert.IsNotNull(this.searchResult);
        }
Esempio n. 2
0
        public void SearchOrganizationInformationViewModelTest_PhoneNumberSearch_SearchResultIsUpdated()
        {
            // Arrange
            PubSub <ObservableCollection <OrganizationModel> > .RegisterEvent(EventNames.SearchResultReceivedEvent, this.SearchResultReceivedEventHandler);

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

            IList <Organization> orgs = new List <Organization>();

            orgs.Add(new Organization());

            Mock <IRestQuery> query = new Mock <IRestQuery>();

            query.Setup(s => s.Get <Organization>(It.Is <KeyValuePair <string, string> >(pair => pair.Key == SearchType.PhoneNumber.ToString()))).Returns(orgs);

            SearchOrganizationInformationModel search = new SearchOrganizationInformationModel
            {
                SearchType = SearchType.Smart,
                SearchText = "47419641"
            };

            SearchOrganizationInformationViewModel target = new SearchOrganizationInformationViewModel(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);

            Assert.IsTrue(search.LabelText.Contains(Resources.PhoneNumber) && search.LabelText.Contains(search.SearchText));
        }
        private async void SearchCommandHandler(SearchOrganizationInformationModel obj)
        {
            this.logger.Debug(this.GetType().FullName + " Searching for: " + obj.SearchText + ", " + obj.SearchType);

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

            // Removing all whitespaces from the search string.
            string searchText = new string(obj.SearchText.Where(c => !char.IsWhiteSpace(c)).ToArray());

            if (string.IsNullOrEmpty(searchText))
            {
                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.SearchStartedEvent, this, new PubSubEventArgs <bool>(true));

            // After having removed the radio buttons where the user could select search type, search is always Smart, but the check
            // is kept in case the radio buttons comes back in a future release. For example as advanced search.
            SearchType searchType = obj.SearchType == SearchType.Smart ? IdentifySearchType(searchText) : obj.SearchType;

            IList <Organization> organizations = new List <Organization>();

            try
            {
                switch (searchType)
                {
                case SearchType.EMail:
                    obj.LabelText = string.Format(Resources.SearchLabelResultat, Resources.EMail + " " + searchText);
                    organizations = await this.GetOrganizations(searchType, searchText);

                    break;

                case SearchType.PhoneNumber:
                    obj.LabelText = string.Format(Resources.SearchLabelResultat, Resources.PhoneNumber + " " + searchText);
                    organizations = await this.GetOrganizations(searchType, searchText);

                    break;

                case SearchType.OrganizationNumber:
                    obj.LabelText = string.Format(Resources.SearchLabelResultat, Resources.OrganizationNumber + " " + searchText);
                    Organization organization = await this.GetOrganization(searchText);

                    organizations.Add(organization);
                    break;

                case SearchType.Smart:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (RestClientException rex)
            {
                this.logger.Error("Exception from the RestClient", rex);

                obj.LabelBrush = Brushes.Red;

                switch (rex.ErrorCode)
                {
                case RestClientErrorCodes.RemoteApiReturnedStatusBadRequest:
                    obj.LabelText = searchType == SearchType.OrganizationNumber
                            ? Resources.SearchLabelErrorOrganizationNotFound
                            : 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 <OrganizationModel> orgmodellist = organizations != null
                         ? this.mapper.Map <ICollection <Organization>, ObservableCollection <OrganizationModel> >(organizations)
                         : new ObservableCollection <OrganizationModel>();

            PubSub <ObservableCollection <OrganizationModel> > .RaiseEvent(
                EventNames.SearchResultReceivedEvent, this, new PubSubEventArgs <ObservableCollection <OrganizationModel> >(orgmodellist));
        }