View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.docsearch;
18  
19  import org.junit.Test;
20  import org.kuali.rice.core.util.ClassLoaderUtils;
21  import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
22  import org.kuali.rice.kew.doctype.bo.DocumentType;
23  import org.kuali.rice.kew.doctype.service.DocumentTypeService;
24  import org.kuali.rice.kew.dto.NetworkIdDTO;
25  import org.kuali.rice.kew.dto.WorkflowAttributeDefinitionDTO;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kew.service.WorkflowDocument;
28  import org.kuali.rice.kew.util.KEWConstants;
29  import org.kuali.rice.kew.util.Utilities;
30  import org.kuali.rice.kim.bo.Person;
31  import org.kuali.rice.kim.service.KIMServiceLocator;
32  import org.kuali.rice.kns.bo.Parameter;
33  import org.kuali.rice.kns.service.KNSServiceLocator;
34  import org.kuali.rice.kns.util.KNSConstants;
35  
36  
37  /**
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class CustomDocumentSearchGeneratorTest extends DocumentSearchTestBase {
42  //	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomSearchAttributesTest.class);
43  
44      protected void loadTestData() throws Exception {
45          loadXmlFile("SearchAttributeConfig.xml");
46      }
47  
48      @Test public void testCustomDocumentSearchGeneratorUse() throws Exception {
49      	DocumentType docType = ((DocumentTypeService)KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName("SearchDocType");
50      	assertEquals("The document search Generator class is incorrect.",StandardDocumentSearchGenerator.class,(ClassLoaderUtils.unwrapFromProxy(docType.getDocumentSearchGenerator())).getClass());
51      	docType = ((DocumentTypeService)KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName("SearchDocType_DefaultCustomProcessor");
52      	assertEquals("The document search Generator class is incorrect.",CustomDocumentSearchGenerator.class,(ClassLoaderUtils.unwrapFromProxy(docType.getDocumentSearchGenerator())).getClass());
53      }
54  
55  	private DocumentType getValidDocumentType(String documentTypeFullName) {
56  		if (Utilities.isEmpty(documentTypeFullName)) {
57  			return null;
58  		}
59  		DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeFullName);
60  		if (docType == null) {
61  			throw new RuntimeException("No Valid Document Type Found for document type name '" + documentTypeFullName + "'");
62  		} else {
63  			return docType;
64  		}
65  	}
66  
67      @Test public void testCustomDocSearchGeneratorResultSetLimit() throws Exception {
68          String documentTypeName = "SearchDocType_DefaultCustomProcessor";
69          String userNetworkId = "rkirkend";
70          Person user = KIMServiceLocator.getPersonService().getPersonByPrincipalName(userNetworkId);
71  
72  
73          DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO();
74          criteria.setDocTypeFullName(documentTypeName);
75          criteria.addSearchableAttribute(createSearchAttributeCriteriaComponent(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE, KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName)));
76  
77          // adjust the app constant to be greater than custom generator value
78          adjustResultSetCapApplicationConstantValue(CustomDocumentSearchGenerator.RESULT_SET_LIMIT + 1);
79          KEWServiceLocator.getDocumentSearchService().getList(user.getPrincipalId(), criteria);
80          assertEquals("Criteria threshold should equal custom generator class threshold", CustomDocumentSearchGenerator.RESULT_SET_LIMIT, criteria.getThreshold().intValue());
81  
82          // adjust the app constant to be less than custom generator value
83          int newLimit = CustomDocumentSearchGenerator.RESULT_SET_LIMIT - 1;
84          adjustResultSetCapApplicationConstantValue(newLimit);
85          KEWServiceLocator.getDocumentSearchService().getList(user.getPrincipalId(), criteria);
86          assertEquals("Criteria threshold should equal system result set threshold", newLimit, criteria.getThreshold().intValue());
87  
88          // delete the parameter
89          KNSServiceLocator.getBusinessObjectService().delete(KNSServiceLocator.getParameterService().retrieveParameter(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.DOCUMENT_SEARCH_DETAIL_TYPE, KEWConstants.DOC_SEARCH_RESULT_CAP));
90  
91          // old parameter value will still be cached, let's flush the cache
92          KNSServiceLocator.getParameterService().clearCache();
93          
94          KEWServiceLocator.getDocumentSearchService().getList(user.getPrincipalId(), criteria);
95          assertEquals("Criteria threshold should equal custom generator class threshold", CustomDocumentSearchGenerator.RESULT_SET_LIMIT, criteria.getThreshold().intValue());
96      }
97  
98      private void adjustResultSetCapApplicationConstantValue(Integer newValue) {
99          Parameter ps = KNSServiceLocator.getParameterService().retrieveParameter(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.DOCUMENT_SEARCH_DETAIL_TYPE, KEWConstants.DOC_SEARCH_RESULT_CAP);
100         if (ps == null) {
101             ps = new Parameter( KEWConstants.DOC_SEARCH_RESULT_CAP, newValue.toString(), "A" );
102         }
103         ps.setParameterNamespaceCode(KEWConstants.KEW_NAMESPACE);
104         ps.setParameterName(KEWConstants.DOC_SEARCH_RESULT_CAP);
105         ps.setParameterValue(newValue.toString());
106         ps.setParameterTypeCode("CONFG");
107         //ps.setParameterWorkgroupName(KEWConstants.WORKFLOW_SUPER_USER_WORKGROUP_NAME);
108         ps.setParameterDetailTypeCode(KNSConstants.DetailTypes.DOCUMENT_SEARCH_DETAIL_TYPE);
109         KNSServiceLocator.getBusinessObjectService().save(ps);
110     }
111 
112     /**
113      * Tests function of adding extra document type names to search including using searchable attributes
114      * that may or may not exist on all the document type names being searched on.
115      *
116      * @throws Exception
117      */
118     @Test public void testSearchOnExtraDocType() throws Exception {
119         String userNetworkId = "rkirkend";
120         DocumentSearchService docSearchService = (DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE);
121         Person user = KIMServiceLocator.getPersonService().getPersonByPrincipalName(userNetworkId);
122 
123         String documentTypeName1 = "SearchDocType_DefaultCustomProcessor";
124         WorkflowDocument workDoc_Matching1 = new WorkflowDocument(new NetworkIdDTO(userNetworkId), documentTypeName1);
125     	DocumentType docType1 = ((DocumentTypeService)KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName(documentTypeName1);
126         WorkflowAttributeDefinitionDTO stringXMLDef1 = new WorkflowAttributeDefinitionDTO("SearchableAttributeVisible");
127         stringXMLDef1.addProperty(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE);
128         workDoc_Matching1.addSearchableDefinition(stringXMLDef1);
129         workDoc_Matching1.routeDocument("");
130 
131         String documentTypeName2 = "SearchDocType_DefaultCustomProcessor_2";
132         WorkflowDocument workDoc_Matching2 = new WorkflowDocument(new NetworkIdDTO(userNetworkId), documentTypeName2);
133     	DocumentType docType2 = ((DocumentTypeService)KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName(documentTypeName2);
134         WorkflowAttributeDefinitionDTO stringXMLDef2 = new WorkflowAttributeDefinitionDTO("SearchableAttributeVisible");
135         stringXMLDef2.addProperty(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE);
136         workDoc_Matching2.addSearchableDefinition(stringXMLDef2);
137         workDoc_Matching2.routeDocument("");
138 
139         // do search with attribute using doc type 1... make sure both docs are returned
140         DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO();
141         criteria.setDocTypeFullName(documentTypeName1);
142         criteria.addSearchableAttribute(createSearchAttributeCriteriaComponent(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE, docType1));
143         DocumentSearchResultComponents result = docSearchService.getList(user.getPrincipalId(), criteria);
144         assertEquals("Search results should have one document.", 2, result.getSearchResults().size());
145 
146         // do search with attribute using doc type 2... make sure both docs are returned
147         criteria = new DocSearchCriteriaDTO();
148         criteria.setDocTypeFullName(documentTypeName2);
149         criteria.addSearchableAttribute(createSearchAttributeCriteriaComponent(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE, docType2));
150         result = docSearchService.getList(user.getPrincipalId(), criteria);
151         assertEquals("Search results should have one document.", 2, result.getSearchResults().size());
152 
153         // do search without attribute using doc type 1... make sure both docs are returned
154         criteria = new DocSearchCriteriaDTO();
155         criteria.setDocTypeFullName(documentTypeName1);
156         result = docSearchService.getList(user.getPrincipalId(), criteria);
157         assertEquals("Search results should have one document.", 2, result.getSearchResults().size());
158 
159         // do search without attribute using doc type 2... make sure both docs are returned
160         criteria = new DocSearchCriteriaDTO();
161         criteria.setDocTypeFullName(documentTypeName2);
162         result = docSearchService.getList(user.getPrincipalId(), criteria);
163         assertEquals("Search results should have one document.", 2, result.getSearchResults().size());
164 
165         String documentTypeName3 = "SearchDocType_DefaultCustomProcessor_3";
166         WorkflowDocument workDoc_Matching3 = new WorkflowDocument(new NetworkIdDTO(userNetworkId), documentTypeName3);
167     	DocumentType docType3 = ((DocumentTypeService)KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName(documentTypeName3);
168         WorkflowAttributeDefinitionDTO stringXMLDef3 = new WorkflowAttributeDefinitionDTO("SearchableAttributeVisible");
169         stringXMLDef3.addProperty(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE);
170         workDoc_Matching3.addSearchableDefinition(stringXMLDef3);
171         workDoc_Matching3.routeDocument("");
172 
173         // do search with attribute using doc type 3... make sure 1 doc is returned
174         criteria = new DocSearchCriteriaDTO();
175         criteria.setDocTypeFullName(documentTypeName3);
176         criteria.addSearchableAttribute(createSearchAttributeCriteriaComponent(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE, docType3));
177         result = docSearchService.getList(user.getPrincipalId(), criteria);
178         assertEquals("Search results should have one document.", 1, result.getSearchResults().size());
179 
180         // do search without attribute using doc type 3... make sure 1 doc is returned
181         criteria = new DocSearchCriteriaDTO();
182         criteria.setDocTypeFullName(documentTypeName3);
183         result = docSearchService.getList(user.getPrincipalId(), criteria);
184         assertEquals("Search results should have one document.", 1, result.getSearchResults().size());
185 
186         WorkflowDocument workDoc_NonMatching2 = new WorkflowDocument(new NetworkIdDTO(userNetworkId), documentTypeName2);
187         WorkflowAttributeDefinitionDTO stringXMLDef1a = new WorkflowAttributeDefinitionDTO("SearchableAttributeVisible");
188         // TODO delyea - adding underscore below invalidates via REGEX but doesn't blow up on route or addSearchable?
189         String searchAttributeValue = TestXMLSearchableAttributeString.SEARCH_STORAGE_VALUE + "nonMatching";
190         stringXMLDef1a.addProperty(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, searchAttributeValue);
191         workDoc_NonMatching2.addSearchableDefinition(stringXMLDef1a);
192         workDoc_NonMatching2.routeDocument("");
193 
194         // do search with attribute using doc type 1... make sure 1 doc is returned
195         criteria = new DocSearchCriteriaDTO();
196         criteria.setDocTypeFullName(documentTypeName1);
197         criteria.addSearchableAttribute(createSearchAttributeCriteriaComponent(TestXMLSearchableAttributeString.SEARCH_STORAGE_KEY, searchAttributeValue, docType1));
198         result = docSearchService.getList(user.getPrincipalId(), criteria);
199         assertEquals("Search results should have one document.", 1, result.getSearchResults().size());
200 
201         // do search without attribute using doc type 1... make sure all 3 docs are returned
202         criteria = new DocSearchCriteriaDTO();
203         criteria.setDocTypeFullName(documentTypeName1);
204         result = docSearchService.getList(user.getPrincipalId(), criteria);
205         assertEquals("Search results should have one document.", 3, result.getSearchResults().size());
206     }
207 }