View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.workflow;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
20  import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
21  import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
22  import org.kuali.rice.kew.doctype.bo.DocumentType;
23  import org.kuali.rice.kew.engine.RouteContext;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.krad.UserSession;
27  import org.kuali.rice.krad.document.Document;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.test.document.SearchAttributeIndexTestDocument;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.test.KRADTestCase;
32  
33  import static org.junit.Assert.assertEquals;
34  import org.junit.Assert;
35  
36  /**
37   * tests that a document, which goes through a regular or blanket approval process, is indexed correctly
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   * @deprecated KNS test class, convert to KRAD equivalent if applicable.
42   */
43  @Deprecated
44  public class SearchAttributeIndexRequestTest extends KRADTestCase {
45  
46      private static final String SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE = "SearchAttributeIndexTestDocument";
47  	
48  	private enum DOCUMENT_FIXTURE {
49  		NORMAL_DOCUMENT("hippo","routing");
50  		
51  		private String constantString;
52  		private String routingString;
53  		private DOCUMENT_FIXTURE(String constantString, String routingString) {
54  			this.constantString = constantString;
55  			this.routingString = routingString;
56  		}
57  		
58  		public Document getDocument() throws Exception {
59              Document document = KRADServiceLocatorWeb.getDocumentService().getNewDocument(SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
60  			SearchAttributeIndexTestDocument searchAttributeIndexTestDocument = (SearchAttributeIndexTestDocument) document;
61              searchAttributeIndexTestDocument.initialize(constantString, routingString);
62  
63              return searchAttributeIndexTestDocument;
64  		}
65  	}
66  	
67  	/**
68  	 * Tests that a document, which goes through a regular approval process, is indexed correctly
69  	 */
70  	@Test
71  	public void regularApproveTest() throws Exception {
72          final String principalName = "quickstart";
73          final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
74          GlobalVariables.setUserSession(new UserSession(principalName));
75          RouteContext.clearCurrentRouteContext();
76  
77  		Document document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument();
78  		document.getDocumentHeader().setDocumentDescription("Routed SAIndexTestDoc");
79  		final DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
80  
81          assertApproveAttributes(document, documentType, principalId);
82  	}
83  
84      protected void assertApproveAttributes(Document document, DocumentType documentType, String principalId) throws Exception {
85          KRADServiceLocatorWeb.getDocumentService().routeDocument(document, "Routed SearchAttributeIndexTestDocument", null);
86  
87          assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount",
88                  new String[] {"1","0","2","7"},
89                  new int[] {1, 0, 0, 0}
90          );
91  
92          assertDDSearchableAttributesWork(documentType, principalId, "constantString",
93                  new String[] {"hippo","monkey"},
94                  new int[] {1, 0}
95          );
96  
97          assertDDSearchableAttributesWork(documentType, principalId, "routedString",
98                  new String[] {"routing","hippo"},
99                  new int[] {1, 0}
100         );
101 
102         GlobalVariables.setUserSession(new UserSession("user1"));
103         document = KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(document.getDocumentNumber());
104         KRADServiceLocatorWeb.getDocumentService().approveDocument(document, "User1 approved document", null);
105 
106         assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount",
107                 new String[] {"1","0","2","7"},
108                 new int[] {0, 0, 1, 0}
109         );
110 
111         assertDDSearchableAttributesWork(documentType, principalId, "constantString",
112                 new String[] {"hippo","monkey"},
113                 new int[] {1, 0}
114         );
115 
116         assertDDSearchableAttributesWork(documentType, principalId, "routedString",
117                 new String[] {"routing","hippo"},
118                 new int[] {1, 0}
119         );
120 
121         GlobalVariables.setUserSession(new UserSession("user2"));
122         document = KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(document.getDocumentNumber());
123         KRADServiceLocatorWeb.getDocumentService().approveDocument(document, "User1 approved document", null);
124 
125         assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount",
126                 new String[] {"1","0","2","3","4","7"},
127                 new int[] {0, 0, 0, 1, 0, 0}
128         );
129 
130         assertDDSearchableAttributesWork(documentType, principalId, "constantString",
131                 new String[] {"hippo","monkey"},
132                 new int[] {1, 0}
133         );
134 
135         assertDDSearchableAttributesWork(documentType, principalId, "routedString",
136                 new String[] {"routing","hippo"},
137                 new int[] {1, 0}
138         );
139 
140         GlobalVariables.setUserSession(new UserSession("user3"));
141         document = KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(document.getDocumentNumber());
142         KRADServiceLocatorWeb.getDocumentService().approveDocument(document, "User3 approved document", null);
143 
144         assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount",
145                 new String[] {"1","0","2","3","4","7"},
146                 new int[] {0, 0, 0, 1, 0, 0}
147         );
148 
149         assertDDSearchableAttributesWork(documentType, principalId, "constantString",
150                 new String[] {"hippo","monkey"},
151                 new int[] {1, 0}
152         );
153 
154         assertDDSearchableAttributesWork(documentType, principalId, "routedString",
155                 new String[] {"routing","hippo"},
156                 new int[] {1, 0}
157         );
158 
159         GlobalVariables.setUserSession(null);
160     }
161 	
162 	/**
163 	 * Tests that a blanket approved document is indexed correctly
164 	 */
165 	@Test
166 	public void blanketApproveTest() throws Exception {
167         final String principalName = "admin";
168         final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
169         GlobalVariables.setUserSession(new UserSession(principalName));
170 
171         Document document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument();
172         document.getDocumentHeader().setDocumentDescription("Blanket Approved SAIndexTestDoc");
173         final DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
174 
175         assertBlanketApproveAttributes(document, documentType, principalId);
176 	}
177 
178     protected void assertBlanketApproveAttributes(Document document, DocumentType documentType, String principalId) throws Exception {
179         KRADServiceLocatorWeb.getDocumentService().blanketApproveDocument(document, "Blanket Approved SearchAttributeIndexTestDocument", null);
180 
181         assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount",
182                 new String[] {"1","0","2","3","7"},
183                 new int[] {0, 0, 0, 1, 0}
184         );
185 
186         assertDDSearchableAttributesWork(documentType, principalId, "constantString",
187                 new String[] {"hippo","monkey"},
188                 new int[] {1, 0}
189         );
190 
191         assertDDSearchableAttributesWork(documentType, principalId, "routedString",
192                 new String[] {"routing","hippo"},
193                 new int[] {1, 0}
194         );
195 
196         GlobalVariables.setUserSession(null);
197     }
198 	
199 	/**
200      * A convenience method for testing wildcards on data dictionary searchable attributes.
201      *
202      * @param docType The document type containing the attributes.
203      * @param principalId The ID of the user performing the search.
204      * @param fieldName The name of the field on the test document.
205      * @param searchValues The search expressions to test. Has to be a String array (for regular fields) or a String[] array (for multi-select fields).
206      * @param resultSizes The number of expected documents to be returned by the search; use -1 to indicate that an error should have occurred.
207      * @throws Exception
208      */
209     private void assertDDSearchableAttributesWork(DocumentType docType, String principalId, String fieldName, Object[] searchValues,
210     		int[] resultSizes) throws Exception {
211     	if (!(searchValues instanceof String[]) && !(searchValues instanceof String[][])) {
212     		throw new IllegalArgumentException("'searchValues' parameter has to be either a String[] or a String[][]");
213     	}
214     	DocumentSearchCriteria.Builder criteria = null;
215         DocumentSearchResults results = null;
216         DocumentSearchService docSearchService = KEWServiceLocator.getDocumentSearchService();
217         for (int i = 0; i < resultSizes.length; i++) {
218         	criteria = DocumentSearchCriteria.Builder.create();
219         	criteria.setDocumentTypeName(docType.getName());
220         	criteria.addDocumentAttributeValue(fieldName, searchValues[i].toString());
221         	try {
222         		results = docSearchService.lookupDocuments(principalId, criteria.build());
223         		if (resultSizes[i] < 0) {
224         			Assert.fail(fieldName + "'s search at loop index " + i + " should have thrown an exception");
225         		}
226         		if(resultSizes[i] != results.getSearchResults().size()){
227         			assertEquals(fieldName + "'s search results at loop index " + i + " returned the wrong number of documents.", resultSizes[i], results.getSearchResults().size());
228         		}
229         	} catch (Exception ex) {
230         		if (resultSizes[i] >= 0) {
231         			Assert.fail(fieldName + "'s search at loop index " + i + " should not have thrown an exception");
232         		}
233         	}
234         	GlobalVariables.clear();
235         }
236     }
237 }