View Javadoc

1   package org.kuali.student.r2.common.class1.search;
2   
3   import org.kuali.student.r2.common.dto.ContextInfo;
4   import org.kuali.student.r2.common.exceptions.DoesNotExistException;
5   import org.kuali.student.r2.common.exceptions.InvalidParameterException;
6   import org.kuali.student.r2.common.exceptions.MissingParameterException;
7   import org.kuali.student.r2.common.exceptions.OperationFailedException;
8   import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
9   import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
10  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
11  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
12  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
13  import org.kuali.student.r2.core.search.service.SearchService;
14  
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  /**
22   * Mock implementation of SearchService which can be configured by providing a
23   * SearchRequestInfo and the desired SearchResultsInfo via addSearchResult().
24   *
25   * This is for use in unit tests.
26   */
27  public class SearchServiceMockImpl implements SearchService {
28  
29      /*
30       *  Storage for search results. Key is build from search key plus concatenated list of params and values.
31       */
32      private Map<String, SearchResultInfo> searchResults = new HashMap<String, SearchResultInfo>();
33  
34      @Override
35      public List<TypeInfo> getSearchTypes(ContextInfo contextInfo)
36              throws InvalidParameterException, MissingParameterException, OperationFailedException {
37          return null;
38      }
39  
40      @Override
41      public TypeInfo getSearchType(String searchTypeKey, ContextInfo contextInfo)
42              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
43          return null;
44      }
45  
46      @Override
47      public SearchResultInfo search(SearchRequestInfo searchRequestInfo, ContextInfo contextInfo)
48              throws MissingParameterException, InvalidParameterException, OperationFailedException, PermissionDeniedException {
49          String searchResultsKey = makeSearchResultsKey(searchRequestInfo);
50          return searchResults.get(searchResultsKey);
51      }
52  
53      /**
54       * Data loader method for mock implementation.
55       * @param searchRequestInfo
56       * @param searchResultInfo
57       */
58      public void addSearchResult(SearchRequestInfo searchRequestInfo, SearchResultInfo searchResultInfo) {
59          String key = makeSearchResultsKey(searchRequestInfo);
60          searchResults.put(key, searchResultInfo);
61      }
62  
63      /**
64       * Build a search results key from a SearchRequestInfo in the
65       * format: "searchKey[searchParam[value,...], searchParam[value,...]]" where params are in alphabetical order.
66       * @param searchRequestInfo The search request.
67       * @return A key derived from the SearchRequestInfo.
68       */
69      private String makeSearchResultsKey(SearchRequestInfo searchRequestInfo) {
70          List<String> paramsAndValues = new ArrayList<String>();
71  
72          for (SearchParamInfo param : searchRequestInfo.getParams()) {
73              paramsAndValues.add(param.getKey() + "" + param.getValues().toString());
74          }
75  
76          Collections.sort(paramsAndValues);
77          return String.format("%s%s", searchRequestInfo.getSearchKey(), paramsAndValues);
78      }
79  }