View Javadoc

1   /**
2    * Copyright 2005-2011 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.krad.workflow;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.uif.RemotableAttributeField;
20  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
21  import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
22  import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
23  import org.kuali.rice.kew.doctype.bo.DocumentType;
24  import org.kuali.rice.kew.engine.RouteContext;
25  import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
26  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.krad.UserSession;
30  import org.kuali.rice.krad.service.DocumentService;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33  import org.kuali.rice.krad.test.document.SearchAttributeIndexTestDocument;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.test.KRADTestCase;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.fail;
39  
40  /**
41   * This is a description of what this class does - jksmith don't forget to fill this in. 
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   *
45   */
46  public class SearchAttributeIndexRequestTest extends KRADTestCase {
47  	static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchAttributeIndexRequestTest.class);
48  	final static String SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE = "SearchAttributeIndexTestDocument";
49  	
50  	enum DOCUMENT_FIXTURE {
51  		NORMAL_DOCUMENT("hippo","routing");
52  		
53  		private String constantString;
54  		private String routingString;
55  		private DOCUMENT_FIXTURE(String constantString, String routingString) {
56  			this.constantString = constantString;
57  			this.routingString = routingString;
58  		}
59  		
60  		public SearchAttributeIndexTestDocument getDocument(DocumentService documentService) throws Exception {
61  			SearchAttributeIndexTestDocument doc = (SearchAttributeIndexTestDocument)documentService.getNewDocument(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
62  			doc.initialize(constantString, routingString);
63  			return doc;
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  		LOG.warn("message.delivery state: "+ KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
73                  "message.delivery"));
74  		
75  		final DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
76  		final String principalName = "quickstart";
77          final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
78          GlobalVariables.setUserSession(new UserSession(principalName));
79          RouteContext.clearCurrentRouteContext();
80  
81  		SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
82  		document.getDocumentHeader().setDocumentDescription("Routed SAIndexTestDoc");
83  		final String documentNumber = document.getDocumentNumber();
84  		final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
85  		
86  		documentService.routeDocument(document, "Routed SearchAttributeIndexTestDocument", null);
87  		
88  		document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
89  		DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
90  						
91  		assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
92  				new String[] {"1","0","2","7"},
93  				new int[] {1, 0, 0, 0}
94  		);
95  		
96  		assertDDSearchableAttributesWork(docType,principalId,"constantString",
97  				new String[] {"hippo","monkey"},
98  				new int[] {1, 0}
99  		);
100 		
101 		assertDDSearchableAttributesWork(docType,principalId,"routedString",
102 				new String[] {"routing","hippo"},
103 				new int[] {1, 0}
104 		);
105 		
106 		GlobalVariables.setUserSession(new UserSession("user1"));
107 		document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
108 		documentService.approveDocument(document, "User1 approved document", null);
109 		
110 		routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
111 						
112 		assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
113 				new String[] {"1","0","2","7"},
114 				new int[] {0, 0, 1, 0}
115 		);
116 		
117 		assertDDSearchableAttributesWork(docType,principalId,"constantString",
118 				new String[] {"hippo","monkey"},
119 				new int[] {1, 0}
120 		);
121 		
122 		assertDDSearchableAttributesWork(docType,principalId,"routedString",
123 				new String[] {"routing","hippo"},
124 				new int[] {1, 0}
125 		);
126 		
127 		LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
128 		
129 		GlobalVariables.setUserSession(new UserSession("user2"));
130 		document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
131 		documentService.approveDocument(document, "User1 approved document", null);
132 		
133 		routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
134 				
135 		assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
136 				new String[] {"1","0","2","3","4","7"},
137 				new int[] {0, 0, 0, 1, 0, 0}
138 		);
139 		
140 		assertDDSearchableAttributesWork(docType,principalId,"constantString",
141 				new String[] {"hippo","monkey"},
142 				new int[] {1, 0}
143 		);
144 		
145 		assertDDSearchableAttributesWork(docType,principalId,"routedString",
146 				new String[] {"routing","hippo"},
147 				new int[] {1, 0}
148 		);
149 		
150 		LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
151 		
152 		GlobalVariables.setUserSession(new UserSession("user3"));
153 		document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
154 		documentService.approveDocument(document, "User3 approved document", null);
155 		
156 		routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
157 						
158 		assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
159 				new String[] {"1","0","2","3","4","7"},
160 				new int[] {0, 0, 0, 1, 0, 0}
161 		);
162 		
163 		assertDDSearchableAttributesWork(docType,principalId,"constantString",
164 				new String[] {"hippo","monkey"},
165 				new int[] {1, 0}
166 		);
167 		
168 		assertDDSearchableAttributesWork(docType,principalId,"routedString",
169 				new String[] {"routing","hippo"},
170 				new int[] {1, 0}
171 		);
172 		
173 		LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
174 		
175 		GlobalVariables.setUserSession(null);
176 	}
177 	
178 	/**
179 	 * Tests that a blanket approved document is indexed correctly
180 	 */
181 	@Test
182 	public void blanketApproveTest() throws Exception {
183 		LOG.warn("message.delivery state: "+ KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
184                 "message.delivery"));
185 		
186 		final DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
187 		final String principalName = "admin";
188         final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
189         GlobalVariables.setUserSession(new UserSession(principalName));
190 
191 		SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
192 		document.getDocumentHeader().setDocumentDescription("Blanket Approved SAIndexTestDoc");
193 		final String documentNumber = document.getDocumentNumber();
194 		final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
195 				
196 		documentService.blanketApproveDocument(document, "Blanket Approved SearchAttributeIndexTestDocument", null);
197 		
198 		document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
199 		DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
200 						
201 		assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
202 				new String[] {"1","0","2","3","7"},
203 				new int[] {0, 0, 0, 1, 0}
204 		);
205 		
206 		assertDDSearchableAttributesWork(docType,principalId,"constantString",
207 				new String[] {"hippo","monkey"},
208 				new int[] {1, 0}
209 		);
210 		
211 		assertDDSearchableAttributesWork(docType,principalId,"routedString",
212 				new String[] {"routing","hippo"},
213 				new int[] {1, 0}
214 		);
215 		
216 		LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
217 		
218 		GlobalVariables.setUserSession(null);
219 	}
220 	
221 	/**
222      * A convenience method for testing wildcards on data dictionary searchable attributes.
223      *
224      * @param docType The document type containing the attributes.
225      * @param principalId The ID of the user performing the search.
226      * @param fieldName The name of the field on the test document.
227      * @param searchValues The search expressions to test. Has to be a String array (for regular fields) or a String[] array (for multi-select fields).
228      * @param resultSizes The number of expected documents to be returned by the search; use -1 to indicate that an error should have occurred.
229      * @throws Exception
230      */
231     private void assertDDSearchableAttributesWork(DocumentType docType, String principalId, String fieldName, Object[] searchValues,
232     		int[] resultSizes) throws Exception {
233     	if (!(searchValues instanceof String[]) && !(searchValues instanceof String[][])) {
234     		throw new IllegalArgumentException("'searchValues' parameter has to be either a String[] or a String[][]");
235     	}
236     	DocumentSearchCriteria.Builder criteria = null;
237         DocumentSearchResults results = null;
238         DocumentSearchService docSearchService = KEWServiceLocator.getDocumentSearchService();
239         for (int i = 0; i < resultSizes.length; i++) {
240         	criteria = DocumentSearchCriteria.Builder.create();
241         	criteria.setDocumentTypeName(docType.getName());
242         	criteria.addDocumentAttributeValue(fieldName, searchValues[i].toString());
243         	try {
244         		results = docSearchService.lookupDocuments(principalId, criteria.build());
245         		if (resultSizes[i] < 0) {
246         			fail(fieldName + "'s search at loop index " + i + " should have thrown an exception");
247         		}
248         		if(resultSizes[i] != results.getSearchResults().size()){
249         			assertEquals(fieldName + "'s search results at loop index " + i + " returned the wrong number of documents.", resultSizes[i], results.getSearchResults().size());
250         		}
251         	} catch (Exception ex) {
252         		if (resultSizes[i] >= 0) {
253         			fail(fieldName + "'s search at loop index " + i + " should not have thrown an exception");
254         		}
255         	}
256         	GlobalVariables.clear();
257         }
258     }
259 }