001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kns.workflow; 017 018 import org.junit.Test; 019 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria; 020 import org.kuali.rice.kew.api.document.search.DocumentSearchResults; 021 import org.kuali.rice.kew.docsearch.service.DocumentSearchService; 022 import org.kuali.rice.kew.doctype.bo.DocumentType; 023 import org.kuali.rice.kew.engine.RouteContext; 024 import org.kuali.rice.kew.service.KEWServiceLocator; 025 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 026 import org.kuali.rice.krad.UserSession; 027 import org.kuali.rice.krad.document.Document; 028 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 029 import org.kuali.rice.krad.test.document.SearchAttributeIndexTestDocument; 030 import org.kuali.rice.krad.util.GlobalVariables; 031 import org.kuali.rice.krad.test.KRADTestCase; 032 033 import static org.junit.Assert.assertEquals; 034 import org.junit.Assert; 035 036 /** 037 * tests that a document, which goes through a regular or blanket approval process, is indexed correctly 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 * 041 * @deprecated KNS test class, convert to KRAD equivalent if applicable. 042 */ 043 @Deprecated 044 public class SearchAttributeIndexRequestTest extends KRADTestCase { 045 046 private static final String SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE = "SearchAttributeIndexTestDocument"; 047 048 private enum DOCUMENT_FIXTURE { 049 NORMAL_DOCUMENT("hippo","routing"); 050 051 private String constantString; 052 private String routingString; 053 private DOCUMENT_FIXTURE(String constantString, String routingString) { 054 this.constantString = constantString; 055 this.routingString = routingString; 056 } 057 058 public Document getDocument() throws Exception { 059 Document document = KRADServiceLocatorWeb.getDocumentService().getNewDocument(SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE); 060 SearchAttributeIndexTestDocument searchAttributeIndexTestDocument = (SearchAttributeIndexTestDocument) document; 061 searchAttributeIndexTestDocument.initialize(constantString, routingString); 062 063 return searchAttributeIndexTestDocument; 064 } 065 } 066 067 /** 068 * Tests that a document, which goes through a regular approval process, is indexed correctly 069 */ 070 @Test 071 public void regularApproveTest() throws Exception { 072 final String principalName = "quickstart"; 073 final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId(); 074 GlobalVariables.setUserSession(new UserSession(principalName)); 075 RouteContext.clearCurrentRouteContext(); 076 077 Document document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(); 078 document.getDocumentHeader().setDocumentDescription("Routed SAIndexTestDoc"); 079 final DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE); 080 081 assertApproveAttributes(document, documentType, principalId); 082 } 083 084 protected void assertApproveAttributes(Document document, DocumentType documentType, String principalId) throws Exception { 085 KRADServiceLocatorWeb.getDocumentService().routeDocument(document, "Routed SearchAttributeIndexTestDocument", null); 086 087 assertDDSearchableAttributesWork(documentType, principalId, "routeLevelCount", 088 new String[] {"1","0","2","7"}, 089 new int[] {1, 0, 0, 0} 090 ); 091 092 assertDDSearchableAttributesWork(documentType, principalId, "constantString", 093 new String[] {"hippo","monkey"}, 094 new int[] {1, 0} 095 ); 096 097 assertDDSearchableAttributesWork(documentType, principalId, "routedString", 098 new String[] {"routing","hippo"}, 099 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 }