Search in sources :

Example 6 with SearchResultTab

use of uk.nhs.digital.intranet.model.SearchResultTab in project hippo by NHS-digital-website.

the class SearchPageComponent method searchPeople.

private void searchPeople(HstRequest request, List<SearchResultTab> searchResultTabs) {
    final String accessToken = (String) RequestContextProvider.get().getAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME);
    final SearchArea area = getAreaOption(request);
    final String searchQuery = getSearchQuery(request, false);
    final int peopleLimit = getComponentInfo(request).getPeopleLimit();
    int numberOfResults;
    if (!StringUtils.hasText(accessToken)) {
        numberOfResults = 0;
        request.setAttribute(REQUEST_ATTR_ACCESS_TOKEN_REQUIRED, true);
    } else if (!StringUtils.hasText(searchQuery)) {
        numberOfResults = 0;
        request.setAttribute(REQUEST_ATTR_MISSING_TERM_ERROR_MESSAGE, true);
    } else {
        try {
            List<Person> people = graphProvider.getPeople(searchQuery);
            int peopleCount = people.size();
            if (SearchArea.ALL.equals(area) && peopleCount > peopleLimit) {
                people = people.subList(0, peopleLimit);
                request.setAttribute(REQUEST_ATTR_MORE_PEOPLE, true);
            }
            numberOfResults = peopleCount;
            request.setAttribute(REQUEST_ATTR_PEOPLE_RESULTS, people);
        } catch (ProviderCommunicationException e) {
            numberOfResults = 0;
            request.setAttribute(REQUEST_ATTR_API_ERROR_MESSAGE, true);
        }
    }
    searchResultTabs.add(new SearchResultTab(SearchArea.PEOPLE, numberOfResults));
}
Also used : ProviderCommunicationException(uk.nhs.digital.intranet.model.exception.ProviderCommunicationException) SearchArea(uk.nhs.digital.intranet.enums.SearchArea) SearchResultTab(uk.nhs.digital.intranet.model.SearchResultTab)

Example 7 with SearchResultTab

use of uk.nhs.digital.intranet.model.SearchResultTab in project hippo by NHS-digital-website.

the class SearchPageComponentTest method peopleResultsEmptyIfQueryEmpty.

@Test
public void peopleResultsEmptyIfQueryEmpty() throws ProviderCommunicationException {
    MockHstRequest request = new MockHstRequest();
    request.setParameterMap("", Collections.emptyMap());
    request.addParameter(REQUEST_ATTR_QUERY, "");
    mockHstRequestContext.setAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME, "logged-into-microsoft-access-token");
    underTest.doBeforeRender(request, new MockHstResponse());
    Mockito.verify(graphProvider, Mockito.never()).getPeople(Mockito.nullable(String.class));
    assertNull(request.getAttribute(REQUEST_ATTR_PEOPLE_RESULTS));
    assertTrue(((List) request.getAttribute(REQUEST_ATTR_SEARCH_TABS)).contains(new SearchResultTab(SearchArea.PEOPLE, 0)));
}
Also used : MockHstRequest(org.hippoecm.hst.mock.core.component.MockHstRequest) MockHstResponse(org.hippoecm.hst.mock.core.component.MockHstResponse) SearchResultTab(uk.nhs.digital.intranet.model.SearchResultTab) Test(org.junit.Test)

Example 8 with SearchResultTab

use of uk.nhs.digital.intranet.model.SearchResultTab in project hippo by NHS-digital-website.

the class SearchPageComponentTest method peopleResultsEmptyIfNotLoggedIn.

@Test
public void peopleResultsEmptyIfNotLoggedIn() throws ProviderCommunicationException {
    MockHstRequest request = new MockHstRequest();
    request.setParameterMap("", Collections.emptyMap());
    request.addParameter(REQUEST_ATTR_QUERY, "queryString is here");
    mockHstRequestContext.setAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME, "");
    underTest.doBeforeRender(request, new MockHstResponse());
    Mockito.verify(graphProvider, Mockito.never()).getPeople(Mockito.nullable(String.class));
    assertNull(request.getAttribute(REQUEST_ATTR_PEOPLE_RESULTS));
    assertTrue(((List) request.getAttribute(REQUEST_ATTR_SEARCH_TABS)).contains(new SearchResultTab(SearchArea.PEOPLE, 0)));
}
Also used : MockHstRequest(org.hippoecm.hst.mock.core.component.MockHstRequest) MockHstResponse(org.hippoecm.hst.mock.core.component.MockHstResponse) SearchResultTab(uk.nhs.digital.intranet.model.SearchResultTab) Test(org.junit.Test)

Aggregations

SearchResultTab (uk.nhs.digital.intranet.model.SearchResultTab)8 MockHstRequest (org.hippoecm.hst.mock.core.component.MockHstRequest)5 MockHstResponse (org.hippoecm.hst.mock.core.component.MockHstResponse)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)2 SearchArea (uk.nhs.digital.intranet.enums.SearchArea)2 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)1 DefaultPagination (org.onehippo.cms7.essentials.components.paging.DefaultPagination)1 Task (uk.nhs.digital.intranet.beans.Task)1 Person (uk.nhs.digital.intranet.model.Person)1 ProviderCommunicationException (uk.nhs.digital.intranet.model.exception.ProviderCommunicationException)1