View Javadoc

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